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.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/vimamsa/gui.rb', line 199

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
  @entry_has_focus = false
end

Instance Attribute Details

#action_trail_labelObject (readonly)

Returns the value of attribute action_trail_label.



197
198
199
# File 'lib/vimamsa/gui.rb', line 197

def action_trail_label
  @action_trail_label
end

#active_windowObject (readonly)

Returns the value of attribute active_window.



197
198
199
# File 'lib/vimamsa/gui.rb', line 197

def active_window
  @active_window
end

#appObject (readonly)

Returns the value of attribute app.



197
198
199
# File 'lib/vimamsa/gui.rb', line 197

def app
  @app
end

#buf1Object

Returns the value of attribute buf1.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def buf1
  @buf1
end

#buffersObject

Returns the value of attribute buffers.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def buffers
  @buffers
end

#delexObject

Returns the value of attribute delex.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def delex
  @delex
end

#file_panelObject (readonly)

Returns the value of attribute file_panel.



197
198
199
# File 'lib/vimamsa/gui.rb', line 197

def file_panel
  @file_panel
end

#func_panelObject (readonly)

Returns the value of attribute func_panel.



197
198
199
# File 'lib/vimamsa/gui.rb', line 197

def func_panel
  @func_panel
end

#overlayObject

Returns the value of attribute overlay.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def overlay
  @overlay
end

#statnfoObject

Returns the value of attribute statnfo.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def statnfo
  @statnfo
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



197
198
199
# File 'lib/vimamsa/gui.rb', line 197

def subtitle
  @subtitle
end

#sw1Object

Returns the value of attribute sw1.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def sw1
  @sw1
end

#sw2Object

Returns the value of attribute sw2.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def sw2
  @sw2
end

#swsObject

Returns the value of attribute sws.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def sws
  @sws
end

#two_cObject

Returns the value of attribute two_c.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def two_c
  @two_c
end

#two_columnObject (readonly)

Returns the value of attribute two_column.



197
198
199
# File 'lib/vimamsa/gui.rb', line 197

def two_column
  @two_column
end

#viewObject

Returns the value of attribute view.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def view
  @view
end

#windowObject

Returns the value of attribute window.



196
197
198
# File 'lib/vimamsa/gui.rb', line 196

def window
  @window
end

#windowsObject (readonly)

Returns the value of attribute windows.



197
198
199
# File 'lib/vimamsa/gui.rb', line 197

def windows
  @windows
end

Instance Method Details

#add_to_minibuf(msg) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/vimamsa/gui.rb', line 325

def add_to_minibuf(msg)
  @minibuf_messages ||= []
  @minibuf_messages.unshift(msg)
  @minibuf_messages = @minibuf_messages.first(50)

  @minibuf_label.label = msg
  @minibuf_textview.buffer.text = @minibuf_messages.join("\n")

  return if @minibuf_expanded

  @minibuf_stack.visible_child_name = "label"
  @minibuf_content.visible = true

  @minibuf_vpane.position = @minibuf_vpane.height - 26
  
  GLib::Source.remove(@minibuf_hide_source) if @minibuf_hide_source
  @minibuf_hide_source = GLib::Timeout.add(7000) do
    @minibuf_content.visible = false
    @minibuf_hide_source = nil
    false
  end
end

#clear_overlayObject



271
272
273
274
275
276
# File 'lib/vimamsa/gui.rb', line 271

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

#current_viewObject



949
950
951
# File 'lib/vimamsa/gui.rb', line 949

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

#debug_idle_funcObject



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

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

#editor_areaObject

── editor-area helpers (delegates to @minibuf_vpane start child) ─────────



385
386
387
# File 'lib/vimamsa/gui.rb', line 385

def editor_area
  @minibuf_vpane.start_child
end

#end_overlay_drawObject



287
288
289
# File 'lib/vimamsa/gui.rb', line 287

def end_overlay_draw()
  @da.show
end

#ensure_cursor_drawnObject



1035
1036
1037
1038
# File 'lib/vimamsa/gui.rb', line 1035

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

#file_panel_initObject



1040
1041
1042
1043
# File 'lib/vimamsa/gui.rb', line 1040

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

#file_panel_refreshObject



