Get Toolbars from ObjectSpace
-
I can get a collection of UI::Toolbar objects using the following code:
toolbars = [] ObjectSpace.each_object(UI::Toolbar) {|t| toolbars.push(t)}
But, in some plugins, the Toolbar is created in a module namespace:
module JF tb = UI::Toolbar.new("Hello Toolbar") end
And these do not show up when using the ObjectSpace code. Is there a way to find them also?
-
Hmm, that's not what I'm seeing. The tool I'm working on, I create the toolbar from within my module. But it comes up just fine with your code, slightly modified to show names
ObjectSpace.each_object(UI::Toolbar) {|t| puts t.name}
I guess it probably won't show them if they are created after SU loads though, since they are not created in SU.
-
You are right - my bad, I see the Toolbar also. Just an oversight on my part.
However, when I do the show/hide on a Ruby Toolbar, it looks like a new Toolbar object is created each time, so multiple Toolbars of the same name (but different id's) are being created using UI.toolbar("name") to access the Toolbar.
UI.toolbar("Test").object_id
returns a new value each time it is called.5.times {p UI.toolbar("Jim").object_id} 86540730 86540700 86540670 86540640 86540610
-
@jim said:
However, when I do the show/hide on a Ruby Toolbar, it looks like a new Toolbar object is created each time, ...
This is a Windows thing. For instance each time the Console window is shown (then hidden,) then shown again, a new Window instance is created. (It's handle changes.) Since toolbars are also window objects, they'll have the same behavour.
-
Thanks Dan.
I was seeing a lot of what looked like unnecessary Toolbars objects being created in ObjectSpace after using UI.toolbar("name"), but I see they go away after a GC.start - so I guess this is a non-issue.
-
Ok, I see it now.
If I create a Toolbar like so:
cmd = UI::Command.new("Command") { do_something } UI.toolbar("Do Something").add_item(cmd)
or
UI::Toolbar.new("Do Something").add_item(cmd)
The Toolbar does not show up in ObjectSpace (although does exist in SketchUp,) presumably because there is no reference to it?
If I do it this way:
cmd = UI::Command.new("Command") { do_something } tb = UI.toolbar("Do Something") tb.add_item(cmd)
The Toolbar does show in ObjectSpace, even when tb is a local var that goes out of scope.
-
Yea that's weird. I never recommend using any constructor without making sure that there is a reference to it for later use.
We are still lacking (auto-generated) collections for many types of things, incl. toolbars.
Either the collection object would be "held" by the
UI
module (ie:UI::ToolbarSet
,) or "held" by the class itself, and accessed via a class method.
Advertisement