Class: VMAgui

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/gui.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVMAgui

Returns a new instance of VMAgui.



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/vimamsa/gui.rb', line 203

def initialize()
  @two_column = false
  @active_column = 1
  @show_overlay = true
  @da = nil
  @sws = []
  @buffers = {}
  @view = nil
  @buf1 = nil
  @img_resizer_active = false
  @windows = {}
  @app = nil
end

Instance Attribute Details

#action_trail_labelObject (readonly)

Returns the value of attribute action_trail_label.



201
202
203
# File 'lib/vimamsa/gui.rb', line 201

def action_trail_label
  @action_trail_label
end

#active_windowObject (readonly)

Returns the value of attribute active_window.



201
202
203
# File 'lib/vimamsa/gui.rb', line 201

def active_window
  @active_window
end

#appObject (readonly)

Returns the value of attribute app.



201
202
203
# File 'lib/vimamsa/gui.rb', line 201

def app
  @app
end

#buf1Object

Returns the value of attribute buf1.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def buf1
  @buf1
end

#buffersObject

Returns the value of attribute buffers.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def buffers
  @buffers
end

#delexObject

Returns the value of attribute delex.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def delex
  @delex
end

#file_panelObject (readonly)

Returns the value of attribute file_panel.



201
202
203
# File 'lib/vimamsa/gui.rb', line 201

def file_panel
  @file_panel
end

#overlayObject

Returns the value of attribute overlay.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def overlay
  @overlay
end

#statnfoObject

Returns the value of attribute statnfo.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def statnfo
  @statnfo
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



201
202
203
# File 'lib/vimamsa/gui.rb', line 201

def subtitle
  @subtitle
end

#sw1Object

Returns the value of attribute sw1.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def sw1
  @sw1
end

#sw2Object

Returns the value of attribute sw2.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def sw2
  @sw2
end

#swsObject

Returns the value of attribute sws.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def sws
  @sws
end

#two_cObject

Returns the value of attribute two_c.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def two_c
  @two_c
end

#two_columnObject (readonly)

Returns the value of attribute two_column.



201
202
203
# File 'lib/vimamsa/gui.rb', line 201

def two_column
  @two_column
end

#viewObject

Returns the value of attribute view.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def view
  @view
end

#windowObject

Returns the value of attribute window.



200
201
202
# File 'lib/vimamsa/gui.rb', line 200

def window
  @window
end

#windowsObject (readonly)

Returns the value of attribute windows.



201
202
203
# File 'lib/vimamsa/gui.rb', line 201

def windows
  @windows
end

Instance Method Details

#add_to_minibuf(msg) ⇒ Object



322
323
324
325
326
327
# File 'lib/vimamsa/gui.rb', line 322

def add_to_minibuf(msg)
  # return #TODO:gtk4
  startiter = @minibuf.buffer.get_iter_at(:offset => 0)
  @minibuf.buffer.insert(startiter, "#{msg}\n")
  @minibuf.signal_emit("move-cursor", Gtk::MovementStep.new(:PAGES), -1, false)
end

#clear_overlayObject



268
269
270
271
272
273
# File 'lib/vimamsa/gui.rb', line 268

def clear_overlay()
  if @da != nil
    # @overlay.remove(@da)
    @active_window[:overlay].remove_overlay(@da)
  end
end

#current_viewObject



825
826
827
# File 'lib/vimamsa/gui.rb', line 825

def current_view
  return @active_window[:sw].child
end

#debug_idle_funcObject



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/vimamsa/gui.rb', line 390

def debug_idle_func
  return false if @shutdown == true
  if Time.now - @last_debug_idle > 1
    @last_debug_idle = Time.now
    # puts "DEBUG IDLE #{Time.now}"
    # @view.check_controllers
  end

  ctrl_fn = File.expand_path(get_dot_path("ripl_ctrl"))
  # Allows to debug in case keyboard handling is lost
  if File.exist?(ctrl_fn)
    File.delete(ctrl_fn)
    start_ripl
  end

  sleep 0.02
  return true
end

#end_overlay_drawObject



284
285
286
# File 'lib/vimamsa/gui.rb', line 284

def end_overlay_draw()
  @da.show
end

#ensure_cursor_drawnObject



911
912
913
914
# File 'lib/vimamsa/gui.rb', line 911

