cocoawebview 0.1.0 → 0.1.2

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: 1d24ba42918984c4d53cdf7e278b0e7ecc4c1e7d152467456b7f7a08d1249dfd
4
- data.tar.gz: c323ebd3189fc650fd824df4253240d411d69f493454070f0b9e7225cb5017be
3
+ metadata.gz: 38486e705f04b56aad1b11d9c0fa746ccc4adb4889df6d7d1d8061a5b3afee58
4
+ data.tar.gz: eb2b937693ff7b8a42479904091d299f17fe4a01211258b27b9cf9f80bce7757
5
5
  SHA512:
6
- metadata.gz: c0dfe687f762ccfd711721116b0d85b9a0a9c430ec694bebc5b49d7fd8ff5d273683409f8b3848431db89d6fed126e1d6e2866f1d69072513a2102fc4af8a6c0
7
- data.tar.gz: fd9f28eda5c112416eca9f8939dd524baa461ac8965778378fcf40a65f0ec001367081d37d2e34d21806884641cbec78a12ca3a047bd316b0d93a0eaa7aa3d0e
6
+ metadata.gz: b38e54462caafcaefa0c7f1cfef0f130abef9baa9d5c903085185ce6e798b7a0ddc017aaa4aca360c007bc45885d8ddd6700edc5987f8e277b6269c409033c45
7
+ data.tar.gz: deb1a4504f60bfd53c333b672bdbeccc58f35d3abad99454ffe60937494a06a441bc17df9b6324f048ee800be0cedb3d5e387a0ebccff3d06ca055db4f691c13
@@ -1,13 +1,21 @@
1
1
  #import <Cocoa/Cocoa.h>
2
+ #import <WebKit/WebKit.h>
2
3
  #include "cocoawebview.h"
3
4
 
4
5
  VALUE rb_mCocoawebview;
5
6
  VALUE rb_mNSAppClass = Qnil;
7
+ VALUE rb_mCocoaWebviewClass = Qnil;
6
8
  NSApplication *application = nil;
7
9
 
8
10
  VALUE nsapp_initialize(VALUE self);
9
11
  VALUE nsapp_run(VALUE self);
10
12
 
13
+ VALUE webview_initialize(VALUE self, VALUE debug);
14
+ VALUE webview_show(VALUE self);
15
+ VALUE webview_hide(VALUE self);
16
+ VALUE webview_eval(VALUE self, VALUE code);
17
+
18
+
11
19
  @interface AppDelegate : NSObject <NSApplicationDelegate> {
12
20
  VALUE app;
13
21
  }
@@ -22,17 +30,123 @@ VALUE nsapp_run(VALUE self);
22
30
  - (void)applicationDidFinishLaunching:(NSNotification *)notification {
23
31
  rb_funcall(app, rb_intern("app_did_launch"), 0);
24
32
  }
33
+ @end
34
+
35
+ @interface CocoaWebview : NSWindow {
36
+ WKWebView *webView;
37
+ VALUE rb_cocoawebview;
38
+ BOOL showDevTool;
39
+ }
40
+ - (void)setDevTool:(BOOL)flag;
41
+ - (id)initWithFrame:(NSRect)frame debug:(BOOL)flag;
42
+ - (void)eval:(NSString*)code;
43
+ - (void)setCocoaWebview:(VALUE)view;
44
+ @end
45
+
46
+ @implementation CocoaWebview
47
+ - (id)initWithFrame:(NSRect)frame debug:(BOOL)flag {
48
+ int style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
49
+ NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskFullSizeContentView;
50
+ style &= ~NSWindowStyleMaskFullScreen;
51
+ self = [super initWithContentRect:frame
52
+ styleMask:style
53
+ backing:NSBackingStoreBuffered
54
+ defer:NO];
55
+ if (self) {
56
+ [self setTitle:@"My Custom Window"];
57
+ [self setDevTool:flag];
58
+ [self setTitlebarAppearsTransparent: YES];
59
+ [self addWebViewToWindow:self];
60
+ }
61
+ return self;
62
+ }
63
+
64
+ - (void)setDevTool:(BOOL)flag {
65
+ showDevTool = flag;
66
+ }
67
+
68
+ - (void)setCocoaWebview:(VALUE)view {
69
+ rb_cocoawebview = view;
70
+ }
71
+
72
+ - (void)eval:(NSString*)code {
73
+ [webView evaluateJavaScript:code completionHandler:^(id result, NSError *error) {
74
+ if (error) {
75
+ NSLog(@"JavaScript error: %@", error);
76
+ }
77
+ }];
78
+ }
79
+
80
+ - (void)addWebViewToWindow:(NSWindow *)window {
81
+ // Create a configuration if needed
82
+ WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
83
+
84
+ [[config preferences] setValue:@YES forKey:@"fullScreenEnabled"];
85
+
86
+ if (showDevTool) {
87
+ [[config preferences] setValue:@YES forKey:@"developerExtrasEnabled"];
88
+ }
89
+
90
+ [[config preferences] setValue:@YES forKey:@"javaScriptCanAccessClipboard"];
91
+
92
+ [[config preferences] setValue:@YES forKey:@"DOMPasteAllowed"];
93
+
94
+ // Create the WKWebView with the configuration
95
+ NSRect contentRect = [[window contentView] bounds];
96
+ webView = [[WKWebView alloc] initWithFrame:contentRect configuration:config];
97
+
98
+ // Enable autoresizing
99
+ [webView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
100
+
101
+ // Load a URL
102
+ /*
103
+ NSURL *url = [NSURL URLWithString:@"https://www.apple.com"];
104
+ NSURLRequest *request = [NSURLRequest requestWithURL:url];
105
+ [webView loadRequest:request];
106
+ */
107
+
108
+ // Add to window's contentView
109
+ [window setContentView:webView];
110
+
111
+ webView.navigationDelegate = self;
112
+
113
+ NSString *html = @"<html><body><h1>Hello from WKWebView</h1><script>function sayHello() { console.log('Hello JS!'); }</script></body></html>";
114
+ [webView loadHTMLString:html baseURL:nil];
115
+ }
25
116
 
117
+ // Called when the web view finishes loading
118
+ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
119
+ rb_funcall(rb_cocoawebview, rb_intern("webview_did_load"), 0);
120
+ }
26
121
  @end
