use iced::{ widget::{button::Status, column}, Theme, }; use crate::{ gui::components::{navigation, panel_button, Text}, traits::Focusable, }; #[derive(Clone, Debug, PartialEq)] pub struct Panel { pub err: Option, pub id: Id, pub small: Option, pub name: String, pub is_focused: bool, } impl Panel { pub fn new(id: Id, text: &str, small: Option, err: Option) -> Self { Self { err: err.clone(), id, small: small.clone(), name: text.to_string(), is_focused: false, } } } impl<'a, Id> From<&'a Panel> for iced::Element<'a, navigation::Message>> where Id: Clone, { fn from(value: &'a Panel) -> Self { let mut cols: Vec>>> = Vec::new(); if let Some(err) = &value.err { cols.push( Text::default(err) .highlight_maybe(value.is_focused) .small() .danger() .into(), ); } if let Some(small) = &value.small { cols.push( Text::default(small) .highlight_maybe(value.is_focused) .small() .weak() .into(), ); } cols.push( Text::default(&value.name) .highlight_maybe(value.is_focused) .into(), ); panel_button( column(cols), navigation::Message::ActivateOption(value.clone()), ) .style(|theme, status| match (status, value.is_focused) { (Status::Hovered, false) => hovered_style(theme), (_, true) | (Status::Pressed, _) => focused_style(theme), (Status::Active, false) | (Status::Disabled, false) => Default::default(), }) .into() } } fn hovered_style(theme: &Theme) -> iced::widget::button::Style { iced::widget::button::Style { background: Some(iced::Background::Color( theme.extended_palette().background.weak.color, )), text_color: theme.extended_palette().primary.base.text, ..Default::default() } } fn focused_style(theme: &Theme) -> iced::widget::button::Style { iced::widget::button::Style { background: Some(iced::Background::Color( theme.extended_palette().primary.base.text, )), text_color: theme.extended_palette().primary.base.color, ..Default::default() } } impl Focusable for Panel { fn focus(&mut self) { self.is_focused = true; } fn unfocus(&mut self) { self.is_focused = false; } }