2021-06-01 19:14:51 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package drop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"golang.org/x/text/encoding/unicode"
|
|
|
|
"opslag.de/schobers/geom"
|
2021-06-04 15:17:22 +00:00
|
|
|
"opslag.de/schobers/zntg/ui"
|
2021-06-01 19:14:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
void SetDragAndDropHook(void* window);
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
2021-06-04 15:17:22 +00:00
|
|
|
type handler struct {
|
|
|
|
Target ui.DragDropEventTarget
|
|
|
|
Files []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var targets map[ui.DragDropEventTarget]struct{} = map[ui.DragDropEventTarget]struct{}{}
|
|
|
|
var droppedFiles []string
|
2021-06-01 19:14:51 +00:00
|
|
|
|
2021-06-04 15:17:22 +00:00
|
|
|
//export clearDrop
|
|
|
|
func clearDrop() {
|
|
|
|
droppedFiles = nil
|
2021-06-01 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 15:17:22 +00:00
|
|
|
//export dropFinished
|
|
|
|
func dropFinished(x, y C.INT) {
|
|
|
|
for target := range targets {
|
|
|
|
target.Drop(geom.PtF32(float32(x), float32(y)), droppedFiles)
|
|
|
|
}
|
2021-06-01 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 15:17:22 +00:00
|
|
|
//export addDroppedFile
|
|
|
|
func addDroppedFile(filePath *C.wchar_t, filePathLength C.UINT) {
|
2021-06-01 19:14:51 +00:00
|
|
|
pathBytes := C.GoBytes(unsafe.Pointer(filePath), C.int(filePathLength)*C.sizeof_wchar_t)
|
|
|
|
decoder := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()
|
|
|
|
path, err := decoder.Bytes(pathBytes)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-04 15:17:22 +00:00
|
|
|
droppedFiles = append(droppedFiles, string(path))
|
2021-06-01 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 15:17:22 +00:00
|
|
|
func RegisterAsDefaultProvider() {
|
|
|
|
ui.DefaultDragDropProvider = &provider{}
|
2021-06-01 19:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 15:17:22 +00:00
|
|
|
type provider struct{}
|
|
|
|
|
|
|
|
func (p provider) Register(windowHandle uintptr, target ui.DragDropEventTarget) {
|
|
|
|
C.SetDragAndDropHook(unsafe.Pointer(windowHandle))
|
|
|
|
targets[target] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ui.DefaultDragDropProvider = provider{}
|
2021-06-01 19:14:51 +00:00
|
|
|
}
|