Module: Teek::MenuInterceptor Private

Defined in:
lib/teek/menu_interceptor.rb

Overview

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

Live-scan helper for the "menu" CommandInterceptors entry below.

Menu entries are not windows (only the menu itself is one), so they never fire ; entry deletion is silent and Tk renumbers the survivors internally. Because of that, entry-level callbacks can't be tracked by index or by any per-entry event - the only sound way to know which callbacks are still needed is to ask Tk what's actually live after every mutating call and release whatever dropped out.

Constant Summary collapse

ENTRY_SUBCOMMANDS =

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

%w[add insert entryconfigure delete].freeze
LIVE_COMMANDS_TCL_PROC =

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

<<~TCL.freeze
  proc ::teek_menu_live_commands {path} {
    set result {}
    if {![winfo exists $path]} { return $result }
    set last [$path index end]
    if {$last eq "none"} { return $result }
    for {set i 0} {$i <= $last} {incr i} {
      set cmd ""
      catch {set cmd [$path entrycget $i -command]}
      lappend result $cmd
    }
    return $result
  }
TCL

Class Method Summary collapse

Class Method Details

.live_command_ids(app, path) ⇒ Object

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.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/teek/menu_interceptor.rb', line 35

def self.live_command_ids(app, path)
  app.ensure_tcl_helper(:menu_live_commands) { LIVE_COMMANDS_TCL_PROC }
  raw = app.tcl_eval("::teek_menu_live_commands #{path}")
  # \z anchors this to a BARE `ruby_callback <id>` with nothing after -
  # correct today because teek never appends %-substitutions to a menu
  # entry's -command (that only happens for App#bind's widget bindings).
  # If substitution support is ever added here, this regex silently
  # stops matching and those ids leak on rebuild/delete - drop the \z
  # anchor (match just the leading `ruby_callback <id>`) if that changes.
  app.split_list(raw).each_with_object({}) do |cmd, ids|
    ids[Regexp.last_match(1)] = Regexp.last_match(1) if cmd =~ /\Aruby_callback (\S+)\z/
  end
end