cocoawebview 0.1.0 → 0.1.1

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: b6d8353c646afd0ec95dcfc21b339ccbff41b11723bbfb35f1a97fcf10245542
4
+ data.tar.gz: e368d715dd3aebd24b50eeb384aa9210e9f269546d7b0aaea9b198329310bf9d
5
5
  SHA512:
6
- metadata.gz: c0dfe687f762ccfd711721116b0d85b9a0a9c430ec694bebc5b49d7fd8ff5d273683409f8b3848431db89d6fed126e1d6e2866f1d69072513a2102fc4af8a6c0
7
- data.tar.gz: fd9f28eda5c112416eca9f8939dd524baa461ac8965778378fcf40a65f0ec001367081d37d2e34d21806884641cbec78a12ca3a047bd316b0d93a0eaa7aa3d0e
6
+ metadata.gz: 63c472a2de65ae80617a268252fb55844439bc7590cd5ca17aac9018b583cd623eb7b7c2bc251c4e20f76105965a978d2d3b2e61536223582d3c318db2f53ac6
7
+ data.tar.gz: 78c2d06a6aef4b6ed82a2eb194cf3c9812c1d98af30f028dbad248604c3c8b927de39b5500a67142a23fcd2a19b8f03cfa55502035caee4f835a3407f1d7c87d
@@ -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);
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,114 @@ 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
+ }
39
+ - (id)initWithFrame:(NSRect)frame;
40
+ - (void)eval:(NSString*)code;
41
+ - (void)setCocoaWebview:(VALUE)view;
42
+ @end
43
+
44
+ @implementation CocoaWebview
45
+ - (id)initWithFrame:(NSRect)frame{
46
+ int style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable |
47
+ NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskFullSizeContentView;
48
+ style &= ~NSWindowStyleMaskFullScreen;
49
+ self = [super initWithContentRect:frame
50
+ styleMask:style
51
+ backing:NSBackingStoreBuffered
52
+ defer:NO];
53
+ if (self) {
54
+ [self setTitle:@"My Custom Window"];
55
+ [self setTitlebarAppearsTransparent: YES];
56
+ [self addWebViewToWindow:self];
57
+ }
58
+ return self;
59
+ }
60
+
61
+ - (void)setCocoaWebview:(VALUE)view {
62
+ rb_cocoawebview = view;
63
+ }
64
+
65
+ - (void)eval:(NSString*)code {
66
+ [webView evaluateJavaScript:code completionHandler:^(id result, NSError *error) {
67
+ if (error) {
68
+ NSLog(@"JavaScript error: %@", error);
69
+ }
70
+ }];
71
+ }
72
+
73
+ - (void)addWebViewToWindow:(NSWindow *)window {
74
+ // Create a configuration if needed
75
+ WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
76
+
77
+ [[config preferences] setValue:@YES forKey:@"fullScreenEnabled"];
78
+
79
+ [[config preferences] setValue:@YES forKey:@"developerExtrasEnabled"];
80
+
81
+ [[config preferences] setValue:@YES forKey:@"javaScriptCanAccessClipboard"];
82
+
83
+ [[config preferences] setValue:@YES forKey:@"DOMPasteAllowed"];
84
+
85
+ // Create the WKWebView with the configuration
86
+ NSRect contentRect = [[window contentView] bounds];
87
+ webView = [[WKWebView alloc] initWithFrame:contentRect configuration:config];
88
+
89
+ // Enable autoresizing
90
+ [webView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
91
+
92
+ // Load a URL
93
+ /*
94
+ NSURL *url = [NSURL URLWithString:@"https://www.apple.com"];
95
+ NSURLRequest *request = [NSURLRequest requestWithURL:url];
96
+ [webView loadRequest:request];
97
+ */
98
+
99
+ // Add to window's contentView
100
+ [window setContentView:webView];
101
+
102
+ webView.navigationDelegate = self;
103
+
104
+ NSString *html = @"<html><body><h1>Hello from WKWebView</h1><script>function sayHello() { console.log('Hello JS!'); }</script></body></html>";
105
+ [webView loadHTMLString:html baseURL:nil];
106
+ }
25
107
 
108
+ // Called when the web view finishes loading
109
+ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
110
+ rb_funcall(rb_cocoawebview, rb_intern("webview_did_load"), 0);
111
+ }
26
112
  @end
