Skip to content

Commit

Permalink
darwin-external-dragndrop: implement external drag n' drop for darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
StarHack committed Apr 15, 2023
1 parent 5f818bc commit b80e27f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/os_macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ package app
import (
"errors"
"image"
"io"
"mime"
"path/filepath"
"runtime"
"strings"
"time"
"unicode"
"unicode/utf8"
Expand All @@ -18,6 +22,7 @@ import (
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/system"
"gioui.org/io/transfer"
"gioui.org/unit"

_ "gioui.org/internal/cocoainit"
Expand Down Expand Up @@ -557,6 +562,22 @@ func gio_onMouse(view, evt C.CFTypeRef, cdir C.int, cbtn C.NSInteger, x, y, dx,
})
}

//export gio_onExternalDrop
func gio_onExternalDrop(view C.CFTypeRef, path *C.char) {
fileUrl := C.GoString(path)
w := mustView(view)

fileExtension := filepath.Ext(fileUrl)
mime := mime.TypeByExtension(fileExtension)

w.w.Event(transfer.DataEvent{
Type: mime,
Open: func() io.ReadCloser {
return io.NopCloser(strings.NewReader(fileUrl))
},
})
}

//export gio_onDraw
func gio_onDraw(view C.CFTypeRef) {
w := mustView(view)
Expand Down
15 changes: 15 additions & 0 deletions app/os_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ - (void)mouseMoved:(NSEvent *)event {
- (void)mouseDragged:(NSEvent *)event {
handleMouse(self, event, MOUSE_MOVE, 0, 0);
}
-(NSDragOperation)draggingEntered:(id < NSDraggingInfo >)sender
{
return NSDragOperationCopy; // NSDragOperationGeneric
}
- (void)draggingEnded:(id <NSDraggingInfo>)sender
{
NSPasteboard* pbrd = [sender draggingPasteboard];
NSArray* droppedFiles = [pbrd propertyListForType:NSFilenamesPboardType];

for (NSString* filePath in droppedFiles) {
NSURL* url = [NSURL fileURLWithPath:filePath];
gio_onExternalDrop((__bridge CFTypeRef)self, (char*)[[url path] UTF8String]);
}
}
- (void)scrollWheel:(NSEvent *)event {
CGFloat dx = -event.scrollingDeltaX;
CGFloat dy = -event.scrollingDeltaY;
Expand Down Expand Up @@ -366,6 +380,7 @@ CFTypeRef gio_createView(void) {
@autoreleasepool {
NSRect frame = NSMakeRect(0, 0, 0, 0);
GioView* view = [[GioView alloc] initWithFrame:frame];
[view registerForDraggedTypes: [NSArray arrayWithObjects:NSTIFFPboardType, NSFilenamesPboardType, nil]];
view.wantsLayer = YES;
view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
return CFBridgingRetain(view);
Expand Down
8 changes: 8 additions & 0 deletions io/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ func (q *Router) Queue(events ...event.Event) bool {
}
case clipboard.Event:
q.cqueue.Push(e, &q.handlers)
case transfer.DataEvent:
for tag, handler := range q.pointer.queue.handlers {
for _, mimeType := range (*handler).targetMimes {
if mimeType == e.Type {
q.handlers.Add(tag, e)
}
}
}
}
}
return q.handlers.HadEvents()
Expand Down

0 comments on commit b80e27f

Please sign in to comment.