Files
magpie/internal/gui/filedrag_darwin.go
T
yetone e42ee3b816 library: Magpie Browser's extension sits in ~/Magpie Browser Extension, dragged from the sheet onto chrome://extensions
The owner, on the v0.1.2442 sheet: the extension was written to
~/.config/magpie/browser-extension, a hidden folder no one can find to
choose in Load unpacked.

- Set up writes it to "Magpie Browser Extension" in the home folder
  (browserExtDir), where Finder and Explorer show it, and shows it
  selected in its parent (open -R on the Mac, explorer /select, on
  Windows, the home folder on Linux) rather than opened, to drag from.
- On the Mac the sheet has the folder itself: a press on it that moves
  hands the folder to the system's drag (POST /api/library/browser/drag,
  filedrag_darwin.go: beginDraggingSession from the key window with a
  file URL and the folder's icon, only while the button is still down),
  so it drops onto chrome://extensions as from Finder. Chromium's
  extensions page loads a dropped folder as unpacked
  (drag_and_drop_handler.ts: entry.isDirectory -> loadUnpackedFromDrag),
  with Developer mode on, which step 2 asks for.
- Elsewhere step 3 says to drag the folder from the window that opened,
  or Load unpacked and choose it.
- Old installs: a browser that loaded the hidden folder still does, and
  that folder is kept up to date as long as it has a manifest; the sheet
  offers set up again, into the new folder.

Run at ace58117a on mote: go vet ./internal/gui (darwin), GOOS=linux -tags
nogui and GOOS=windows vet; library-browser-setup and i18n-keys GUI
tests, 46 of 46 (5 languages, Chromium and WebKit, 1100 and 420 wide,
with and without the drag: a click posts no drag, a press that moves
posts one); internal/cli parity tests ok. At df0320156: go test
./internal/gui ./internal/cli, cli ok, gui red only in
TestMainWindowCorners, which is #3949 and red the same at df0320156
without this change.
Not tried: the native drag by hand into a real Chrome (no way to drive
a mouse drag here), and Explorer's /select on the Windows box.
2026-11-01 17:28:15 +08:00

68 lines
2.5 KiB
Go

//go:build !nogui
package gui
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include <stdlib.h>
#import <Cocoa/Cocoa.h>
// The drag's source: a copy, wherever it is dropped. An object of its
// own, not the webview, which would take the session for one of its
// page's drags when it ends.
@interface MagpieFileDrag : NSObject <NSDraggingSource>
@end
@implementation MagpieFileDrag
- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context {
return NSDragOperationCopy;
}
@end
static MagpieFileDrag *fileDragSource;
// dragFile starts the system's drag of the file at path from under the
// mouse, as Finder drags it, while the button the page saw go down is
// still held. Says 0 when it isn't, or there is no window to drag from.
static int dragFile(const char *path) {
NSString *p = [NSString stringWithUTF8String:path];
__block int ok = 0;
dispatch_sync(dispatch_get_main_queue(), ^{
if (([NSEvent pressedMouseButtons] & 1) == 0) return;
NSWindow *win = [NSApp keyWindow];
NSView *view = win.contentView;
if (view == nil) return;
if (fileDragSource == nil) fileDragSource = [MagpieFileDrag new];
NSPoint inWin = [win mouseLocationOutsideOfEventStream];
NSPoint at = [view convertPoint:inWin fromView:nil];
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:p];
icon.size = NSMakeSize(48, 48);
NSDraggingItem *item = [[NSDraggingItem alloc] initWithPasteboardWriter:[NSURL fileURLWithPath:p isDirectory:YES]];
[item setDraggingFrame:NSMakeRect(at.x - 24, at.y - 24, 48, 48) contents:icon];
// the page's own mouse-down went by in WebKit; the session starts from
// a drag at where the mouse is now
NSEvent *ev = [NSEvent mouseEventWithType:NSEventTypeLeftMouseDragged location:inWin modifierFlags:0
timestamp:[[NSProcessInfo processInfo] systemUptime] windowNumber:win.windowNumber context:nil
eventNumber:0 clickCount:1 pressure:1];
NSDraggingSession *s = [view beginDraggingSessionWithItems:@[item] event:ev source:fileDragSource];
s.animatesToStartingPositionsOnCancelOrFail = YES;
ok = 1;
});
return ok;
}
*/
import "C"
import "unsafe"
// canDragFile: the page may hand a file to the system's drag here.
const canDragFile = true
// dragFile drags the file or folder at path out of magpie's window, onto
// whatever takes a file from Finder (a browser's extensions page).
func dragFile(path string) bool {
p := C.CString(path)
defer C.free(unsafe.Pointer(p))
return C.dragFile(p) != 0
}