Class: UI::HtmlDialog

继承于:
Object
  • Object
显示所有

概述

Ruby HtmlDialog类允许你从Ruby创建HTML对话框并与之互动。这是在SketchUp内部生成复杂的嵌入式UI的最好方法,但它通常需要HTML和JavaScript的专业知识。

如果你的目标是向你的用户简单地显示一个网站,考虑使用#openURL,这将在他们的默认浏览器中显示一个网页,而不是在SketchUp的一个对话框中。

对话框的左、上、宽、高等尺寸都是以逻辑单位表示的。这意味着你要提供单位,就像他们在一个 "正常 "DPI的显示器上一样。给出的单位将被乘以相同的系数,该系数由scale_factor.

关于使用例子,包括如何从旧的WebDialog类迁移,请参见github.com/SketchUp/htmldialog-examples. 你可以使用Trimble Modus框架,使你的对话框的外观和感觉与SketchUp的对话框一致。

HtmlDialog使用以下版本的CEF(Chromium Embedded Framework)。

SketchUp 2021.1

CEF 88

SketchUp 2019.0

CEF 64

SketchUp 2018.0

CEF 56

SketchUp 2017.0

CEF 52

版本。

  • SketchUp 2017

恒定摘要#

UI::HtmlDialog::STYLE_WINDOW
UI::HtmlDialog::STYLE_DIALOG
UI::HtmlDialog::STYLE_UTILITY
UI::HtmlDialog::CEF_VERSION
UI::HtmlDialog::CHROME_VERSION

实例方法总结 崩溃

构造函数的细节

#initialize(属性) ⇒HtmlDialog

注意:

前缀preference_key前缀为你的扩展特有的东西。

注意。

如果没有保留对HtmlDialog对象的引用,一旦运行垃圾回收,窗口将关闭。这种行为在琐碎的测试代码中可能会引起混淆,但在现实生活中通常不会有什么问题。通常情况下,一个持久的引用,例如一个实例变量,应该被保留下来,以便在用户第二次请求时,将对话框带到前面,而不是创建一个重复的对话框。

新方法用于创建一个新的HtmlDialog。

In SketchUp 2021.1 use_content_size被添加。当设置为true,width,height,min_width,max width,min_height,max_height将代表窗口的内容区域的大小。这不包括标题栏和窗口框架。当use_content_size被设置为false(默认值),大小尺寸将代表外框尺寸。

这些属性哈希接受一个可选的键风格其中的值是以下之一。

UI::HtmlDialog::STYLE_DIALOG

HtmlDialog会停留在SketchUp的顶部。

UI::HtmlDialog::STYLE_WINDOW

HtmlDialog可以走到SketchUp后面,并且在SketchUp失去焦点时不会消失。

UI::HtmlDialog::STYLE_UTILITY

HtmlDialog会以小标题栏显示,并保持在SketchUp的顶部。

例子:

使用选项 Hash

dialog = UI::HtmlDialog.new(
{
  :dialog_title => "对话框示例",
  :preferences_key => "com.sample.plugin",
  :rollable => 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
})
dialog.set_url("http://www.sketchup.com")
对话.show

带有关键字样式的参数

dialog = UI::HtmlDialog.new(
  dialog_title: "对话框示例",
  preferences_key: "my_name_my_extension_my_dialog",
  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
)
dialog.set_url("https://www.sketchup.com")
dialog.show

参数:

  • properties (Hash)&mdash。

    一个包含新创建的对话框的初始属性的哈希值。

选项哈希(properties):

  • :dialog_title (String)
  • :preferences_key (String)
  • :scrollable (Boolean)
  • :resizable (Boolean) — 默认:true
  • :use_content_size (Boolean) — 默认:false
  • :宽度 (整数) — 默认:250
  • :height (整数) — 默认:250
  • :left (整数) — 默认:0
  • :top (整数) — 默认:0
  • :min_width (整数) — 默认值:0
  • :min_height (整数) — 默认值:0
  • :max_width (整数) — 默认值:-1
  • :max_height (整数) — 默认:-1
  • :style (整数) — 默认:UI::HtmlDialog::STYLE_DIALOG

版本:

  • SketchUp 2017

