-
-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathcommands.rs
More file actions
572 lines (507 loc) · 21.1 KB
/
commands.rs
File metadata and controls
572 lines (507 loc) · 21.1 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/// All #[tauri::command] functions exposed to the Svelte frontend via Tauri IPC.
///
/// Commands are grouped by domain: Timer, Settings, Themes, Stats.
/// Each command returns `Result<T, String>` so errors surface cleanly in JS.
use log::LevelFilter;
use tauri::{AppHandle, Emitter, Manager, State};
use std::sync::Arc;
use crate::audio::{self, AudioManager};
use crate::notifications;
use crate::db::{queries, DbState};
use crate::settings::{self, Settings};
use crate::shortcuts;
use crate::themes::{self, Theme};
use crate::timer::{TimerController, TimerSnapshot};
use crate::tray::{self, TrayState};
use crate::websocket::{self, WsState};
// ---------------------------------------------------------------------------
// CMD-01 — Timer commands
// ---------------------------------------------------------------------------
/// Toggle the timer: start if idle, resume if paused, pause if running.
/// This is the primary action bound to the space bar and the play/pause button.
#[tauri::command]
pub fn timer_toggle(timer: State<'_, TimerController>) {
timer.toggle();
}
/// Reset the current round's timer without advancing the sequence.
#[tauri::command]
pub fn timer_reset(timer: State<'_, TimerController>) {
timer.reset();
}
/// Skip the current round: fires Complete immediately and advances to the next.
#[tauri::command]
pub fn timer_skip(timer: State<'_, TimerController>) {
timer.skip();
}
/// Restart the current round from zero without advancing the sequence.
/// Round type and round number are preserved; only elapsed time is reset.
#[tauri::command]
pub fn timer_restart_round(timer: State<'_, TimerController>) {
timer.restart_round();
}
/// Return a full snapshot of the current timer state.
/// Called once on frontend mount to hydrate stores.
#[tauri::command]
pub fn timer_get_state(timer: State<'_, TimerController>) -> TimerSnapshot {
timer.get_snapshot()
}
// ---------------------------------------------------------------------------
// CMD-02 — Settings commands
// ---------------------------------------------------------------------------
/// Return all current settings.
#[tauri::command]
pub fn settings_get(db: State<'_, DbState>) -> Result<Settings, String> {
let conn = db.lock().map_err(|e| e.to_string())?;
settings::load(&conn).map_err(|e| {
log::error!("[settings] failed to load settings: {e}");
e.to_string()
})
}
/// Persist a single setting and emit `settings:changed` with the updated set.
///
/// `key` must be one of the DB column names (see `settings::defaults::DEFAULTS`).
/// `value` is always a string; the loader converts it to the appropriate type.
#[tauri::command]
pub fn settings_set(
key: String,
value: String,
db: State<'_, DbState>,
timer: State<'_, TimerController>,
tray_state: State<'_, Arc<TrayState>>,
ws_state: State<'_, Arc<WsState>>,
app: AppHandle,
) -> Result<Settings, String> {
log::debug!("[settings] set {key}={value}");
let new_settings = {
let conn = db.lock().map_err(|e| e.to_string())?;
settings::save_setting(&conn, &key, &value).map_err(|e| {
log::error!("[settings] failed to save '{key}': {e}");
e.to_string()
})?;
// When SIT is turned off, cascade-reset the dependent tray settings so
// the close-to-tray handler cannot hide the window with no icon to
// restore from.
if key == "tray_icon_enabled" && value == "false" {
settings::save_setting(&conn, "min_to_tray", "false").map_err(|e| e.to_string())?;
settings::save_setting(&conn, "min_to_tray_on_close", "false").map_err(|e| e.to_string())?;
}
settings::load(&conn).map_err(|e| {
log::error!("[settings] failed to reload after save: {e}");
e.to_string()
})?
};
// Apply verbose_logging change immediately without a restart.
if key == "verbose_logging" {
if new_settings.verbose_logging {
log::set_max_level(LevelFilter::Debug);
log::info!("Verbose logging enabled — log level set to DEBUG");
} else {
log::set_max_level(LevelFilter::Info);
log::info!("Verbose logging disabled — log level set to INFO");
}
}
// Keep the timer engine in sync when time-related settings change.
timer.apply_settings(new_settings.clone());
// Broadcast an updated snapshot so the frontend immediately reflects any
// changed settings (round count, durations, etc.) regardless of timer
// state. The timer:reset handler only calls timerState.set(), so emitting
// while running does not interrupt the countdown; the next timer:tick
// event will reconcile total_secs from the engine within one second.
app.emit("timer:reset", &timer.get_snapshot()).ok();
// Propagate volume and tick-sound changes to the audio engine (optional state).
if let Some(audio) = app.try_state::<Arc<AudioManager>>() {
audio.apply_settings(&new_settings);
}
// Apply always-on-top window flag immediately when the setting changes,
// accounting for the current round type so break_always_on_top takes
// effect without waiting for the next round transition.
if matches!(key.as_str(), "always_on_top" | "break_always_on_top") {
if let Some(window) = app.get_webview_window("main") {
let snap = timer.get_snapshot();
let is_break = snap.round_type != "work";
let effective_aot = new_settings.always_on_top
&& !(new_settings.break_always_on_top && is_break);
let _ = window.set_always_on_top(effective_aot);
}
}
// Sync tray countdown mode when the dial setting changes, then immediately
// re-render the icon so it matches the dial without waiting for a timer event.
if key == "dial_countdown" {
*tray_state.countdown_mode.lock().unwrap() = new_settings.dial_countdown;
let snap = timer.get_snapshot();
let progress = if snap.total_secs > 0 {
snap.elapsed_secs as f32 / snap.total_secs as f32
} else {
0.0
};
tray::update_icon(&tray_state, &snap.round_type, snap.is_paused, progress);
}
// Update tray icon colors when the active theme changes.
if matches!(key.as_str(), "theme_mode" | "theme_light" | "theme_dark") {
let data_dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
let tray_theme_name = match new_settings.theme_mode.as_str() {
"dark" => &new_settings.theme_dark,
_ => &new_settings.theme_light,
};
if let Some(theme) = themes::find(&data_dir, tray_theme_name) {
*tray_state.colors.lock().unwrap() = tray::TrayColors::from_colors_map(&theme.colors);
let snap = timer.get_snapshot();
let progress = if snap.total_secs > 0 {
snap.elapsed_secs as f32 / snap.total_secs as f32
} else {
0.0
};
tray::update_icon(&tray_state, &snap.round_type, snap.is_paused, progress);
}
}
// Create or destroy the tray when tray_icon_enabled or min_to_tray changes.
// The tray exists when either flag is true.
if matches!(key.as_str(), "tray_icon_enabled" | "min_to_tray") {
if new_settings.tray_icon_enabled || new_settings.min_to_tray {
tray::create_tray(&app, &tray_state);
} else {
tray::destroy_tray(&tray_state);
}
}
// Re-register global shortcuts when any shortcut key changes.
if matches!(key.as_str(), "shortcut_toggle" | "shortcut_reset" | "shortcut_skip" | "shortcut_restart") {
shortcuts::register_all(&app, &new_settings);
}
// Start or stop the WebSocket server when the enabled flag or port changes.
if matches!(key.as_str(), "websocket_enabled" | "websocket_port") {
let ws = Arc::clone(&*ws_state);
let port = new_settings.websocket_port;
let enabled = new_settings.websocket_enabled;
let app_clone = app.clone();
tauri::async_runtime::spawn(async move {
// Always stop the old server first.
websocket::stop(&ws).await;
if enabled {
websocket::start(port, app_clone, &ws).await;
}
});
}
app.emit("settings:changed", &new_settings).ok();
Ok(new_settings)
}
// ---------------------------------------------------------------------------
// CMD-06 — Shortcuts command
// ---------------------------------------------------------------------------
/// Re-register all global shortcuts from the current settings.
/// The frontend can call this after bulk-updating shortcut settings.
#[tauri::command]
pub fn shortcuts_reload(db: State<'_, DbState>, app: AppHandle) -> Result<(), String> {
let conn = db.lock().map_err(|e| e.to_string())?;
let s = settings::load(&conn).map_err(|e| e.to_string())?;
shortcuts::register_all(&app, &s);
Ok(())
}
/// Reset all settings to factory defaults and return the resulting settings.
#[tauri::command]
pub fn settings_reset_defaults(
db: State<'_, DbState>,
timer: State<'_, TimerController>,
tray_state: State<'_, Arc<TrayState>>,
app: AppHandle,
) -> Result<Settings, String> {
log::info!("[settings] reset to defaults");
let new_settings = {
let conn = db.lock().map_err(|e| e.to_string())?;
// Delete all rows so seed_defaults can insert fresh defaults.
conn.execute("DELETE FROM settings", [])
.map_err(|e| e.to_string())?;
settings::seed_defaults(&conn).map_err(|e| e.to_string())?;
settings::load(&conn).map_err(|e| e.to_string())?
};
timer.apply_settings(new_settings.clone());
*tray_state.countdown_mode.lock().unwrap() = new_settings.dial_countdown;
// Broadcast a reset snapshot so the frontend dial and display reflect the
// restored default durations without requiring the user to manually reset.
{
let snap = timer.get_snapshot();
if !snap.is_running && !snap.is_paused {
app.emit("timer:reset", &snap).ok();
}
}
// After reset, defaults have tray_icon_enabled=false and min_to_tray=false,
// so destroy any active tray icon.
tray::destroy_tray(&tray_state);
let data_dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
// Clear custom alert sounds: delete files from disk and reset in-memory paths.
if let Some(audio_state) = app.try_state::<Arc<AudioManager>>() {
let audio_dir = data_dir.join("audio");
for stem in [audio::STEM_WORK, audio::STEM_SHORT, audio::STEM_LONG] {
if let Ok(entries) = std::fs::read_dir(&audio_dir) {
for entry in entries.filter_map(|e| e.ok()) {
let p = entry.path();
if p.file_stem().and_then(|s| s.to_str()) == Some(stem) {
let _ = std::fs::remove_file(&p);
}
}
}
}
audio_state.clear_custom_path("work_alert");
audio_state.clear_custom_path("short_break_alert");
audio_state.clear_custom_path("long_break_alert");
log::info!("[audio] custom sounds cleared on settings reset");
}
let tray_theme_name = match new_settings.theme_mode.as_str() {
"dark" => &new_settings.theme_dark,
_ => &new_settings.theme_light,
};
if let Some(theme) = themes::find(&data_dir, tray_theme_name) {
*tray_state.colors.lock().unwrap() = tray::TrayColors::from_colors_map(&theme.colors);
}
app.emit("settings:changed", &new_settings).ok();
Ok(new_settings)
}
// ---------------------------------------------------------------------------
// CMD-03 — Theme commands
// ---------------------------------------------------------------------------
/// List all available themes (17 bundled + any user-created ones).
#[tauri::command]
pub fn themes_list(app: AppHandle) -> Result<Vec<Theme>, String> {
let data_dir = app
.path()
.app_data_dir()
.map_err(|e| e.to_string())?;
Ok(themes::list_all(&data_dir))
}
// ---------------------------------------------------------------------------
// CMD-04 — Stats commands
// ---------------------------------------------------------------------------
/// Batched stats for Today + This Week tabs (minimises IPC round-trips).
#[tauri::command]
pub fn stats_get_detailed(db: State<'_, DbState>) -> Result<DetailedStats, String> {
let conn = db.lock().map_err(|e| e.to_string())?;
let today = queries::get_daily_stats(&conn).map_err(|e| {
log::error!("[stats] failed to query daily stats: {e}");
e.to_string()
})?;
let week = queries::get_weekly_stats(&conn).map_err(|e| {
log::error!("[stats] failed to query weekly stats: {e}");
e.to_string()
})?;
let streak = queries::get_streak(&conn).map_err(|e| {
log::error!("[stats] failed to query streak: {e}");
e.to_string()
})?;
Ok(DetailedStats { today, week, streak })
}
/// Heatmap data + lifetime totals for the All Time tab.
#[tauri::command]
pub fn stats_get_heatmap(db: State<'_, DbState>) -> Result<HeatmapStats, String> {
let conn = db.lock().map_err(|e| e.to_string())?;
let entries = queries::get_heatmap_data(&conn).map_err(|e| {
log::error!("[stats] failed to query heatmap data: {e}");
e.to_string()
})?;
let raw = queries::get_all_time_stats(&conn).map_err(|e| {
log::error!("[stats] failed to query all-time stats: {e}");
e.to_string()
})?;
let streak = queries::get_streak(&conn).map_err(|e| {
log::error!("[stats] failed to query streak for heatmap: {e}");
e.to_string()
})?;
Ok(HeatmapStats {
entries,
total_rounds: raw.completed_work_sessions as u32,
total_hours: (raw.total_work_secs / 3600) as u32,
longest_streak: streak.longest,
})
}
// ---------------------------------------------------------------------------
// CMD-05 — Window commands
// ---------------------------------------------------------------------------
/// Show or hide the main window.
#[tauri::command]
pub fn window_set_visibility(visible: bool, app: AppHandle) -> Result<(), String> {
log::debug!("[window] set visibility={visible}");
let window = app
.get_webview_window("main")
.ok_or_else(|| "main window not found".to_string())?;
if visible {
window.show().map_err(|e| e.to_string())?;
window.set_focus().map_err(|e| e.to_string())?;
} else {
window.hide().map_err(|e| e.to_string())?;
}
Ok(())
}
// ---------------------------------------------------------------------------
// CMD-07 — Audio commands
// ---------------------------------------------------------------------------
/// Copy a user-selected audio file into the app config dir for the given cue slot.
///
/// `cue` must be one of: `"work_alert"`, `"short_break_alert"`, `"long_break_alert"`.
/// `src_path` is the full path to the file chosen by the user.
///
/// The file is stored with a fixed stem (e.g. `custom_work_alert.mp3`) so that
/// selecting a new file for the same slot automatically replaces the old one —
/// no orphan files accumulate.
///
/// Returns the original filename for display in the UI.
#[tauri::command]
pub fn audio_set_custom(
cue: String,
src_path: String,
app: AppHandle,
) -> Result<String, String> {
let audio_state = app
.try_state::<Arc<AudioManager>>()
.ok_or_else(|| "audio engine is not available".to_string())?;
let stem = cue_to_stem(&cue)?;
let audio_dir = app
.path()
.app_data_dir()
.map_err(|e| e.to_string())?
.join("audio");
std::fs::create_dir_all(&audio_dir).map_err(|e| e.to_string())?;
let src = std::path::Path::new(&src_path);
let ext = src
.extension()
.and_then(|e| e.to_str())
.unwrap_or("mp3");
// Remove any existing custom file for this slot (preserves zero orphans).
if let Ok(entries) = std::fs::read_dir(&audio_dir) {
for entry in entries.filter_map(|e| e.ok()) {
let p = entry.path();
if p.file_stem().and_then(|s| s.to_str()) == Some(stem) {
let _ = std::fs::remove_file(&p);
}
}
}
let dest = audio_dir.join(format!("{stem}.{ext}"));
std::fs::copy(src, &dest).map_err(|e| e.to_string())?;
audio_state.set_custom_path(&cue, dest);
let display_name = src
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("custom")
.to_string();
log::info!("[audio] custom sound set cue={cue} file={display_name}");
Ok(display_name)
}
/// Restore the built-in sound for the given cue slot by deleting the custom file.
#[tauri::command]
pub fn audio_clear_custom(cue: String, app: AppHandle) -> Result<(), String> {
let audio_state = app
.try_state::<Arc<AudioManager>>()
.ok_or_else(|| "audio engine is not available".to_string())?;
let stem = cue_to_stem(&cue)?;
let audio_dir = app
.path()
.app_data_dir()
.map_err(|e| e.to_string())?
.join("audio");
if let Ok(entries) = std::fs::read_dir(&audio_dir) {
for entry in entries.filter_map(|e| e.ok()) {
let p = entry.path();
if p.file_stem().and_then(|s| s.to_str()) == Some(stem) {
std::fs::remove_file(&p).map_err(|e| e.to_string())?;
}
}
}
audio_state.clear_custom_path(&cue);
log::info!("[audio] custom sound cleared cue={cue}");
Ok(())
}
/// Return the display names of any currently configured custom audio files.
/// Fields are `null` when the built-in sound is in use for that slot.
#[tauri::command]
pub fn audio_get_custom_info(app: AppHandle) -> Result<audio::CustomAudioInfo, String> {
let audio_state = app
.try_state::<Arc<AudioManager>>()
.ok_or_else(|| "audio engine is not available".to_string())?;
Ok(audio_state.get_custom_info())
}
// ---------------------------------------------------------------------------
// CMD-08 — Notification command
// ---------------------------------------------------------------------------
/// Show a desktop notification with the given title and body.
///
/// String construction (including translation) is the caller's (frontend's)
/// responsibility. This command is a thin platform-dispatch wrapper.
#[tauri::command]
pub fn notification_show(title: String, body: String, app: AppHandle) {
notifications::show(&app, &title, &body);
}
// ---------------------------------------------------------------------------
// CMD-09 — Diagnostic log commands
// ---------------------------------------------------------------------------
/// Open the application log directory in the OS file manager.
#[tauri::command]
pub fn open_log_dir(app: AppHandle) {
match app.path().app_log_dir() {
Ok(log_dir) => {
if let Err(e) = tauri_plugin_opener::open_path(&log_dir, None::<&str>) {
log::warn!("[log] failed to open log dir {}: {e}", log_dir.display());
}
}
Err(e) => log::warn!("[log] failed to resolve log dir: {e}"),
}
}
/// Return the compile-time build version string.
#[tauri::command]
pub fn app_version() -> &'static str {
env!("APP_BUILD_VERSION")
}
// ---------------------------------------------------------------------------
// CMD-10 — Platform commands
// ---------------------------------------------------------------------------
/// Returns whether the app has macOS Accessibility permission.
/// On macOS, calls AXIsProcessTrusted() from the ApplicationServices framework.
/// On all other platforms, always returns true.
#[tauri::command]
pub fn accessibility_trusted() -> bool {
#[cfg(target_os = "macos")]
{
#[link(name = "ApplicationServices", kind = "framework")]
extern "C" {
fn AXIsProcessTrusted() -> bool;
}
unsafe { AXIsProcessTrusted() }
}
#[cfg(not(target_os = "macos"))]
{
true
}
}
/// Return the application log directory path as a string.
#[tauri::command]
pub fn get_log_dir(app: AppHandle) -> Result<String, String> {
app.path()
.app_log_dir()
.map(|p| p.to_string_lossy().into_owned())
.map_err(|e| {
log::warn!("[log] failed to resolve log dir: {e}");
e.to_string()
})
}
fn cue_to_stem(cue: &str) -> Result<&'static str, String> {
match cue {
"work_alert" => Ok(audio::STEM_WORK),
"short_break_alert" => Ok(audio::STEM_SHORT),
"long_break_alert" => Ok(audio::STEM_LONG),
_ => Err(format!("unknown audio cue: '{cue}'")),
}
}
// ---------------------------------------------------------------------------
// Stats payload types
// ---------------------------------------------------------------------------
/// Batched payload for Today + This Week tabs.
#[derive(serde::Serialize)]
pub struct DetailedStats {
pub today: queries::DailyStats,
pub week: Vec<queries::DayStat>,
pub streak: queries::StreakInfo,
}
/// Payload for the All Time tab.
#[derive(serde::Serialize)]
pub struct HeatmapStats {
pub entries: Vec<queries::HeatmapEntry>,
pub total_rounds: u32,
pub total_hours: u32,
pub longest_streak: u32,
}