taskbar-focus 0.3.3

A tiny Pomodoro/focus timer that lives in the Windows tray and toggles Do Not Disturb
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
//! Human-readable configuration, persisted as TOML.
//!
//! Location: `%APPDATA%\taskbar-focus\config.toml` (resolved via
//! `SHGetKnownFolderPath(FOLDERID_RoamingAppData)`, never hardcoded), so the
//! file is easy to back up, diff or sync.
//!
//! Every field carries a `#[serde(default)]`, which means a hand-edited file
//! that is missing keys - or was written by an older version - still loads.
//! Unknown keys are preserved-by-ignoring rather than being an error, so a
//! newer config does not break an older binary.

use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::time::Duration;

use crate::timer::{Phase, Plan};

pub const APP_DIR: &str = "taskbar-focus";
pub const CONFIG_FILE: &str = "config.toml";

/// A named set of durations the user can switch between.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Preset {
    pub name: String,
    pub focus_minutes: f64,
    pub short_break_minutes: f64,
    pub long_break_minutes: f64,
    #[serde(default = "default_sessions")]
    pub sessions_before_long_break: u32,
}

fn default_sessions() -> u32 {
    4
}

impl Preset {
    pub fn duration_of(&self, phase: Phase) -> Duration {
        let mins = match phase {
            Phase::Focus => self.focus_minutes,
            Phase::ShortBreak => self.short_break_minutes,
            Phase::LongBreak => self.long_break_minutes,
        };

        Duration::from_secs_f64(if mins.is_finite() && mins > 0.0 {
            mins * 60.0
        } else {
            60.0
        })
    }
}

fn default_presets() -> Vec<Preset> {
    vec![
        Preset {
            name: "Pomodoro 25/5".into(),
            focus_minutes: 25.0,
            short_break_minutes: 5.0,
            long_break_minutes: 15.0,
            sessions_before_long_break: 4,
        },
        Preset {
            name: "Deep Work 90/15".into(),
            focus_minutes: 90.0,
            short_break_minutes: 15.0,
            long_break_minutes: 30.0,
            sessions_before_long_break: 2,
        },
        Preset {
            name: "Short 15/3".into(),
            focus_minutes: 15.0,
            short_break_minutes: 3.0,
            long_break_minutes: 10.0,
            sessions_before_long_break: 4,
        },
    ]
}

/// What to do with a running timer when the machine wakes from sleep.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum WakePolicy {
    /// Subtract the time spent asleep - a 25 minute session started before a
    /// 2 hour nap is simply over. This is the least surprising default.
    #[default]
    CountSleep,
    /// Pretend the machine never slept and keep the remaining time.
    IgnoreSleep,
    /// Pause and let the user decide.
    Pause,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Behavior {
    pub sequence_enabled: bool,
    pub auto_start_break: bool,
    pub auto_start_focus: bool,
    /// Require confirmation before stopping or skipping a *focus* session.
    pub strict_focus: bool,
    /// Re-arm an interrupted session when the app restarts.
    pub restore_session_on_restart: bool,
    pub wake_policy: WakePolicy,
}

