From 554be8ff4cc5aa76770c94716ff1e654fd7d4444 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Fri, 3 Nov 2023 20:15:06 +0000 Subject: [PATCH] Show current light colour --- shalom/src/oracle.rs | 9 +++++---- shalom/src/pages/room.rs | 9 ++++++++- shalom/src/widgets/toggle_card.rs | 23 +++++++++++++++-------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/shalom/src/oracle.rs b/shalom/src/oracle.rs index 3e0eda7..f0fbf99 100644 --- a/shalom/src/oracle.rs +++ b/shalom/src/oracle.rs @@ -16,11 +16,12 @@ use url::Url; use crate::{ hass_client::{ responses::{ - Area, AreaRegistryList, ColorMode, DeviceRegistryList, Entity, - EntityRegistryList, StateAttributes, StateLightAttributes, StateMediaPlayerAttributes, + Area, AreaRegistryList, ColorMode, DeviceRegistryList, Entity, EntityRegistryList, + StateAttributes, StateLightAttributes, StateMediaPlayerAttributes, StateWeatherAttributes, StatesList, WeatherCondition, - }, CallServiceRequestData, CallServiceRequestLight, - CallServiceRequestLightTurnOn, Event, HassRequestKind, + }, + CallServiceRequestData, CallServiceRequestLight, CallServiceRequestLightTurnOn, Event, + HassRequestKind, }, widgets::colour_picker::clamp_to_u8, }; diff --git a/shalom/src/pages/room.rs b/shalom/src/pages/room.rs index 1715e53..98bbee4 100644 --- a/shalom/src/pages/room.rs +++ b/shalom/src/pages/room.rs @@ -16,6 +16,7 @@ use crate::{ subscriptions::download_image, theme::Icon, widgets, + widgets::colour_picker::colour_from_hsb, }; #[derive(Debug)] @@ -108,7 +109,13 @@ impl Room { light.on.unwrap_or_default(), light.on.is_none(), ) - .icon(Icon::Bulb); + .icon(Icon::Bulb) + .active_icon_colour( + light + .hs_color + .zip(light.brightness) + .map(|((h, s), b)| colour_from_hsb(h, s, b / 255.)), + ); if let Some(state) = light.on { toggle_card = toggle_card diff --git a/shalom/src/widgets/toggle_card.rs b/shalom/src/widgets/toggle_card.rs index 60241db..a303e3f 100644 --- a/shalom/src/widgets/toggle_card.rs +++ b/shalom/src/widgets/toggle_card.rs @@ -36,6 +36,7 @@ pub struct ToggleCard { width: Length, active: bool, disabled: bool, + active_icon_colour: Option, on_press: Option, on_long_press: Option, } @@ -49,6 +50,7 @@ impl Default for ToggleCard { width: Length::Fill, active: false, disabled: false, + active_icon_colour: None, on_press: None, on_long_press: None, } @@ -56,6 +58,11 @@ impl Default for ToggleCard { } impl ToggleCard { + pub fn active_icon_colour(mut self, color: Option) -> Self { + self.active_icon_colour = color; + self + } + pub fn on_press(mut self, msg: M) -> Self { self.on_press = Some(msg); self @@ -116,8 +123,8 @@ impl iced::widget::Component for ToggleCard { fn view(&self, state: &Self::State) -> Element<'_, Self::Event, Renderer> { let style = match (self.disabled, self.active, state.mouse_down_start) { (true, _, _) => Style::Disabled, - (_, true, None) => Style::Active, - (_, true, Some(_)) => Style::ActiveHover, + (_, true, None) => Style::Active(self.active_icon_colour), + (_, true, Some(_)) => Style::ActiveHover(self.active_icon_colour), (_, false, None) => Style::Inactive, (_, false, Some(_)) => Style::InactiveHover, }; @@ -185,8 +192,8 @@ pub enum ToggleCardEvent { #[derive(Copy, Clone)] pub enum Style { - Active, - ActiveHover, + Active(Option), + ActiveHover(Option), Inactive, InactiveHover, Disabled, @@ -218,14 +225,14 @@ impl container::StyleSheet for Style { border_width: 0.0, border_color: Color::default(), }, - Style::Active => container::Appearance { + Style::Active(_) => container::Appearance { text_color: Some(Color::WHITE), background: Some(Background::Color(SKY_400)), border_radius: 5.0.into(), border_width: 0.0, border_color: Color::default(), }, - Style::ActiveHover => container::Appearance { + Style::ActiveHover(_) => container::Appearance { text_color: Some(Color::WHITE), background: Some(Background::Color(SKY_500)), border_radius: 5.0.into(), @@ -241,8 +248,8 @@ impl svg::StyleSheet for Style { fn appearance(&self, _style: &Self::Style) -> svg::Appearance { match self { - Style::Active | Style::ActiveHover => svg::Appearance { - color: Some(AMBER_200), + Style::Active(c) | Style::ActiveHover(c) => svg::Appearance { + color: Some(c.unwrap_or(AMBER_200)), }, Style::Inactive | Style::InactiveHover | Style::Disabled => svg::Appearance { color: Some(Color::BLACK), -- libgit2 1.7.2