|
1 | | -package main |
2 | | - |
3 | | -import ( |
4 | | - "fmt" |
5 | | - "os" |
6 | | - |
7 | | - "click-guardian/internal/gui" |
8 | | - "click-guardian/internal/version" |
9 | | -) |
10 | | - |
11 | | -func main() { |
12 | | - // Check for command line arguments |
13 | | - startMinimized := false |
14 | | - autoProtect := false |
15 | | - |
16 | | - for _, arg := range os.Args[1:] { |
17 | | - switch arg { |
18 | | - case "--minimized": |
19 | | - startMinimized = true |
20 | | - case "--auto-protect": |
21 | | - autoProtect = true |
22 | | - case "--version", "-v": |
23 | | - fmt.Println(version.GetFullVersionString()) |
24 | | - return |
25 | | - case "--help", "-h": |
26 | | - showHelp() |
27 | | - return |
28 | | - } |
29 | | - } |
30 | | - |
31 | | - app := gui.NewApplication() |
32 | | - if startMinimized { |
33 | | - app.RunMinimized() |
34 | | - } else { |
35 | | - if autoProtect { |
36 | | - app.RunWithAutoProtect() |
37 | | - } else { |
38 | | - app.Run() |
39 | | - } |
40 | | - } |
41 | | -} |
42 | | - |
43 | | -// showHelp displays command-line usage information |
44 | | -func showHelp() { |
45 | | - info := version.GetAppInfo() |
46 | | - fmt.Printf("%s v%s\n", info.Name, info.Version) |
47 | | - fmt.Printf("%s\n\n", info.Description) |
48 | | - |
49 | | - fmt.Println("Usage:") |
50 | | - fmt.Printf(" %s [options]\n\n", os.Args[0]) |
51 | | - |
52 | | - fmt.Println("Options:") |
53 | | - fmt.Println(" --minimized Start minimized to system tray") |
54 | | - fmt.Println(" --auto-protect Start with protection automatically enabled") |
55 | | - fmt.Println(" --version, -v Show version information") |
56 | | - fmt.Println(" --help, -h Show this help message") |
57 | | - fmt.Println() |
58 | | - |
59 | | - fmt.Println("Examples:") |
60 | | - fmt.Printf(" %s # Start normally\n", os.Args[0]) |
61 | | - fmt.Printf(" %s --minimized # Start minimized to tray\n", os.Args[0]) |
62 | | - fmt.Printf(" %s --auto-protect # Start with protection enabled\n", os.Args[0]) |
63 | | - fmt.Println() |
64 | | - |
65 | | - fmt.Printf("%s\n", info.Copyright) |
66 | | -} |
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + "click-guardian/internal/gui" |
| 9 | + "click-guardian/internal/version" |
| 10 | + "click-guardian/pkg/platform" |
| 11 | + |
| 12 | + "github.com/juju/mutex/v2" |
| 13 | +) |
| 14 | + |
| 15 | +// realClock implements mutex.Clock using the real system clock |
| 16 | +type realClock struct{} |
| 17 | + |
| 18 | +// After waits for the duration to elapse and then sends the current time on the returned channel |
| 19 | +func (realClock) After(d time.Duration) <-chan time.Time { |
| 20 | + return time.After(d) |
| 21 | +} |
| 22 | + |
| 23 | +// Now returns the current clock time |
| 24 | +func (realClock) Now() time.Time { |
| 25 | + return time.Now() |
| 26 | +} |
| 27 | + |
| 28 | +func main() { |
| 29 | + // Check for command line arguments |
| 30 | + startMinimized := false |
| 31 | + autoProtect := false |
| 32 | + |
| 33 | + for _, arg := range os.Args[1:] { |
| 34 | + switch arg { |
| 35 | + case "--minimized": |
| 36 | + startMinimized = true |
| 37 | + case "--auto-protect": |
| 38 | + autoProtect = true |
| 39 | + case "--version", "-v": |
| 40 | + fmt.Println(version.GetFullVersionString()) |
| 41 | + return |
| 42 | + case "--help", "-h": |
| 43 | + showHelp() |
| 44 | + return |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Define a unique name for your app's mutex |
| 49 | + spec := mutex.Spec{ |
| 50 | + Name: "click-guardian-single-instance", // Must be unique per app (valid format) |
| 51 | + Clock: realClock{}, // Use real-time clock |
| 52 | + Delay: 500 * time.Millisecond, // Polling interval |
| 53 | + Timeout: 1 * time.Second, // How long to wait for the mutex |
| 54 | + } |
| 55 | + |
| 56 | + // Try to acquire the mutex |
| 57 | + releaser, err := mutex.Acquire(spec) |
| 58 | + if err != nil { |
| 59 | + // If mutex acquisition fails, another instance is running |
| 60 | + showAlreadyRunningMessage() |
| 61 | + os.Exit(1) |
| 62 | + } |
| 63 | + defer releaser.Release() // Release mutex when the app exits |
| 64 | + |
| 65 | + // If we reach here, this is the only instance |
| 66 | + app := gui.NewApplication() |
| 67 | + if startMinimized { |
| 68 | + app.RunMinimized() |
| 69 | + } else { |
| 70 | + if autoProtect { |
| 71 | + app.RunWithAutoProtect() |
| 72 | + } else { |
| 73 | + app.Run() |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// showAlreadyRunningMessage shows a dialog informing the user that another instance is running |
| 79 | +func showAlreadyRunningMessage() { |
| 80 | + // Show a native Windows message box |
| 81 | + platform.ShowMessageBox("Click Guardian", "Click Guardian is already running.\n\nLook for the icon in your system tray or check your taskbar.") |
| 82 | +} |
| 83 | + |
| 84 | +// showHelp displays command-line usage information |
| 85 | +func showHelp() { |
| 86 | + info := version.GetAppInfo() |
| 87 | + fmt.Printf("%s v%s\n", info.Name, info.Version) |
| 88 | + fmt.Printf("%s\n\n", info.Description) |
| 89 | + |
| 90 | + fmt.Println("Usage:") |
| 91 | + fmt.Printf(" %s [options]\n\n", os.Args[0]) |
| 92 | + |
| 93 | + fmt.Println("Options:") |
| 94 | + fmt.Println(" --minimized Start minimized to system tray") |
| 95 | + fmt.Println(" --auto-protect Start with protection automatically enabled") |
| 96 | + fmt.Println(" --version, -v Show version information") |
| 97 | + fmt.Println(" --help, -h Show this help message") |
| 98 | + fmt.Println() |
| 99 | + |
| 100 | + fmt.Println("Examples:") |
| 101 | + fmt.Printf(" %s # Start normally\n", os.Args[0]) |
| 102 | + fmt.Printf(" %s --minimized # Start minimized to tray\n", os.Args[0]) |
| 103 | + fmt.Printf(" %s --auto-protect # Start with protection enabled\n", os.Args[0]) |
| 104 | + fmt.Println() |
| 105 | + |
| 106 | + fmt.Printf("%s\n", info.Copyright) |
| 107 | +} |
0 commit comments