diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 56993361..9a026496 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -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}; @@ -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}"); @@ -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 @@ -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); } @@ -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. @@ -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, 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)) } @@ -351,7 +358,11 @@ pub fn stats_get_detailed(db: State<'_, DbState>) -> Result Connection { let conn = Connection::open_in_memory().unwrap(); @@ -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); } @@ -787,4 +806,3 @@ mod tests { assert_eq!(n, 0); } } - diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 3f4596d7..1ee07add 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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, @@ -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]; diff --git a/src-tauri/src/settings/defaults.rs b/src-tauri/src/settings/defaults.rs index 2edea8ff..4937e557 100644 --- a/src-tauri/src/settings/defaults.rs +++ b/src-tauri/src/settings/defaults.rs @@ -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"), diff --git a/src-tauri/src/settings/mod.rs b/src-tauri/src/settings/mod.rs index 268c76c1..3fb9940b 100644 --- a/src-tauri/src/settings/mod.rs +++ b/src-tauri/src/settings/mod.rs @@ -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, @@ -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(), @@ -191,6 +193,7 @@ pub fn load(conn: &Connection) -> Result { 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() diff --git a/src-tauri/src/tray/mod.rs b/src-tauri/src/tray/mod.rs index c6f05ee9..5d961589 100644 --- a/src-tauri/src/tray/mod.rs +++ b/src-tauri/src/tray/mod.rs @@ -62,7 +62,8 @@ impl TrayColors { pub fn from_colors_map(colors: &std::collections::HashMap) -> Self { let d = Self::default(); let get = |key: &str, fallback: [u8; 4]| { - colors.get(key) + colors + .get(key) .and_then(|hex| parse_hex_color(hex)) .unwrap_or(fallback) }; @@ -70,8 +71,8 @@ impl TrayColors { background: get("--color-background", d.background), focus_round: get("--color-focus-round", d.focus_round), short_round: get("--color-short-round", d.short_round), - long_round: get("--color-long-round", d.long_round), - foreground: get("--color-foreground", d.foreground), + long_round: get("--color-long-round", d.long_round), + foreground: get("--color-foreground", d.foreground), } } } @@ -114,6 +115,7 @@ pub struct TrayState { pub icon: Mutex>>, pub colors: Mutex, pub countdown_mode: Mutex, + pub reverse_direction: Mutex, pub menu_items: Mutex>, } @@ -123,6 +125,7 @@ impl TrayState { icon: Mutex::new(None), colors: Mutex::new(TrayColors::default()), countdown_mode: Mutex::new(false), + reverse_direction: Mutex::new(false), menu_items: Mutex::new(None), }) } @@ -232,31 +235,63 @@ pub fn create_tray(app: &AppHandle, state: &Arc) { let toggle_item = match MenuItem::with_id(app, "toggle", "Start", true, None::<&str>) { Ok(i) => i, - Err(e) => { log::warn!("[tray] menu item error: {e}"); return; } + Err(e) => { + log::warn!("[tray] menu item error: {e}"); + return; + } }; let skip_item = match MenuItem::with_id(app, "skip", "Skip", false, None::<&str>) { Ok(i) => i, - Err(e) => { log::warn!("[tray] menu item error: {e}"); return; } + Err(e) => { + log::warn!("[tray] menu item error: {e}"); + return; + } }; - let reset_item = match MenuItem::with_id(app, "reset-round", "Reset Round", false, None::<&str>) { + let reset_item = match MenuItem::with_id(app, "reset-round", "Reset Round", false, None::<&str>) + { Ok(i) => i, - Err(e) => { log::warn!("[tray] menu item error: {e}"); return; } + Err(e) => { + log::warn!("[tray] menu item error: {e}"); + return; + } }; let sep = match PredefinedMenuItem::separator(app) { Ok(i) => i, - Err(e) => { log::warn!("[tray] menu item error: {e}"); return; } + Err(e) => { + log::warn!("[tray] menu item error: {e}"); + return; + } }; let show_item = match MenuItem::with_id(app, "show", "Show", true, None::<&str>) { Ok(i) => i, - Err(e) => { log::warn!("[tray] menu item error: {e}"); return; } + Err(e) => { + log::warn!("[tray] menu item error: {e}"); + return; + } }; let exit_item = match MenuItem::with_id(app, "exit", "Exit", true, None::<&str>) { Ok(i) => i, - Err(e) => { log::warn!("[tray] menu item error: {e}"); return; } + Err(e) => { + log::warn!("[tray] menu item error: {e}"); + return; + } }; - let menu = match Menu::with_items(app, &[&toggle_item, &skip_item, &reset_item, &sep, &show_item, &exit_item]) { + let menu = match Menu::with_items( + app, + &[ + &toggle_item, + &skip_item, + &reset_item, + &sep, + &show_item, + &exit_item, + ], + ) { Ok(m) => m, - Err(e) => { log::warn!("[tray] menu error: {e}"); return; } + Err(e) => { + log::warn!("[tray] menu error: {e}"); + return; + } }; // Render the initial idle icon using the current state (respects countdown mode @@ -264,7 +299,8 @@ pub fn create_tray(app: &AppHandle, state: &Arc) { let image = { let colors = state.colors.lock().unwrap().clone(); let countdown = *state.countdown_mode.lock().unwrap(); - let bytes = render_tray_icon_rgba(&colors, false, 0.0, "work", countdown); + let reversed = *state.reverse_direction.lock().unwrap(); + let bytes = render_tray_icon_rgba(&colors, false, 0.0, "work", countdown, reversed); Image::new_owned(bytes, SIZE, SIZE) }; @@ -300,37 +336,35 @@ pub fn create_tray(app: &AppHandle, state: &Arc) { } } }) - .on_menu_event(|app, event| { - match event.id().as_ref() { - "toggle" => { - if let Some(timer) = app.try_state::() { - timer.toggle(); - } - } - "skip" => { - if let Some(timer) = app.try_state::() { - timer.skip(); - } + .on_menu_event(|app, event| match event.id().as_ref() { + "toggle" => { + if let Some(timer) = app.try_state::() { + timer.toggle(); } - "reset-round" => { - if let Some(timer) = app.try_state::() { - timer.restart_round(); - } + } + "skip" => { + if let Some(timer) = app.try_state::() { + timer.skip(); } - "show" => { - log::info!("[tray] show"); - if let Some(window) = app.get_webview_window("main") { - let _ = window.show(); - let _ = window.unminimize(); - let _ = window.set_focus(); - } + } + "reset-round" => { + if let Some(timer) = app.try_state::() { + timer.restart_round(); } - "exit" => { - log::info!("[tray] exit"); - app.exit(0); + } + "show" => { + log::info!("[tray] show"); + if let Some(window) = app.get_webview_window("main") { + let _ = window.show(); + let _ = window.unminimize(); + let _ = window.set_focus(); } - _ => {} } + "exit" => { + log::info!("[tray] exit"); + app.exit(0); + } + _ => {} }) .build(app); @@ -376,7 +410,8 @@ pub fn update_icon(state: &Arc, round_type: &str, paused: bool, progr let colors = state.colors.lock().unwrap().clone(); let countdown = *state.countdown_mode.lock().unwrap(); - let bytes = render_tray_icon_rgba(&colors, paused, progress, round_type, countdown); + let reversed = *state.reverse_direction.lock().unwrap(); + let bytes = render_tray_icon_rgba(&colors, paused, progress, round_type, countdown, reversed); let image = Image::new_owned(bytes, SIZE, SIZE); let _ = tray.set_icon(Some(image)); @@ -396,7 +431,13 @@ pub fn update_menu_items(state: &Arc, is_running: bool, is_paused: bo let guard = state.menu_items.lock().unwrap(); let Some(items) = guard.as_ref() else { return }; - let toggle_label = if is_running { "Pause" } else if is_paused { "Resume" } else { "Start" }; + let toggle_label = if is_running { + "Pause" + } else if is_paused { + "Resume" + } else { + "Start" + }; let controls_enabled = is_running || is_paused; let _ = items.toggle.set_text(toggle_label); @@ -434,10 +475,14 @@ pub fn render_tray_icon_rgba( progress: f32, round_type: &str, countdown: bool, + reversed: bool, ) -> Vec { let mut pixmap = Pixmap::new(SIZE, SIZE).expect("pixmap alloc"); - let mut paint = Paint { anti_alias: true, ..Default::default() }; + let mut paint = Paint { + anti_alias: true, + ..Default::default() + }; let stroke = Stroke { width: STROKE_WIDTH, @@ -460,21 +505,27 @@ pub fn render_tray_icon_rgba( // Round-type color: used for both the progress arc and the pause bars. let round_color = match round_type { "short-break" => rgba_color(colors.short_round), - "long-break" => rgba_color(colors.long_round), - _ => rgba_color(colors.focus_round), + "long-break" => rgba_color(colors.long_round), + _ => rgba_color(colors.focus_round), }; // Progress arc from 12 o'clock, clockwise, in the round-type colour. paint.set_color(round_color); // In elapsed mode the arc grows as time passes; in countdown mode it shrinks. + // Mirror around the vertical centre line when the dial direction is reversed. let effective = if countdown { 1.0 - progress } else { progress }; let sweep = effective.clamp(0.0, 1.0) * TAU; if sweep > 0.001 { let start = -FRAC_PI_2; - let end = start + sweep; - let path = build_arc_path(CENTER, CENTER, RADIUS, start, end); - pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); + let end = start + sweep; + let path = build_arc_path(CENTER, CENTER, RADIUS, start, end); + let arc_transform = if reversed { + Transform::from_row(-1.0, 0.0, 0.0, 1.0, SIZE as f32, 0.0) + } else { + Transform::identity() + }; + pixmap.stroke_path(&path, &paint, &stroke, arc_transform, None); } if paused { @@ -489,8 +540,11 @@ pub fn render_tray_icon_rgba( if let Some(rect) = tiny_skia::Rect::from_xywh(x, bar_y, bar_w, bar_h) { let p = PathBuilder::from_rect(rect); pixmap.fill_path( - &p, &paint, tiny_skia::FillRule::Winding, - Transform::identity(), None, + &p, + &paint, + tiny_skia::FillRule::Winding, + Transform::identity(), + None, ); } } @@ -522,11 +576,17 @@ fn arc_segment(pb: &mut PathBuilder, cx: f32, cy: f32, r: f32, a0: f32, a1: f32, let alpha = ((a1 - a0) / 4.0).tan() * 4.0 / 3.0; let (s0, c0) = a0.sin_cos(); let (s1, c1) = a1.sin_cos(); - let x0 = cx + r * c0; let y0 = cy + r * s0; - let x3 = cx + r * c1; let y3 = cy + r * s1; - let x1 = x0 - alpha * r * s0; let y1 = y0 + alpha * r * c0; - let x2 = x3 + alpha * r * s1; let y2 = y3 - alpha * r * c1; - if first { pb.move_to(x0, y0); } + let x0 = cx + r * c0; + let y0 = cy + r * s0; + let x3 = cx + r * c1; + let y3 = cy + r * s1; + let x1 = x0 - alpha * r * s0; + let y1 = y0 + alpha * r * c0; + let x2 = x3 + alpha * r * s1; + let y2 = y3 - alpha * r * c1; + if first { + pb.move_to(x0, y0); + } pb.cubic_to(x1, y1, x2, y2, x3, y3); } @@ -540,33 +600,34 @@ mod tests { #[test] fn render_returns_correct_byte_count() { - let bytes = render_tray_icon_rgba(&TrayColors::default(), false, 0.5, "work", false); + let bytes = render_tray_icon_rgba(&TrayColors::default(), false, 0.5, "work", false, false); assert_eq!(bytes.len(), (SIZE * SIZE * 4) as usize); } #[test] fn render_paused_returns_correct_byte_count() { - let bytes = render_tray_icon_rgba(&TrayColors::default(), true, 0.0, "work", false); + let bytes = render_tray_icon_rgba(&TrayColors::default(), true, 0.0, "work", false, false); assert_eq!(bytes.len(), (SIZE * SIZE * 4) as usize); } #[test] fn render_paused_preserves_progress_arc() { - let empty = render_tray_icon_rgba(&TrayColors::default(), true, 0.0, "work", false); - let half = render_tray_icon_rgba(&TrayColors::default(), true, 0.5, "work", false); + let empty = render_tray_icon_rgba(&TrayColors::default(), true, 0.0, "work", false, false); + let half = render_tray_icon_rgba(&TrayColors::default(), true, 0.5, "work", false, false); assert!(empty != half); } #[test] fn render_paused_still_shows_pause_indicator() { - let running = render_tray_icon_rgba(&TrayColors::default(), false, 0.5, "work", false); - let paused = render_tray_icon_rgba(&TrayColors::default(), true, 0.5, "work", false); + let running = + render_tray_icon_rgba(&TrayColors::default(), false, 0.5, "work", false, false); + let paused = render_tray_icon_rgba(&TrayColors::default(), true, 0.5, "work", false, false); assert!(running != paused); } #[test] fn render_zero_progress_returns_correct_byte_count() { - let bytes = render_tray_icon_rgba(&TrayColors::default(), false, 0.0, "work", false); + let bytes = render_tray_icon_rgba(&TrayColors::default(), false, 0.0, "work", false, false); assert_eq!(bytes.len(), (SIZE * SIZE * 4) as usize); } @@ -601,4 +662,13 @@ mod tests { fn tray_click_restores_hidden_window() { assert_eq!(tray_window_action(false, false), TrayWindowAction::Restore); } + + #[test] + fn render_reversed_differs_from_default() { + let normal = + render_tray_icon_rgba(&TrayColors::default(), false, 0.25, "work", false, false); + let reversed = + render_tray_icon_rgba(&TrayColors::default(), false, 0.25, "work", false, true); + assert!(normal != reversed); + } } diff --git a/src/lib/components/Timer.svelte b/src/lib/components/Timer.svelte index c82dd9b9..5472983b 100644 --- a/src/lib/components/Timer.svelte +++ b/src/lib/components/Timer.svelte @@ -117,7 +117,11 @@
- +
diff --git a/src/lib/components/TimerDial.svelte b/src/lib/components/TimerDial.svelte index 87980af8..96c0c465 100644 --- a/src/lib/components/TimerDial.svelte +++ b/src/lib/components/TimerDial.svelte @@ -9,9 +9,10 @@ interface Props { snap: TimerState; countdown?: boolean; + dial_direction?: boolean; } - let { snap, countdown = false }: Props = $props(); + let { snap, countdown = false, dial_direction = false }: Props = $props(); // SVG constants (matching original Pomotroid geometry) const CIRCUMFERENCE = 691.15; // 2π × 110 ≈ 691.15 @@ -30,6 +31,9 @@ // Not reactive — only used for comparison inside $effect. let prevRound = $state(''); + // Reverse the direction the arc moves by mirroring the SVG around its vertical axis. + const direction = $derived(dial_direction ? -1 : 1); + $effect(() => { const rt = snap.round_type; const progress = snap.total_secs > 0 ? snap.elapsed_secs / snap.total_secs : 0; @@ -49,7 +53,7 @@ }); -