This commit is contained in:
11
client/internal/singleinstance/singleinstance.go
Normal file
11
client/internal/singleinstance/singleinstance.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package singleinstance
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrAlreadyRunning — đã có một tiến trình Simple Care đang chạy.
|
||||
var ErrAlreadyRunning = errors.New("simple care already running")
|
||||
|
||||
const (
|
||||
mutexName = "Local\\SimpleCare_SingleInstance"
|
||||
windowTitle = "Simple Care"
|
||||
)
|
||||
49
client/internal/singleinstance/singleinstance_unix.go
Normal file
49
client/internal/singleinstance/singleinstance_unix.go
Normal file
@@ -0,0 +1,49 @@
|
||||
//go:build unix
|
||||
|
||||
package singleinstance
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"client/internal/winapi"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Giữ file lock mở suốt đời process.
|
||||
var lockFile *os.File
|
||||
|
||||
// Acquire — flock non-blocking trên file trong config dir.
|
||||
func Acquire() error {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
configDir = os.TempDir()
|
||||
}
|
||||
dir := filepath.Join(configDir, "SimpleCare")
|
||||
_ = os.MkdirAll(dir, 0755)
|
||||
path := filepath.Join(dir, "instance.lock")
|
||||
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := unix.Flock(int(f.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
|
||||
_ = f.Close()
|
||||
winapi.ActivateAppWindow(windowTitle)
|
||||
winapi.ShowWarningMessageBox(
|
||||
"Simple Care",
|
||||
"Ứng dụng Simple Care đang chạy.\n\nChỉ được mở một cửa sổ.",
|
||||
)
|
||||
// Cho dialog async kịp hiện trước khi process thoát.
|
||||
time.Sleep(1500 * time.Millisecond)
|
||||
fmt.Fprintln(os.Stderr, "Simple Care is already running")
|
||||
return ErrAlreadyRunning
|
||||
}
|
||||
_, _ = f.WriteString(fmt.Sprintf("%d\n", os.Getpid()))
|
||||
_ = f.Sync()
|
||||
lockFile = f
|
||||
return nil
|
||||
}
|
||||
47
client/internal/singleinstance/singleinstance_windows.go
Normal file
47
client/internal/singleinstance/singleinstance_windows.go
Normal file
@@ -0,0 +1,47 @@
|
||||
//go:build windows
|
||||
|
||||
package singleinstance
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"client/internal/winapi"
|
||||
)
|
||||
|
||||
var (
|
||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
procCreateMutexW = kernel32.NewProc("CreateMutexW")
|
||||
user32 = syscall.NewLazyDLL("user32.dll")
|
||||
procMessageBoxW = user32.NewProc("MessageBoxW")
|
||||
|
||||
// Giữ handle mutex suốt đời process — đóng = nhả lock.
|
||||
mutexHandle uintptr
|
||||
)
|
||||
|
||||
const errorAlreadyExists = 183
|
||||
|
||||
// Acquire — chỉ cho phép 1 instance. Instance thứ 2: đưa cửa sổ cũ lên rồi báo lỗi.
|
||||
func Acquire() error {
|
||||
namePtr, err := syscall.UTF16PtrFromString(mutexName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r1, _, lastErr := procCreateMutexW.Call(0, 0, uintptr(unsafe.Pointer(namePtr)))
|
||||
if r1 == 0 {
|
||||
return lastErr
|
||||
}
|
||||
mutexHandle = r1
|
||||
if errno, ok := lastErr.(syscall.Errno); ok && errno == errorAlreadyExists {
|
||||
winapi.ActivateAppWindow(windowTitle)
|
||||
showAlreadyRunningDialog()
|
||||
return ErrAlreadyRunning
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func showAlreadyRunningDialog() {
|
||||
title, _ := syscall.UTF16PtrFromString("Simple Care")
|
||||
msg, _ := syscall.UTF16PtrFromString("Ứng dụng Simple Care đang chạy.\n\nChỉ được mở một cửa sổ. Cửa sổ hiện có đã được đưa lên phía trước.")
|
||||
_, _, _ = procMessageBoxW.Call(0, uintptr(unsafe.Pointer(msg)), uintptr(unsafe.Pointer(title)), 0x00000040) // MB_ICONINFORMATION
|
||||
}
|
||||
@@ -4,14 +4,23 @@ package winapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// ActivateAppWindow — đưa cửa sổ app lên trước
|
||||
// ActivateAppWindow — đưa cửa sổ Simple Care đang chạy lên trước (theo tên process / title).
|
||||
func ActivateAppWindow(titleHint string) {
|
||||
pid := os.Getpid()
|
||||
script := fmt.Sprintf(`tell application "System Events" to set frontmost of first process whose unix id is %d to true`, pid)
|
||||
hint := titleHint
|
||||
if hint == "" {
|
||||
hint = "Simple Care"
|
||||
}
|
||||
script := fmt.Sprintf(`
|
||||
tell application "System Events"
|
||||
set candidates to every process whose name contains %q or name contains "simple_care" or name contains "SimpleCare"
|
||||
if (count of candidates) > 0 then
|
||||
set frontmost of item 1 of candidates to true
|
||||
end if
|
||||
end tell
|
||||
`, hint)
|
||||
cmd := exec.Command("osascript", "-e", script)
|
||||
_ = cmd.Run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user