Ruby callbacks not called from javascript

Hi All,
I create a dialog and save its instance for showing/hiding.
When I show it first everything works fine. If I close the dialog and show it again then sketchup methods are not called. Here is the test code

  d = UI::HtmlDialog.new()
      d.set_html(%[<!DOCTYPE html>
<html>
   <head>
      <link rel="stylesheet" href="./../css/style.css" type="text/css" />
      <title>Test</title>
   </head>
   <body>
      <script>
        window.onload = function() {
          sketchup.onloaded();
        }
      </script>
   </body>
</html>
])
      d.add_action_callback("onloaded") do |wd|
        puts "On loaded called from js"
        d.close
        d.show
      end
      d.show {
        puts "Show the dialog"
      }

I know it is recursive but it is not behaving as it should.
Javascript error : sketchup.onloaded is not a function (sketchup instance exists)
What I am doing wrong here ? or I have to create new instance of dialog everytime ?

nothing, it was broken in an attempt to stop CEF crashing SU during application close…

the crashes were deemed to be caused when javascript was left ‘open’ in CEF by extensions, but why it needed to clear the ruby callbacks is a mystery…

on a mac it also uses a different window ‘style’ when opened more than once…

you don’t want to make additional UI::HtmlDialog.new()

but you do need to re-add the call backs

@d = UI::HtmlDialog.new()
@d.set_html(%[<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./../css/style.css" type="text/css" />
<title>Test</title>
</head>
<body>
Message
<script>
  window.onload = function() {
    sketchup.onloaded();
  }
</script>
</body>
</html>
])

def dlg
  # add the callback each time...
  @d.add_action_callback("onloaded") do |wd|
    puts "On loaded called from js from #{@d}"
    @d.close
  end
  # this happens first
  @d.show 
  # #show{} this only works with WebDialogs
  # not HtmlDialogs, so put 'puts' on new line
  puts "Show the dialog"
end

john

If you call dlg multiple times onloaded callback is not called everytime.

Although it works for WebDialog but I do not want to switch WebDialog as it is deprecated.

The behaviour of HtmlDialog where you need to re-register action callbacks is described in our HtmlDialog examples. (Though we should update the docs to make this clearer.)

The examples can be found here: GitHub - SketchUp/htmldialog-examples: Examples of using SketchUp Ruby API's UI::HtmlDialog class

Example 02 demonstrate how the action callbacks are lost between close/reopen. Example 03 demonstrate how to make it reusable: htmldialog-examples/step03.rb at main · SketchUp/htmldialog-examples · GitHub

@tt_su, why does it change the formatting?

I added some extra css to illustrate what I mean…

the only way to avoid this is to make an entirely new object…

EDIT: this is a mac only bug…

CommonWebDialog.nib file, contains a controls box that is being used by bring_to_front or a second show

the nib file doesn’t contain a webView at all, so that must be generated programatically…

john

Thanks @tt_su.
Lot to learn.

I’ve not seen that before. Can you report this in the Ruby API issue tracker please?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.