def ensure_cursor_drawn
  # view.place_cursor_onscreen #TODO: needed?
  view.draw_cursor
end

#file_panel_initObject



916
917
918
919
# File 'lib/vimamsa/gui.rb', line 916

def file_panel_init
  @file_panel = FileTreePanel.new
  @file_panel_shown = false
end

#file_panel_refreshObject



921
922
923
924
# File 'lib/vimamsa/gui.rb', line 921

def file_panel_refresh
  return unless @file_panel_shown
  @file_panel.refresh
end

#handle_deltasObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/vimamsa/gui.rb', line 304

def handle_deltas()
  view.delete_cursor_char
  while d = buf.deltas.shift
    pos = d[0]
    op = d[1]
    num = d[2]
    txt = d[3]
    if op == DELETE
      startiter = @buf1.get_iter_at(:offset => pos)
      enditer = @buf1.get_iter_at(:offset => pos + num)
      @buf1.delete(startiter, enditer)
    elsif op == INSERT
      startiter = @buf1.get_iter_at(:offset => pos)
      @buf1.insert(startiter, txt)
    end
  end
end

#handle_image_resizeObject

TODO:gtk4



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/vimamsa/gui.rb', line 245

def handle_image_resize #TODO:gtk4
  return if @img_resizer_active == true
  @dtime = Time.now

  $gcrw = 0
  vma.gui.window.signal_connect "configure-event" do |widget, cr|
    if $gcrw != cr.width
      @delex.run
    end
    $gcrw = cr.width
    false
  end

  @img_resizer_active = true
end

#hide_file_panelObject



941
942
943
944
945
946
947
948
949
# File 'lib/vimamsa/gui.rb', line 941

def hide_file_panel
  return unless @file_panel_shown
  inner = @file_panel_pane.end_child
  @file_panel_pane.set_start_child(nil)
  @file_panel_pane.set_end_child(nil)
  @vbox.remove(@file_panel_pane)
  @vbox.attach(inner, 0, 2, 2, 1)
  @file_panel_shown = false
end

#idle_ensure_cursor_drawnObject



907
908
909
# File 'lib/vimamsa/gui.rb', line 907

def idle_ensure_cursor_drawn
  run_as_idle proc { self.ensure_cursor_drawn }
end

#idle_set_sizeObject



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/vimamsa/gui.rb', line 485

def idle_set_size()
  # Need to wait for a while to window to be maximized to get correct @window.width
  @window.maximize
  wait_for_resize(@window)
  # Set new size as half of the screeen
  width = @window.width / 2
  height = @window.height - 5
  width = 600 if width < 600
  height = 600 if height < 600

  #Minimum size:
  @window.set_size_request(600, 600)
  @window.set_default_size(width, height)
  debug "size #{[width, height]}", 2
  @window.unmaximize

  #set_default_size doesn't always have effect if run immediately
  wait_for_resize(@window)
  @window.set_default_size(width, height)

  return false
end

#init_header_barObject



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/vimamsa/gui.rb', line 368

def init_header_bar()
  header = Gtk::HeaderBar.new
  @header = header

  file_box = Gtk::Box.new(:horizontal, 0)
  file_box.style_context.add_class("linked")
  file_box.append(make_header_button("hdr-open", "document-open-symbolic", proc { open_file_dialog }))
  file_box.append(make_header_button("hdr-save", "document-save-symbolic", proc { buf.save }))
  file_box.append(make_header_button("hdr-new",  "document-new-symbolic",  proc { create_new_file }))
  header.pack_start(file_box)

  nav_box = Gtk::Box.new(:horizontal, 0)
  nav_box.style_context.add_class("linked")
  nav_box.append(make_header_button("hdr-prev",  "pan-start-symbolic", proc { history_switch_backwards }))
  nav_box.append(make_header_button("hdr-next",  "pan-end-symbolic",   proc { history_switch_forwards }))
  header.pack_start(nav_box)

  header.pack_end(make_header_button("hdr-close", "window-close-symbolic", proc { bufs.close_current_buffer }))

  @window.titlebar = header
end

#init_menuObject



674
675
676
# File 'lib/vimamsa/gui.rb', line 674

def init_menu
  Vimamsa::Menu.new(@menubar, @app)
end

