- 
                Notifications
    You must be signed in to change notification settings 
- Fork 148
macOS: external drag and drop support #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -8,6 +8,10 @@ package app | |
| import ( | ||
| "errors" | ||
| "image" | ||
| "io" | ||
| "mime" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "time" | ||
| "unicode" | ||
|  | @@ -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" | ||
|  | @@ -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, | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have a new field in the event that includes the filename/URL please? In my app, I would like to use the filename and do not require access to the file content when the DnD event happens. This kind of logic is impossible when the event only contains the  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation triggers an event for every file dropped so providing multiple  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I posted another API idea specifically adding files in the main review comment. Let's see what @eliasnaur has to say about it... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think nothing prevents you from getting the filepath: evt  :=  transfer.DataEvent{} // Received on main-loop
file, _ := evt.Open()
var filepath string
if f, ok := file.(*os.File); ok {
     filepath = f.Name()
}That works because   | ||
| Open: func() (io.ReadCloser, error) { | ||
| return os.Open(fileUrl) | ||
| }, | ||
| }) | ||
| } | ||
|  | ||
| //export gio_onDraw | ||
| func gio_onDraw(view C.CFTypeRef) { | ||
| w := mustView(view) | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -182,6 +182,8 @@ func (q *Router) Queue(events ...event.Event) bool { | |
| } | ||
| case clipboard.Event: | ||
| q.cqueue.Push(e, &q.handlers) | ||
| case transfer.DataEvent: | ||
| q.pointer.queue.notifyPotentialTargets(&pointerHandler{sourceMimes: []string{e.Type}}, &q.handlers, e) | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is quite right: as written, notifyPotentialTargets still gives every handler the event. It's made for  | ||
| } | ||
| } | ||
| return q.handlers.HadEvents() | ||
|  | ||
Uh oh!
There was an error while loading. Please reload this page.