Class: Vimamsa::Menu

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

Instance Method Summary collapse

Constructor Details

#initialize(menubar, _app) ⇒ Menu

Returns a new instance of Menu.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vimamsa/gui_menu.rb', line 85

def initialize(menubar, _app)
  @app = _app
  @nfo = {}
  @menubar = menubar
  # Gio::Menu for the "Modules" top-level menu, created on first use.
  @module_menu = nil
  # Ordered list of action symbols in @module_menu, used to find removal positions.
  @module_actions = []

  add_menu_items

  for k, v in @nfo
    build_menu(v, menubar)
  end
end

Instance Method Details

#add_menu_itemsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vimamsa/gui_menu.rb', line 23

def add_menu_items()
  add_to_menu "File.Example", { :label => "<span foreground='#888888'>Action, [mode] key binding</span>", :action => nil }
  add_to_menu "File.Save", { :label => "Save", :action => :buf_save }
  add_to_menu "File.Save as", { :label => "Save As...", :action => :buf_save_as }
  add_to_menu "File.Open", { :label => "Open", :action => :open_file_dialog }

  add_to_menu "File.New", { :label => "New file", :action => :buf_new }
  add_to_menu "File.Revert", { :label => "Reload file from disk", :action => :buf_revert }
  add_to_menu "File.List", { :label => "List open files", :action => :start_buf_manager }
  add_to_menu "File.Close", { :label => "Close file", :action => :close_current_buffer }

  add_to_menu "File.Quit", { :label => "Quit", :action => :quit }

  add_to_menu "Edit.Undo", { :label => "Undo edit", :action => :edit_undo }
  add_to_menu "Edit.Redo", { :label => "Redo edit", :action => :edit_redo }
  add_to_menu "Edit.SearchReplace", { :label => "Search and replace", :action => :gui_search_replace }
  add_to_menu "Edit.Find", { :label => "Find", :action => :find_in_buffer }

  # add_to_menu "Edit.StartCompletion", { :label => "StartCompletion", :action => :start_autocomplete }
  # add_to_menu "Edit.ShowCompletion", { :label => "ShowCompletion", :action => :show_autocomplete }

  add_to_menu "Settings.Preferences", { :label => "Preferences...", :action => :show_settings }
  add_to_menu "Settings.Customize", { :action => :edit_customrb }
  add_to_menu "Settings.ReloadCustom", { :action => :reload_customrb }

  add_to_menu "Actions.SearchForActions", { :label => "Search for Actions", :action => :search_actions }

  add_to_menu "Actions.Grep", { :label => "Grep lines", :action => :invoke_grep_search }

  add_to_menu "Actions.FileHistoryFinder", { :label => "Search files in history", :action => :gui_file_history_finder }

  add_to_menu "Actions.experimental.Diff", { :label => "Show Diff of\nunsaved changes", :action => :diff_buffer }
  add_to_menu "Actions.experimental.GitDiff", { :label => "Show git diff", :action => :git_diff_buffer }
  add_to_menu "Actions.experimental.GitDiffW", { :label => "Show git diff -w (repo)", :action => :git_diff_w }

  add_to_menu "Actions.experimental.PrintBufferAccessList", { :label => "Print buffers by access time", :action => :print_buffer_access_list }
  add_to_menu "Actions.experimental.EnableDebug", { :label => "Enable debug", :action => :enable_debug }
  add_to_menu "Actions.experimental.DisableDebug", { :label => "Disable debug", :action => :disable_debug }
  add_to_menu "Actions.experimental.ShowImages", { :label => "Show images ⟦img:path⟧", :action => :show_images }
  
  add_to_menu "Actions.experimental.ShowImages", { :action => :experimental_eval }
  

  add_to_menu "Actions.debug.dumpkbd", { :label => "Dump kbd state", :action => :kbd_dump_state }
  add_to_menu "Actions.debug.ToggleKbdPassthrough", { :label => "Toggle kbd event passthrough", :action => :toggle_kbd_passthrough }

  add_to_menu "View.BufferManager", { :label => "Show open files", :action => :start_buf_manager }
  add_to_menu "View.TwoColumn", { :label => "Toggle two column mode", :action => :toggle_two_column }
  add_to_menu "View.FilePanel", { :label => "Toggle file panel", :action => :toggle_file_panel }
  add_to_menu "View.FuncPanel", { :label => "Toggle function panel (LSP)", :action => :toggle_func_panel }
  add_to_menu "View.MsgHistory", { :label => "Message history", :action => :show_message_history }

  add_to_menu "Actions.EncryptFile", { :label => "Encrypt file", :action => :encrypt_file }
  add_to_menu "Help.KeyBindings", { :label => "Show key bindings", :action => :show_key_bindings }
  add_to_menu "Help.InstallDemo", { :action => :install_demo_files }

  #TODO: :auto_indent_buffer

  # add_to_menu "Actions.Ack", { :label => "source code search (Ack)", :action => :ack_search }

