Webdialog visible/open?
-
Probably an easy one; how do I check (while I'm drawing f.e.) whether my dialog is still open/visible or not?
I've got something like this (concentrate on the bold part):class Show Dialog # works great, I can access my dialog def initialize my_dialog = UI;;WebDialog.new("Information", true, "", 400, 400, 100, 100, true) my_dialog.add_action_callback("get_data") do |web_dialog,action_name| #......some nice code end html_path = Sketchup.find_support_file "webdialog.html" ,"Plugins/webdialogs" my_dialog.set_file(html_path) my_dialog.show() end end
and an observer where I would like to check if the webdialog is currently open while I'm drawing:
class addEntities < Sketchup;;EntitiesObserver def onElementAdded(entities, entity) #......some nice code (works!) my_dialog = UI;;WebDialog.new("Information", true, "", 400, 400, 100, 100, true) vis = my_dialog.visible? UI.messagebox vis # this returns false, even if the dialog is open... end end
-
How can it be open when you don't call the
.show
method?my_dialog = UI;;WebDialog.new("Information", true, "", 400, 400, 100, 100, true) vis = my_dialog.visible?
That will never display a WebDialog.
my_dialog = UI;;WebDialog.new("Information", true, "", 400, 400, 100, 100, true) my_dialog.show vis = my_dialog.visible?
That should show the dialog and return true.
-
hm...
class Show Dialog
<- that is not a valid class name - space in it. I guess you renamed for this example?You then create a WebDialog - I suppose this is the dialog you want to be using?
Then in your observer:
class addEntities < Sketchup::EntitiesObserver
(Also invalid name as it starts with a lower case...)You are creating a new WebDialog - and it appear that you expect that to be the same as the one you created in
class Show Dialog
?It helps if you post a real example that doesn't work - the snippets you have here is just plain invalid. Hard to make a guess.
-
Thnx for lookling into it Thomthom!
You're right for this example: I made some typing mistakes...sorry!This is the order of appaerance:
-
In an early stage I call my webdialog by initializing ShowDialog (which contains an info-table). After calling the dialog, I leave it open (or I might close it...whatever), and go on drawing stuff, make some coffee etc. etc.
-
Somewhere later I attach the EntitiesObserver "AddEntities" by code. From this point, when I'm drawing I would like Ruby to know if the webdialog is still open. If open, I would like to refresh the content of the webdialog. But how does Ruby know if the webdialog is still open? I thought by using .visible?. But for using that I need to reach the object webdialog. And I don't know how...
I don't have a clue how to attach code propperly, but here's the code with minimized abstraction:
require 'sketchup.rb' #======================================================================================================================================== class ShowDialog def initialize my_dialog = UI;;WebDialog.new("Information", true, "", 400, 400, 100, 100, true) my_dialog.add_action_callback("get_data") do |web_dialog,action_name| #...some code here... end html_path = Sketchup.find_support_file "tableHTML.html" ,"Plugins/Toolbar_v2/webdialogs" my_dialog.set_file(html_path) my_dialog.show() end end #======================================================================================================================================== class AddEntities < Sketchup;;EntitiesObserver def onElementAdded(entities, entity) #...some code here #here I would like to check if the dialog (created earlier..or not) is open/visible #But how do I reach the webdialog? I don't want to create a new one, I would liketo reach the open one... end end #========================================================================================================================================
-
-
Keep a reference to your WebDialog.
You should be doing this for the lifespan of the Webdialog anyway - otherwise it might be garbage collected and close.
At the moment your reference to it lives only within the init method of your class. From that point on you're at the grace of the GC.
Use an instance variable to keep track of your webdialog.
class ShowDialog attr_reader ;window def initialize @window = UI;;WebDialog.new("Information", true, "", 400, 400, 100, 100, true) # ... end end
Now you can access the webdialog via the accessors in your instance of ShowDialog.
class AddEntities < Sketchup;;EntitiesObserver def initalize(show_dialog) @show_dialog_instance = show_dialog end def onElementAdded(entities, entity) webdialog = @show_dialog_instance.window webdialog.visible? end end
-
Though, I think there might be problems getting the WebDialog and Observer to be garbage collected if you keep a reference to the webdialog in the observer class. Depending on how the rest of your code looks like you might want to use a different method of getting the instance of ShowDialog - like using a Singleton/Factory pattern for ShowDialog.
-
Hmmmm. can't get it working right now, hope tomorrow will bring me sunshine. Thnx for your help Thomthom, I will ty to get it working
-
Well, bad news: its snowing outside...no sun for me
I get the part of making an instance variable in the class ShowDialog.
But I still cannot reach it in de Observer with the code you gave to me. What is passed to "def initialize(show_dialog)"??? I put a msgbox there, but it never gets triggered... Because I don't understand what is happening in "def initialize(show_dialog)" , I also don't understand the part of "webdialog = @show_dialog_instance.window" in "def onElementAdded(entities, entity)" neither. The code fails after this part... Shouldn't it be something like ShowDialog.my_dialog or something...(also this aint working)??? -
HArd to tell without seeing the whole of your code.
But the essence is that you need to pass a reference to the webdialog to your observer. Wither by arguments, or by a singleton pattern. -
@hgroeneveld said:
What is passed to "
def initialize(show_dialog)
"???
Theinitialize()
method is an "internal" Ruby method that all Ruby class objects have. IF you do not override it, the class just inherits a copy from it'ssuperclass
.This
initialize()
method is called automatically by Ruby at the end of thenew()
class constructor method, afternew()
has created the instance. Realize thatinitialize()
is an instance method, andnew()
is a class method, therefornew()
calls theinitialize()
instance method, in the instance object, that it just created, passing along the entire argument list it itself received.So when you do:
@dlg = ShowDialog.new("Plugin Options")
The
new()
method (internally,) will create the new instance of theShowDialog
class, (referenced internally asobj
,) and THEN call the new instance'sinitialize()
instance method, passing it the string argument"Plugin Options"
, like:
obj.initialize("Plugin Options")
Lastly, the
new()
method returns it's internal local reference toobj
, which you then assign to a reference in the outer scope. In the example above, that reference is the@dlg
variable.@hgroeneveld said:
I put a msgbox there, but it never gets triggered...
UI.messagebox()
is a Ruby method, that when evaluated (after the messagebox closes,) returnsnil
, so in effect you would be passingnil
into theinitialize()
method.So.. whatever arguments that you want to pass into the new instance, at creation time, are passed via the class constructor method
new()
, and YOU must override the instance methodinitialize()
in order to process those arguments, whatever they may be. (Usually by assigning them to instance variables that the instance will later use.)This is all in Ruby, Part I, Chapter 2, Classes, Objects, and Variables
Advertisement