zenity/util_windows.go

376 lines
8.3 KiB
Go
Raw Permalink Normal View History

package zenity
import (
2022-05-19 08:47:38 -04:00
"bytes"
2020-01-30 09:14:42 -05:00
"context"
2022-07-26 19:25:18 -04:00
"io"
2021-01-05 10:20:42 -05:00
"os"
2022-07-26 11:56:43 -04:00
"path/filepath"
2021-03-26 08:40:24 -04:00
"runtime"
2021-04-08 20:05:48 -04:00
"strconv"
"sync"
2021-03-26 08:40:24 -04:00
"sync/atomic"
"syscall"
"unsafe"
2021-09-09 20:32:20 -04:00
2024-07-10 00:06:01 -04:00
"git.bigun.dev/evan/zenity/internal/win"
2022-06-21 07:28:21 -04:00
)
const (
_WS_ZEN_DIALOG = win.WS_POPUPWINDOW | win.WS_CLIPSIBLINGS | win.WS_DLGFRAME
_WS_EX_ZEN_DIALOG = win.WS_EX_CONTROLPARENT | win.WS_EX_WINDOWEDGE | win.WS_EX_DLGMODALFRAME
_WS_ZEN_LABEL = win.WS_CHILD | win.WS_VISIBLE | win.WS_GROUP | win.SS_WORDELLIPSIS | win.SS_EDITCONTROL | win.SS_NOPREFIX
_WS_ZEN_CONTROL = win.WS_CHILD | win.WS_VISIBLE | win.WS_GROUP | win.WS_TABSTOP
_WS_ZEN_BUTTON = _WS_ZEN_CONTROL
)
2022-06-20 11:05:11 -04:00
func intptr(i int64) uintptr { return uintptr(i) }
func strptr(s string) *uint16 { return syscall.StringToUTF16Ptr(s) }
2022-06-21 09:27:19 -04:00
2022-06-28 09:52:02 -04:00
func setup(wnd win.HWND) context.CancelFunc {
if wnd == 0 {
win.EnumWindows(syscall.NewCallback(setupEnumCallback), unsafe.Pointer(&wnd))
}
if wnd == 0 {
2022-06-18 19:48:38 -04:00
wnd = win.GetConsoleWindow()
2021-01-05 10:20:42 -05:00
}
if wnd != 0 {
2022-06-19 21:14:08 -04:00
win.SetForegroundWindow(wnd)
2021-01-05 10:20:42 -05:00
}
2021-03-26 08:40:24 -04:00
runtime.LockOSThread()
2021-04-27 20:27:28 -04:00
var restore uintptr
cookie := enableVisualStyles()
2022-06-19 21:14:08 -04:00
for dpi := win.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2; dpi <= win.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE; dpi++ {
var err error
restore, err = win.SetThreadDpiAwarenessContext(dpi)
if restore != 0 || err != nil {
break
2021-03-26 08:40:24 -04:00
}
}
2022-06-17 22:45:49 -04:00
var icc win.INITCOMMONCONTROLSEX
2021-04-27 09:05:04 -04:00
icc.Size = uint32(unsafe.Sizeof(icc))
2022-06-22 10:14:52 -04:00
icc.ICC = win.ICC_STANDARD_CLASSES | win.ICC_DATE_CLASSES | win.ICC_PROGRESS_CLASS
2022-06-17 22:45:49 -04:00
win.InitCommonControlsEx(&icc)
2021-04-27 09:05:04 -04:00
2021-03-26 08:40:24 -04:00
return func() {
2021-04-27 20:27:28 -04:00
if restore != 0 {
2022-06-19 21:14:08 -04:00
win.SetThreadDpiAwarenessContext(restore)
2021-04-27 20:27:28 -04:00
}
if cookie != 0 {
2022-06-20 08:37:28 -04:00
win.DeactivateActCtx(0, cookie)
2021-03-26 08:40:24 -04:00
}
2021-04-09 08:39:15 -04:00
runtime.UnlockOSThread()
2021-03-26 08:40:24 -04:00
}
2021-01-05 10:20:42 -05:00
}
2022-06-18 19:48:38 -04:00
func setupEnumCallback(wnd win.HWND, lparam *win.HWND) uintptr {
var pid uint32
win.GetWindowThreadProcessId(wnd, &pid)
if int(pid) == os.Getpid() {
*lparam = wnd
return 0 // stop enumeration
}
return 1 // continue enumeration
}
2022-06-18 19:48:38 -04:00
func hookDialog(ctx context.Context, icon any, title *string, init func(wnd win.HWND)) (unhook context.CancelFunc, err error) {
2022-07-26 09:49:49 -04:00
if ctx == nil && icon == nil && title == nil && init == nil {
2022-06-23 09:49:08 -04:00
return func() {}, nil
}
2020-01-30 09:14:42 -05:00
if ctx != nil && ctx.Err() != nil {
return nil, ctx.Err()
}
2022-05-19 18:39:21 -04:00
hook, err := newDialogHook(ctx, icon, title, init)
if err != nil {
return nil, err
}
return hook.unhook, nil
}
2020-01-30 09:14:42 -05:00
type dialogHook struct {
2022-05-19 18:39:21 -04:00
ctx context.Context
2022-06-18 19:48:38 -04:00
tid uint32
2022-05-19 18:39:21 -04:00
wnd uintptr
2022-06-21 09:27:19 -04:00
hook win.Handle
2022-05-19 18:39:21 -04:00
done chan struct{}
2022-07-26 09:49:49 -04:00
icon icon
2022-05-19 18:39:21 -04:00
title *string
2022-06-18 19:48:38 -04:00
init func(wnd win.HWND)
}
2020-01-30 09:14:42 -05:00
2022-06-18 19:48:38 -04:00
func newDialogHook(ctx context.Context, icon any, title *string, init func(wnd win.HWND)) (*dialogHook, error) {
2022-06-18 19:48:38 -04:00
tid := win.GetCurrentThreadId()
2022-06-21 09:27:19 -04:00
hk, err := win.SetWindowsHookEx(win.WH_CALLWNDPROCRET,
syscall.NewCallback(dialogHookProc), 0, tid)
if hk == 0 {
2020-01-30 09:14:42 -05:00
return nil, err
}
2022-07-26 11:56:43 -04:00
ico, _ := getIcon(icon)
hook := dialogHook{
2022-05-19 18:39:21 -04:00
ctx: ctx,
tid: tid,
hook: hk,
2022-07-26 11:56:43 -04:00
icon: ico,
2022-05-19 18:39:21 -04:00
title: title,
init: init,
}
2024-02-23 08:07:06 -05:00
if ctx != nil && ctx.Done() != nil {
hook.done = make(chan struct{})
go hook.wait()
2020-01-30 09:14:42 -05:00
}
2022-06-18 19:48:38 -04:00
saveBackRef(uintptr(tid), unsafe.Pointer(&hook))
return &hook, nil
}
2022-06-21 19:58:14 -04:00
func dialogHookProc(code int32, wparam uintptr, lparam *win.CWPRETSTRUCT) uintptr {
2022-06-21 07:28:21 -04:00
if lparam.Message == win.WM_INITDIALOG {
2022-06-18 19:48:38 -04:00
tid := win.GetCurrentThreadId()
hook := (*dialogHook)(loadBackRef(uintptr(tid)))
2022-06-18 19:48:38 -04:00
atomic.StoreUintptr(&hook.wnd, uintptr(lparam.Wnd))
2022-05-09 10:49:32 -04:00
if hook.ctx != nil && hook.ctx.Err() != nil {
2022-06-21 07:28:21 -04:00
win.SendMessage(lparam.Wnd, win.WM_SYSCOMMAND, win.SC_CLOSE, 0)
2022-05-19 18:39:21 -04:00
} else {
2022-07-26 09:49:49 -04:00
if hook.icon.handle != 0 {
win.SendMessage(lparam.Wnd, win.WM_SETICON, 0, uintptr(hook.icon.handle))
2022-05-19 18:39:21 -04:00
}
if hook.title != nil {
2022-06-20 11:05:11 -04:00
win.SetWindowText(lparam.Wnd, strptr(*hook.title))
2022-05-19 18:39:21 -04:00
}
if hook.init != nil {
2022-05-23 19:12:38 -04:00
hook.init(lparam.Wnd)
2022-05-19 18:39:21 -04:00
}
2022-03-23 09:24:02 -04:00
}
}
2022-06-21 09:27:19 -04:00
return win.CallNextHookEx(0, code, wparam, unsafe.Pointer(lparam))
}
func (h *dialogHook) unhook() {
2022-06-18 19:48:38 -04:00
deleteBackRef(uintptr(h.tid))
if h.done != nil {
close(h.done)
}
2022-07-26 09:49:49 -04:00
h.icon.delete()
2022-06-21 09:27:19 -04:00
win.UnhookWindowsHookEx(h.hook)
}
func (h *dialogHook) wait() {
select {
case <-h.ctx.Done():
if wnd := atomic.LoadUintptr(&h.wnd); wnd != 0 {
2022-06-21 07:28:21 -04:00
win.SendMessage(win.HWND(wnd), win.WM_SYSCOMMAND, win.SC_CLOSE, 0)
2020-01-30 09:14:42 -05:00
}
case <-h.done:
}
}
2022-03-23 09:24:02 -04:00
var backRefs struct {
sync.Mutex
2022-03-23 09:24:02 -04:00
m map[uintptr]unsafe.Pointer
}
2022-03-23 09:24:02 -04:00
func saveBackRef(id uintptr, ptr unsafe.Pointer) {
backRefs.Lock()
defer backRefs.Unlock()
if backRefs.m == nil {
backRefs.m = map[uintptr]unsafe.Pointer{}
2022-05-10 05:30:52 -04:00
} else if _, ok := backRefs.m[id]; ok {
panic("saveBackRef")
}
2022-03-23 09:24:02 -04:00
backRefs.m[id] = ptr
}
2022-03-23 09:24:02 -04:00
func loadBackRef(id uintptr) unsafe.Pointer {
backRefs.Lock()
defer backRefs.Unlock()
return backRefs.m[id]
2020-01-30 09:14:42 -05:00
}
2022-03-23 09:24:02 -04:00
func deleteBackRef(id uintptr) {
backRefs.Lock()
defer backRefs.Unlock()
delete(backRefs.m, id)
2020-01-24 08:59:03 -05:00
}
2022-06-20 08:37:28 -04:00
type dpi int
2021-04-08 19:55:49 -04:00
2022-06-20 11:05:11 -04:00
func getDPI(wnd win.HWND) dpi {
2022-06-20 08:37:28 -04:00
res, _ := win.GetDpiForWindow(wnd)
if res != 0 {
return dpi(res)
}
if dc := win.GetWindowDC(wnd); dc != 0 {
res = win.GetDeviceCaps(dc, win.LOGPIXELSY)
win.ReleaseDC(wnd, dc)
2021-04-08 19:55:49 -04:00
}
if res == 0 {
2022-06-20 08:37:28 -04:00
return win.USER_DEFAULT_SCREEN_DPI
2021-04-08 19:55:49 -04:00
}
return dpi(res)
2021-04-05 12:54:46 -04:00
}
2022-06-20 11:05:11 -04:00
func (d dpi) scale(dim int) int {
2021-04-08 19:55:49 -04:00
if d == 0 {
return dim
}
2022-06-20 11:05:11 -04:00
return dim * int(d) / win.USER_DEFAULT_SCREEN_DPI
2021-04-08 19:55:49 -04:00
}
type font struct {
2022-06-18 07:37:39 -04:00
handle win.Handle
logical win.LOGFONT
2021-04-08 19:55:49 -04:00
}
func getFont() font {
2022-06-21 07:28:21 -04:00
var metrics win.NONCLIENTMETRICS
2021-04-08 19:55:49 -04:00
metrics.Size = uint32(unsafe.Sizeof(metrics))
2022-06-21 07:28:21 -04:00
win.SystemParametersInfo(win.SPI_GETNONCLIENTMETRICS,
unsafe.Sizeof(metrics), unsafe.Pointer(&metrics), 0)
2021-04-08 19:55:49 -04:00
return font{logical: metrics.MessageFont}
}
2022-03-23 09:52:52 -04:00
func (f *font) forDPI(dpi dpi) uintptr {
if h := -int32(dpi.scale(12)); f.handle == 0 || f.logical.Height != h {
f.delete()
2021-04-08 19:55:49 -04:00
f.logical.Height = h
2022-06-18 07:37:39 -04:00
f.handle = win.CreateFontIndirect(&f.logical)
2021-04-08 19:55:49 -04:00
}
2022-06-18 07:37:39 -04:00
return uintptr(f.handle)
2021-04-08 19:55:49 -04:00
}
2022-03-23 09:52:52 -04:00
func (f *font) delete() {
2021-04-08 19:55:49 -04:00
if f.handle != 0 {
2022-06-18 07:37:39 -04:00
win.DeleteObject(f.handle)
2021-04-08 19:55:49 -04:00
f.handle = 0
}
}
2022-05-19 08:47:38 -04:00
type icon struct {
2022-06-20 08:37:28 -04:00
handle win.Handle
2022-05-19 08:47:38 -04:00
destroy bool
}
2022-07-26 11:56:43 -04:00
func getIcon(i any) (icon icon, err error) {
2022-05-19 08:47:38 -04:00
var resource uintptr
switch i {
case ErrorIcon:
2022-06-22 10:14:52 -04:00
resource = win.IDI_ERROR
2022-05-19 08:47:38 -04:00
case QuestionIcon:
2022-06-22 10:14:52 -04:00
resource = win.IDI_QUESTION
2022-05-19 08:47:38 -04:00
case WarningIcon:
2022-06-22 10:14:52 -04:00
resource = win.IDI_WARNING
2022-05-19 08:47:38 -04:00
case InfoIcon:
2022-06-22 10:14:52 -04:00
resource = win.IDI_INFORMATION
2022-05-19 08:47:38 -04:00
}
if resource != 0 {
2022-07-26 11:56:43 -04:00
icon.handle, err = win.LoadIcon(0, resource)
return icon, err
2022-05-19 08:47:38 -04:00
}
path, ok := i.(string)
if !ok {
2022-07-26 11:56:43 -04:00
return icon, nil
2022-05-19 08:47:38 -04:00
}
2022-07-26 19:25:18 -04:00
file, err := os.Open(path)
2022-05-19 08:47:38 -04:00
if err != nil {
2022-07-26 11:56:43 -04:00
return icon, err
2022-05-19 08:47:38 -04:00
}
2022-07-26 19:25:18 -04:00
defer file.Close()
2022-05-19 08:47:38 -04:00
2022-07-26 19:25:18 -04:00
var peek [8]byte
_, err = file.ReadAt(peek[:], 0)
if err != nil {
return icon, err
}
if bytes.Equal(peek[:], []byte("\x89PNG\r\n\x1a\n")) {
data, err := io.ReadAll(file)
if err != nil {
return icon, err
}
2022-07-26 11:56:43 -04:00
icon.handle, err = win.CreateIconFromResourceEx(
2022-07-26 09:49:49 -04:00
data, true, 0x00030000, 0, 0,
win.LR_DEFAULTSIZE)
2022-07-26 11:56:43 -04:00
if err != nil {
2022-07-26 19:25:18 -04:00
return icon, err
}
} else {
instance, err := win.GetModuleHandle(nil)
if err != nil {
return icon, err
2022-07-26 11:56:43 -04:00
}
path, err = filepath.Abs(path)
if err != nil {
2022-07-26 19:25:18 -04:00
return icon, err
2022-07-26 11:56:43 -04:00
}
var i uint16
icon.handle, err = win.ExtractAssociatedIcon(
instance, strptr(path), &i)
2022-07-26 19:25:18 -04:00
if err != nil {
return icon, err
}
2022-05-19 08:47:38 -04:00
}
2022-07-26 19:25:18 -04:00
icon.destroy = true
return icon, nil
2022-05-19 08:47:38 -04:00
}
func (i *icon) delete() {
2022-07-26 11:56:43 -04:00
if i.handle != 0 && i.destroy {
2022-06-20 08:37:28 -04:00
win.DestroyIcon(i.handle)
2022-05-19 08:47:38 -04:00
i.handle = 0
}
}
2022-06-20 11:05:11 -04:00
func centerWindow(wnd win.HWND) {
var rect win.RECT
win.GetWindowRect(wnd, &rect)
2022-06-21 07:28:21 -04:00
x := win.GetSystemMetrics(win.SM_CXSCREEN) - int(rect.Right-rect.Left)
y := win.GetSystemMetrics(win.SM_CYSCREEN) - int(rect.Bottom-rect.Top)
win.SetWindowPos(wnd, 0, x/2, y/2, 0, 0, win.SWP_NOZORDER|win.SWP_NOSIZE)
2021-04-08 20:05:48 -04:00
}
2022-06-20 11:05:11 -04:00
func registerClass(instance, icon win.Handle, proc uintptr) (*uint16, error) {
2021-04-08 20:05:48 -04:00
name := "WC_" + strconv.FormatUint(uint64(proc), 16)
2022-06-20 08:37:28 -04:00
var wcx win.WNDCLASSEX
2021-04-08 20:05:48 -04:00
wcx.Size = uint32(unsafe.Sizeof(wcx))
wcx.WndProc = proc
2022-05-23 19:12:38 -04:00
wcx.Icon = icon
2021-04-08 20:05:48 -04:00
wcx.Instance = instance
2022-06-22 10:14:52 -04:00
wcx.Background = win.COLOR_WINDOW
2022-06-20 11:05:11 -04:00
wcx.ClassName = strptr(name)
2021-04-08 20:05:48 -04:00
2022-06-20 11:05:11 -04:00
if err := win.RegisterClassEx(&wcx); err != nil {
return nil, err
}
return wcx.ClassName, nil
2021-04-08 20:05:48 -04:00
}
2021-04-27 20:27:28 -04:00
// https://stackoverflow.com/questions/4308503/how-to-enable-visual-styles-without-a-manifest
func enableVisualStyles() (cookie uintptr) {
2022-06-18 19:48:38 -04:00
dir, err := win.GetSystemDirectory()
if err != nil {
2021-04-27 20:27:28 -04:00
return
}
2022-06-20 08:37:28 -04:00
var ctx win.ACTCTX
2021-04-27 20:27:28 -04:00
ctx.Size = uint32(unsafe.Sizeof(ctx))
2022-06-20 08:37:28 -04:00
ctx.Flags = win.ACTCTX_FLAG_RESOURCE_NAME_VALID | win.ACTCTX_FLAG_SET_PROCESS_DEFAULT | win.ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID
2022-06-20 11:05:11 -04:00
ctx.Source = strptr("shell32.dll")
ctx.AssemblyDirectory = strptr(dir)
2021-04-27 20:27:28 -04:00
ctx.ResourceName = 124
2022-06-20 08:37:28 -04:00
if hnd, err := win.CreateActCtx(&ctx); err == nil {
win.ActivateActCtx(hnd, &cookie)
win.ReleaseActCtx(hnd)
2021-04-27 20:27:28 -04:00
}
return
}