Wednesday, December 19, 2007

WebDialog

One of the interesting features of SketchUp's Ruby API is the UI::WebDialog class. Since WebDialog supports DHTML and JavaScripts, you can provide a rich, custom user experience for your SketchUp plugin users. You may be thinking, just another dialog box pretending to be a browser-lite, right? But there's more: with WebDialog you can call Ruby code from within the DHTML content of the dialog's document. No, this isn't some kind of listen-to-port-80-and-redirect trick. You define a block of execution in your Ruby code, assign it to the WebDialog object by name, and when the user selects the input element with that name the block is executed. Let's say you have an input element such as this:

<a href="skp:myAction@Hello">say hi!</a>

Now, all you have to do is define a block of execution and assign it with the name "myAction."

dialog.add_action_callback("myAction") { |d, p| puts p }

When the user clicks on the input element, it's the equivalent to

proc = Proc.new { |d, p| puts d}
proc.call(dialog, "Hello")

Simple and useful.

4 comments:

Unknown said...

This is awesome! Thanks to this post I now have a much nicer dialog for my script :)

I noticed a couple things:
The examples in the docs are not accurate like the blog post (inconsistent action)
WebDialog is missing from: these docs
Are those docs not used anymore?

I also tried using set_file and it doesn't do the nice ruby interpretation.

And I have a slightly unrelated question, is latlong being worked on? it still doesn't work.

Thanks!
-Ian

Unknown said...

Hi Ian,

The WebDialog is created using UI::WebDialog.new(params, .., ..)You won't find any WebDialog methods in the UI class you need to look in the WebDialog class WebDialog Class (http://download.sketchup.com/sketchuphelp/gsu6_ruby/Docs/Ruby-WebDialog.html)

Also, the set_file methods works but you need to put the file(html file) name first and then a absolute or relative path to that file example: mydialog.set_file("file.html", "/path/")

I'm not sure about the state of latlong, I'd hate to give you misinformation.

Hope this helps

Brad

Unknown said...

Brad.

Thanks for the response! I see I didn't look hard enough at the docs :)

I did get the mydialog.set_file reading in a file, it was just that things like #{somevar} and skb:action@param weren't interpreted, they were simply rendered as text. I ended up just using mydialog.set_html since it wasn't that much, and that worked wonderfully!

Thanks again!
-Ian

Anonymous said...

Wonderful, I know such function exists, but only today i really had to use it. As other post mentioned, docs are little hard to understand about webdialog.

Thanks