From f943f0505e7d2aff999153bf52a166e7daa791e1 Mon Sep 17 00:00:00 2001 From: unsecretised Date: Sun, 28 Dec 2025 20:55:58 +0800 Subject: [PATCH] Improve styling code --- src/app.rs | 12 ++++++------ src/config.rs | 41 +++++++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/app.rs b/src/app.rs index 849abc8..075ba87 100644 --- a/src/app.rs +++ b/src/app.rs @@ -61,10 +61,10 @@ impl App { }, ] } - pub fn render(&self, show_icons: bool) -> impl Into> { + pub fn render(&self, theme: &crate::config::Theme) -> impl Into> { let mut tile = Row::new().width(Fill).height(55); - if show_icons { + if theme.show_icons { if let Some(icon) = &self.icons { tile = tile .push(Viewer::new(icon).height(35).width(35)) @@ -83,6 +83,7 @@ impl App { Text::new(&self.name) .height(Fill) .width(Fill) + .color(theme.text_color(1.)) .align_y(Vertical::Center), ) .on_press(Message::RunFunction(self.open_command.clone())) @@ -98,7 +99,7 @@ impl App { ); tile = tile - .push(container(Text::new(&self.desc)).padding(15)) + .push(container(Text::new(&self.desc).color(theme.text_color(0.4))).padding(15)) .width(Fill); container(tile) @@ -204,7 +205,7 @@ impl Tile { frontmost: None, focused: false, config: config.clone(), - theme: config.theme.to_owned().to_iced_theme(), + theme: config.theme.to_owned().into(), open_hotkey_id: keybind_id, }, Task::batch([open.map(|_| Message::OpenWindow)]), @@ -394,8 +395,7 @@ impl Tile { let mut search_results = Column::new(); for result in &self.results { - search_results = - search_results.push(result.render(self.config.theme.clone().show_icons)); + search_results = search_results.push(result.render(&self.config.theme)); } Column::new() diff --git a/src/config.rs b/src/config.rs index 928133f..e3c4d0c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -55,23 +55,11 @@ impl Default for Theme { } } -impl Theme { - pub fn to_iced_theme(&self) -> iced::Theme { - let text_color = self.text_color; - let bg_color = self.background_color; +impl From for iced::Theme { + fn from(value: Theme) -> Self { let palette = iced::theme::Palette { - background: iced::Color { - r: bg_color.0, - g: bg_color.1, - b: bg_color.2, - a: self.background_opacity, - }, - text: iced::Color { - r: text_color.0, - g: text_color.1, - b: text_color.2, - a: 1., - }, + background: value.bg_color(), + text: value.text_color(1.), primary: iced::Color { r: 0.22, g: 0.55, @@ -101,6 +89,27 @@ impl Theme { } } +impl Theme { + pub fn text_color(&self, opacity: f32) -> iced::Color { + let theme = self.to_owned(); + iced::Color { + r: theme.text_color.0, + g: theme.text_color.1, + b: theme.text_color.2, + a: opacity, + } + } + + pub fn bg_color(&self) -> iced::Color { + iced::Color { + r: self.background_color.0, + g: self.background_color.1, + b: self.background_color.2, + a: self.background_opacity, + } + } +} + #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(default)] pub struct Buffer {