webview_util 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebviewUtil
4
+ VERSION = "0.1.3"
5
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ffi"
4
+ require "ffi-compiler/loader"
5
+ require "json"
6
+ require_relative "webview_util/version"
7
+
8
+ module WebviewUtil
9
+ extend FFI::Library
10
+ ffi_lib FFI::Compiler::Loader.find("webview-ext")
11
+ attach_function :webview_create, [:int, :pointer], :pointer
12
+ attach_function :webview_run, [:pointer], :void, blocking: true
13
+ attach_function :webview_terminate, [:pointer], :void
14
+ attach_function :webview_set_title, [:pointer, :string], :void
15
+ attach_function :webview_set_size, [:pointer, :int, :int, :int], :void
16
+ attach_function :webview_navigate, [:pointer, :string], :void
17
+ attach_function :webview_destroy, [:pointer], :void
18
+ attach_function :webview_bind, [:pointer, :string, :pointer, :pointer], :void
19
+ attach_function :webview_eval, [:pointer, :string], :void
20
+ attach_function :webview_init, [:pointer, :string], :void
21
+
22
+ # A minimal wrapper around the webview C library.
23
+ #
24
+ # Opens a native OS window (WebKit on macOS/Linux, WebView2 on Windows)
25
+ # and navigates it to a URL. Blocks in +run+ until the window is closed.
26
+ #
27
+ # @example
28
+ # wv = WebviewUtil::Window.new(title: "My App", width: 1024, height: 768)
29
+ # wv.navigate("http://localhost:9292")
30
+ # wv.run
31
+ class Window
32
+ # @param title [String] window title
33
+ # @param width [Integer] initial width in pixels
34
+ # @param height [Integer] initial height in pixels
35
+ # @param debug [Boolean] enable webview devtools / debug mode
36
+ def initialize(title: "App", width: 1024, height: 768, debug: false)
37
+ @window = WebviewUtil.webview_create(debug ? 1 : 0, nil)
38
+ WebviewUtil.webview_set_title(@window, title)
39
+ WebviewUtil.webview_set_size(@window, width, height, 0)
40
+ end
41
+
42
+ # Navigate the window to +url+.
43
+ # @param url [String]
44
+ def navigate(url)
45
+ WebviewUtil.webview_navigate(@window, url)
46
+ end
47
+
48
+ # Block until the window is closed by the user.
49
+ #
50
+ # On Linux/GTK a GLib SIGINT handler is installed at the C level so that
51
+ # Ctrl+C exits the GTK main loop cleanly.
52
+ def run
53
+ WebviewUtil.webview_run(@window)
54
+ ensure
55
+ WebviewUtil.webview_destroy(@window)
56
+ end
57
+
58
+ # Programmatically close the window.
59
+ def terminate
60
+ WebviewUtil.webview_terminate(@window)
61
+ end
62
+
63
+ # Evaluate JavaScript in the window context.
64
+ # @param js [String]
65
+ def eval(js)
66
+ WebviewUtil.webview_eval(@window, js)
67
+ end
68
+
69
+ # Inject JavaScript that runs before any page script.
70
+ # @param js [String]
71
+ def init(js)
72
+ WebviewUtil.webview_init(@window, js)
73
+ end
74
+
75
+ # Bind a Ruby block to a JavaScript function name.
76
+ # The block receives parsed JSON arguments.
77
+ def bind(name, &block)
78
+ callback = FFI::Function.new(:void, [:string, :string, :pointer]) do |_seq, req, _arg|
79
+ block.call(*JSON.parse(req))
80
+ rescue StandardError => e
81
+ warn "WebviewUtil bind error: #{e.message}"
82
+ terminate
83
+ end
84
+ @bindings ||= []
85
+ @bindings << callback
86
+ WebviewUtil.webview_bind(@window, name, callback, nil)
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webview_util
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Marco Concetto Rudilosso
8
+ - hmdne
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 1980-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ffi-compiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ email:
42
+ - marcoc.r@outlook.com
43
+ executables: []
44
+ extensions:
45
+ - ext/Rakefile
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rubocop.yml"
49
+ - CHANGELOG.md
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - ext/Rakefile
59
+ - ext/webview/webview.cpp
60
+ - ext/webview/webview.h
61
+ - lib/webview_util.rb
62
+ - lib/webview_util/version.rb
63
+ homepage: https://github.com/rbutils/webview_utily
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/rbutils/webview_utily
68
+ source_code_uri: https://github.com/rbutils/webview_utily
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 4.0.6
84
+ specification_version: 4
85
+ summary: Ruby bindings for webview
86
+ test_files: []