From e3581344f734e0a61553863905e9d170dfb8eacf Mon Sep 17 00:00:00 2001 From: RanXom Date: Sat, 13 Jun 2026 16:54:39 +0530 Subject: create tauri app backend --- desktop/src-tauri/Cargo.toml | 21 ++ desktop/src-tauri/build.rs | 3 + desktop/src-tauri/capabilities/default.json | 10 + desktop/src-tauri/src/lib.rs | 319 ++++++++++++++++++++++++++++ desktop/src-tauri/src/main.rs | 6 + desktop/src-tauri/tauri.conf.json | 41 ++++ 6 files changed, 400 insertions(+) create mode 100644 desktop/src-tauri/Cargo.toml create mode 100644 desktop/src-tauri/build.rs create mode 100644 desktop/src-tauri/capabilities/default.json create mode 100644 desktop/src-tauri/src/lib.rs create mode 100644 desktop/src-tauri/src/main.rs create mode 100644 desktop/src-tauri/tauri.conf.json (limited to 'desktop/src-tauri') diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml new file mode 100644 index 0000000..6c93b26 --- /dev/null +++ b/desktop/src-tauri/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "pomoru-desktop" +version = "2.0.0" +description = "Pomoru Desktop — A minimal Pomodoro timer" +authors = ["RanXom"] +edition = "2026" + +[lib] +name = "desktop_lib" +crate-type = ["staticlib", "cdylib", "rlib"] + +[build-dependencies] +tauri-build = { version = "2", features = [] } + +[dependencies] +pomoru-core = { path = "../../crates/pomoru-core" } +tauri = { version = "2", features = ["tray-icon"] } +tauri-plugin-opener = "2" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +directories = "6.0.0" diff --git a/desktop/src-tauri/build.rs b/desktop/src-tauri/build.rs new file mode 100644 index 0000000..d860e1e --- /dev/null +++ b/desktop/src-tauri/build.rs @@ -0,0 +1,3 @@ +fn main() { + tauri_build::build() +} diff --git a/desktop/src-tauri/capabilities/default.json b/desktop/src-tauri/capabilities/default.json new file mode 100644 index 0000000..4cdbf49 --- /dev/null +++ b/desktop/src-tauri/capabilities/default.json @@ -0,0 +1,10 @@ +{ + "$schema": "../gen/schemas/desktop-schema.json", + "identifier": "default", + "description": "Capability for the main window", + "windows": ["main"], + "permissions": [ + "core:default", + "opener:default" + ] +} diff --git a/desktop/src-tauri/src/lib.rs b/desktop/src-tauri/src/lib.rs new file mode 100644 index 0000000..0512f46 --- /dev/null +++ b/desktop/src-tauri/src/lib.rs @@ -0,0 +1,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, + pub tasks: Mutex, +} + +// Serializable snapshot of the full app state sent to the frontend. +#[derive(Serialize)] +pub struct AppSnapshot { + pub timer: TimerState, + pub tasks: Vec, + pub selected_task: Option, + pub active_task_title: Option, +} + +#[derive(Serialize)] +pub struct TickResult { + pub timer: TimerState, + pub event: Option, +} + +// --- Timer Commands --- + +#[tauri::command] +fn get_state(state: State) -> 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) -> 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) { + 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) { + 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) { + 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) { + 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) { + 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) { + 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) { + let mut timer = state.timer.lock().unwrap(); + timer.toggle_auto_switch(); +} + +#[tauri::command] +fn set_duration(state: State, 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) -> Vec { + let tasks = state.tasks.lock().unwrap(); + tasks.items().to_vec() +} + +#[tauri::command] +fn add_task(state: State, title: String) { + let mut tasks = state.tasks.lock().unwrap(); + tasks.add(title); +} + +#[tauri::command] +fn edit_task(state: State, index: usize, title: String) { + let mut tasks = state.tasks.lock().unwrap(); + tasks.edit(index, title); +} + +#[tauri::command] +fn delete_task(state: State, index: usize) { + let mut tasks = state.tasks.lock().unwrap(); + tasks.delete(index); +} + +#[tauri::command] +fn toggle_task(state: State, index: usize) { + let mut tasks = state.tasks.lock().unwrap(); + tasks.toggle(index); +} + +#[tauri::command] +fn reorder_task(state: State, 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, 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) -> 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::(&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.work_time_secs = cfg.work_time_mins * 60; + timer.short_break_secs = cfg.short_break_mins * 60; + timer.long_break_secs = cfg.long_break_mins * 60; + timer.auto_switch_sessions = cfg.auto_switch_sessions; + timer.reset_timer_to_mode(); + + let tasks = TaskList::from_tasks(cfg.tasks); + + // Initial status export + let _ = status::export_status(timer.mode, timer.time_remaining_secs); + + tauri::Builder::default() + .plugin(tauri_plugin_opener::init()) + .manage(AppState { + timer: Mutex::new(timer), + tasks: Mutex::new(tasks), + }) + .invoke_handler(tauri::generate_handler![ + get_state, + tick, + start_timer, + pause_timer, + toggle_timer, + reset_timer, + skip_session, + cycle_mode, + toggle_auto_switch, + set_duration, + get_tasks, + add_task, + edit_task, + delete_task, + toggle_task, + reorder_task, + move_task, + save_config, + get_theme, + ]) + .setup(|app| { + let quit_i = tauri::menu::MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?; + let menu = tauri::menu::Menu::with_items(app, &[&quit_i])?; + + tauri::tray::TrayIconBuilder::new() + .icon(app.default_window_icon().unwrap().clone()) + .menu(&menu) + .on_menu_event(|app, event| { + if event.id.as_ref() == "quit" { + app.exit(0); + } + }) + .on_tray_icon_event(|tray, event| { + if let tauri::tray::TrayIconEvent::Click { + button: tauri::tray::MouseButton::Left, + button_state: tauri::tray::MouseButtonState::Up, + .. + } = event { + let app = tray.app_handle(); + if let Some(window) = app.get_webview_window("main") { + let _ = window.show(); + let _ = window.set_focus(); + } + } + }) + .build(app)?; + + Ok(()) + }) + .on_window_event(|window, event| { + // Save config when window closes + if let tauri::WindowEvent::CloseRequested { .. } = event { + if let Some(state) = window.try_state::() { + 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, + }; + let _ = config::save(&cfg); + let _ = status::clear_status(); + } + } + }) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} diff --git a/desktop/src-tauri/src/main.rs b/desktop/src-tauri/src/main.rs new file mode 100644 index 0000000..bea3c23 --- /dev/null +++ b/desktop/src-tauri/src/main.rs @@ -0,0 +1,6 @@ +// Prevents additional console window on Windows in release, DO NOT REMOVE!! +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] + +fn main() { + desktop_lib::run() +} diff --git a/desktop/src-tauri/tauri.conf.json b/desktop/src-tauri/tauri.conf.json new file mode 100644 index 0000000..f8bec45 --- /dev/null +++ b/desktop/src-tauri/tauri.conf.json @@ -0,0 +1,41 @@ +{ + "$schema": "https://schema.tauri.app/config/2", + "productName": "Pomoru", + "version": "2.0.0", + "identifier": "com.pomoru.desktop", + "build": { + "beforeDevCommand": "npm run dev", + "devUrl": "http://localhost:1420", + "beforeBuildCommand": "npm run build", + "frontendDist": "../dist" + }, + "app": { + "windows": [ + { + "title": "Pomoru", + "width": 900, + "height": 560, + "minWidth": 720, + "minHeight": 480, + "resizable": true, + "decorations": false, + "transparent": true, + "center": true + } + ], + "security": { + "csp": null + } + }, + "bundle": { + "active": true, + "targets": "all", + "icon": [ + "icons/32x32.png", + "icons/128x128.png", + "icons/128x128@2x.png", + "icons/icon.icns", + "icons/icon.ico" + ] + } +} -- cgit v1.2.3