Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 42 additions & 24 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ 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::notifications;
use crate::settings::{self, Settings};
use crate::shortcuts;
use crate::themes::{self, Theme};
Expand Down Expand Up @@ -94,7 +94,8 @@ pub fn settings_set(
// 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::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}");
Expand Down Expand Up @@ -135,16 +136,17 @@ pub fn settings_set(
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 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" {
if matches!(key.as_str(), "dial_countdown" | "dial_direction") {
*tray_state.countdown_mode.lock().unwrap() = new_settings.dial_countdown;
*tray_state.reverse_direction.lock().unwrap() = new_settings.dial_direction;
let snap = timer.get_snapshot();
let progress = if snap.total_secs > 0 {
snap.elapsed_secs as f32 / snap.total_secs as f32
Expand Down Expand Up @@ -195,7 +197,14 @@ pub fn settings_set(
}

// Re-register global shortcuts when any shortcut key changes or the enabled flag toggles.
if matches!(key.as_str(), "shortcut_toggle" | "shortcut_reset" | "shortcut_skip" | "shortcut_restart" | "global_shortcuts_enabled") {
if matches!(
key.as_str(),
"shortcut_toggle"
| "shortcut_reset"
| "shortcut_skip"
| "shortcut_restart"
| "global_shortcuts_enabled"
) {
shortcuts::register_all(&app, &new_settings);
}

Expand Down Expand Up @@ -252,6 +261,7 @@ pub fn settings_reset_defaults(

timer.apply_settings(new_settings.clone());
*tray_state.countdown_mode.lock().unwrap() = new_settings.dial_countdown;
*tray_state.reverse_direction.lock().unwrap() = new_settings.dial_direction;

// Broadcast a reset snapshot so the frontend dial and display reflect the
// restored default durations without requiring the user to manually reset.
Expand Down Expand Up @@ -306,10 +316,7 @@ pub fn settings_reset_defaults(
/// 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())?;
let data_dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
Ok(themes::list_all(&data_dir))
}

Expand Down Expand Up @@ -351,7 +358,11 @@ pub fn stats_get_detailed(db: State<'_, DbState>) -> Result<DetailedStats, Strin
log::error!("[stats] failed to query streak: {e}");
e.to_string()
})?;
Ok(DetailedStats { today, week, streak })
Ok(DetailedStats {
today,
week,
streak,
})
}

/// Heatmap data + lifetime totals for the All Time tab.
Expand Down Expand Up @@ -433,10 +444,7 @@ pub fn audio_set_custom(
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");
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) {
Expand Down Expand Up @@ -510,8 +518,11 @@ pub fn audio_clear_custom(
// Remove the persisted display name.
let name_key = cue_to_name_key(&cue)?;
let conn = db.lock().map_err(|e| e.to_string())?;
conn.execute("DELETE FROM settings WHERE key = ?1", rusqlite::params![name_key])
.map_err(|e| e.to_string())?;
conn.execute(
"DELETE FROM settings WHERE key = ?1",
rusqlite::params![name_key],
)
.map_err(|e| e.to_string())?;

log::info!("[audio] custom sound cleared cue={cue}");
Ok(())
Expand All @@ -538,7 +549,8 @@ pub fn audio_get_custom_info(
settings::get_setting(&conn, key).or_else(|| stored.clone())
};
info.work_alert = override_name(&info.work_alert, "custom_work_alert_name");
info.short_break_alert = override_name(&info.short_break_alert, "custom_short_break_alert_name");
info.short_break_alert =
override_name(&info.short_break_alert, "custom_short_break_alert_name");
info.long_break_alert = override_name(&info.long_break_alert, "custom_long_break_alert_name");

Ok(info)
Expand Down Expand Up @@ -749,8 +761,8 @@ pub struct HeatmapStats {

#[cfg(test)]
mod tests {
use rusqlite::Connection;
use crate::db::migrations;
use rusqlite::Connection;

fn setup() -> Connection {
let conn = Connection::open_in_memory().unwrap();
Expand All @@ -759,24 +771,31 @@ mod tests {
}

fn seed_sessions(conn: &Connection) {
conn.execute_batch("
conn.execute_batch(
"
INSERT INTO sessions (started_at, ended_at, round_type, duration_secs, completed)
VALUES (1000, 1060, 'work', 60, 1),
(2000, 2300, 'short-break', 300, 1);
").unwrap();
",
)
.unwrap();
}

#[test]
fn sessions_clear_removes_all_rows() {
let conn = setup();
seed_sessions(&conn);
let before: i64 = conn.query_row("SELECT COUNT(*) FROM sessions", [], |r| r.get(0)).unwrap();
let before: i64 = conn
.query_row("SELECT COUNT(*) FROM sessions", [], |r| r.get(0))
.unwrap();
assert_eq!(before, 2);

let n = conn.execute("DELETE FROM sessions", []).unwrap();
assert_eq!(n, 2);

let after: i64 = conn.query_row("SELECT COUNT(*) FROM sessions", [], |r| r.get(0)).unwrap();
let after: i64 = conn
.query_row("SELECT COUNT(*) FROM sessions", [], |r| r.get(0))
.unwrap();
assert_eq!(after, 0);
}

Expand All @@ -787,4 +806,3 @@ mod tests {
assert_eq!(n, 0);
}
}

5 changes: 2 additions & 3 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fn run() {

// Sync tray state from saved settings.
*tray_state.countdown_mode.lock().unwrap() = initial_settings.dial_countdown;
*tray_state.reverse_direction.lock().unwrap() = initial_settings.dial_direction;
let tray_theme_name = match initial_settings.theme_mode.as_str() {
"dark" => &initial_settings.theme_dark,
_ => &initial_settings.theme_light,
Expand Down Expand Up @@ -234,9 +235,7 @@ pub fn run() {
// register it with NSApplication. This causes macOS to
// auto-populate it with all standard window-management items.
unsafe {
let ns_app_class = AnyClass::get(
std::ffi::CStr::from_bytes_with_nul_unchecked(b"NSApplication\0"),
);
let ns_app_class = AnyClass::get(c"NSApplication");
if let Some(cls) = ns_app_class {
let ns_app: *mut AnyObject =
msg_send![cls, sharedApplication];
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/settings/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const DEFAULTS: &[(&str, &str)] = &[
("notifications", "false"),
("work_rounds", "4"),
("dial_countdown", "true"),
("dial_direction", "false"),
("theme_mode", "auto"),
("theme_light", "Pomotroid Light"),
("theme_dark", "Pomotroid"),
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Settings {
pub long_breaks_enabled: bool,
/// When true the dial arc starts full and subtracts; when false it fills from empty.
pub dial_countdown: bool,
pub dial_direction: bool,
pub theme_mode: String,
pub theme_light: String,
pub theme_dark: String,
Expand Down Expand Up @@ -82,6 +83,7 @@ impl Default for Settings {
short_breaks_enabled: true,
long_breaks_enabled: true,
dial_countdown: true,
dial_direction: false,
theme_mode: "auto".to_string(),
theme_light: "Pomotroid Light".to_string(),
theme_dark: "Pomotroid".to_string(),
Expand Down Expand Up @@ -191,6 +193,7 @@ pub fn load(conn: &Connection) -> Result<Settings> {
short_breaks_enabled: parse_bool(&map, "short_breaks_enabled", d.short_breaks_enabled),
long_breaks_enabled: parse_bool(&map, "long_breaks_enabled", d.long_breaks_enabled),
dial_countdown: parse_bool(&map, "dial_countdown", d.dial_countdown),
dial_direction: parse_bool(&map, "dial_direction", d.dial_direction),
theme_mode: map
.get("theme_mode")
.cloned()
Expand Down
Loading