From 3770f9924abb0d5aa6c2b063ac82b7c6dd50e31c Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 5 Jan 2021 15:20:42 +0000 Subject: [PATCH] Fix hidden dialogs. --- color_windows.go | 1 + file_windows.go | 5 ++++ internal/zenutil/osascripts/notify.gojs | 1 - msg_windows.go | 1 + util_windows.go | 40 ++++++++++++++++++++----- 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/color_windows.go b/color_windows.go index a47c097..49c7dd6 100644 --- a/color_windows.go +++ b/color_windows.go @@ -54,6 +54,7 @@ func selectColor(options []Option) (color.Color, error) { defer unhook() } + activate() s, _, _ := chooseColor.Call(uintptr(unsafe.Pointer(&args))) if opts.ctx != nil && opts.ctx.Err() != nil { return nil, opts.ctx.Err() diff --git a/file_windows.go b/file_windows.go index 16843eb..2c44fa7 100644 --- a/file_windows.go +++ b/file_windows.go @@ -54,6 +54,7 @@ func selectFile(options []Option) (string, error) { defer unhook() } + activate() s, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args))) if opts.ctx != nil && opts.ctx.Err() != nil { return "", opts.ctx.Err() @@ -101,6 +102,7 @@ func selectFileMutiple(options []Option) ([]string, error) { defer unhook() } + activate() s, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args))) if opts.ctx != nil && opts.ctx.Err() != nil { return nil, opts.ctx.Err() @@ -179,6 +181,7 @@ func selectFileSave(options []Option) (string, error) { defer unhook() } + activate() s, _, _ := getSaveFileName.Call(uintptr(unsafe.Pointer(&args))) if opts.ctx != nil && opts.ctx.Err() != nil { return "", opts.ctx.Err() @@ -253,6 +256,7 @@ func pickFolders(opts options, multi bool) (str string, lst []string, err error) defer unhook() } + activate() hr, _, _ = dialog.Call(dialog.vtbl.Show, 0) if opts.ctx != nil && opts.ctx.Err() != nil { return "", nil, opts.ctx.Err() @@ -335,6 +339,7 @@ func browseForFolder(opts options) (string, []string, error) { defer unhook() } + activate() ptr, _, _ := shBrowseForFolder.Call(uintptr(unsafe.Pointer(&args))) if opts.ctx != nil && opts.ctx.Err() != nil { return "", nil, opts.ctx.Err() diff --git a/internal/zenutil/osascripts/notify.gojs b/internal/zenutil/osascripts/notify.gojs index 8018ab8..4bddb25 100644 --- a/internal/zenutil/osascripts/notify.gojs +++ b/internal/zenutil/osascripts/notify.gojs @@ -1,6 +1,5 @@ var app = Application.currentApplication() app.includeStandardAdditions = true -app.activate() var opts = {} diff --git a/msg_windows.go b/msg_windows.go index 45ccdcf..bc24820 100644 --- a/msg_windows.go +++ b/msg_windows.go @@ -53,6 +53,7 @@ func message(kind messageKind, text string, options []Option) (bool, error) { defer unhook() } + activate() s, _, err := messageBox.Call(0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(opts.title))), flags) diff --git a/util_windows.go b/util_windows.go index cb6c928..5aaee26 100644 --- a/util_windows.go +++ b/util_windows.go @@ -3,6 +3,7 @@ package zenity import ( "context" "fmt" + "os" "sync" "syscall" "unsafe" @@ -19,22 +20,45 @@ var ( commDlgExtendedError = comdlg32.NewProc("CommDlgExtendedError") getCurrentThreadId = kernel32.NewProc("GetCurrentThreadId") + getConsoleWindow = kernel32.NewProc("GetConsoleWindow") coInitializeEx = ole32.NewProc("CoInitializeEx") coUninitialize = ole32.NewProc("CoUninitialize") coCreateInstance = ole32.NewProc("CoCreateInstance") coTaskMemFree = ole32.NewProc("CoTaskMemFree") - sendMessage = user32.NewProc("SendMessageW") - getClassName = user32.NewProc("GetClassNameW") - setWindowsHookEx = user32.NewProc("SetWindowsHookExW") - unhookWindowsHookEx = user32.NewProc("UnhookWindowsHookEx") - callNextHookEx = user32.NewProc("CallNextHookEx") - enumChildWindows = user32.NewProc("EnumChildWindows") - getDlgCtrlID = user32.NewProc("GetDlgCtrlID") - setWindowText = user32.NewProc("SetWindowTextW") + sendMessage = user32.NewProc("SendMessageW") + getClassName = user32.NewProc("GetClassNameW") + setWindowsHookEx = user32.NewProc("SetWindowsHookExW") + unhookWindowsHookEx = user32.NewProc("UnhookWindowsHookEx") + callNextHookEx = user32.NewProc("CallNextHookEx") + enumWindows = user32.NewProc("EnumWindows") + enumChildWindows = user32.NewProc("EnumChildWindows") + getDlgCtrlID = user32.NewProc("GetDlgCtrlID") + setWindowText = user32.NewProc("SetWindowTextW") + setForegroundWindow = user32.NewProc("SetForegroundWindow") + getWindowThreadProcessId = user32.NewProc("GetWindowThreadProcessId") ) +func activate() { + var hwnd uintptr + enumWindows.Call(syscall.NewCallback(func(wnd, lparam uintptr) uintptr { + var pid uintptr + getWindowThreadProcessId.Call(wnd, uintptr(unsafe.Pointer(&pid))) + if int(pid) == os.Getpid() { + hwnd = wnd + return 0 + } + return 1 + }), 0) + if hwnd == 0 { + hwnd, _, _ = getConsoleWindow.Call() + } + if hwnd != 0 { + setForegroundWindow.Call(hwnd) + } +} + func commDlgError() error { s, _, _ := commDlgExtendedError.Call() if s == 0 {