已知的错误。

  • 在SketchUp 2019之前的:width:height的提供被忽略了,如果一个:preference_key也存在的话,所提供的 :width 和 :height 就会被忽略。为了解决这个错误,在旧版本中使用#set_size在你初始化对话框后使用#set_size。

  • SketchUp 2022.0修复了一个bug,即在以下情况下位置会被错误地设置use_content_size被设置为true.

  • SketchUp 2022.0修复了一个错误,即当设置了use_content_size为true时,位置设置不正确。max_heightmax_width被调换了。use_content_size被设置为.

  • SketchUp 2022.0修复了一个错误,即use_content_size没有持续存在(当preferences_key被设置时)。

  • SketchUp 2022.0修复了一个在Mac上的错误,即当SketchUp被关闭时,HtmlDialog的大小和位置没有被持续保留,而没有首先关闭HtmlDialog窗口。

实例方法细节

#add_action_callback(callback_name) {|action_context, ...| ... } ⇒布尔型

注意:

当一个HtmlDialog被关闭时,该实例的所有回调都被清除了。如果你需要再次打开该对话框,请重新连接它们。

列表中的#add_action_callback方法建立了一个Ruby回调方法,你的html对话框可以调用它来执行一些功能。

使用sketchup.callback_method_name来调用html对话框中的回调方法。你在html对话框中的JavaScript将以相同数量的参数调用回调。

该调用是异步的。JavaScript调用可能会在Ruby回调调用之前返回。使用onCompleted回调来获得完成的通知。

基本类型,如布尔、数字、字符串、数组和哈希值,在Ruby和JavaScript之间自动转换。

例子:

Ruby代码

dialog.add_action_callback("say") { |action_context, param1, param2|
  puts "JavaScript说#{param1}and#{param2}"
}

脚本

sketchup.('你好,世界', 42);

带回调的JavaScript

sketchup.say('你好,世界', 42, {
  onCompleted: function() {
    console.log('Ruby方面完成。');
  }
});

如果动作添加成功,返回true,否则返回false。

参数:

  • callback_name (String)

    从html对话框中调用的回调方法的名称。

Yields:

  • (action_context,...)

产量参数:

  • action_context (Object)

    action_context 目前未使用。

  • ... (Object)

    从JavaScript发送的参数。

返回:

  • (布尔值)&mdash。

    如果动作成功添加,则为true,否则为false。

版本:

  • SketchUp 2017

#bring_to_frontnil

#bring_to_front方法用于将窗口移到前面,即使它被最小化了,也要将它放在其他窗口的上面。

例子。

dialog.bring_to_front

返回:

  • (nil)

也请看:

版本:

  • SketchUp 2017

已知的错误。

  • 在SketchUp 2021.1之前,在Mac上,焦点没有被设置在UI::HtmlDialog.

#centertrue

#centermethod is used to center the HtmlDialog relative to the active model window. If there is no active model window, this function doesn't do anything.

Examples:

dialog.center

Returns:

  • (true)

    This always return true, never false.

Version:

  • SketchUp 2017

#closenil

The#closemethod is used to close a dialog box.

Examples:

dialog.close

Returns:

  • (nil)

Version:

  • SketchUp 2017

#execute_script(script) ⇒nil

The#execute_scriptmethod is used to execute a JavaScript string on the html dialog asynchronously.

Examples:

js_command = "document.getElementById('id').innerHTML = '<b>Hi!</b>'"
dialog.execute_script(js_command)

Parameters:

  • script (String)

    The JavaScript script to execute on the HtmlDialog.

Returns:

  • (nil)

Version:

  • SketchUp 2017

#get_content_sizeArray(Integer, Integer)?

The#get_content_sizemethod is used to get the content size of the HtmlDialog, in logical pixels.

Examples:

width, height = dialog.get_content_size

Returns:

  • (Array(Integer, Integer),nil)

    Content width and height of the HtmlDialog. A nil value is returned if the HtmlDialog is not visible.

Version:

  • SketchUp 2021.1

#get_positionArray(Integer, Integer)?

The#get_positionmethod is used to get the position of the HtmlDialog relative to the screen, in logical pixels.

Examples:

left, top = dialog.get_position

Returns:

  • (Array(Integer, Integer),nil)

    Left and top position of the dialog. A nil value is returned if the HtmlDialog is not visible.

Version:

  • SketchUp 2021.1

#get_sizeArray(Integer, Integer)?