#init_minibufferObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/vimamsa/gui.rb', line 329

def init_minibuffer()
  # Init minibuffer
  sw = Gtk::ScrolledWindow.new
  sw.set_policy(:automatic, :automatic)
  overlay = Gtk::Overlay.new
  overlay.set_child(sw)
  @vbox.attach(overlay, 0, 3, 2, 1)
  sw.set_size_request(-1, 12)

  view = VSourceView.new(nil, nil)
  view.set_highlight_current_line(false)
  view.set_show_line_numbers(false)
  # view.set_buffer(buf1)
  ssm = GtkSource::StyleSchemeManager.new
  ssm.set_search_path(ssm.search_path << ppath("styles/"))
  sty = ssm.get_scheme("molokai_edit")
  view.buffer.highlight_matching_brackets = false #TODO
  view.buffer.style_scheme = sty
  provider = Gtk::CssProvider.new
  # provider.load(data: "textview { font-family: Monospace; font-size: 11pt; }")
  provider.load(data: "textview { font-family: Arial; font-size: 10pt; color:#eeeeee}")
  view.style_context.add_provider(provider)
  view.wrap_mode = :char
  @minibuf = view
  # startiter = view.buffer.get_iter_at(:offset => 0)
  message("STARTUP")
  sw.set_child(view)
end

#init_windowObject



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/vimamsa/gui.rb', line 508

