cocoawebview 0.3.2 → 0.3.4
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 +4 -4
- data/ext/cocoawebview/cocoawebview.m +60 -48
- data/lib/cocoawebview/version.rb +1 -1
- data/lib/cocoawebview.rb +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f69adb840f4e0a253d570a08155b78718676ad46ee5774d51f4e9a4130f02b27
|
4
|
+
data.tar.gz: f3299408dc6712453b62cdda86bf8a9eb7c8e42d631201e2ac6823dcb319c39a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46debf5c14c10abd9f9a179727eb9a5ca868e8ce491cfee673bf54ac0612fea967194d743194bf4b57737ed2f5495c4c0215b8490f08896552b1781f8b4ed6ca
|
7
|
+
data.tar.gz: dab7764310762b5eba6cdc6f68824d68d8c97cdea53a2a1c956ae3c7dabc3b90051cc1d15deda41b1cd23e9c5466b7731dbee386c27568112d3f890aa297a0ea
|
@@ -24,13 +24,61 @@ VALUE webview_set_title(VALUE self, VALUE title);
|
|
24
24
|
VALUE webview_center(VALUE self);
|
25
25
|
VALUE webview_is_visible(VALUE self);
|
26
26
|
VALUE webview_set_topmost(VALUE self, VALUE topmost);
|
27
|
-
|
27
|
+
|
28
|
+
@interface FileDropContainerView : NSView {
|
29
|
+
VALUE rb_cocoawebview;
|
30
|
+
}
|
31
|
+
|
32
|
+
- (void)setObj:(VALUE)o;
|
33
|
+
@end
|
34
|
+
|
35
|
+
@implementation FileDropContainerView
|
36
|
+
|
37
|
+
- (instancetype)initWithFrame:(NSRect)frame {
|
38
|
+
self = [super initWithFrame:frame];
|
39
|
+
if (self) {
|
40
|
+
[self registerForDraggedTypes:@[NSPasteboardTypeFileURL]];
|
41
|
+
}
|
42
|
+
return self;
|
43
|
+
}
|
44
|
+
|
45
|
+
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
|
46
|
+
return NSDragOperationCopy;
|
47
|
+
}
|
48
|
+
|
49
|
+
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {
|
50
|
+
return YES;
|
51
|
+
}
|
52
|
+
|
53
|
+
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
|
54
|
+
NSPasteboard *pasteboard = [sender draggingPasteboard];
|
55
|
+
NSArray<NSURL *> *fileURLs = [pasteboard readObjectsForClasses:@[[NSURL class]]
|
56
|
+
options:@{NSPasteboardURLReadingFileURLsOnlyKey: @YES}];
|
57
|
+
|
58
|
+
if (fileURLs.count > 0) {
|
59
|
+
NSString *filePath = fileURLs[0].path;
|
60
|
+
VALUE ruby_file_path = rb_str_new_cstr([filePath UTF8String]);
|
61
|
+
rb_funcall(rb_cocoawebview, rb_intern("file_did_drop"), 1, ruby_file_path);
|
62
|
+
return YES;
|
63
|
+
}
|
64
|
+
return NO;
|
65
|
+
}
|
66
|
+
|
67
|
+
- (void)setObj:(VALUE)o {
|
68
|
+
rb_cocoawebview = o;
|
69
|
+
}
|
70
|
+
@end
|
28
71
|
|
29
72
|
@interface CocoaWKWebView : WKWebView
|
30
73
|
@property (nonatomic, strong) NSEvent *lastMouseDownEvent;
|
31
74
|
@end
|
32
75
|
|
33
76
|
@implementation CocoaWKWebView
|
77
|
+
|
78
|
+
- (void)mouseDown:(NSEvent *)event {
|
79
|
+
self.lastMouseDownEvent = event;
|
80
|
+
[super mouseDown:event];
|
81
|
+
}
|
34
82
|
@end
|
35
83
|
|
36
84
|
|
@@ -50,11 +98,12 @@ VALUE webview_start_drag_drop(VALUE self);
|
|
50
98
|
}
|
51
99
|
@end
|
52
100
|
|
53
|
-
@interface CocoaWebview : NSWindow <WKScriptMessageHandler
|
101
|
+
@interface CocoaWebview : NSWindow <WKScriptMessageHandler> {
|
54
102
|
CocoaWKWebView *webView;
|
55
103
|
VALUE rb_cocoawebview;
|
56
104
|
BOOL showDevTool;
|
57
105
|
BOOL shouldMoveTitleButtons;
|
106
|
+
FileDropContainerView *fileDropView;
|
58
107
|
}
|
59
108
|
- (void)setShouldMoveTitleButtons:(BOOL)flag;
|
60
109
|
- (void)setDevTool:(BOOL)flag;
|
@@ -62,7 +111,6 @@ VALUE webview_start_drag_drop(VALUE self);
|
|
62
111
|
- (void)eval:(NSString*)code;
|
63
112
|
- (void)setCocoaWebview:(VALUE)view;
|
64
113
|
- (void)dragging;
|
65
|
-
- (void)beginNativeFileDrag;
|
66
114
|
@end
|
67
115
|
|
68
116
|
@implementation CocoaWebview
|
@@ -90,40 +138,6 @@ VALUE webview_start_drag_drop(VALUE self);
|
|
90
138
|
return self;
|
91
139
|
}
|
92
140
|
|
93
|
-
- (void)beginNativeFileDrag {
|
94
|
-
NSString *filePath = @"/Users/rkt/Music/1.mp3";
|
95
|
-
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
|
96
|
-
|
97
|
-
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
|
98
|
-
NSLog(@"File doesn't exist");
|
99
|
-
return;
|
100
|
-
}
|
101
|
-
|
102
|
-
NSPasteboardItem *item = [[NSPasteboardItem alloc] init];
|
103
|
-
[item setString:filePath forType:NSPasteboardTypeFileURL];
|
104
|
-
|
105
|
-
NSDraggingItem *dragItem = [[NSDraggingItem alloc] initWithPasteboardWriter:fileURL];
|
106
|
-
|
107
|
-
NSRect draggingRect = NSMakeRect(0, 0, 100, 30);
|
108
|
-
NSImage *dragImage = [[NSImage alloc] initWithSize:draggingRect.size];
|
109
|
-
[dragImage lockFocus];
|
110
|
-
[[NSColor grayColor] set];
|
111
|
-
NSRectFill(draggingRect);
|
112
|
-
[dragImage unlockFocus];
|
113
|
-
|
114
|
-
[dragItem setDraggingFrame:draggingRect contents:dragImage];
|
115
|
-
|
116
|
-
NSDraggingSession *session = [webView beginDraggingSessionWithItems:@[dragItem]
|
117
|
-
event:[NSApp currentEvent]
|
118
|
-
source:self];
|
119
|
-
session.animatesToStartingPositionsOnCancelOrFail = YES;
|
120
|
-
session.draggingFormation = NSDraggingFormationDefault;
|
121
|
-
}
|
122
|
-
|
123
|
-
- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
|
124
|
-
return NSDragOperationCopy;
|
125
|
-
}
|
126
|
-
|
127
141
|
- (void)setShouldMoveTitleButtons:(BOOL)flag {
|
128
142
|
shouldMoveTitleButtons = flag;
|
129
143
|
}
|
@@ -177,6 +191,7 @@ VALUE webview_start_drag_drop(VALUE self);
|
|
177
191
|
|
178
192
|
- (void)setCocoaWebview:(VALUE)view {
|
179
193
|
rb_cocoawebview = view;
|
194
|
+
[fileDropView setObj:view];
|
180
195
|
}
|
181
196
|
|
182
197
|
- (void)eval:(NSString*)code {
|
@@ -188,6 +203,11 @@ VALUE webview_start_drag_drop(VALUE self);
|
|
188
203
|
}
|
189
204
|
|
190
205
|
- (void)addWebViewToWindow:(NSWindow *)window {
|
206
|
+
NSRect contentRect = [[window contentView] bounds];
|
207
|
+
|
208
|
+
fileDropView = [[FileDropContainerView alloc] initWithFrame:contentRect];
|
209
|
+
fileDropView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
210
|
+
|
191
211
|
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
|
192
212
|
[userContentController addScriptMessageHandler:self name:@"native"];
|
193
213
|
|
@@ -206,7 +226,6 @@ VALUE webview_start_drag_drop(VALUE self);
|
|
206
226
|
[[config preferences] setValue:@YES forKey:@"DOMPasteAllowed"];
|
207
227
|
|
208
228
|
// Create the WKWebView with the configuration
|
209
|
-
NSRect contentRect = [[window contentView] bounds];
|
210
229
|
webView = [[CocoaWKWebView alloc] initWithFrame:contentRect configuration:config];
|
211
230
|
|
212
231
|
// Enable autoresizing
|
@@ -220,7 +239,9 @@ VALUE webview_start_drag_drop(VALUE self);
|
|
220
239
|
*/
|
221
240
|
|
222
241
|
// Add to window's contentView
|
223
|
-
[window
|
242
|
+
//[[window contentView] addSubview:fileDropView];
|
243
|
+
[[window contentView] addSubview: webView];
|
244
|
+
[[window contentView] addSubview:fileDropView positioned:NSWindowAbove relativeTo:webView];
|
224
245
|
|
225
246
|
webView.navigationDelegate = self;
|
226
247
|
}
|
@@ -268,7 +289,6 @@ Init_cocoawebview(void)
|
|
268
289
|
rb_define_method(rb_mCocoaWebviewClass, "center", webview_center, 0);
|
269
290
|
rb_define_method(rb_mCocoaWebviewClass, "visible?", webview_is_visible, 0);
|
270
291
|
rb_define_method(rb_mCocoaWebviewClass, "set_topmost", webview_set_topmost, 1);
|
271
|
-
rb_define_method(rb_mCocoaWebviewClass, "start_drag_drop", webview_start_drag_drop, 0);
|
272
292
|
|
273
293
|
}
|
274
294
|
|
@@ -456,11 +476,3 @@ VALUE webview_set_topmost(VALUE self, VALUE topmost) {
|
|
456
476
|
[webview setLevel:NSNormalWindowLevel];
|
457
477
|
}
|
458
478
|
}
|
459
|
-
|
460
|
-
VALUE webview_start_drag_drop(VALUE self) {
|
461
|
-
VALUE wrapper = rb_ivar_get(self, rb_intern("@webview"));
|
462
|
-
CocoaWebview *webview;
|
463
|
-
TypedData_Get_Struct(wrapper, CocoaWebview, &cocoawebview_obj_type, webview);
|
464
|
-
|
465
|
-
[webview beginNativeFileDrag];
|
466
|
-
}
|
data/lib/cocoawebview/version.rb
CHANGED
data/lib/cocoawebview.rb
CHANGED
@@ -46,6 +46,10 @@ module CocoaWebview
|
|
46
46
|
puts "CocoaWebview did loaded"
|
47
47
|
end
|
48
48
|
|
49
|
+
def file_did_drop(file_path)
|
50
|
+
puts "#{file_path} dropped"
|
51
|
+
end
|
52
|
+
|
49
53
|
def bind(name, &block)
|
50
54
|
param_info = block.parameters
|
51
55
|
param_count = param_info.count { |type, _| type == :req || type == :opt }
|