1045
1046
1047
1048
# File 'lib/vimamsa/gui.rb', line 1045

def file_panel_refresh
  return unless @file_panel_shown
  @file_panel.refresh
end

#func_panel_initObject



1085
1086
1087
1088
# File 'lib/vimamsa/gui.rb', line 1085

def func_panel_init
  @func_panel = FuncPanel.new
  @func_panel_shown = false
end

#func_panel_refreshObject



1090
1091
1092
1093
# File 'lib/vimamsa/gui.rb', line 1090

def func_panel_refresh
  return unless @func_panel_shown
  @func_panel.refresh
end

#handle_deltasObject



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

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



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/vimamsa/gui.rb', line 248

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



1072
1073
1074
1075
1076
1077
1078
1079
# File 'lib/vimamsa/gui.rb', line 1072

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)
  set_editor_area(inner)
  @file_panel_shown = false
end

#hide_func_panelObject



1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
# File 'lib/vimamsa/gui.rb', line 1122

def hide_func_panel
  return unless @func_panel_shown
  inner = @func_panel_pane.end_child
  @func_panel_pane.set_start_child(nil)
  @func_panel_pane.set_end_child(nil)
  if @file_panel_shown
    @file_panel_pane.set_end_child(inner)
  else
    set_editor_area(inner)
  end
  @func_panel_shown = false
end

#idle_ensure_cursor_drawnObject



1031
1032
1033
# File 'lib/vimamsa/gui.rb', line 1031

def idle_ensure_cursor_drawn
  run_as_idle proc { self.ensure_cursor_drawn }
end

#idle_set_sizeObject



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/vimamsa/gui.rb', line 587

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



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/vimamsa/gui.rb', line 463

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 }))
  nav_box.append(make_header_button("hdr-close", "window-close-symbolic", proc { bufs.close_current_buffer }))
  header.pack_start(nav_box)

  # header.pack_end()

  @window.titlebar = header
end

#init_menuObject



780
781
782
# File 'lib/vimamsa/gui.rb', line 780

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

#init_minibufferObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
# File 'lib/vimamsa/gui.rb', line 393

def init_minibuffer
  @minibuf_messages = []
  @minibuf_expanded = false
  @minibuf_history_height = 120

  css = "label.minibuf, textview.minibuf { color: #cdd6f4; font-family: Monospace; font-size: 10pt; padding: 3px 8px; background-color: #1e1e2e; }"
  provider = Gtk::CssProvider.new
  provider.load(data: css)

  # Collapsed view: single line, latest message
  @minibuf_label = Gtk::Label.new("")
  @minibuf_label.xalign = 0.0
  @minibuf_label.hexpand = true
  @minibuf_label.ellipsize = Pango::EllipsizeMode::END
  @minibuf_label.add_css_class("minibuf")
  @minibuf_label.style_context.add_provider(provider)

  # Expanded view: scrollable history (fills whatever height the pane gives)
  @minibuf_textview = Gtk::TextView.new
  @minibuf_textview.editable = false
  @minibuf_textview.cursor_visible = false
  @minibuf_textview.wrap_mode = :word_char
  @minibuf_textview.add_css_class("minibuf")
  @minibuf_textview.style_context.add_provider(provider)
  @minibuf_textview.vexpand = true

  scroll = Gtk::ScrolledWindow.new
  scroll.set_policy(:never, :automatic)
  scroll.set_child(@minibuf_textview)
  scroll.vexpand = true

  @minibuf_stack = Gtk::Stack.new
  @minibuf_stack.vhomogeneous = false
  @minibuf_stack.transition_type = :crossfade
  @minibuf_stack.transition_duration = 100
  @minibuf_stack.add_named(@minibuf_label, "label")
  @minibuf_stack.add_named(scroll, "history")

  @minibuf_content = Gtk::Box.new(:vertical, 0)
  @minibuf_content.append(Gtk::Separator.new(:horizontal))
  @minibuf_content.append(@minibuf_stack)
  @minibuf_content.visible = false  # hidden until first message

  # Vertical pane: editor fills top, minibuffer sits at bottom, draggable
  editor_w = @windows[1][:overlay]
  @vbox.remove(editor_w)

  @minibuf_vpane = Gtk::Paned.new(:vertical)
  @minibuf_vpane.vexpand = true
  @minibuf_vpane.hexpand = true
  @minibuf_vpane.resize_start_child = true   # editor absorbs window resize
  @minibuf_vpane.resize_end_child = false    # minibuffer keeps its height
  @minibuf_vpane.shrink_end_child = true     # allow fully collapsing minibuffer
  @minibuf_vpane.set_start_child(editor_w)
  @minibuf_vpane.set_end_child(@minibuf_content)

  @vbox.attach(@minibuf_vpane, 0, 2, 2, 1)
  @minibuf_hide_source = nil