end

#add_module_action(action, label) ⇒ Object

Add a menu item under the top-level “Modules” menu. Creates the Modules menu the first time it is called.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/vimamsa/gui_menu.rb', line 103

def add_module_action(action, label)
  if @module_menu.nil?
    @module_menu = Gio::Menu.new
    modules_item = Gio::MenuItem.new("Modules", nil)
    modules_item.submenu = @module_menu
    @menubar.append_item(modules_item)
  end

  act = Gio::SimpleAction.new(action.to_s)
  @app.add_action(act)
  act.signal_connect("activate") { call_action(action) }

  item = Gio::MenuItem.new(label, "app.#{action}")
  @module_menu.append_item(item)
  @module_actions << action
end

#add_to_menu(_mpath, x) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vimamsa/gui_menu.rb', line 3

def add_to_menu(_mpath, x)
  # If no menu label provided, take from the action definition
  if !x[:action].nil? and x[:label].nil?
    x[:label] = vma.actions[x[:action]].method_name
  end

  mpath = _mpath.split(".")
  curnfo = @nfo
  for y in mpath
    debug(curnfo.inspect)
    if y.equal?(mpath.last)
      curnfo[y] = x
    elsif curnfo[y].nil?
      curnfo[y] = { :label => y, :items => {} }
    end
    curnfo[y][:items] = {} if curnfo[y][:items].class != Hash
    curnfo = curnfo[y][:items]
  end #end for
end

#build_menu(nfo, parent) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/vimamsa/gui_menu.rb', line 134

def build_menu(nfo, parent)
  menu = Gio::Menu.new
  if nfo[:action]
    kbd_str = ""
    for mode_str in ["C", "V"]
      c_kbd = vma.kbd.act_bindings[mode_str][nfo[:action]]
      if c_kbd.class == String
        kbd_str = "   <span foreground='#888888'><span weight='bold'>[#{mode_str}]</span> #{c_kbd}</span>"
        break
      end
    end

    label_str = nfo[:label] + kbd_str
    actkey = nfo[:action].to_s
    menuitem = Gio::MenuItem.new(label_str, "app.#{actkey}")

    # This worked in GTK3:
    # But seems there is no way to access the Label object in GTK4
    # menuitem.children[0].set_markup(label_str)

    act = Gio::SimpleAction.new(actkey)
    @app.add_action(act)
    act.signal_connect "activate" do |_simple_action, _parameter|
      call_action(nfo[:action])
    end
  else
    menuitem = Gio::MenuItem.new(nfo[:label], nil)
  end

  # Apparently requires Gtk 4.6 to work.
  # According to instructions in: https://discourse.gnome.org/t/gtk4-and-pango-markup-in-menu-items/16082
  # Boolean true here should work but doesn't yet in GTK 4.6. The string version does work.
  menuitem.set_attribute_value("use-markup", "true")
  # menuitem.set_attribute_value("use-markup", true)
  # This might change in the future(?), but the string version still works in gtk-4.13.0 (gtk/gtkmenutrackeritem.c)

  if !nfo[:items].nil? and !nfo[:items].empty?
    for k2, item in nfo[:items]
      build_menu(item, menu)
    end
    menuitem.submenu = menu
  end
  o = parent.append_item(menuitem)
end

#module_action?(action) ⇒ Boolean

Return true if action was added via add_module_action and not yet removed.

Returns:

  • (Boolean)


130
131
132
# File 'lib/vimamsa/gui_menu.rb', line 130

def module_action?(action)
  @module_actions.include?(action)
end

#remove_module_action(action) ⇒ Object

Remove a previously added module menu item.



121
122
123
124
125
126
127
# File 'lib/vimamsa/gui_menu.rb', line 121

def remove_module_action(action)
  idx = @module_actions.index(action)
  return if idx.nil?
  @module_menu.remove(idx)
  @module_actions.delete_at(idx)
  @app.remove_action(action.to_s)
end