def init_window
  @last_debug_idle = Time.now
  app = Gtk::Application.new("net.samiddhi.vimamsa.r#{rand(1000)}", :flags_none)
  @app = app
  
  

  Gtk::Settings.default.gtk_application_prefer_dark_theme = true
  Gtk::Settings.default.gtk_theme_name = "Adwaita"
  Gtk::Settings.default.gtk_cursor_blink = false
  Gtk::Settings.default.gtk_cursor_blink_time = 4000

  app.signal_connect "activate" do
    @window = Gtk::ApplicationWindow.new(app)
    @window.set_application(app)

    @window.title = "Multiple Views"
    @vpaned = Gtk::Paned.new(:vertical)

    @vbox = Gtk::Grid.new()
    @window.set_child(@vbox)

    Thread.new {
      GLib::Idle.add(proc { debug_idle_func })
    }

    reset_controllers

    focus_controller = Gtk::EventControllerFocus.new

    focus_controller.signal_connect("enter") do
      debug "Gained focus"
      draw_cursor_bug_workaround
    end
    @window.add_controller(focus_controller)

    motion_controller = Gtk::EventControllerMotion.new
    motion_controller.signal_connect("motion") do |controller, x, y|
      # label.set_text("Mouse at: (%.1f, %.1f)" % [x, y])
      # puts "MOVE #{x} #{y}"
      
      # Cursor vanishes when hovering over menubar
      draw_cursor_bug_workaround if y < 30 
      @last_cursor = [x, y]
      @cursor_move_time = Time.now
    end
    @window.add_controller(motion_controller)

    @windows[1] = new_window(1)

    @last_adj_time = Time.now

    # To show keyboard key binding state
    @statnfo = Gtk::Label.new

    # To show e.g. current folder
    @subtitle = Gtk::Label.new("")

    @statbox = Gtk::Box.new(:horizontal, 2)
    @statnfo.set_size_request(150, 10)
    @statbox.append(@subtitle)
    @subtitle.hexpand = true
    @statbox.append(@statnfo)
    provider = Gtk::CssProvider.new
    @statnfo.add_css_class("statnfo")
    provider.load(data: "label.statnfo {   background-color:#353535; font-size: 10pt; margin-top:2px; margin-bottom:2px; align:right;}")

    provider = Gtk::CssProvider.new
    @statnfo.style_context.add_provider(provider)

    # numbers: left, top, width, height
    @vbox.attach(@windows[1][:overlay], 0, 2, 2, 1)

    # column, row, width height
    @vbox.attach(@statbox, 1, 1, 1, 1)

    init_minibuffer

    menubar = Gio::Menu.new
    @menubar = menubar

    # TODO: Doesn't work, why?:
    # menubar_bar = Gtk::PopoverMenuBar.new(menu_model: menubar)
    
    menubar_bar = Gtk::PopoverMenuBar.new()
    menubar_bar.set_menu_model(menubar)
    
    menubar_bar.hexpand = true
    @action_trail_label = Gtk::Label.new("")
    @action_trail_label.add_css_class("action-trail")
    menubar_row = Gtk::Box.new(:horizontal, 0)
    menubar_row.append(menubar_bar)
    menubar_row.append(@action_trail_label)
    @vbox.attach(menubar_row, 0, 0, 2, 1)

    @active_window = @windows[1]

    init_header_bar
    file_panel_init

    @window.show

    surface = @window.native.surface
    tt = Time.now
    surface.signal_connect("layout") do
      # puts "Window resized or moved, other redraw."
    end

    run_as_idle proc { idle_set_size }

    prov = Gtk::CssProvider.new
    # See gtk-4.9.4/gtk/theme/Default/_common.scss  on how to theme
    # gtksourceview/gtksourcestyleschemepreview.c
    # gtksourceview/gtksourcestylescheme.c
    prov.load(data: " headerbar { padding: 0 0px; min-height: 16px; border-width: 0 0 0px; border-style: solid; }
    
    textview border.left gutter { color: #8aa; font-size:8pt; }     
    
    textview border.left gutter { padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px; color: #8aa; font-size:9pt; }     
    
       headerbar .title {
    font-weight: bold;
    font-size: 11pt;
    color: #cdffee;
}

 headerbar > windowhandle > box .start {
    border-spacing: 6px;
}
 
 headerbar windowcontrols button {
      min-height: 15px;
      min-width: 15px;
    }

 popover background > contents { padding: 8px; border-radius: 20px; }

 label.action-trail { font-family: monospace; font-size: 10pt; margin-right: 8px; color: #aaaaaa; }
       ")
    @window.style_context.add_provider(prov)

    sc = Gtk::StyleContext.add_provider_for_display(Gdk::Display.default, prov)

    vma.start
  end

  GLib::Idle.add(proc { self.monitor })

  app.run
end

#is_buffer_open(bufid) ⇒ Object



773
774
775
776
# File 'lib/vimamsa/gui.rb', line 773

def is_buffer_open(bufid)
  openbufids = @windows.keys.collect { |x| @windows[x][:sw].child.bufo.id }
  return openbufids.include?(bufid)
end

#make_header_button(action_id, icon, cb) ⇒ Object



358
359
360
361
362
363
364
365
366
# File 'lib/vimamsa/gui.rb', line 358

def make_header_button(action_id, icon, cb)
  act = Gio::SimpleAction.new(action_id)
  @app.add_action(act)
  act.signal_connect("activate") { |_a, _p| cb.call }
  btn = Gtk::Button.new
  btn.set_child(Gtk::Image.new(icon_name: icon))
  btn.action_name = "app.#{action_id}"
  btn
end

#monitorObject



659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/vimamsa/gui.rb', line 659

def monitor
  swa = @windows[1][:sw]
  @monitor_time ||= Time.now
  @sw_width ||= swa.width
  return true if Time.now - @monitor_time < 0.2
  # Detect element resize
  if swa.width != @sw_width
    debug "Width change sw_width #{@sw_width}"
    @sw_width = swa.width
    DelayExecutioner.exec(id: :scale_images, wait: 0.7, callable: proc { debug ":scale_images"; vma.gui.scale_all_images })
  end
  @monitor_time = Time.now
  return true
end

#new_scrolled_windowObject



879
880
881
882
883
884
885
886
887
888
# File 'lib/vimamsa/gui.rb', line 879

def new_scrolled_window
  sw = Gtk::ScrolledWindow.new
  sw.set_policy(:automatic, :automatic)
  @last_adj_time = Time.now
  sw.vadjustment.signal_connect("value-changed") { |x|
    @last_adj_time = Time.now
    debug "@sw.vadjustment #{x.value}", 2
  }
  return sw
end

#new_window(win_id) ⇒ Object



705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/vimamsa/gui.rb', line 705

def new_window(win_id)
  n_sw = Gtk::ScrolledWindow.new
  n_sw.set_policy(:automatic, :automatic)
  n_overlay = Gtk::Overlay.new
  n_overlay.add_overlay(n_sw)
  # @pane = Gtk::Paned.new(:horizontal)

  win = { :sw => n_sw, :overlay => n_overlay, :id => win_id }
  # @windows[2] = { :sw => @sw2, :overlay => @overlay2, :id => 2 }

  # @vbox.remove(@overlay)

  # @pane.set_start_child(@overlay2)
  # @pane.set_end_child(@overlay)

  # numbers: left, top, width, height
  # @vbox.attach(@pane, 0, 2, 2, 1)

  n_sw.vexpand = true
  n_sw.hexpand = true

  n_overlay.vexpand = true
  n_overlay.hexpand = true

  # TODO: remove??
  n_sw.vadjustment.signal_connect("value-changed") { |x|
 # pp x.page_increment
         # pp x.page_size
         # pp x.step_increment
         # pp x.upper
         # pp x.value
         # pp x
         # @last_adj_time = Time.now
    }

  # @sw2.show
  return win
end

#overlay_draw_text(text, textpos) ⇒ Object



275
276
277
278
279
280
281
282
# File 'lib/vimamsa/gui.rb', line 275

def overlay_draw_text(text, textpos)
  # debug "overlay_draw_text #{[x,y]}"
  (x, y) = @view.pos_to_coord(textpos)
  # debug "overlay_draw_text #{[x,y]}"
  label = Gtk::Label.new("<span background='#000000ff' foreground='#ff0000' weight='ultrabold'>#{text}</span>")
  label.use_markup = true
  @da.put(label, x, y)
end

#page_down(multip: 1.0) ⇒ Object



890
891
892
893
894
895
896
# File 'lib/vimamsa/gui.rb', line 890

def page_down(multip: 1.0)
  sw = @active_window[:sw]
  va = sw.vadjustment
  newval = va.value + va.page_increment * multip
  va.value = newval
  sw.child.set_cursor_to_top
end

#page_up(multip: 1.0) ⇒ Object



898
899
900
901
902
903
904
905
# File 'lib/vimamsa/gui.rb', line 898

def page_up(multip: 1.0)
  sw = @active_window[:sw]
  va = sw.vadjustment
  newval = va.value - va.page_increment * multip
  newval = 0 if newval < 0
  va.value = newval
  sw.child.set_cursor_to_top
end

#quitObject



222
223
224
225
226
227
228
229
230
231
# File 'lib/vimamsa/gui.rb', line 222

def quit
  @window.destroy
  @shutdown = true
  for t in Thread.list
    if t != Thread.current
      t.exit
    end
  end
  @app.quit
end

#remove_extra_controllersObject



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/vimamsa/gui.rb', line 469

def remove_extra_controllers
  clist = vma.gui.window.observe_controllers
  to_remove = []
  (0..(clist.n_items - 1)).each { |x|
    ctr = clist.get_item(x)
    if ctr.class == Gtk::EventControllerKey and ctr != @press
      to_remove << ctr
    end
  }
  if to_remove.size > 0
    # puts "Removing controllers:"
    # pp to_remove
    to_remove.each { |x| vma.gui.window.remove_controller(x) }
  end
end

#reset_controllersObject

Remove widget event controllers added by gtk, and add our own.



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/vimamsa/gui.rb', line 410

def reset_controllers
  clist = @window.observe_controllers
  to_remove = []
  (0..(clist.n_items - 1)).each { |x|
    ctr = clist.get_item(x)
    if ctr.class == Gtk::EventControllerKey
      to_remove << ctr
    end
  }
  if to_remove.size > 0
    # debug "Removing controllers:"
    # pp to_remove
    to_remove.each { |x| @window.remove_controller(x) }
  end

  press = Gtk::EventControllerKey.new
  @press = press
  # to prevent SourceView key handler capturing any keypresses
  press.set_propagation_phase(Gtk::PropagationPhase::CAPTURE)
  @window.add_controller(press)

  press.signal_connect "key-pressed" do |gesture, keyval, keycode, y|
    name = Gdk::Keyval.to_name(keyval)
    uki = Gdk::Keyval.to_unicode(keyval)
    keystr = uki.chr("UTF-8")
    debug "key pressed #{keyval} #{keycode} name:#{name} str:#{keystr} unicode:#{uki}"
    buf.view.handle_key_event(keyval, keystr, :key_press)
    true
  end

  press.signal_connect "modifiers" do |eventctr, modtype|
    # eventctr: Gtk::EventControllerKey
    # modtype: Gdk::ModifierType
    debug "modifier change"
    vma.kbd.modifiers[:ctrl] = modtype.control_mask?
    vma.kbd.modifiers[:alt] = modtype.alt_mask?
    vma.kbd.modifiers[:hyper] = modtype.hyper_mask?
    vma.kbd.modifiers[:lock] = modtype.lock_mask?
    vma.kbd.modifiers[:meta] = modtype.meta_mask?
    vma.kbd.modifiers[:shift] = modtype.shift_mask?
    vma.kbd.modifiers[:super] = modtype.super_mask?

    #TODO:?
    # button1_mask?
    # ...
    # button5_mask?
    true # = handled, do not propagate further
  end

  press.signal_connect "key-released" do |gesture, keyval, keycode, y|
    name = Gdk::Keyval.to_name(keyval)
    uki = Gdk::Keyval.to_unicode(keyval)
    keystr = uki.chr("UTF-8")
    debug "key released #{keyval} #{keycode} name:#{name} str:#{keystr} unicode:#{uki}"
    buf.view.handle_key_event(keyval, keystr, :key_release)
    true # = handled, do not propagate further
  end
end

#runObject



217
218
219
220
# File 'lib/vimamsa/gui.rb', line 217

def run
  init_window
  # init_rtext
end

#run_after_scrolling(p) ⇒ Object

Run proc after animated scrolling has stopped (e.g. after page down)



289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/vimamsa/gui.rb', line 289

def run_after_scrolling(p)
  Thread.new {
    # After running
    #   view.signal_emit("move-cursor", Gtk::MovementStep.new(:PAGES)
    # have to wait for animated page down scrolling to actually start
    # Then have to wait determine that it has stopped if scrolling adjustment stops changing. There should be a better way to do this.
    sleep 0.1
    while Time.now - @last_adj_time < 0.1
      sleep 0.1
    end
    debug "SCROLLING ENDED", 2
    run_as_idle p
  }
end

#scale_all_imagesObject



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/vimamsa/gui.rb', line 233

def scale_all_images
  for k, window in @windows
    bu = window[:sw].child.bufo
    for img in bu.images
      if !img[:obj].destroyed?
        img[:obj].scale_image
      end
    end
  end
  false
end

#set_active_window(id) ⇒ Object



801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
# File 'lib/vimamsa/gui.rb', line 801

def set_active_window(id)
  return if !@two_column
  return if id == @active_column
  return if id == @active_window[:id]

  if @windows[id].nil?
    debug "No such window #{id}", 2
    return
  end

  @active_window = @windows[id]
  @active_column = id

  @active_window[:sw].child.focus_in()
  for k, w in @windows
    if w != @active_window
      fochild = w[:sw].child
      run_as_idle proc { fochild.focus_out() }
    end
  end

  vma.buffers.set_current_buffer_by_id(@active_window[:sw].child.bufo.id)
end

#set_buffer_to_window(bufid, winid) ⇒ Object



829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/vimamsa/gui.rb', line 829

def set_buffer_to_window(bufid, winid)
  view = @buffers[bufid]
  debug "vma.gui.set_buffer_to_window(#{bufid}), winid=#{winid}"
  buf1 = view.buffer

  @windows[winid][:sw].set_child(view)
  idle_ensure_cursor_drawn

  #TODO:???
  # @view = view
  # @buf1 = buf1
  # $view = view ???
  # $vbuf = buf1

end

#set_current_buffer(id) ⇒ Object



845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# File 'lib/vimamsa/gui.rb', line 845

def set_current_buffer(id)
  view2 = @buffers[id]
  debug "vma.gui.set_current_buffer(#{id}), view=#{view}"
  buf1 = view2.buffer
  @view = view2
  @buf1 = buf1
  $view = view
  $vbuf = buf1

  # Check if buffer is already open in another column
  if @two_column and @active_column == 2 and id == @windows[1][:sw].child.bufo.id
    toggle_active_window
  elsif @two_column && @active_column == 1 && !@windows[2][:sw].child.nil? && id == @windows[2][:sw].child.bufo.id
    #TODO: should not need [email protected]? here. If this happens then other column is empty.
    toggle_active_window
  else
    #TODO: improve
    swa = @active_window[:sw]
    ol = @active_window[:overlay]
    ol.remove_overlay(swa)
    swa.set_child(nil)
    # Creating a new ScrolledWindow every time to avoid a layout bug
    # https://gitlab.gnome.org/GNOME/gtk/-/issues/6189
    swb = new_scrolled_window
    swb.set_child(view2)
    ol.add_overlay(swb)
    @active_window[:view] = view
    @active_window[:sw] = swb
  end
  view.grab_focus

  idle_ensure_cursor_drawn
end

#set_current_view(view) ⇒ Object

Activate that window which has the given view



788
789
790
791
792
793
794
795
796
797
798
799
# File 'lib/vimamsa/gui.rb', line 788

def set_current_view(view)
  # Window of current view:
  w = @windows.find { |k, v| v[:sw].child == view }
  # All other windows:
  otherw = @windows.find_all { |k, v| v[:sw].child != view }
  if !w.nil?
    set_active_window(w[0])
    for k, x in otherw
      x[:sw].child.focus_out()
    end
  end
end

#set_one_columnObject



686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/vimamsa/gui.rb', line 686

def set_one_column
  return if !@two_column
  #This always closes the leftmost column/window
  #TODO: close rightmost column if left active
  set_active_window(1)

  @windows[2][:sw].set_child(nil)
  @windows.delete(2)
  w1 = @windows[1]

  @pane.set_start_child(nil)
  @pane.set_end_child(nil)

  @vbox.remove(@pane)
  @vbox.attach(w1[:overlay], 0, 2, 2, 1)
  # @vbox.attach(@statbox, 1, 1, 1, 1)
  @two_column = false
end

#set_two_columnObject



744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/vimamsa/gui.rb', line 744

def set_two_column
  return if @two_column
  @windows[2] = new_window(2)

  w1 = @windows[1]
  w2 = @windows[2]

  # Remove overlay from @vbox and add the Gtk::Paned instead
  @pane = Gtk::Paned.new(:horizontal)
  @vbox.remove(w1[:overlay])
  @pane.set_start_child(w2[:overlay])
  @pane.set_end_child(w1[:overlay])

  # numbers: left, top, width, height
  @vbox.attach(@pane, 0, 2, 2, 1)

  w2[:sw].show
  @two_column = true

  last = vma.buffers.get_last_visited_id
  if !last.nil?
    set_buffer_to_window(last, 2)
  else
    # If there is only one buffer, create a new one and add to the new window/column
    bf = create_new_buffer "\n\n", "buff", false
    set_buffer_to_window(bf.id, 2)
  end
end

#show_file_panelObject



926
927
928
929
930
931
932
933
934
935
936
937
938
939
# File 'lib/vimamsa/gui.rb', line 926

def show_file_panel
  return if @file_panel_shown
  inner = @two_column ? @pane : @windows[1][:overlay]
  @vbox.remove(inner)
  @file_panel_pane = Gtk::Paned.new(:horizontal)
  @file_panel_pane.hexpand = true
  @file_panel_pane.vexpand = true
  @file_panel_pane.set_start_child(@file_panel.widget)
  @file_panel_pane.set_end_child(inner)
  @file_panel_pane.set_position(180)
  @vbox.attach(@file_panel_pane, 0, 2, 2, 1)
  @file_panel_shown = true
  @file_panel.refresh
end

#start_overlay_drawObject



261
262
263
264
265
266
# File 'lib/vimamsa/gui.rb', line 261

def start_overlay_draw()
  @da = Gtk::Fixed.new
  @active_window[:overlay].add_overlay(@da)

  # @overlay.set_overlay_pass_through(@da, true) #TODO:gtk4
end

#toggle_active_windowObject



778
779
780
781
782
783
784
785
# File 'lib/vimamsa/gui.rb', line 778

def toggle_active_window()
  return if !@two_column
  if @active_column == 1
    set_active_window(2)
  else
    set_active_window(1)
  end
end

#toggle_file_panelObject



951
952
953
# File 'lib/vimamsa/gui.rb', line 951

def toggle_file_panel
  @file_panel_shown ? hide_file_panel : show_file_panel
end

#toggle_two_columnObject



678
679
680
681
682
683
684
# File 'lib/vimamsa/gui.rb', line 678

def toggle_two_column
  if @two_column
    set_one_column
  else
    set_two_column
  end
end