end

#init_windowObject



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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'lib/vimamsa/gui.rb', line 610

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.signal_connect("close-request") do
      vma.shutdown
      true  # prevent default destroy; shutdown->gui.quit handles it
    end

    @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
    func_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



897
898
899
900
# File 'lib/vimamsa/gui.rb', line 897

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



453
454
455
456
457
458
459
460
461
# File 'lib/vimamsa/gui.rb', line 453

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


784
785
786
# File 'lib/vimamsa/gui.rb', line 784

def menu
  @menu
end

#minibuf_toggle_expandedObject



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/vimamsa/gui.rb', line 348

def minibuf_toggle_expanded
  @minibuf_expanded = !@minibuf_expanded
  if @minibuf_expanded
    GLib::Source.remove(@minibuf_hide_source) if @minibuf_hide_source
    @minibuf_hide_source = nil
    @minibuf_content.visible = true
    @minibuf_stack.visible_child_name = "history"
    run_as_idle proc {
      pos = @minibuf_vpane.max_position - (@minibuf_history_height || 120)
      @minibuf_vpane.position = [pos, 0].max
    }
  else
    # Save height before collapsing so next open restores it
    @minibuf_history_height = @minibuf_vpane.max_position - @minibuf_vpane.position
    @minibuf_stack.visible_child_name = "label"
    run_as_idle proc {
      #TODO: automatic way of resizing doesn't work:
      # h = [@minibuf_label.height, 24].max + 2
      # @minibuf_vpane.position = @minibuf_vpane.max_position - h
      @minibuf_vpane.position = @minibuf_vpane.height - 26
    }
    @minibuf_hide_source = GLib::Timeout.add(7000) do
      @minibuf_content.visible = false
      @minibuf_expanded = false
      @minibuf_stack.visible_child_name = "label"
      @minibuf_hide_source = nil
      false
    end
  end
end

#monitorObject



765
766
767
768
769
770
771
772
773
774
775
776
777
778
# File 'lib/vimamsa/gui.rb', line 765

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



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
# File 'lib/vimamsa/gui.rb', line 1003

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



819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'lib/vimamsa/gui.rb', line 819

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

#notify_entry_focus(active) ⇒ Object

Called by embedded widgets (e.g. calculator entries) when a text entry gains or loses focus, so the vimamsa key handler knows to step aside.



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

def notify_entry_focus(active)
  @entry_has_focus = active
end

#overlay_draw_text(text, textpos) ⇒ Object



278
279
280
281
282
283
284
285
# File 'lib/vimamsa/gui.rb', line 278

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



1014
1015
1016
1017
1018
1019
1020
# File 'lib/vimamsa/gui.rb', line 1014

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



1022
1023
1024
1025
1026
1027
1028
1029
# File 'lib/vimamsa/gui.rb', line 1022

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



225
226
227
228
229
230
231
232
233
234
# File 'lib/vimamsa/gui.rb', line 225

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



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/vimamsa/gui.rb', line 571

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.



506
507
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
# File 'lib/vimamsa/gui.rb', line 506

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|
    # Step aside when a text entry widget (e.g. calculator var field) has focus.
    # @entry_has_focus is set explicitly via notify_entry_focus by those widgets.
    next false if @entry_has_focus
    next false if @kbd_passthrough
    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|
    next false if @entry_has_focus
    next false if @kbd_passthrough
    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



220
221
222
223
# File 'lib/vimamsa/gui.rb', line 220

def run
  init_window
  # init_rtext
end

#run_after_scrolling(p) ⇒ Object

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



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/vimamsa/gui.rb', line 292

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



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/vimamsa/gui.rb', line 236

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



