sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Webdialog visible/open?

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 3 Posters 193 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      hgroeneveld
      last edited by

      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
      
      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        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.

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          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.

          Thomas Thomassen β€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • H Offline
            hgroeneveld
            last edited by

            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
            
             #========================================================================================================================================
            
            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              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
              

              Thomas Thomassen β€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                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.

                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • H Offline
                  hgroeneveld
                  last edited by

                  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

                  1 Reply Last reply Reply Quote 0
                  • H Offline
                    hgroeneveld
                    last edited by

                    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)???

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      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.

                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        @hgroeneveld said:

                        What is passed to " def initialize(show_dialog)"???
                        The initialize() 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's superclass.

                        This initialize() method is called automatically by Ruby at the end of the new() class constructor method, after new() has created the instance. Realize that initialize() is an instance method, and new() is a class method, therefor new() calls the initialize() 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 the ShowDialog class, (referenced internally as obj,) and THEN call the new instance's initialize() instance method, passing it the string argument "Plugin Options", like:
                        obj.initialize("Plugin Options")

                        Lastly, the new() method returns it's internal local reference to obj, 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,) returns nil, so in effect you would be passing nil into the initialize() 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 method initialize() 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

                        πŸ’­

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement