Class: MittensUi::FileMenu

Inherits:
Core
  • Object
show all
Defined in:
lib/mittens_ui/file_menu.rb

Overview

A menu bar widget that renders a GTK4 popover menu bar with dropdown menus. Menu items are defined as a nested hash and automatically become callable methods on the FileMenu instance. Wraps Gtk::PopoverMenuBar backed by Gio::Menu.

Examples:

Basic usage

menus = {
  "File": { sub_menus: ["New", "Open", :separator, "Exit"] },
  "Edit": { sub_menus: ["Copy", "Paste"] }
}.freeze
fm = MittensUi::FileMenu.new(menus)
fm.exit  { MittensUi::Application.exit }
fm.new   { puts "New file" }

Nested submenus

menus = {
  "File": {
    sub_menus: [
      { "Recent": ["file1.rb", "file2.rb"] },
      :separator,
      "Exit"
    ]
  }
}.freeze

Instance Attribute Summary

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

Methods inherited from Core

#hidden?, #hide, #keyboard_shortcut, #remove, #remove_keyboard_shortcut, #render, #shortcuts, #show

Methods included from Helpers

#icon_map, #list_system_icons, #set_margin_from_opts_for

Constructor Details

#initialize(menu_items, options = {}) ⇒ FileMenu

Creates a new FileMenu widget.

Parameters:

  • menu_items (Hash)

    nested hash defining the menu structure. Top level keys are menu names. Each value is a hash with a :sub_menus key containing an array of items. Items can be:

    • String — a clickable menu item

    • :separator — a horizontal divider line

    • Hash — a nested submenu

  • options (Hash) (defaults to: {})

    configuration options

Options Hash (options):

  • :width (Symbol) — default: :full

    column width in the layout grid

  • :defer_render (Boolean) — default: false

    skip auto-rendering into layout



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mittens_ui/file_menu.rb', line 44

def initialize(menu_items, options = {})
  @menu_items     = menu_items
  @raw_actions    = {}
  @handlers       = {}
  @action_group   = Gio::SimpleActionGroup.new

  @gio_menu = build_gio_menu
  @menu_bar = Gtk::PopoverMenuBar.new(@gio_menu)

  create_menu_methods

  super(@menu_bar, options)
end