77 lines
2.3 KiB
Go
77 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"unsafe"
|
|
|
|
"github.com/jchv/go-webview2"
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
var (
|
|
user32 = windows.NewLazySystemDLL("user32.dll")
|
|
procFindWindowW = user32.NewProc("FindWindowW")
|
|
procFindWindowExW = user32.NewProc("FindWindowExW")
|
|
procSendMessageTimeoutW = user32.NewProc("SendMessageTimeoutW")
|
|
procSetParent = user32.NewProc("SetParent")
|
|
procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
|
|
procGetWindowRect = user32.NewProc("GetWindowRect")
|
|
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
|
|
procMoveWindow = user32.NewProc("MoveWindow")
|
|
procShowWindow = user32.NewProc("ShowWindow")
|
|
procSetProcessDPIAware = user32.NewProc("SetProcessDPIAware")
|
|
procSetWindowLongPtrW = user32.NewProc("SetWindowLongPtrW")
|
|
procGetMessageW = user32.NewProc("GetMessageW")
|
|
procPostMessageW = user32.NewProc("PostMessageW")
|
|
procTranslateMessage = user32.NewProc("TranslateMessage")
|
|
procDispatchMessageW = user32.NewProc("DispatchMessageW")
|
|
)
|
|
|
|
var wv webview2.WebView
|
|
var wvHwnd uintptr
|
|
var jsQueue = make(chan string, 64)
|
|
var paused int32
|
|
|
|
const wmEvalJS = 0x0401
|
|
const wmSetHtml = 0x0402
|
|
|
|
var htmlQueue = make(chan string, 1)
|
|
|
|
func evalJS(js string) {
|
|
select {
|
|
case jsQueue <- js:
|
|
log.Println("evalJS queued:", js[:min(len(js), 60)])
|
|
default:
|
|
log.Println("jsQueue full, dropping eval")
|
|
}
|
|
if wvHwnd != 0 {
|
|
procPostMessageW.Call(wvHwnd, wmEvalJS, 0, 0)
|
|
}
|
|
}
|
|
|
|
func findWorkerW() uintptr {
|
|
progman, _, _ := procFindWindowW.Call(
|
|
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr("Progman"))), 0)
|
|
if progman == 0 {
|
|
return 0
|
|
}
|
|
var result uintptr
|
|
procSendMessageTimeoutW.Call(progman, 0x052C, 0, 0, 0x0000, 1000, uintptr(unsafe.Pointer(&result)))
|
|
shellDefView, _, _ := procFindWindowExW.Call(progman, 0,
|
|
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr("SHELLDLL_DefView"))), 0)
|
|
workerwAfterShell, _, _ := procFindWindowExW.Call(progman, shellDefView,
|
|
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr("WorkerW"))), 0)
|
|
if workerwAfterShell != 0 {
|
|
return workerwAfterShell
|
|
}
|
|
ww, _, _ := procFindWindowExW.Call(progman, 0,
|
|
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr("WorkerW"))), 0)
|
|
return ww
|
|
}
|
|
|
|
func getScreenSize() (int32, int32) {
|
|
w, _, _ := procGetSystemMetrics.Call(0)
|
|
h, _, _ := procGetSystemMetrics.Call(1)
|
|
return int32(w), int32(h)
|
|
}
|