Class: Ruflet::UI::Controls::RufletComponents::WebViewControl

Inherits:
Control
  • Object
show all
Defined in:
lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb

Overview

WebView control — parity with Flet's WebView (https://flet.dev/docs/controls/webview/).

Properties: url, bgcolor, prevent_links, plus the usual layout props. Events: on_page_started, on_page_ended, on_web_resource_error, on_progress, on_url_change, on_scroll, on_console_message, on_javascript_alert_dialog. Methods (invoked over the wire on a mounted control): reload, go_back, go_forward, can_go_back, can_go_forward, run_javascript, load_html, load_request, load_file, scroll_to, scroll_by, clear_cache, clear_local_storage, enable_zoom, disable_zoom, set_javascript_mode, get_current_url, get_title, get_user_agent.

Platform note: the native webview runs on iOS, Android, macOS, Windows, and Linux. Linux distributions must provide WebKitGTK 4.1. On web it uses an iframe, so browser cross-origin restrictions still apply.

Constant Summary collapse

TYPE =
"WebView".freeze
WIRE =
"WebView".freeze

Constants inherited from Control

Control::HOST_EXPANDED_TYPES, Control::SCHEMA_METADATA_CACHE

Instance Attribute Summary

Attributes inherited from Control

#children, #id, #props, #runtime_page, #type, #wire_id

Instance Method Summary collapse

Methods inherited from Control

#attach_handler, #emit, generate_id, #has_handler?, #merge_props, #on, #to_patch

Constructor Details

#initialize(id: nil, bgcolor: nil, data: nil, enable_javascript: nil, expand: nil, height: nil, key: nil, method: nil, opacity: nil, prevent_links: nil, rtl: nil, tooltip: nil, url: nil, visible: nil, width: nil, on_page_ended: nil, on_page_started: nil, on_web_resource_error: nil, on_progress: nil, on_url_change: nil, on_scroll: nil, on_console_message: nil, on_javascript_alert_dialog: nil) ⇒ WebViewControl

Returns a new instance of WebViewControl.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 27

def initialize(id: nil, bgcolor: nil, data: nil, enable_javascript: nil, expand: nil,
               height: nil, key: nil, method: nil, opacity: nil, prevent_links: nil,
               rtl: nil, tooltip: nil, url: nil, visible: nil, width: nil,
               on_page_ended: nil, on_page_started: nil, on_web_resource_error: nil,
               on_progress: nil, on_url_change: nil, on_scroll: nil,
               on_console_message: nil, on_javascript_alert_dialog: nil)
  props = {}
  props[:bgcolor] = bgcolor unless bgcolor.nil?
  props[:data] = data unless data.nil?
  props[:enable_javascript] = enable_javascript unless enable_javascript.nil?
  props[:expand] = expand unless expand.nil?
  props[:height] = height unless height.nil?
  props[:key] = key unless key.nil?
  props[:method] = method unless method.nil?
  props[:opacity] = opacity unless opacity.nil?
  props[:prevent_links] = prevent_links unless prevent_links.nil?
  props[:rtl] = rtl unless rtl.nil?
  props[:tooltip] = tooltip unless tooltip.nil?
  props[:url] = url unless url.nil?
  props[:visible] = visible unless visible.nil?
  props[:width] = width unless width.nil?
  props[:on_page_ended] = on_page_ended unless on_page_ended.nil?
  props[:on_page_started] = on_page_started unless on_page_started.nil?
  props[:on_web_resource_error] = on_web_resource_error unless on_web_resource_error.nil?
  props[:on_progress] = on_progress unless on_progress.nil?
  props[:on_url_change] = on_url_change unless on_url_change.nil?
  props[:on_scroll] = on_scroll unless on_scroll.nil?
  props[:on_console_message] = on_console_message unless on_console_message.nil?
  props[:on_javascript_alert_dialog] = on_javascript_alert_dialog unless on_javascript_alert_dialog.nil?
  super(type: TYPE, id: id, **props)
end

Instance Method Details

#can_go_back(timeout: 10, &on_result) ⇒ Object



65
66
67
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 65

def can_go_back(timeout: 10, &on_result)
  invoke_webview_method("can_go_back", timeout: timeout, on_result: on_result)
end

#can_go_forward(timeout: 10, &on_result) ⇒ Object



69
70
71
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 69

def can_go_forward(timeout: 10, &on_result)
  invoke_webview_method("can_go_forward", timeout: timeout, on_result: on_result)
end

#clear_cacheObject

--- Storage / zoom ----------------------------------------------



114
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 114

def clear_cache = invoke_webview_method("clear_cache")

#clear_local_storageObject



115
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 115

def clear_local_storage = invoke_webview_method("clear_local_storage")

#disable_zoomObject



117
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 117

def disable_zoom = invoke_webview_method("disable_zoom")

#enable_zoomObject



116
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 116

def enable_zoom = invoke_webview_method("enable_zoom")

#get_current_url(timeout: 10, &on_result) ⇒ Object

--- Introspection (result delivered to the block) ---------------



121
122
123
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 121

def get_current_url(timeout: 10, &on_result)
  invoke_webview_method("get_current_url", timeout: timeout, on_result: on_result)
end

#get_title(timeout: 10, &on_result) ⇒ Object



125
126
127
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 125

def get_title(timeout: 10, &on_result)
  invoke_webview_method("get_title", timeout: timeout, on_result: on_result)
end

#get_user_agent(timeout: 10, &on_result) ⇒ Object



129
130
131
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 129

def get_user_agent(timeout: 10, &on_result)
  invoke_webview_method("get_user_agent", timeout: timeout, on_result: on_result)
end

#go_backObject



62
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 62

def go_back = invoke_webview_method("go_back")

#go_forwardObject



63
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 63

def go_forward = invoke_webview_method("go_forward")

#load_file(path) ⇒ Object



85
86
87
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 85

def load_file(path)
  invoke_webview_method("load_file", { "path" => path.to_s })
end

#load_html(value, base_url: nil) ⇒ Object



79
80
81
82
83
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 79

def load_html(value, base_url: nil)
  args = { "value" => value.to_s }
  args["base_url"] = base_url.to_s unless base_url.nil?
  invoke_webview_method("load_html", args)
end

#load_request(url, method: "get") ⇒ Object

--- Loading content ---------------------------------------------



75
76
77
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 75

def load_request(url, method: "get")
  invoke_webview_method("load_request", { "url" => url.to_s, "method" => method.to_s })
end

#reloadObject

--- Navigation --------------------------------------------------



61
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 61

def reload = invoke_webview_method("reload")

#run_javascript(value) ⇒ Object

Run arbitrary JS inside the page — e.g. hide a node so a native control can take its place:

webview.run_javascript("document.getElementById('banner').remove()")


94
95
96
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 94

def run_javascript(value)
  invoke_webview_method("run_javascript", { "value" => value.to_s })
end

#scroll_by(x, y) ⇒ Object



108
109
110
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 108

def scroll_by(x, y)
  invoke_webview_method("scroll_by", { "x" => x.to_i, "y" => y.to_i })
end

#scroll_to(x, y) ⇒ Object

--- Scrolling ---------------------------------------------------



104
105
106
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 104

def scroll_to(x, y)
  invoke_webview_method("scroll_to", { "x" => x.to_i, "y" => y.to_i })
end

#set_javascript_mode(mode) ⇒ Object



98
99
100
# File 'lib/ruflet_ui/ruflet/ui/controls/materials/webview_control.rb', line 98

def set_javascript_mode(mode)
  invoke_webview_method("set_javascript_mode", { "mode" => mode.to_s })
end