From 9e9a14f3238a13b6e52a6445bb7711b98e2469b3 Mon Sep 17 00:00:00 2001 From: tarkah Date: Sat, 29 Oct 2022 10:26:41 -0700 Subject: [PATCH] Update text color on theme change --- examples/editor-libcosmic/src/main.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/editor-libcosmic/src/main.rs b/examples/editor-libcosmic/src/main.rs index e0212d7..f5646da 100644 --- a/examples/editor-libcosmic/src/main.rs +++ b/examples/editor-libcosmic/src/main.rs @@ -3,6 +3,7 @@ use cosmic::{ iced::{ self, + Color, Alignment, Application, Command, @@ -202,10 +203,19 @@ impl Application for Window { let mut buffer = self.buffer.lock().unwrap(); buffer.set_metrics(metrics); }, - Message::ThemeChanged(theme) => match theme { - "Dark" => self.theme = Theme::Dark, - "Light" => self.theme = Theme::Light, - _ => (), + Message::ThemeChanged(theme) => { + self.theme = match theme { + "Dark" => Theme::Dark, + "Light" => Theme::Light, + _ => return Command::none(), + }; + + let Color { r, g, b, a } = self.theme.palette().text; + let as_u8 = |component: f32| (component * 255.0) as u8; + self.attrs = self.attrs.color(cosmic_text::Color::rgba(as_u8(r), as_u8(g), as_u8(b), as_u8(a))); + + let mut buffer = self.buffer.lock().unwrap(); + update_attrs(&mut buffer, self.attrs); }, }