The#get_sizemethod is used to get the outer frame size of the HtmlDialog, in logical pixels.

Examples:

width, height = dialog.get_size

Returns:

  • (Array(Integer, Integer),nil)

    Outer frame width and height of the HtmlDialog. A nil value is returned if the HtmlDialog is not visible.

Version:

  • SketchUp 2021.1

#set_can_closeBoolean

The#set_can_closemethod is used to attach a block that is executed just before closing, this block has to return a boolean, if the block returns false the close will be canceled.

Examples:

dialog.set_can_close { false }

Yield Returns:

  • (Boolean)

    Return a boolean to indicate if the dialogs should close.

Returns:

  • (Boolean)

Version:

  • SketchUp 2017

#set_content_size(width, height) ⇒nil

The#set_content_sizemethod is used to set the content size of the HtmlDialog, in logical pixels.

Examples:

dialog.set_content_size(600, 400)

Parameters:

  • width (Integer)

    Content width of the HtmlDialog.

  • height (Integer)

    Content height of the HtmlDialog.

Returns:

  • (nil)

Version:

  • SketchUp 2021.1

#set_file(filename) ⇒nil

The#set_filemethod is used to identify a local HTML file to display in the HtmlDialog.

Examples:

dialog.set_file("c:/mypage.html")

Parameters:

  • filename (String)

    The filename for the HtmlDialog file (HTML file)

Returns:

  • (nil)

Version:

  • SketchUp 2017

#set_html(html_string) ⇒nil

The#set_htmlmethod is used to load a HtmlDialog with a string of provided HTML.

Examples:

html = '<b>Hello world!</b>'
dialog.set_html(html)

Parameters:

  • html_string (String)

    A string of valid html to display in your HtmlDialog.

Returns:

  • (nil)

Version:

  • SketchUp 2017

#set_on_closedBoolean

The#set_on_closedmethod is used to attach a block that will be executed when a dialog is already in the process of closing, do any last minute operations within this block such as saving the current state.

Examples:

dialog.set_on_closed { save_selection }

Returns:

  • (Boolean)

Version:

  • SketchUp 2017

#set_position(left, top) ⇒true

The#set_positionmethod is used to set the position of the HtmlDialog relative to the screen, in pixels.

Examples:

dialog.set_position(100, 50)

Parameters:

  • left (Integer)

    The number of pixels from the left.

  • top (Integer)

    The number of pixels from the top of the screen.

Returns:

  • (true)

    This always return true, never false.

Version:

  • SketchUp 2017

Known Bugs:

  • Prior to SketchUp 2021.1, on Windows, calling this method incorrectly set the focus on theUI::HtmlDialog.

#set_size(width, height) ⇒true

The#set_sizemethod is used to set the outer frame size of the HtmlDialog, in pixels.

Examples:

dialog.set_size(320, 240)

Parameters:

  • width (Integer)

    Outer frame width of the HtmlDialog.

  • height (Integer)

    Outer frame height of the HtmlDialog.

Returns:

  • (true)

    This always return true, never false.

Version:

  • SketchUp 2017

Known Bugs:

  • Prior to SketchUp 2021.1, on Windows, calling this method incorrectly set the focus on theUI::HtmlDialog.

#set_url(url) ⇒nil

The#set_urlmethod is used to load a HtmlDialog with the content at a specific URL. This method allows you to load web sites in a HtmlDialog.

Examples:

dialog.set_url("https://www.sketchup.com")

Parameters:

  • url (String)

    The URL for a specific web site.

Returns:

  • (nil)

Version:

  • SketchUp 2017

#shownil

The#showmethod is used to display a non-modal dialog box.

Examples:

dialog.show

Returns:

  • (nil)

Version:

  • SketchUp 2017

#show_modalnil

The#show_modalmethod is used to display a modal dialog box.

Examples:

dialog.show_modal

Returns:

  • (nil)

Version:

  • SketchUp 2017

#visible?Boolean

The#visible?method is useful to tell if the dialog is shown and still alive, if the dialog is minimized or not visible on the screen this will still returntrue.

Examples:

if dialog.visible?
  dialog.bring_to_front
else
  dialog = UI::HtmlDialog.new
  dialog.set_url("https://www.sketchup.com")
  dialog.show
end

Returns:

  • (Boolean)

    Returns true if the dialog is open.

Version:

  • SketchUp 2017