zntg/addons/drop/drop_windows.go

66 lines
1.3 KiB
Go
Raw Permalink Normal View History

2021-06-01 19:14:51 +00:00
// +build windows
package drop
import (
"unsafe"
"golang.org/x/text/encoding/unicode"
"opslag.de/schobers/geom"
"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"
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
//export clearDrop
func clearDrop() {
droppedFiles = nil
2021-06-01 19:14:51 +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
}
//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
}
droppedFiles = append(droppedFiles, string(path))
2021-06-01 19:14:51 +00:00
}
func RegisterAsDefaultProvider() {
ui.DefaultDragDropProvider = &provider{}
2021-06-01 19:14:51 +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
}