-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
142 lines (132 loc) · 6.19 KB
/
lib.rs
File metadata and controls
142 lines (132 loc) · 6.19 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
mod commands;
mod keychain;
mod tray;
use tauri::Manager;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.plugin(tauri_plugin_positioner::init())
.plugin(tauri_plugin_notification::init())
.plugin(tauri_plugin_store::Builder::default().build())
.plugin(tauri_plugin_process::init())
.plugin(tauri_plugin_updater::Builder::default().build())
.manage(commands::GatewayProcess(std::sync::Mutex::new(None)))
.setup(|app| {
// Hide from macOS Dock — this is a menu-bar-only app
#[cfg(target_os = "macos")]
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
// Create system tray icon
tray::create_tray(app.handle())?;
// Hide panel when it loses focus (click outside)
// In dev mode, use a delay to avoid HMR/DevTools stealing focus
if let Some(window) = app.get_webview_window("panel") {
let w = window.clone();
window.on_window_event(move |event| {
if let tauri::WindowEvent::Focused(false) = event {
let win = w.clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(200));
if !win.is_focused().unwrap_or(true) {
let _ = win.hide();
}
});
}
});
}
// Hide spotlight when it loses focus
if let Some(window) = app.get_webview_window("spotlight") {
let w = window.clone();
window.on_window_event(move |event| {
if let tauri::WindowEvent::Focused(false) = event {
let win = w.clone();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(200));
if !win.is_focused().unwrap_or(true) {
let _ = win.hide();
}
});
}
});
}
// Register global hotkeys
#[cfg(desktop)]
{
use tauri_plugin_global_shortcut::{
Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState,
};
let shortcut_panel =
Shortcut::new(Some(Modifiers::META | Modifiers::SHIFT), Code::KeyR);
let shortcut_spotlight =
Shortcut::new(Some(Modifiers::META | Modifiers::SHIFT), Code::KeyX);
let shortcut_translate =
Shortcut::new(Some(Modifiers::META | Modifiers::SHIFT), Code::KeyT);
let shortcut_summarize =
Shortcut::new(Some(Modifiers::META | Modifiers::SHIFT), Code::KeyS);
let shortcut_explain =
Shortcut::new(Some(Modifiers::META | Modifiers::SHIFT), Code::KeyE);
let handle = app.handle().clone();
let sc_panel = shortcut_panel.clone();
let sc_spotlight = shortcut_spotlight.clone();
let sc_translate = shortcut_translate.clone();
let sc_summarize = shortcut_summarize.clone();
app.handle().plugin(
tauri_plugin_global_shortcut::Builder::new()
.with_handler(move |_app, shortcut, event| {
if event.state() == ShortcutState::Pressed {
if *shortcut == sc_panel {
let _ = commands::toggle_panel_internal(&handle);
} else if *shortcut == sc_spotlight {
let _ = commands::toggle_spotlight_internal(&handle);
} else if *shortcut == sc_translate {
let _ = commands::clipboard_action_sync(&handle, "translate");
} else if *shortcut == sc_summarize {
let _ = commands::clipboard_action_sync(&handle, "summarize");
} else {
let _ = commands::clipboard_action_sync(&handle, "explain");
}
}
})
.build(),
)?;
app.global_shortcut().register(shortcut_panel)?;
app.global_shortcut().register(shortcut_spotlight)?;
app.global_shortcut().register(shortcut_translate)?;
app.global_shortcut().register(shortcut_summarize)?;
app.global_shortcut().register(shortcut_explain)?;
}
Ok(())
})
.invoke_handler(tauri::generate_handler![
commands::store_token,
commands::get_token,
commands::delete_token,
commands::copy_to_clipboard,
commands::show_notification,
commands::toggle_panel,
commands::toggle_spotlight,
commands::open_chat,
commands::read_clipboard,
commands::clipboard_action,
commands::spawn_gateway,
commands::stop_gateway,
commands::is_gateway_running,
])
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(|app_handle, event| {
if let tauri::RunEvent::ExitRequested { .. } = event {
// Kill gateway process before exiting to prevent orphaned bun processes
let state = app_handle.state::<commands::GatewayProcess>();
let mut guard = match state.0.lock() {
Ok(g) => g,
Err(_) => return,
};
if let Some(ref mut child) = *guard {
let _ = child.kill();
let _ = child.wait();
}
*guard = None;
}
});
}