[v3 plugins] Support single-instance on Windows

This commit is contained in:
Lea Anthony 2023-06-06 21:06:52 +10:00
commit 3b0a57ca6d
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
2 changed files with 44 additions and 0 deletions

View file

@ -137,6 +137,7 @@ var (
procEnumDisplayDevices = moduser32.NewProc("EnumDisplayDevicesW")
procEnumDisplaySettings = moduser32.NewProc("EnumDisplaySettingsW")
procEnumDisplaySettingsEx = moduser32.NewProc("EnumDisplaySettingsExW")
procEnumWindows = moduser32.NewProc("EnumWindows")
procChangeDisplaySettingsEx = moduser32.NewProc("ChangeDisplaySettingsExW")
procSendInput = moduser32.NewProc("SendInput")
procSetWindowsHookEx = moduser32.NewProc("SetWindowsHookExW")
@ -656,6 +657,14 @@ func ReleaseCapture() bool {
return ret != 0
}
func EnumWindows(enumFunc uintptr, lparam uintptr) bool {
ret, _, _ := procEnumWindows.Call(
enumFunc,
lparam)
return ret != 0
}
func GetWindowThreadProcessId(hwnd HWND) (HANDLE, int) {
var processId int
ret, _, _ := procGetWindowThreadProcessId.Call(

View file

@ -0,0 +1,35 @@
//go:build windows
package single_instance
import (
"github.com/wailsapp/wails/v3/pkg/w32"
"syscall"
)
type enumWindowsCallback func(hwnd syscall.Handle, lParam uintptr) uintptr
func enumWindowsProc(hwnd syscall.Handle, lParam uintptr) uintptr {
_, processID := w32.GetWindowThreadProcessId(uintptr(hwnd))
targetProcessID := uint32(lParam)
if uint32(processID) == targetProcessID {
// Bring the window forward
w32.SetForegroundWindow(w32.HWND(hwnd))
println("Found window: ", hwnd)
}
// Continue enumeration
return 1
}
func (p *Plugin) activeInstance(pid int) error {
// Get the window associated with the process ID.
targetProcessID := uint32(pid) // Replace with the desired process ID
w32.EnumWindows(
syscall.NewCallback(enumWindowsCallback(enumWindowsProc)),
uintptr(targetProcessID),
)
return nil
}