-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindow.rs
More file actions
executable file
·427 lines (387 loc) · 13.4 KB
/
window.rs
File metadata and controls
executable file
·427 lines (387 loc) · 13.4 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::sync::Mutex;
use tauri::{AppHandle, Manager, WebviewUrl, WebviewWindowBuilder};
use tracing::{error, info};
use uuid::Uuid;
/// Apply vibrancy/blur effect to a window for glassmorphism
/// DISABLED: Using solid background instead of transparent vibrancy
#[allow(dead_code)]
fn apply_window_vibrancy(_window: &tauri::WebviewWindow) {
// Vibrancy disabled - using solid JetBrains-style background
// To re-enable, uncomment the platform-specific code below:
/*
#[cfg(target_os = "windows")]
{
use window_vibrancy::apply_acrylic;
match apply_acrylic(_window, Some((18, 18, 18, 180))) {
Ok(_) => info!("Acrylic blur effect applied to window: {}", _window.label()),
Err(e) => error!("Failed to apply acrylic effect to {}: {:?}", _window.label(), e),
}
}
#[cfg(target_os = "macos")]
{
use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial};
if let Err(e) = apply_vibrancy(_window, NSVisualEffectMaterial::HudWindow, None, None) {
error!("Failed to apply vibrancy to {}: {}", _window.label(), e);
} else {
info!("macOS vibrancy applied to window: {}", _window.label());
}
}
*/
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowBounds {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
pub is_maximized: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowSession {
pub label: String,
pub path: Option<String>,
pub bounds: Option<WindowBounds>,
}
pub struct WindowManagerState {
pub sessions: Mutex<HashMap<String, (Option<String>, Option<WindowBounds>)>>,
pub is_exiting: Mutex<bool>,
}
impl WindowManagerState {
pub fn new() -> Self {
Self {
sessions: Mutex::new(HashMap::new()),
is_exiting: Mutex::new(false),
}
}
}
fn get_session_file_path(app: &AppHandle) -> Result<PathBuf, String> {
let mut path = app
.path()
.app_config_dir()
.map_err(|e| format!("Failed to get app config dir: {}", e))?;
if !path.exists() {
fs::create_dir_all(&path).ok();
}
path.push("window_sessions.json");
Ok(path)
}
fn save_window_sessions(
app: &AppHandle,
sessions: &HashMap<String, (Option<String>, Option<WindowBounds>)>,
) {
let session_vec: Vec<WindowSession> = sessions
.iter()
.map(|(label, (path, bounds))| WindowSession {
label: label.clone(),
path: path.clone(),
bounds: bounds.clone(),
})
.collect();
let path = match get_session_file_path(app) {
Ok(p) => p,
Err(e) => {
error!("Failed to get session file path: {}", e);
return;
}
};
if let Ok(json) = serde_json::to_string(&session_vec) {
fs::write(path, json).ok();
}
}
#[tauri::command]
pub async fn register_window_project(
app: AppHandle,
label: String,
path: Option<String>,
) -> Result<(), String> {
let state = app.state::<WindowManagerState>();
let mut guard = state
.sessions
.lock()
.map_err(|_| "Failed to acquire session lock".to_string())?;
if let Some(entry) = guard.get_mut(&label) {
entry.0 = path;
} else {
guard.insert(label, (path, None));
}
save_window_sessions(&app, &guard);
Ok(())
}
#[tauri::command]
pub async fn update_window_state(
app: AppHandle,
label: String,
x: f64,
y: f64,
width: f64,
height: f64,
is_maximized: bool,
) -> Result<(), String> {
let state = app.state::<WindowManagerState>();
let mut guard = state
.sessions
.lock()
.map_err(|_| "Failed to acquire session lock".to_string())?;
let bounds = Some(WindowBounds {
x,
y,
width,
height,
is_maximized,
});
if let Some(entry) = guard.get_mut(&label) {
entry.1 = bounds;
} else {
guard.insert(label, (None, bounds));
}
save_window_sessions(&app, &guard);
Ok(())
}
pub fn remove_window_session(app: &AppHandle, label: &str) {
let state = app.state::<WindowManagerState>();
// Don't remove if app is exiting, we want to restore these windows next time
let is_exiting = match state.is_exiting.lock() {
Ok(guard) => *guard,
Err(_) => {
error!("Failed to acquire is_exiting lock");
return;
}
};
if is_exiting {
return;
}
let mut guard = match state.sessions.lock() {
Ok(g) => g,
Err(_) => {
error!("Failed to acquire session lock");
return;
}
};
guard.remove(label);
save_window_sessions(app, &guard);
}
#[tauri::command]
pub async fn unregister_window(app: AppHandle, label: String) -> Result<(), String> {
remove_window_session(&app, &label);
Ok(())
}
pub async fn restore_windows(app: &AppHandle) {
let path = match get_session_file_path(app) {
Ok(p) => p,
Err(e) => {
error!("Failed to get session file path: {}", e);
return;
}
};
if let Ok(json) = tokio::fs::read_to_string(&path).await {
if let Ok(sessions) = serde_json::from_str::<Vec<WindowSession>>(&json) {
let state = app.state::<WindowManagerState>();
let mut guard = match state.sessions.lock() {
Ok(g) => g,
Err(_) => {
error!("Failed to acquire session lock");
return;
}
};
for session in sessions {
// Skip restoring windows that start with "main-" (additional IDE windows)
// These windows were created by the user via "New Window" and shouldn't
// persist across restarts to avoid the double window bug.
// Only restore auxiliary windows (aux-*) or update the main window bounds.
if session.label.starts_with("main-") {
info!(
"Skipping restoration of additional IDE window: {}",
session.label
);
continue;
}
guard.insert(
session.label.clone(),
(session.path.clone(), session.bounds.clone()),
);
// Re-create the window if it's not the main one (which is created by default)
// Note: "main" window is created by tauri.conf.json, we only update its bounds
if session.label != "main" {
let _ =
create_window_internal(app, session.label, session.path, session.bounds);
} else if let Some(bounds) = session.bounds {
// Update main window bounds if it exists
if let Some(window) = app.get_webview_window("main") {
if bounds.is_maximized {
let _ = window.maximize();
} else {
let _ = window.set_size(tauri::Size::Logical(tauri::LogicalSize {
width: bounds.width,
height: bounds.height,
}));
let _ = window.set_position(tauri::Position::Logical(
tauri::LogicalPosition {
x: bounds.x,
y: bounds.y,
},
));
}
}
}
}
}
}
}
fn create_window_internal(
app: &AppHandle,
label: String,
path: Option<String>,
bounds: Option<WindowBounds>,
) -> Result<(), String> {
let mut url = format!("index.html?window={}", label);
if let Some(p) = path {
let normalized = p.replace('\\', "/");
url.push_str(&format!("&project={}", url_encode(&normalized)));
}
let mut builder = WebviewWindowBuilder::new(app, label, WebviewUrl::App(url.into()))
.title("Cortex")
.decorations(false)
.transparent(false) // Disabled - matches tauri.conf.json for consistent Windows behavior
.visible(false); // Hidden until frontend signals UI shell is ready
let is_max = if let Some(b) = bounds {
builder = builder.inner_size(b.width, b.height).position(b.x, b.y);
b.is_maximized
} else {
builder = builder.inner_size(1200.0, 800.0);
false
};
let window = builder
.build()
.map_err(|e| format!("Failed to create window: {}", e))?;
// Apply vibrancy effect for glassmorphism
apply_window_vibrancy(&window);
// Store maximized state for when window becomes visible
if is_max {
let window_clone = window.clone();
tauri::async_runtime::spawn(async move {
// Listen for the show event, then maximize
let _ = window_clone.maximize();
});
}
Ok(())
}
fn url_encode(s: &str) -> String {
// Simple manual encoding for common path characters
s.replace('%', "%25")
.replace(' ', "%20")
.replace('&', "%26")
.replace('#', "%23")
.replace('+', "%2B")
}
#[tauri::command]
pub async fn create_new_window(app: AppHandle, path: Option<String>) -> Result<(), String> {
let id = Uuid::new_v4().to_string();
let label = format!("main-{}", &id[..8]);
info!(
"Creating new IDE window with label: {}, path: {:?}",
label, path
);
let mut url = format!("index.html?window={}", label);
if let Some(p) = path {
// Convert backslashes to forward slashes for URL compatibility
let normalized = p.replace('\\', "/");
url.push_str(&format!("&project={}", url_encode(&normalized)));
}
let window = WebviewWindowBuilder::new(&app, label.clone(), WebviewUrl::App(url.into()))
.title("Cortex")
.inner_size(1200.0, 800.0)
.decorations(false) // Custom title bar is used
.transparent(false) // Disabled - matches tauri.conf.json for consistent Windows behavior
.visible(false) // Hidden until frontend signals UI shell is ready
.build()
.map_err(|e| format!("Failed to create window: {}", e))?;
// Fallback: show window after 3 seconds if frontend hasn't signaled ready
// This prevents invisible windows if frontend fails to load
let window_clone = window.clone();
let label_clone = label.clone();
tauri::async_runtime::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
// Only show if window is still not visible
if let Ok(visible) = window_clone.is_visible() {
if !visible {
info!("Fallback: showing window {} after timeout", label_clone);
let _ = window_clone.show();
let _ = window_clone.set_focus();
}
}
});
Ok(())
}
#[tauri::command]
pub async fn create_auxiliary_window(
app: AppHandle,
window_type: String,
content_id: String,
title: Option<String>,
width: Option<f64>,
height: Option<f64>,
) -> Result<(), String> {
let id = Uuid::new_v4().to_string();
let label = format!("aux-{}", &id[..8]);
let title = title.unwrap_or_else(|| format!("{}: {}", window_type, content_id));
let width = width.unwrap_or(800.0);
let height = height.unwrap_or(600.0);
// Convert backslashes to forward slashes for content_id if it's a path
let normalized_content = content_id.replace('\\', "/");
let url = format!(
"index.html?window={}&type={}&content={}",
label,
window_type,
url_encode(&normalized_content)
);
info!(
"Creating auxiliary window: {} with type: {}",
label, window_type
);
let window = WebviewWindowBuilder::new(&app, label, WebviewUrl::App(url.into()))
.title(title)
.inner_size(width, height)
.decorations(true) // Auxiliary windows use native decorations
.transparent(false) // Consistent with main windows
.visible(false) // Hidden until frontend signals UI shell is ready
.build()
.map_err(|e| format!("Failed to create auxiliary window: {}", e))?;
// Show the auxiliary window (has decorations, no need to wait for custom titlebar)
let _ = window.show();
Ok(())
}
/// Signal that the UI shell is ready - show the window
/// Called by frontend when titlebar and background are rendered
#[tauri::command]
pub async fn show_window(window: tauri::WebviewWindow) -> Result<(), String> {
if let Err(e) = window.show() {
error!("Failed to show window: {}", e);
return Err(format!("Failed to show window: {}", e));
}
if let Err(e) = window.set_focus() {
error!("Failed to focus window: {}", e);
}
Ok(())
}
/// Toggle developer tools for debugging
#[tauri::command]
pub async fn toggle_devtools(window: tauri::WebviewWindow) -> Result<(), String> {
#[cfg(debug_assertions)]
{
if window.is_devtools_open() {
window.close_devtools();
} else {
window.open_devtools();
}
}
#[cfg(not(debug_assertions))]
{
// In release mode, devtools are typically disabled
let _ = window;
}
Ok(())
}