How to control state of toolbar icons?

I provide toolbars for my extensions. I cannot find a way to control the highlighted (“pressed”) state of the buttons on those toolbars.
What I wish happened: A button is highlighted when it is clicked on. It stays highlighted as long as its command is in operation (e.g., the preferences icon is highlighted as long as the preferences dialog box is open, or a tool’s icon is highlighted as long as the tool is in use). It is not highlighted at any other time.
What happens instead: The button gets highlighted or not at random times. Occasionally, it acts like a toggle (click to highlight, click again to un-highlight), regardless of what my extension is doing or not doing.
Is there a way to access and control this state?

You can control the toolbar icon state using UI::Command#set_validation_proc. Return MF_CHECKED to highlight (pressed), MF_UNCHECKED to unhighlight, or MF_GRAYED to disable the button.

For Ruby tools, you can define a method or variable to track the tool state:

cmd.set_validation_proc {
  Curic::Extend::ToPlaneTool.active? ? MF_CHECKED : MF_UNCHECKED
}

On macOS, set_validation_proc doesn’t always update reliably. I often click and release the middle mouse button (Orbit tool) to force the UI to refresh.

2 Likes

I would add that I always use MF_GRAYED|MF_DISABLED OR’d together as they seem to work different on each platform. (The proverbial “two birds with one stone”.)

Also, take note of the description for the UI.refresh_toolbars method.

2 Likes

Thanks to the both of you!
Sure would be nice if the API documentation mentioned that those commands work on toolbar buttons and not just menu items…

1 Like

:confused:

cognitive bias ?

The UI:::Toolbar class Overview says:

Also see the Command object for details on creating “commands” which can be called from your toolbars.

The UI::Command class Overview section says in the 1st sentence:

The Command class is the preferred class for adding tools to the menus and Ruby toolbars.

… and then the closing sentence of the Overview states:

For example, You might want a toolbar button and a context-click menu item to both point to the same command, and to control the tooltip and its “graying” from a single spot in your code.

And the UI::Command#set_validation_proc method docstring states:

The #set_validation_proc method allows you to change whether the command is enabled, checked, etc. For instance, the command toggling a dialog window may be displayed as checked while the dialog is open.

How is this for service?

:smirking_face:

1 Like

Thank you, Learned One!