Way to check if HtmlDialog open?

I’m programming my first plugin and I’ve come across a situation in which I want to find out if an HTML dialogue is open or not. If it is I want to close it with dialog.close which seems trivial enough but I cant find any way to get the reference to the object.

Within my method I can manipulate it easy enough but once I exit the method and return to an idle state while I wait for my user to interact with a HTML link or something else I’m stuck trying to figure out how to get back.

I guess a simple way to ask is, is there a global reference to the dialogue I can use to get back once the script has completed?

Thanks in advance!

Do not use global references. There is never any good reason for your code to be outside of your own namespace module or any of it’s plugin submodules.

Within your plugin submodule you use locally persistent references. They can be a local module constant, a @@ module variable, or a @ instance variable (since a module is an instance of class Module.)

The SketchUp application is a user driven environment that is better suited to event driven programming.

You need to stop thinking of and organizing your code in a linear manner. (ie, running a linear script as a command / macro.)

Instead you load your code and when the user activates a command (menu or toolbar button) to open the dialog, the method that creates it will assign the reference to a persistent variable within your submodule.

Where @options is a hash within your plugin submodule …

def open_dialog
  @dialog = UI::HtmlDialog.new(@options)

def dialog_open?
  !@dialog.nil? && @dialog.visible?
1 Like

Here is a more complete example …

module GB3
  module SomePlugin

    extend self
    @@loaded = false unless defined?(@@loaded)

    @options ||= {
      :dialog_title => "Plugin Dialog",
      :preferences_key => Module.nesting[0].name.gsub('::','_'),
      :scrollable => true,
      :resizable => true,
      :width => 600,
      :height => 400,
      :left => 100,
      :top => 100,
      :min_width => 50,
      :min_height => 50,
      :max_width =>1000,
      :max_height => 1000,
      :style => UI::HtmlDialog::STYLE_DIALOG

    def open_dialog
      @dialog.close if dialog_is_open?
      @dialog = UI::HtmlDialog.new(@options)

    def dialog_is_open?
      !@dialog.nil? && @dialog.visible?

    unless @@loaded # Run Once Code
      UI.menu('Plugins').add_item("My Command") { open_dialog() }
      @@loaded = true

  end # this plugin's submodule
end # author's namespace module
1 Like

I am new to Ruby so I was unaware of the @ instance variables.

This is exactly what I was looking for, thank you! I was trying to find a way to access the varialbe that i assigned the dialog to outside of the block I was in and this is exactly it. Thanks for your help!

1 Like

Go through the primers at least …