Dynamic updates of UI::Command tooltips?


#1

Is there a clean way to update a UI::Command tooltip after creation? I’m interested to embed some information about the state of model in the tooltip itself, but this requires being able to set the tooltip text after creation. I tried storing the UI::Command in a variable and setting cmd.tooltip manually. This was unsuccessful, as it seems the tooltip text is stored at creation. I thought about trying to wrap the Command object in a closure that I could pass in to UI::Command.new, but this suffers from circular reasoning: I can’t capture a reference to a variable that hasn’t been created yet.

Is there a good way to support dynamic updating of UI::Command tooltips? Better ideas on how to provide ambient information about the state of the system (apart from the status bar)?

Thanks in advance!


#2

pop a self closing WebDialog, maybe…
john


#3

Local variables often go “out of scope” and get garbage collected by Ruby’s GC.

You need to use a persistent reference. Within your extension module you can use @@some_cmd (module variable) or @sucha_cmd (instance variable.)
Both work OK for a plugin with only a few commands.

But with any more than a few, it is likely easier to have a module constant that references a hash of all the plugins commands.

module Klanea
  module ThisPlugin

    COMMAND = {}
   
    class << self
 
      def do_info_command()
        # command code here
        # update tooltip:
        COMMAND[:info].tooltip= info_tooltip()
      end
 
      def info_tooltip(arg=nil)
        if arg
          "The state is #{arg}"
        else
          "The state is #{@state_var}"
        end
      end
 
    end
 
    COMMAND[:info]= UI::Command::new("Info"){ do_info_command() }
    cmd = COMMAND[:info] # temp reference
    cmd.tooltip= info_tooltip("initial state")
    cmd.menu_text= "Some Nifty Information"
 
  end
end

If you need it even more dynamic, you could put the call to update the tooltip within a validation proc.

COMMAND[:info].set_validation_proc {
  COMMAND[:info].tooltip= info_tooltip()
  MF_ENABLED # need to return this!
}

Note that you can also dynamically change the command’s status bar text.