cocoawebview 0.1.4 → 0.1.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb16f097b461c3a2d024f2010171474fb27b12fcdeb6e267984f1eb769e125a8
4
- data.tar.gz: d7d911490621dc00a2db8c4ded4f2e19909406ebf31a930b92d9e73a7a54565e
3
+ metadata.gz: '068ccf8fd010e4bd17c1879227ea7814f973a5ae9c2fd63311db32b3b42d2ef0'
4
+ data.tar.gz: 64cbe286c4dbb37292a4e845b45963423a111bb3e74de811e54dd26aa49425c1
5
5
  SHA512:
6
- metadata.gz: 314995f23a0ff1531ea763f502ec5e2cef7fc5b43cf89b9fbcd3a76760d39aa54d120a6ffd7de98d51d895b84bd575f9ba1f867764397ba42150b0152f49bc77
7
- data.tar.gz: 35e62aeb7d7101721cffa2bf2b8aae93cdca6a8bc37807ea30abb40ef7ea35e11d36079013da0d37aa6066c25074fbf30cef0ef26933071c808261d7481bfa45
6
+ metadata.gz: f90353de2fc60d6a5177db1893c8f0d38fa880a3af912bfeb26c9c8b540b429b4ba59f215b3db3f3a1fff144d6ff0a683aea183978fad49cf879926b64ca53a8
7
+ data.tar.gz: 5807c58f3d8ad7c3260653dbda863d918de93fa635e22c541ba0882af072b9468b5da424e0fdd9758b9be0ced6660d658648951a6c938498f019912f832868d0
@@ -32,7 +32,7 @@ VALUE webview_eval(VALUE self, VALUE code);
32
32
  }
33
33
  @end
34
34
 
35
- @interface CocoaWebview : NSWindow {
35
+ @interface CocoaWebview : NSWindow <WKScriptMessageHandler> {
36
36
  WKWebView *webView;
37
37
  VALUE rb_cocoawebview;
38
38
  BOOL showDevTool;
@@ -62,6 +62,15 @@ VALUE webview_eval(VALUE self, VALUE code);
62
62
  return self;
63
63
  }
64
64
 
65
+ - (void)userContentController:(WKUserContentController *)userContentController
66
+ didReceiveScriptMessage:(WKScriptMessage *)message {
67
+ if ([message.name isEqualToString:@"native"]) {
68
+ const char *body = [message.body UTF8String];
69
+ VALUE rb_body = rb_str_new_cstr(body);
70
+ rb_funcall(rb_cocoawebview, rb_intern("webview_msg_handler"), 1, rb_body);
71
+ }
72
+ }
73
+
65
74
  - (void)setDevTool:(BOOL)flag {
66
75
  showDevTool = flag;
67
76
  }
@@ -79,11 +88,15 @@ VALUE webview_eval(VALUE self, VALUE code);
79
88
  }
80
89
 
81
90
  - (void)addWebViewToWindow:(NSWindow *)window {
91
+ WKUserContentController *userContentController = [[WKUserContentController alloc] init];
92
+ [userContentController addScriptMessageHandler:self name:@"native"];
93
+
82
94
  // Create a configuration if needed
83
95
  WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
84
96
 
85
97
  [[config preferences] setValue:@YES forKey:@"fullScreenEnabled"];
86
98
 
99
+ config.userContentController = userContentController;
87
100
  if (showDevTool) {
88
101
  [[config preferences] setValue:@YES forKey:@"developerExtrasEnabled"];
89
102
  }
@@ -162,6 +175,7 @@ VALUE nsapp_run(VALUE self) {
162
175
 
163
176
  VALUE webview_initialize(VALUE self, VALUE debug) {
164
177
  rb_iv_set(self, "@var", rb_hash_new());
178
+ rb_iv_set(self, "@bindings", rb_hash_new());
165
179
  BOOL flag = NO;
166
180
  if (debug == Qtrue) {
167
181
  flag = YES;
@@ -170,6 +184,7 @@ VALUE webview_initialize(VALUE self, VALUE debug) {
170
184
  }
171
185
  CocoaWebview *webview = [[CocoaWebview alloc] initWithFrame:NSMakeRect(100, 100, 400, 500) debug:flag];
172
186
 
187
+ [webview setReleasedWhenClosed:NO];
173
188
  [webview setCocoaWebview:self];
174
189
 
175
190
  // Wrap the Objective-C pointer into a Ruby object
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cocoawebview
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
5
5
  end
data/lib/cocoawebview.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
3
4
  require_relative "cocoawebview/version"
4
5
  require_relative "cocoawebview/cocoawebview"
5
6
 
@@ -21,5 +22,27 @@ module CocoaWebview
21
22
  def webview_did_load
22
23
  puts "CocoaWebview did loaded"
23
24
  end
25
+
26
+ def bind(name, &block)
27
+ param_info = block.parameters
28
+ param_count = param_info.count { |type, _| type == :req || type == :opt }
29
+ args = (1..param_count).map { |i| "arg#{i}" }.join(", ")
30
+ code = %`
31
+ function #{name}(#{args}) {
32
+ body = {"function": "#{name}", args: [#{args}]};
33
+ window.webkit.messageHandlers.native.postMessage(JSON.stringify(body));
34
+ }
35
+ `
36
+ @bindings[name] = block
37
+ self.eval(code)
38
+ end
39
+
40
+ def webview_msg_handler(msg)
41
+ hash = JSON.parse(msg)
42
+ function = hash["function"]
43
+ args = hash["args"]
44
+ callback = @bindings[function]
45
+ callback.call(*args) if callback
46
+ end
24
47
  end
25
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocoawebview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Jeff