impl Default for Behavior {
    fn default() -> Self {
        Self {
            sequence_enabled: true,
            auto_start_break: true,
            auto_start_focus: false,
            strict_focus: false,
            restore_session_on_restart: true,
            wake_policy: WakePolicy::default(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct DndSettings {
    /// Turn Do Not Disturb on for focus and off again for breaks.
    ///
    /// "On" means the `PriorityOnly` quiet-hours profile - exactly what the
    /// Windows UI calls "Do not disturb" - so the user's own priority apps and
    /// contacts still get through. There is deliberately no profile choice
    /// here; see `os::dnd::profile` for why.
    /// This is the only Do Not Disturb setting, deliberately.
    ///
    /// Restoring the previous state afterwards, and restarting the notification
    /// service so Windows notices the change, are not choices: the first is
    /// simply correct, and the second is what makes the feature work at all.
    /// Offering them as switches invited people to turn the feature off without
    /// realising it.
    pub enabled: bool,

    /// Show the mark as a separate notification-area icon while muted, as
    /// close to Windows' own indicator as an ordinary program can get: its own
    /// icon beside the clock, present only while muted.
    ///
    /// The timer tray icon remains dedicated to progress because it cannot show
    /// both clearly at sixteen pixels; see `ui::icon`.
    pub mute_tray_icon: bool,

    /// Draw the mark beside the countdown in the compact timer window.
    pub mute_window: bool,
}

impl Default for DndSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            mute_tray_icon: true,
            mute_window: true,
        }
    }
}

impl DndSettings {
    /// Whether any muted-state indicator is enabled.
    pub fn wants_indicator(&self) -> bool {
        self.mute_tray_icon || self.mute_window
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Display {
    /// Show the compact timer window: a small, resizable, always-visible
    /// readout of the countdown. More reliable than the taskbar title trick,
    /// and it can be shrunk to roughly the height of a taskbar button.
    pub mini_window: bool,

    /// Keep the compact window above other windows.
    pub always_on_top: bool,

    /// Saved position and size of the compact window: `[x, y, width, height]`.
    /// Absent until the user has moved or resized it.
    pub mini_geometry: Option<[i32; 4]>,
}

impl Default for Display {
    fn default() -> Self {
        Self {
            mini_window: false,
            // On by default. The Windows taskbar is itself a top-most window,
            // so without this the compact window sinks behind it and cannot be
            // parked over the taskbar at all.
            always_on_top: true,
            mini_geometry: None,
        }
    }
}

/// Per-event toggles shared by notifications and sounds.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct EventToggles {
    pub focus_start: bool,
    pub focus_end: bool,
    pub break_start: bool,
    pub break_end: bool,
}

impl Default for EventToggles {
    fn default() -> Self {
        Self {
            focus_start: true,
            focus_end: true,
            break_start: true,
            break_end: true,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Notifications {
    pub enabled: bool,
    pub events: EventToggles,
}

impl Default for Notifications {
    fn default() -> Self {
        Self {
            enabled: true,
            events: EventToggles::default(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct Sounds {
    /// Global mute, independent of the per-event switches.
    pub muted: bool,
    pub events: EventToggles,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Hotkeys {
    pub enabled: bool,
    /// Accelerator strings like "Ctrl+Alt+F"; empty disables that binding.
    pub toggle: String,
    pub skip: String,
}

impl Default for Hotkeys {
    fn default() -> Self {
        Self {
            enabled: true,
            toggle: "Ctrl+Alt+F".into(),
            skip: "Ctrl+Alt+B".into(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
    pub active_preset: String,
    pub presets: Vec<Preset>,
    pub behavior: Behavior,
    pub dnd: DndSettings,
    pub display: Display,
    pub notifications: Notifications,
    pub sounds: Sounds,
    pub hotkeys: Hotkeys,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            active_preset: "Pomodoro 25/5".into(),
            presets: default_presets(),
            behavior: Behavior::default(),
            dnd: DndSettings::default(),
            display: Display::default(),
            notifications: Notifications::default(),
            sounds: Sounds::default(),
            hotkeys: Hotkeys::default(),
        }
    }
}

impl Config {
    /// The preset named by `active_preset`, falling back to the first one and
    /// finally to a built-in, so a bad name in the file can never panic.
    pub fn preset(&self) -> Preset {
        self.presets
            .iter()
            .find(|p| p.name.eq_ignore_ascii_case(&self.active_preset))
            .or_else(|| self.presets.first())
            .cloned()
            .unwrap_or_else(|| default_presets().remove(0))
    }

    /// Project the config into the pure timer's view of the world.
    pub fn plan(&self) -> Plan {
        let p = self.preset();
        Plan {
            focus: p.duration_of(Phase::Focus),
            short_break: p.duration_of(Phase::ShortBreak),
            long_break: p.duration_of(Phase::LongBreak),
            sessions_before_long_break: p.sessions_before_long_break.max(1),
            sequence_enabled: self.behavior.sequence_enabled,
            auto_start_break: self.behavior.auto_start_break,
            auto_start_focus: self.behavior.auto_start_focus,
        }
    }

    pub fn select_preset(&mut self, name: &str) -> bool {
        if let Some(p) = self
            .presets
            .iter()
            .find(|p| p.name.eq_ignore_ascii_case(name))
        {
            self.active_preset = p.name.clone();
            true
        } else {
            false
        }
    }

    /// Load from disk, falling back to defaults when the file is absent or
    /// unparseable. A corrupt file is renamed rather than deleted so the user
    /// can recover whatever was in it.
    pub fn load() -> Self {
        let path = match config_path() {
            Some(p) => p,
            None => return Config::default(),
        };
        let text = match std::fs::read_to_string(&path) {
            Ok(t) => t,
            Err(_) => return Config::default(),
        };
        match toml::from_str::<Config>(&text) {
            Ok(mut c) => {
                if c.presets.is_empty() {
                    c.presets = default_presets();
                }
                c
            }
            Err(e) => {
                eprintln!(
                    "taskbar-focus: {} is invalid ({e}); using defaults",
                    path.display()
                );
                let _ = std::fs::rename(&path, path.with_extension("toml.bad"));
                Config::default()
            }
        }
    }

    /// Save atomically: write a sibling temp file then rename over the target,
    /// so a crash mid-write cannot leave a truncated config behind.
    pub fn save(&self) -> std::io::Result<()> {
        let path = config_path().ok_or_else(|| {
            std::io::Error::new(std::io::ErrorKind::NotFound, "no config directory")
        })?;
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir)?;
        }
        let body = toml::to_string_pretty(self)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
        let text = format!(
            "# taskbar-focus configuration.\n\
             # Edit freely - unknown or missing keys fall back to defaults.\n\
             # Durations are in minutes and may be fractional.\n\n{body}"
        );
        let tmp = path.with_extension("toml.tmp");
        std::fs::write(&tmp, text)?;
        std::fs::rename(&tmp, &path)
    }
}

/// `%APPDATA%\taskbar-focus`, via the documented known-folder API.
pub fn app_dir() -> Option<PathBuf> {
    roaming_appdata().map(|d| d.join(APP_DIR))
}

pub fn config_path() -> Option<PathBuf> {
    app_dir().map(|d| d.join(CONFIG_FILE))
}

/// Has this user ever run the app before?
///
/// Used to make the first launch visible: a tray-only app that opens no window
/// is indistinguishable from one that failed to start, especially on Windows 11
/// where new tray icons are hidden in the overflow by default.
pub fn is_first_run() -> bool {
    !config_path().is_some_and(|p| p.exists())
}

#[cfg(windows)]
fn roaming_appdata() -> Option<PathBuf> {
    use windows::Win32::System::Com::CoTaskMemFree;
    use windows::Win32::UI::Shell::{
        FOLDERID_RoamingAppData, SHGetKnownFolderPath, KF_FLAG_DEFAULT,
    };
    unsafe {
        let pw = SHGetKnownFolderPath(&FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, None).ok()?;
        let s = pw.to_string().ok()?;
        CoTaskMemFree(Some(pw.0 as *const _));
        Some(PathBuf::from(s))
    }
}

#[cfg(not(windows))]
fn roaming_appdata() -> Option<PathBuf> {
    std::env::var_os("APPDATA").map(PathBuf::from)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn partial_file_fills_in_defaults() {
        let text = r#"
            active_preset = "Deep Work 90/15"
            [behavior]
            strict_focus = true
        "#;
        let c: Config = toml::from_str(text).unwrap();
        assert!(c.behavior.strict_focus);
        assert!(
            c.behavior.sequence_enabled,
            "unspecified keys keep defaults"
        );
        assert!(c.dnd.enabled);

        assert_eq!(c.presets, default_presets());
        assert_eq!(c.plan().focus, Duration::from_secs(90 * 60));
    }

    #[test]
    fn nonsense_durations_are_clamped() {
        let p = Preset {
            name: "bad".into(),
            focus_minutes: -5.0,
            short_break_minutes: f64::NAN,
            long_break_minutes: 0.0,
            sessions_before_long_break: 0,
        };
        for ph in [Phase::Focus, Phase::ShortBreak, Phase::LongBreak] {
            assert_eq!(p.duration_of(ph), Duration::from_secs(60));
        }
    }
}