Function: toggle toolbar
-
Hello there
For the project i'm coding on these days, i need to create a command, on a toolbar, which toggle another toolbar.
The method get_last_state from the class Toolbar looks convenient for that but i noticed a strange behaviour.Here is my code for defining my toolbar and the function that toggle the other toolbars.
#here is the function for toggling my toolbar def toggle(toolbar) toolbar.show if toolbar.get_last_state == -1 toolbar.show if toolbar.get_last_state == 0 toolbar.hide if toolbar.get_last_state == 1 end #and then, the block defining my toolbars main_tb= UI;;Toolbar.new("main toolbar") cmd= UI;;Command.new("Toggle the second toolbar"){toggle(second_tb)} cmd.large_icon = cmd.small_icon = File.join(path_to, "maintb.png") cmd.tooltip = cmd.status_bar_text= "Toggle the second toolbar" main_tb.add_item cmd main_tb.show main_tb= UI;;Toolbar.new("my toolbar") second_tb= UI;;Toolbar.new("second toolbar") cmd= UI;;Command.new("hello"){UI.Messagebox("hello")} cmd.large_icon = cmd.small_icon = File.join(path_to, "secondtb.png") cmd.tooltip = cmd.status_bar_text= "hello" second_tb.add_item cmd
But this code works for showing, not for hiding...
hu??so i checked manually the method get_last_state directly in Sketchup, using the ruby console
I noticed that the value returned by get_last_state never change, whatever i do with show and hidetb= UI;;Toolbar.new("my toolbar") #<UI;;Toolbar;0x4b22d08> cmd= UI;;Command.new("my cmd"){UI.Messagebox("hello")} #<UI;;Command;0x4b22ae0> tb.add_item cmd #<UI;;Toolbar;0x4b22d08> tb.get_last_state -1 tb.show nil tb.get_last_state -1 tb.hide nil tb.get_last_state -1
is there a problem with this method?
did i miss something?thank you for your time
-
You could try using toolbar.visible? instead. That gave accurate results for me when I tested it manually.
Chris
-
tb= UI;;Toolbar.new("my toolbar") cmd= UI;;Command.new("my cmd"){UI.Messagebox("hello")} tb.add_item cmd if tb.visible? tb.hide else tb.show end
That works for me. I'm running it in the webconsole by Jim (thanks again Jim) which is much more useful than the ruby console for little snippets of code.
When I put that into the console, it all works swimmingly well. Toolbar on and off
Chris
Edit: I like this idea. It gives me ideas. I might use something like this in future scripts.
-
Thank you Chris.
I'll check that when i'll get back to work next monday.it's such a fast answer to my very first post here.
This forum looks nice for sharing knowledge and ideas ! -
It works!
Thank you again!
-
Woohoo! That might be my first correct response in this forum I'm glad it helped,
Chris
Advertisement