Editing toolbar tooltips on the fly


#1

As with most extensions, I create toolbars with tooltips at initial load. Typically each command is created and a tooltip assigned is code like this:

cmd = UI::Command.new(“mycommandA”) { Sketchup::active_model.select_tool self.mycomandA }
cmd.tooltip = “My commandA tool tip”

Typically I reuse the “cmd” variable multiple times, once for each command.

But if I want to change the tool tip later depending on circumstances or settings changes, what do I do?. I see no way to refer to an existing command by name.

I could of course at load time make each “cmd” a unique class variable as in “@@cmdA”, so I could refer to it later. But I’m wondering if there is a way to refer to an existing command by name?


Dynamic tooltips and status text
#2

That would require to enforce a name is a unique identifier, before you could lookup commands by string.

The actual problem you have is managing references and referencing a command in some way later. That means you would a) just not reuse the variable but use different variables for different commands and b) use a persistent reference that is not garbage collected (instead of a local variable).
(Whether that is a class variable or constant is an implementation detail.)

As far as I remember, button tooltips can not be changed later once the command has been added to the GUI.


#3

Yes they can, as well as status text. I do it all the time. Works on Windows at least. (It is the menu text that you cannot change.)

Here is an example of using hashes (and avoiding the cmd local reference altogether):

module Author
  module SomePlugin
  
    DIR = File.dirname(__FILE__)

    COMMANDS  = {} unless defined?(COMMANDS)
    
    MENUCHECK = {} unless defined?(MENUCHECK)
    MENU_TEXT = {} unless defined?(MENU_TEXT)
    
    TOOLTIP_ON  = {} unless defined?(TOOLTIP_ON)
    TOOLTIP_OFF = {} unless defined?(TOOLTIP_OFF)

    STATUS_TEXT_ON  = {} unless defined?(STATUS_TEXT_ON)
    STATUS_TEXT_OFF = {} unless defined?(STATUS_TEXT_OFF)

    ###  Text containing hashes are loaded from Language files
    ###  here, based upon return value of Sketchup::get_locale()
    
    if COMMANDS[:some_nifty_name].nil?

      MENUCHECK[:some_nifty_name]= MF_UNCHECKED

      COMMANDS[:some_nifty_name]= UI::Command::new('X']) {
      
        some_nifty_method()
        
      }.instance_eval {
      
        set_validation_proc { MENUCHECK[:some_nifty_name] }

        menu_text= MENU_TEXT[:some_nifty_name]

        status_bar_text= STATUS_TEXT_OFF[:some_nifty_name]
        tooltip= TOOLTIP_OFF[:some_nifty_name]
        
        small_icon= File.join(DIR,'images/nifty_16.png')
        large_icon= File.join(DIR,'images/nifty_24.png')
      
      }

    end

    if COMMANDS[:another_command].nil?
    
      # Same kind of code but for another command.

    end

  end
end

It also means a command can be accessed from outside like:

Author::SomePlugin::COMMANDS[:some_nifty_name]

which would have advantages for say a WebDialog based custom toolbar maker (like Aerilius’.)