925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
# File 'lib/vimamsa/gui.rb', line 925

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



953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/vimamsa/gui.rb', line 953

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



969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/vimamsa/gui.rb', line 969

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



912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/vimamsa/gui.rb', line 912

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_editor_area(w) ⇒ Object



389
390
391
# File 'lib/vimamsa/gui.rb', line 389

def set_editor_area(w)
  @minibuf_vpane.set_start_child(w)
end

#set_one_columnObject



796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# File 'lib/vimamsa/gui.rb', line 796

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)

  if @func_panel_shown
    @func_panel_pane.set_end_child(w1[:overlay])
  elsif @file_panel_shown
    @file_panel_pane.set_end_child(w1[:overlay])
  else
    set_editor_area(w1[:overlay])
  end
  @two_column = false
end

#set_two_columnObject



858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
# File 'lib/vimamsa/gui.rb', line 858

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

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

  # Remove overlay from its current parent and add the Gtk::Paned instead
  @pane = Gtk::Paned.new(:horizontal)
  if @func_panel_shown
    @func_panel_pane.set_end_child(nil)
    @pane.set_start_child(w2[:overlay])
    @pane.set_end_child(w1[:overlay])
    @func_panel_pane.set_end_child(@pane)
  elsif @file_panel_shown
    @file_panel_pane.set_end_child(nil)
    @pane.set_start_child(w2[:overlay])
    @pane.set_end_child(w1[:overlay])
    @file_panel_pane.set_end_child(@pane)
  else
    @minibuf_vpane.set_start_child(nil)  # unparent w1[:overlay] before re-parenting
    @pane.set_start_child(w2[:overlay])
    @pane.set_end_child(w1[:overlay])
    set_editor_area(@pane)
  end

  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



1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/vimamsa/gui.rb', line 1050

def show_file_panel
  return if @file_panel_shown
  # If func panel is shown it sits in minibuf_vpane; wrap it too
  inner = if @func_panel_shown
    @func_panel_pane
  elsif @two_column
    @pane
  else
    @windows[1][:overlay]
  end
  @minibuf_vpane.set_start_child(nil)  # unparent inner before re-parenting
  @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)
  set_editor_area(@file_panel_pane)
  @file_panel_shown = true
  @file_panel.refresh
end

#show_func_panelObject



1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# File 'lib/vimamsa/gui.rb', line 1095

def show_func_panel
  return if @func_panel_shown
  if @file_panel_shown
    inner = @file_panel_pane.end_child
    @file_panel_pane.set_end_child(nil)
  elsif @two_column
    inner = @pane
    @minibuf_vpane.set_start_child(nil)
  else
    inner = @windows[1][:overlay]
    @minibuf_vpane.set_start_child(nil)
  end
  @func_panel_pane = Gtk::Paned.new(:horizontal)
  @func_panel_pane.hexpand = true
  @func_panel_pane.vexpand = true
  @func_panel_pane.set_start_child(@func_panel.widget)
  @func_panel_pane.set_end_child(inner)
  @func_panel_pane.set_position(160)
  if @file_panel_shown
    @file_panel_pane.set_end_child(@func_panel_pane)
  else
    set_editor_area(@func_panel_pane)
  end
  @func_panel_shown = true
  @func_panel.refresh
end

#show_message_historyObject



379
380
381
# File 'lib/vimamsa/gui.rb', line 379

def show_message_history
  minibuf_toggle_expanded
end

#start_overlay_drawObject



264
265
266
267
268
269
# File 'lib/vimamsa/gui.rb', line 264

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



902
903
904
905
906
907
908
909
# File 'lib/vimamsa/gui.rb', line 902

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



1081
1082
1083
# File 'lib/vimamsa/gui.rb', line 1081

def toggle_file_panel
  @file_panel_shown ? hide_file_panel : show_file_panel
end

#toggle_func_panelObject



1135
1136
1137
# File 'lib/vimamsa/gui.rb', line 1135

def toggle_func_panel
  @func_panel_shown ? hide_func_panel : show_func_panel
end

#toggle_two_columnObject



788
789
790
791
792
793
794
# File 'lib/vimamsa/gui.rb', line 788

def toggle_two_column
  if @two_column
    set_one_column
  else
    set_two_column
  end
end