From 785a9af9571ab237cfcb5154a689e3a9116ddf5e Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 24 Apr 2026 22:31:48 +0530 Subject: shift dynamic color logic to state.rs parse colors.json generated by noctalia and use it --- Cargo.lock | 20 +++++++++++++++++++ Cargo.toml | 1 + src/pomo/state.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 62fccc8..f3c50c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1240,6 +1240,7 @@ dependencies = [ "notify-rust", "ratatui", "serde", + "serde_json", "tokio", "toml", ] @@ -1523,6 +1524,19 @@ dependencies = [ "syn 2.0.114", ] +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + [[package]] name = "serde_repr" version = "0.1.20" @@ -2458,6 +2472,12 @@ dependencies = [ "zvariant", ] +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" + [[package]] name = "zvariant" version = "5.9.2" diff --git a/Cargo.toml b/Cargo.toml index aa23292..16d5b9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,6 @@ directories = "6.0.0" notify-rust = "4.11.7" ratatui = "0.30.0" serde = { version = "1.0.228", features = ["derive"] } +serde_json = "1.0" tokio = { version = "1.49.0", features = ["full"] } toml = "0.9.11" diff --git a/src/pomo/state.rs b/src/pomo/state.rs index 8574d38..c9eb496 100644 --- a/src/pomo/state.rs +++ b/src/pomo/state.rs @@ -1,7 +1,60 @@ +use directories::BaseDirs; use notify_rust::Notification; -use ratatui::widgets::ListState; +use ratatui::{style::Color, widgets::ListState}; use serde::{Deserialize, Serialize}; -use std::time::Duration; +use std::{fs, time::Duration}; + +#[derive(Clone, Copy)] +pub struct Theme { + pub primary: Color, + pub overlay0: Color, + pub surface0: Color, + pub text: Color, +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct NoctaliaColors { + m_primary: String, + m_on_surface_variant: String, + m_surface_variant: String, + m_on_surface: String, +} + +fn parse_hex_color(hex: &str) -> Option { + if hex.len() == 7 && hex.starts_with('#') { + let r = u8::from_str_radix(&hex[1..3], 16).ok()?; + let g = u8::from_str_radix(&hex[3..5], 16).ok()?; + let b = u8::from_str_radix(&hex[5..7], 16).ok()?; + Some(Color::Rgb(r, g, b)) + } else { + None + } +} + +impl Default for Theme { + fn default() -> Self { + let mut theme = Self { + primary: Color::Rgb(180, 190, 254), + overlay0: Color::Rgb(108, 112, 134), + surface0: Color::Rgb(49, 50, 68), + text: Color::Rgb(205, 214, 244), + }; + + if let Some(base_dirs) = BaseDirs::new() { + let path = base_dirs.home_dir().join(".config/noctalia/colors.json"); + if let Ok(content) = fs::read_to_string(&path) { + if let Ok(colors) = serde_json::from_str::(&content) { + if let Some(c) = parse_hex_color(&colors.m_primary) { theme.primary = c; } + if let Some(c) = parse_hex_color(&colors.m_on_surface_variant) { theme.overlay0 = c; } + if let Some(c) = parse_hex_color(&colors.m_surface_variant) { theme.surface0 = c; } + if let Some(c) = parse_hex_color(&colors.m_on_surface) { theme.text = c; } + } + } + } + theme + } +} #[derive(PartialEq, Clone, Copy)] pub enum SessionMode { @@ -53,6 +106,7 @@ pub struct Pomo { pub task_state: ListState, pub input_buffer: String, pub should_quit: bool, + pub theme: Theme, } impl Pomo { @@ -73,6 +127,7 @@ impl Pomo { task_state: ListState::default(), input_buffer: String::new(), should_quit: false, + theme: Theme::default(), } } -- cgit v1.2.3