27
122
 
123
+ static void cocoawebview_obj_free(void *ptr) {
124
+
125
+ }
126
+
127
+ static const rb_data_type_t cocoawebview_obj_type = {
128
+ "CocoaWebviewWrapper",
129
+ { 0, cocoawebview_obj_free, 0 },
130
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
131
+ };
132
+
28
133
 
29
134
  RUBY_FUNC_EXPORTED void
30
135
  Init_cocoawebview(void)
31
136
  {
32
137
  rb_mCocoawebview = rb_define_module("CocoaWebview");
138
+
139
+ /* NSApp */
33
140
  rb_mNSAppClass = rb_define_class_under(rb_mCocoawebview, "NSApp", rb_cObject);
34
141
  rb_define_method(rb_mNSAppClass, "initialize", nsapp_initialize, 0);
35
142
  rb_define_method(rb_mNSAppClass, "run", nsapp_run, 0);
143
+
144
+ /* CocoaWebview */
145
+ rb_mCocoaWebviewClass = rb_define_class_under(rb_mCocoawebview, "CocoaWebview", rb_cObject);
146
+ rb_define_method(rb_mCocoaWebviewClass, "initialize", webview_initialize, 1);
147
+ rb_define_method(rb_mCocoaWebviewClass, "show", webview_show, 0);
148
+ rb_define_method(rb_mCocoaWebviewClass, "hide", webview_hide, 0);
149
+ rb_define_method(rb_mCocoaWebviewClass, "eval", webview_eval, 1);
36
150
  }
37
151
 
38
152
  VALUE nsapp_initialize(VALUE self) {
@@ -47,3 +161,48 @@ VALUE nsapp_initialize(VALUE self) {
47
161
  VALUE nsapp_run(VALUE self) {
48
162
  [application run];
49
163
  }
164
+
165
+ VALUE webview_initialize(VALUE self, VALUE debug) {
166
+ rb_iv_set(self, "@var", rb_hash_new());
167
+ BOOL flag = NO;
168
+ if (debug == Qtrue) {
169
+ flag = YES;
170
+ } else {
171
+ flag = NO;
172
+ }
173
+ CocoaWebview *webview = [[CocoaWebview alloc] initWithFrame:NSMakeRect(100, 100, 400, 500) debug:flag];
174
+
175
+ [webview setCocoaWebview:self];
176
+
177
+ // Wrap the Objective-C pointer into a Ruby object
178
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &cocoawebview_obj_type, webview);
179
+
180
+ // Store the wrapper in an instance variable
181
+ rb_ivar_set(self, rb_intern("@webview"), wrapper);
182
+ return self;
183
+ }
184
+
185
+ VALUE webview_show(VALUE self) {
186
+ VALUE wrapper = rb_ivar_get(self, rb_intern("@webview"));
187
+ CocoaWebview *webview;
188
+ TypedData_Get_Struct(wrapper, CocoaWebview, &cocoawebview_obj_type, webview);
189
+ [webview makeKeyAndOrderFront:nil];
190
+ }
191
+
192
+ VALUE webview_hide(VALUE self) {
193
+ VALUE wrapper = rb_ivar_get(self, rb_intern("@webview"));
194
+ CocoaWebview *webview;
195
+ TypedData_Get_Struct(wrapper, CocoaWebview, &cocoawebview_obj_type, webview);
196
+ [webview orderOut:nil];
197
+ }
198
+
199
+ VALUE webview_eval(VALUE self, VALUE code) {
200
+ const char *js = StringValueCStr(code);
201
+ NSString *js_code = [[NSString alloc] initWithCString:js encoding:NSUTF8StringEncoding];
202
+
203
+ VALUE wrapper = rb_ivar_get(self, rb_intern("@webview"));
204
+ CocoaWebview *webview;
205
+ TypedData_Get_Struct(wrapper, CocoaWebview, &cocoawebview_obj_type, webview);
206
+
207
+ [webview eval:js_code];
208
+ }
@@ -14,4 +14,7 @@ $CFLAGS << " -ObjC"
14
14
  # Add Cocoa framework to linker flags
15
15
  $LDFLAGS << " -framework Cocoa"
16
16
 
17
+ # Add Webview framework to linker flags
18
+ $LDFLAGS << " -framework WebKit"
19
+
17
20
  create_makefile("cocoawebview/cocoawebview")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cocoawebview
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/cocoawebview.rb CHANGED
@@ -12,4 +12,14 @@ module CocoaWebview
12
12
  puts "NSApp did launch"
13
13
  end
14
14
  end
15
+
16
+ class CocoaWebview
17
+ def get_webview
18
+ @webview
19
+ end
20
+
21
+ def webview_did_load
22
+ puts "CocoaWebview did loaded"
23
+ end
24
+ end
15
25
  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.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Jeff