aboutsummaryrefslogtreecommitdiff
path: root/desktop/src-tauri/src/lib.rs
blob: 0512f46d8ea3098e9735f0e5be73ae593f4e57b4 (plain)
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
use pomoru_core::{config, notification, status, task::TaskList, timer::TimerState};
use serde::{Deserialize, Serialize};
use std::sync::Mutex;
use tauri::{Manager, State};

// Shared application state managed by Tauri.
pub struct AppState {
    pub timer: Mutex<TimerState>,
    pub tasks: Mutex<TaskList>,
}

// Serializable snapshot of the full app state sent to the frontend.
#[derive(Serialize)]
pub struct AppSnapshot {
    pub timer: TimerState,
    pub tasks: Vec<pomoru_core::task::Task>,
    pub selected_task: Option<usize>,
    pub active_task_title: Option<String>,
}

#[derive(Serialize)]
pub struct TickResult {
    pub timer: TimerState,
    pub event: Option<pomoru_core::timer::TimerEvent>,
}

// --- Timer Commands ---

#[tauri::command]
fn get_state(state: State<AppState>) -> AppSnapshot {
    let timer = state.timer.lock().unwrap();
    let tasks = state.tasks.lock().unwrap();
    AppSnapshot {
        timer: timer.clone(),
        tasks: tasks.items().to_vec(),
        selected_task: tasks.selected(),
        active_task_title: tasks.active_task().map(|t| t.title.clone()),
    }
}

#[tauri::command]
fn tick(state: State<AppState>) -> TickResult {
    let mut timer = state.timer.lock().unwrap();
    let event = timer.tick();

    // Export status for Waybar compatibility
    let _ = status::export_status(timer.mode, timer.time_remaining_secs);

    // Send notification if session completed
    if let Some(ref ev) = event {
        notification::send_notification(&ev.title, &ev.message);
    }

    TickResult {
        timer: timer.clone(),
        event,
    }
}

#[tauri::command]
fn start_timer(state: State<AppState>) {
    let mut timer = state.timer.lock().unwrap();
    timer.start();
    let _ = status::export_status(timer.mode, timer.time_remaining_secs);
}

#[tauri::command]
fn pause_timer(state: State<AppState>) {
    let mut timer = state.timer.lock().unwrap();
    timer.pause();
    let _ = status::export_status(timer.mode, timer.time_remaining_secs);
}

#[tauri::command]
fn toggle_timer(state: State<AppState>) {
    let mut timer = state.timer.lock().unwrap();
    timer.toggle_running();
    let _ = status::export_status(timer.mode, timer.time_remaining_secs);
}

#[tauri::command]
fn reset_timer(state: State<AppState>) {
    let mut timer = state.timer.lock().unwrap();
    timer.reset();
    let _ = status::export_status(timer.mode, timer.time_remaining_secs);
}

#[tauri::command]
fn skip_session(state: State<AppState>) {
    let mut timer = state.timer.lock().unwrap();
    timer.skip_session();
    let _ = status::export_status(timer.mode, timer.time_remaining_secs);
}

#[tauri::command]
fn cycle_mode(state: State<AppState>) {
    let mut timer = state.timer.lock().unwrap();
    timer.cycle_mode();
    let _ = status::export_status(timer.mode, timer.time_remaining_secs);
}

#[tauri::command]
fn toggle_auto_switch(state: State<AppState>) {
    let mut timer = state.timer.lock().unwrap();
    timer.toggle_auto_switch();
}

#[tauri::command]
fn set_duration(state: State<AppState>, mins: u64) {
    let mut timer = state.timer.lock().unwrap();
    timer.set_current_duration_mins(mins);
    let _ = status::export_status(timer.mode, timer.time_remaining_secs);
}

// --- Task Commands ---

#[tauri::command]
fn get_tasks(state: State<AppState>) -> Vec<pomoru_core::task::Task> {
    let tasks = state.tasks.lock().unwrap();
    tasks.items().to_vec()
}

#[tauri::command]
fn add_task(state: State<AppState>, title: String) {
    let mut tasks = state.tasks.lock().unwrap();
    tasks.add(title);
}

#[tauri::command]
fn edit_task(state: State<AppState>, index: usize, title: String) {
    let mut tasks = state.tasks.lock().unwrap();
    tasks.edit(index, title);
}

#[tauri::command]
fn delete_task(state: State<AppState>, index: usize) {
    let mut tasks = state.tasks.lock().unwrap();
    tasks.delete(index);
}

#[tauri::command]
fn toggle_task(state: State<AppState>, index: usize) {
    let mut tasks = state.tasks.lock().unwrap();
    tasks.toggle(index);
}

#[tauri::command]
fn reorder_task(state: State<AppState>, index: usize, direction: String) {
    let mut tasks = state.tasks.lock().unwrap();
    tasks.select(Some(index));
    match direction.as_str() {
        "up" => tasks.move_up(),
        "down" => tasks.move_down(),
        _ => {}
    }
}

#[tauri::command]
fn move_task(state: State<AppState>, from: usize, to: usize) {
    let mut tasks = state.tasks.lock().unwrap();
    tasks.move_task(from, to);
}

// --- Config Commands ---

#[tauri::command]
fn save_config(state: State<AppState>) -> Result<(), String> {
    let timer = state.timer.lock().unwrap();
    let tasks = state.tasks.lock().unwrap();
    let cfg = config::Config {
        work_time_mins: timer.work_time_secs / 60,
        short_break_mins: timer.short_break_secs / 60,
        long_break_mins: timer.long_break_secs / 60,
        tasks: tasks.items().to_vec(),
        auto_switch_sessions: timer.auto_switch_sessions,
    };
    config::save(&cfg).map_err(|e| e.to_string())
}

// --- Theme Commands ---

#[derive(Serialize)]
pub struct ThemeColors {
    pub primary: String,
    pub overlay0: String,
    pub surface0: String,
    pub text: String,
}

#[tauri::command]
fn get_theme() -> ThemeColors {
    use directories::BaseDirs;

    // Default Catppuccin Mocha-inspired colors
    let mut colors = ThemeColors {
        primary: "#b4befe".to_string(),
        overlay0: "#6c7086".to_string(),
        surface0: "#313244".to_string(),
        text: "#cdd6f4".to_string(),
    };

    // Try loading Noctalia overrides
    if let Some(base_dirs) = BaseDirs::new() {
        let path = base_dirs.home_dir().join(".config/noctalia/colors.json");
        if let Ok(content) = std::fs::read_to_string(&path) {
            #[derive(Deserialize)]
            #[serde(rename_all = "camelCase")]
            struct NoctaliaColors {
                m_primary: String,
                m_on_surface_variant: String,
                m_surface_variant: String,
                m_on_surface: String,
            }

            if let Ok(noctalia) = serde_json::from_str::<NoctaliaColors>(&content) {
                colors.primary = noctalia.m_primary;
                colors.overlay0 = noctalia.m_on_surface_variant;
                colors.surface0 = noctalia.m_surface_variant;
                colors.text = noctalia.m_on_surface;
            }
        }
    }

    colors
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    // Load config on startup
    let cfg = config::load();
    let mut timer = TimerState::new();
    timer.<