use std::str::FromStr; use iced::{ Task, border, widget::{ button, center, column, container, mouse_area, opaque, pick_list, row, scrollable, space, stack, text, text_input, }, }; use crate::config::{ClockInfo, ConfigFile}; pub struct State { theme: iced::Theme, clocks: Vec, modal: ModalKind, new_clock: ClockInfo, } #[derive(Clone)] pub enum Message { SetTheme(iced::Theme), Tick(iced::time::Instant), OpenSettings, AddNewClock, CloseModal, SetClockTz(chrono_tz::Tz), SetClockName(Option), ConfirmAddClock, } #[derive(Clone, Debug)] pub enum ModalKind { None, Settings, AddClock, } impl State { pub fn new(config: ConfigFile) -> Self { Self { theme: iced::Theme::CatppuccinMocha, clocks: config.clocks, modal: ModalKind::None, new_clock: ClockInfo::default(), } } pub fn theme(&self) -> iced::Theme { self.theme.clone() } pub fn update(&mut self, msg: Message) -> iced::Task { match msg { Message::SetTheme(theme) => self.theme = theme, Message::Tick(_t) => {} Message::OpenSettings => self.modal = ModalKind::Settings, Message::AddNewClock => self.modal = ModalKind::AddClock, Message::CloseModal => self.modal = ModalKind::None, Message::SetClockTz(tz) => self.new_clock.timezone = tz, Message::SetClockName(name) => self.new_clock.name = name, Message::ConfirmAddClock => { self.clocks.push(self.new_clock.clone()); self.new_clock = ClockInfo::default(); self.modal = ModalKind::None; ConfigFile { clocks: self.clocks.clone(), } .save_file(); } }; Task::none() } pub fn view(&self) -> iced::Element<'_, Message> { let content = scrollable( column![ space(), row![ button(text("SETTINGS").center()) .width(iced::Length::FillPortion(1)) .padding(iced::padding::horizontal(15.0).vertical(5.0)) .style(rounded_prinary_btn) .on_press(Message::OpenSettings), text!("Iceclock") .width(iced::Length::FillPortion(3)) .center() .size(30), button(text("ADD").center()) .width(iced::Length::FillPortion(1)) .padding(iced::padding::horizontal(15.0).vertical(5.0)) .style(rounded_prinary_btn) .on_press(Message::AddNewClock) ] .spacing(20) .align_y(iced::Alignment::Center), space() ] .padding(iced::padding::horizontal(75)) .extend(self.clocks.iter().map(|clock| { container(clock_view(&clock)) .padding(15) .max_width(700) .style(clock_container_style(&clock)) .into() })) .align_x(iced::Alignment::Center) .spacing(40) .padding(20), ); match self.modal { ModalKind::None => content.into(), ModalKind::Settings => modal( &self.theme, content, self.settings_modal(), Message::CloseModal, ), ModalKind::AddClock => modal( &self.theme, content, self.add_clock_modal(), Message::CloseModal, ), } } fn settings_modal(&self) -> iced::Element<'_, Message> { column![ text("Iceclock settings").size(30), pick_list(iced::Theme::ALL, Some(&self.theme), Message::SetTheme) ] .spacing(20) .into() } fn add_clock_modal(&self) -> iced::Element<'_, Message> { column![ text("Add a clock").size(30), pick_list( chrono_tz::TZ_VARIANTS, Some(self.new_clock.timezone), Message::SetClockTz ) .width(iced::Length::Fill), text_input( "Opional clock name", self.new_clock .name .as_ref() .map(|s| s.as_str()) .unwrap_or("") ) .width(iced::Length::Fill) .on_input(|s| Message::SetClockName(if s.is_empty() { None } else { Some(s) })), button("ADD") .style(rounded_prinary_btn) // .width(iced::Length::Fill) .padding(iced::padding::horizontal(15.0).vertical(5.0)) .on_press(Message::ConfirmAddClock), ] .spacing(20) .max_width(500.0) .align_x(iced::Alignment::Center) .into() } } fn clock_view(clock: &ClockInfo) -> iced::Element<'_, Message> { let primary_text: iced::Element<'_, Message> = text(match &clock.name { Some(s) => s.clone(), None => clock.timezone.name().replace("_", " "), }) .size(28) .center() .into(); let secondary_text: iced::Element<'_, Message> = match clock.name.as_ref() { Some(_) => text(clock.timezone.name().replace("_", " ")) .size(20) .style(text::secondary) .into(), None => space().into(), }; row![ column![primary_text, secondary_text,] .spacing(5) .width(iced::Length::FillPortion(1)), text!("{}", clock.time().format("%H:%M:%S")) .width(iced::Length::FillPortion(1)) .size(36) .align_x(iced::Alignment::Center), text!("{}", clock.time().format("%b %-d, %Y")) .width(iced::Length::FillPortion(1)) .size(28) .align_x(iced::Alignment::End), ] .align_y(iced::Alignment::Center) .width(iced::Length::Fill) .into() } fn clock_container_style(clock: &ClockInfo) -> impl Fn(&iced::Theme) -> container::Style { let color = clock .color_code .as_ref() .and_then(|s| iced::Color::from_str(s.as_str()).ok()); move |theme: &iced::Theme| container::Style { border: border::rounded(15) .color(color.unwrap_or(theme.palette().primary)) .width(1), ..container::rounded_box(theme) } } // taken + modified from iced example code fn modal<'a, Message>( theme: &'a iced::Theme, base: impl Into>, content: impl Into>, on_blur: Message, ) -> iced::Element<'a, Message> where Message: Clone + 'a, { stack![ base.into(), opaque( mouse_area(center(opaque(content)).style(|_theme| { container::Style { background: Some( iced::Color { a: 0.95, ..theme.palette().background } .into(), ), ..container::Style::default() } })) .on_press(on_blur) ) ] .height(iced::Length::Fill) .into() } fn rounded_prinary_btn(theme: &iced::Theme, status: button::Status) -> button::Style { button::Style { border: border::rounded(15), ..button::primary(theme, status) } }