27
113
 
114
+ static void cocoawebview_obj_free(void *ptr) {
115
+
116
+ }
117
+
118
+ static const rb_data_type_t cocoawebview_obj_type = {
119
+ "CocoaWebviewWrapper",
120
+ { 0, cocoawebview_obj_free, 0 },
121
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
122
+ };
123
+
28
124
 
29
125
  RUBY_FUNC_EXPORTED void
30
126
  Init_cocoawebview(void)
31
127
  {
32
128
  rb_mCocoawebview = rb_define_module("CocoaWebview");
129
+
130
+ /* NSApp */
33
131
  rb_mNSAppClass = rb_define_class_under(rb_mCocoawebview, "NSApp", rb_cObject);
34
132
  rb_define_method(rb_mNSAppClass, "initialize", nsapp_initialize, 0);
35
133
  rb_define_method(rb_mNSAppClass, "run", nsapp_run, 0);
134
+
135
+ /* CocoaWebview */
136
+ rb_mCocoaWebviewClass = rb_define_class_under(rb_mCocoawebview, "CocoaWebview", rb_cObject);
137
+ rb_define_method(rb_mCocoaWebviewClass, "initialize", webview_initialize, 0);
138
+ rb_define_method(rb_mCocoaWebviewClass, "show", webview_show, 0);
139
+ rb_define_method(rb_mCocoaWebviewClass, "hide", webview_hide, 0);
140
+ rb_define_method(rb_mCocoaWebviewClass, "eval", webview_eval, 1);
36
141
  }
37
142
 
38
143
  VALUE nsapp_initialize(VALUE self) {
@@ -47,3 +152,42 @@ VALUE nsapp_initialize(VALUE self) {
47
152
  VALUE nsapp_run(VALUE self) {
48
153
  [application run];
49
154
  }
155
+
156
+ VALUE webview_initialize(VALUE self) {
157
+ rb_iv_set(self, "@var", rb_hash_new());
158
+ CocoaWebview *webview = [[CocoaWebview alloc] initWithFrame:NSMakeRect(100, 100, 400, 500)];
159
+
160
+ [webview setCocoaWebview:self];
161
+
162
+ // Wrap the Objective-C pointer into a Ruby object
163
+ VALUE wrapper = TypedData_Wrap_Struct(rb_cObject, &cocoawebview_obj_type, webview);
164
+
165
+ // Store the wrapper in an instance variable
166
+ rb_ivar_set(self, rb_intern("@webview"), wrapper);
167
+ return self;
168
+ }
169
+
170
+ VALUE webview_show(VALUE self) {
171
+ VALUE wrapper = rb_ivar_get(self, rb_intern("@webview"));
172
+ CocoaWebview *webview;
173
+ TypedData_Get_Struct(wrapper, CocoaWebview, &cocoawebview_obj_type, webview);
174
+ [webview makeKeyAndOrderFront:nil];
175
+ }
176
+
177
+ VALUE webview_hide(VALUE self) {
178
+ VALUE wrapper = rb_ivar_get(self, rb_intern("@webview"));
179
+ CocoaWebview *webview;
180
+ TypedData_Get_Struct(wrapper, CocoaWebview, &cocoawebview_obj_type, webview);
181
+ [webview orderOut:nil];
182
+ }
183
+
184
+ VALUE webview_eval(VALUE self, VALUE code) {
185
+ const char *js = StringValueCStr(code);
186
+ NSString *js_code = [[NSString alloc] initWithCString:js encoding:NSUTF8StringEncoding];
187
+
188
+ VALUE wrapper = rb_ivar_get(self, rb_intern("@webview"));
189
+ CocoaWebview *webview;
190
+ TypedData_Get_Struct(wrapper, CocoaWebview, &cocoawebview_obj_type, webview);
191
+
192
+ [webview eval:js_code];
193
+ }
@@ -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.1"
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Jeff