-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
207 lines (181 loc) · 5.26 KB
/
app.go
File metadata and controls
207 lines (181 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"time"
"SimpleAI/modWindowMemory"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// Version is set during build from wails.json
var Version = "ersion dev"
// App struct
type App struct {
ctx context.Context
startupService string
windowPosMgr *modWindowMemory.WindowPositionManager
windowPosPath string // Path to windows.json
}
// NewApp creates a new App application struct
func NewApp() *App {
configDir, _ := os.UserConfigDir()
return &App{
windowPosMgr: modWindowMemory.NewWindowPositionManager(),
windowPosPath: filepath.Join(configDir, "SimpleAI", "windows.json"),
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
dbg := false // Set to true for debug output
a.ctx = ctx
if dbg {
println("[DEBUG] Startup - Service:", a.startupService, "PID:", os.Getpid())
}
a.windowPosMgr.Load(a.windowPosPath)
// Get window title for position restore
windowTitle := "SimpleAI"
if a.startupService != "" {
// Find service label
serviceLabels := map[string]string{
"chatgpt": "ChatGPT",
"claude": "Claude (Sonnet)",
"copilot": "Copilot",
"deepseek": "Deepseek",
"gemini": "Gemini",
"grok": "Grok",
"meta": "Meta AI",
"perplexity": "Perplexity",
}
if label, ok := serviceLabels[a.startupService]; ok {
windowTitle = "SimpleAI - " + label
}
}
wailsRuntime.WindowSetTitle(ctx, windowTitle)
a.windowPosMgr.RestorePosition(ctx, windowTitle)
}
// shutdown is called when the app is about to quit
func (a *App) shutdown(ctx context.Context) {
dbg := false // Set to true for debug output
if dbg {
println("[DEBUG] Shutdown - Service:", a.startupService, "PID:", os.Getpid())
}
// Note: Window position is already saved in OnBeforeClose hook (main.go)
// Don't save here as window may already be destroyed
}
// GetStartupService returns the service name to navigate to on startup
func (a *App) GetStartupService() string {
return a.startupService
}
// GetVersion returns the application version
func (a *App) GetVersion() string {
return Version
}
// GetWindowTitle returns the current window title based on startup service
func (a *App) GetWindowTitle() string {
windowTitle := "SimpleAI"
if a.startupService != "" {
serviceLabels := map[string]string{
"chatgpt": "ChatGPT",
"claude": "Claude (Sonnet)",
"copilot": "Copilot",
"deepseek": "Deepseek",
"gemini": "Gemini",
"grok": "Grok",
"meta": "Meta AI",
"perplexity": "Perplexity",
}
if label, ok := serviceLabels[a.startupService]; ok {
windowTitle = "SimpleAI - " + label
}
}
return windowTitle
}
// GoHome navigates back to the launcher page
func (a *App) GoHome() {
wailsRuntime.WindowReload(a.ctx)
}
// OpenNewInstance opens a new instance of the app with the specified service
// or activates an existing window if one is already open
func (a *App) OpenNewInstance(serviceName string) error {
dbg := false // Set to true for debug output
if dbg {
println("[DEBUG] OpenNewInstance called for:", serviceName)
}
// Get window title for this service
serviceLabels := map[string]string{
"chatgpt": "ChatGPT",
"claude": "Claude (Sonnet)",
"copilot": "Copilot",
"deepseek": "Deepseek",
"gemini": "Gemini",
"grok": "Grok",
"meta": "Meta AI",
"perplexity": "Perplexity",
}
windowTitle := "SimpleAI - " + serviceLabels[serviceName]
if dbg {
println("[DEBUG] Trying to find an existing window with title:", windowTitle)
}
// Try to find and activate existing window asynchronously
// This prevents blocking the UI while searching for windows
resultChan := make(chan struct {
found bool
err error
}, 1)
go func() {
found, err := findAndActivateWindow(windowTitle)
resultChan <- struct {
found bool
err error
}{found, err}
}()
// Wait for result with timeout
select {
case result := <-resultChan:
if result.err != nil {
if dbg {
println("[DEBUG] Error searching for window:", result.err)
}
// Continue to open new instance on error
}
if result.found {
if dbg {
println("[DEBUG] Found and activated existing window:", windowTitle)
}
return nil
}
case <-time.After(2 * time.Second):
// Timeout - proceed to open new instance
if dbg {
println("[DEBUG] Window search timed out, opening new instance")
}
}
// No existing window found, start new instance
if dbg {
println("[DEBUG] No existing window found, starting new instance")
}
exePath, err := os.Executable()
if err != nil {
return err
}
cmd := exec.Command(exePath, serviceName)
err = cmd.Start()
if err != nil {
return err
}
if dbg {
println("[DEBUG] Started new instance with PID:", cmd.Process.Pid)
}
return nil
}
// SaveWindowPositionManual allows manual saving of window position from frontend
func (a *App) SaveWindowPositionManual() error {
a.windowPosMgr.SavePosition(a.ctx, a.GetWindowTitle(), a.windowPosPath)
return nil
}
// findAndActivateWindow is implemented in platform-specific files:
// - app_windows.go: Uses native Windows API for fast window search
// - app_linux.go: Uses wmctrl or xdotool
// - app_darwin.go: Uses osascript with AppleScript