From c400649888c6f7512bc579270efdce885a032da2 Mon Sep 17 00:00:00 2001 From: Ryan Brue Date: Wed, 21 Aug 2024 20:15:19 -0500 Subject: [PATCH] clock Signed-off-by: Ryan Brue --- Cargo.lock | 44 ++++++++++++ Cargo.toml | 1 + src/main.rs | 23 ++++-- src/settings_tray/clock_subscription.rs | 94 +++++++++++++++++++++++++ src/settings_tray/mod.rs | 43 ++++++++++- 5 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 src/settings_tray/clock_subscription.rs diff --git a/Cargo.lock b/Cargo.lock index 6d01489..e7882aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,6 +86,12 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + [[package]] name = "android_system_properties" version = "0.1.5" @@ -511,6 +517,20 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets 0.52.6", +] + [[package]] name = "clipboard-win" version = "5.4.0" @@ -1587,6 +1607,29 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df" +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "iced" version = "0.13.0-dev" @@ -2792,6 +2835,7 @@ dependencies = [ name = "ryanabx-shell" version = "0.1.0" dependencies = [ + "chrono", "cosmic-client-toolkit", "cosmic-protocols", "freedesktop-desktop-entry", diff --git a/Cargo.toml b/Cargo.toml index 916b688..ccba539 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols", default "client", ], rev = "c8d3a1c" } once_cell = "1.19.0" +chrono = "0.4.38" diff --git a/src/main.rs b/src/main.rs index df1d1fb..ab3ac5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,10 @@ use iced::{ application::{ actions::layer_surface::SctkLayerSurfaceSettings, layer_surface::Anchor, InitialSurface, }, - widget::{column, container::Style}, - Application, Background, Color, Command, Radius, Settings, Theme, + widget::{column, container::Style, row}, + Application, Background, Color, Command, Radius, Settings, Subscription, Theme, }; +use settings_tray::{SettingsTray, SettingsTrayMessage}; mod app_tray; mod config; @@ -30,6 +31,7 @@ fn main() -> Result<(), iced::Error> { struct Panel<'a> { _panel_config: PanelConfig, app_tray: AppTray<'a>, + settings_tray: SettingsTray, } impl<'a> Default for Panel<'a> { @@ -37,6 +39,7 @@ impl<'a> Default for Panel<'a> { Self { _panel_config: PanelConfig::default(), app_tray: AppTray::new(AppTrayConfig::default()), + settings_tray: SettingsTray::new(), } } } @@ -45,6 +48,7 @@ impl<'a> Default for Panel<'a> { pub enum Message { Panic, AppTray(AppTrayMessage), + SettingsTray(SettingsTrayMessage), } impl<'a> Application for Panel<'a> { @@ -63,6 +67,7 @@ impl<'a> Application for Panel<'a> { } fn update(&mut self, message: Self::Message) -> iced::Command { + println!("Received message: {:?}", message); match message { Message::Panic => { panic!("Panic button pressed hehe"); @@ -71,6 +76,10 @@ impl<'a> Application for Panel<'a> { .app_tray .handle_message(app_tray_msg) .map(Message::AppTray), + Message::SettingsTray(settings_tray_msg) => self + .settings_tray + .handle_message(settings_tray_msg) + .map(Message::SettingsTray), } } @@ -78,7 +87,10 @@ impl<'a> Application for Panel<'a> { &self, _id: iced::window::Id, ) -> iced::Element<'_, Self::Message, Self::Theme, Self::Renderer> { - let panel_items = self.app_tray.view().map(Message::AppTray); + let panel_items = row![ + self.app_tray.view().map(Message::AppTray), + self.settings_tray.view().map(Message::SettingsTray) + ]; iced::widget::container(column![ iced::widget::horizontal_rule(1).style(|_| iced::widget::rule::Style { color: Color::WHITE, @@ -94,7 +106,10 @@ impl<'a> Application for Panel<'a> { } fn subscription(&self) -> iced::Subscription { - self.app_tray.subscription().map(Message::AppTray) + Subscription::batch(vec![ + self.settings_tray.subscription().map(Message::SettingsTray), + self.app_tray.subscription().map(Message::AppTray), + ]) } } diff --git a/src/settings_tray/clock_subscription.rs b/src/settings_tray/clock_subscription.rs new file mode 100644 index 0000000..8ce55f8 --- /dev/null +++ b/src/settings_tray/clock_subscription.rs @@ -0,0 +1,94 @@ +use std::{ + thread::sleep, + time::{Duration, Instant}, +}; + +use chrono::{Timelike, Utc}; +use iced::{ + futures::{SinkExt, Stream}, + widget::{ + column, + text::{self, Style}, + }, + Color, Command, Subscription, +}; + +#[derive(Clone, Debug)] +pub enum ClockMessage { + UpdateClock(String), + UpdateDate(String), +} + +#[derive(Clone, Debug)] +pub struct Clock { + date: String, + time: String, +} + +impl Clock { + pub fn new() -> Self { + Self { + date: "".to_string(), + time: "".to_string(), + } + } + + pub fn handle_message(&mut self, clock_message: ClockMessage) -> iced::Command { + match clock_message { + ClockMessage::UpdateClock(new_time) => { + println!("Updating time to {}", &new_time); + self.time = new_time; + Command::none() + } + _ => todo!(), + } + } + + pub fn view(&self) -> iced::Element { + iced::widget::container(column![iced::widget::text!("{}", self.time).style( + |theme| { + Style { + color: Some(Color::WHITE), + } + } + )]) + .into() + } + + pub fn subscription(&self) -> iced::Subscription { + struct ClockWorker; + iced::subscription::channel( + std::any::TypeId::of::(), + 100, + |mut output| async move { + loop { + let now = Utc::now(); + let formatted_time = now.format("%Y-%m-%d %H:%M:%S").to_string(); + println!("Sending {} to main thread", formatted_time); + output + .send(ClockMessage::UpdateClock(formatted_time)) + .await + .unwrap(); + println!("Through sending thing"); + // Calculate the duration until the next minute + let next_minute = now.with_second(0).unwrap().with_nanosecond(0).unwrap() + + chrono::Duration::minutes(1); + + let duration_until_next_minute = + next_minute.timestamp_millis() - Utc::now().timestamp_millis(); + + // Sleep until the next minute + { + let deadline = Instant::now() + + Duration::from_millis(duration_until_next_minute as u64); + let now = Instant::now(); + + if let Some(delay) = deadline.checked_duration_since(now) { + sleep(delay); + } + }; + } + }, + ) + } +} diff --git a/src/settings_tray/mod.rs b/src/settings_tray/mod.rs index 84c48f4..ad4c473 100644 --- a/src/settings_tray/mod.rs +++ b/src/settings_tray/mod.rs @@ -1 +1,42 @@ -pub fn get_tray_widget() {} +use clock_subscription::{Clock, ClockMessage}; +use iced::widget::{row, text}; + +mod clock_subscription; + +#[derive(Clone, Debug)] +pub struct SettingsTray { + clock: Clock, +} + +#[derive(Clone, Debug)] +pub enum SettingsTrayMessage { + Clock(ClockMessage), +} + +impl SettingsTray { + pub fn new() -> Self { + Self { + clock: Clock::new(), + } + } + + pub fn handle_message( + &mut self, + message: SettingsTrayMessage, + ) -> iced::Command { + match message { + SettingsTrayMessage::Clock(clock_msg) => self + .clock + .handle_message(clock_msg) + .map(SettingsTrayMessage::Clock), + } + } + + pub fn view(&self) -> iced::Element { + iced::widget::container(row![self.clock.view().map(SettingsTrayMessage::Clock)]).into() + } + + pub fn subscription(&self) -> iced::Subscription { + self.clock.subscription().map(SettingsTrayMessage::Clock) + } +}