Something went wrong. Try again.
simple & configurable world clock app
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262use 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<ClockInfo>, 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<String>), 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<Message> { 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 codefn modal<'a, Message>( theme: &'a iced::Theme, base: impl Into<iced::Element<'a, Message>>, content: impl Into<iced::Element<'a, Message>>, 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) }}