Class: Teek::UI::MenuBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/teek/ui/menu_builder.rb

Overview

The build surface inside a menu_bar/menu/context_menu block - a separate, small vocabulary from WidgetDSL (deliberately NOT mixed into Session/yielded as self the way ordinary containers are), since menu entries reuse names ordinary widgets already own (checkbox/radio are ttk widgets one level up, menu entry kinds one level down here) - a shared receiver would collide.

Menu structure realizes through Realizer#create_menu_tree rather than the generic per-node widget-creation path: nothing here is a Tk widget of its own except #menu (a nested cascade, itself a menu command) - #item/#checkbox/#radio are entries added to their parent's menu path, with no live Tk path of their own, addressed via their WidgetType#addressing strategy (MenuEntryAddressing) the same way Handle resolves any other type's - see WidgetDSL#[]. #separator stays unaddressable (nothing to enable/disable/relabel on a divider).

Instance Method Summary collapse

Constructor Details

#initialize(document, stack) ⇒ MenuBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MenuBuilder.



25
26
27
28
# File 'lib/teek/ui/menu_builder.rb', line 25

def initialize(document, stack)
  @document = document
  @stack = stack
end

Instance Method Details

#checkbox(name = nil, label:, bind:, **opts) ⇒ Handle

A checkbutton entry, bound to a reactive Var - ticked when the var is true, unticked when false, the same bind: convention WidgetDSL's own checkbox widget uses.

Parameters:

  • name (Symbol, nil) (defaults to: nil)

    see #item

  • label (String)
  • bind (Var)
  • opts (Hash)

    extra Tk menu-entry options

Returns:



84
85
86
# File 'lib/teek/ui/menu_builder.rb', line 84

def checkbox(name = nil, label:, bind:, **opts)
  Handle.new(append_entry(:menu_checkbox, name, opts.merge(label: label, bind: bind)))
end

#item(name = nil, label:, **opts) { ... } ⇒ Handle

A command entry.

Parameters:

  • name (Symbol, nil) (defaults to: nil)

    for ui[:name] lookup - addressable later as a Handle (.enable/.disable/.configure)

  • label (String)
  • opts (Hash)

    extra Tk menu-entry options (e.g. accelerator:)

Yields:

  • called when the entry is invoked

Returns:



64
65
66
67
# File 'lib/teek/ui/menu_builder.rb', line 64

def item(name = nil, label:, **opts, &block)
  opts = opts.merge(command: block) if block
  Handle.new(append_entry(:menu_item, name, opts.merge(label: label)))
end

A nested cascade - recursive, so the same method builds both a menu_bar's top-level dropdowns (File/Edit/...) and any submenu nested inside one of those.

Parameters:

  • name (Symbol, nil) (defaults to: nil)
  • label (String)

    the cascade's displayed label

  • opts (Hash)

    extra Tk menu-entry options (e.g. underline:)

Yield Parameters:

  • m (MenuBuilder)

    this same builder, scoped to the new menu

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/teek/ui/menu_builder.rb', line 38

def menu(name = nil, label:, **opts, &block)
  node = @document.create(type: :menu, name: name, opts: opts.merge(label: label))
  @stack.last.add_child(node)

  if block
    @stack.push(node)
    @document.notify(:push, node, current_path)
    begin
      block.call(self)
    ensure
      path = current_path
      @stack.pop
      @document.notify(:pop, node, path)
    end
  end

  Handle.new(node)
end

#radio(name = nil, label:, bind:, value:, **opts) ⇒ Handle

A radiobutton entry - bind: is shared across every radio entry in the group, value: is what this one entry sets it to when chosen.

Parameters:

  • name (Symbol, nil) (defaults to: nil)

    see #item

  • label (String)
  • bind (Var)
  • value (Object)
  • opts (Hash)

    extra Tk menu-entry options

Returns:



96
97
98
# File 'lib/teek/ui/menu_builder.rb', line 96

def radio(name = nil, label:, bind:, value:, **opts)
  Handle.new(append_entry(:menu_radio, name, opts.merge(label: label, bind: bind, value: value)))
end

#separatornil

A separator entry.

Returns:

  • (nil)


71
72
73
74
# File 'lib/teek/ui/menu_builder.rb', line 71

def separator
  append_entry(:menu_separator, nil, {})
  nil
end