From 3ba0e74d560f9919cf50c8848fe865190b29944d Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Tue, 10 Sep 2024 21:02:19 +0900 Subject: [PATCH] feat: add time widget --- Cargo.lock | 44 ++++++++++++++++++++++++++++++++++++++++++++ lala_bar/Cargo.toml | 1 + lala_bar/src/main.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11086d7..796cd3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04" +[[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" @@ -591,6 +597,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" @@ -1603,6 +1623,29 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[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.12.1" @@ -1969,6 +2012,7 @@ version = "0.3.11" dependencies = [ "alsa", "anyhow", + "chrono", "env_logger", "futures", "futures-util", diff --git a/lala_bar/Cargo.toml b/lala_bar/Cargo.toml index 246295d..ef5026f 100644 --- a/lala_bar/Cargo.toml +++ b/lala_bar/Cargo.toml @@ -41,3 +41,4 @@ xdg = "2.5.2" url.workspace = true futures.workspace = true zbus_notification.workspace = true +chrono = "0.4.38" diff --git a/lala_bar/src/main.rs b/lala_bar/src/main.rs index 838c50f..07c0178 100644 --- a/lala_bar/src/main.rs +++ b/lala_bar/src/main.rs @@ -18,6 +18,7 @@ use zbus_notification::{ NOTIFICATION_SERVICE_PATH, }; +use chrono::prelude::*; use iced_layershell::reexport::{Anchor, KeyboardInteractivity, Layer, NewLayerShellSettings}; use iced_layershell::settings::{LayerShellSettings, Settings}; use iced_layershell::MultiApplication; @@ -197,9 +198,30 @@ struct LalaMusicBar { sender: Sender, receiver: Arc>>, quite_mode: bool, + + datetime: DateTime, } impl LalaMusicBar { + fn date_widget(&self) -> Element { + let date = self.datetime.date_naive(); + let dateday = date.format("%m-%d").to_string(); + let week = date.format("%A").to_string(); + let time = self.datetime.time(); + let time_info = time.format("%H:%M").to_string(); + + container(row![ + text(week), + Space::with_width(2.), + text(time_info), + Space::with_width(2.), + text(dateday) + ]) + .center_x() + .center_y() + .height(Length::Fill) + .into() + } fn update_hidden_notification(&mut self) { let mut hiddened: Vec = self .notifications @@ -491,6 +513,7 @@ enum Message { RequestPause, RequestPlay, RequestDBusInfoUpdate, + RequestUpdateTime, UpdateBalance, DBusInfoUpdate(Option), BalanceChanged(u8), @@ -556,7 +579,9 @@ impl LalaMusicBar { toggle_launcher, Space::with_width(Length::Fill), container(sound_slider).width(700.), - Space::with_width(Length::Fixed(10.)), + Space::with_width(Length::Fixed(3.)), + self.date_widget(), + Space::with_width(Length::Fixed(3.)), button(text(panel_text)).on_press(Message::ToggleRightPanel) ] .spacing(10); @@ -648,7 +673,9 @@ impl LalaMusicBar { Space::with_width(Length::Fill), buttons, sound_slider, - Space::with_width(Length::Fixed(10.)), + Space::with_width(Length::Fixed(3.)), + self.date_widget(), + Space::with_width(Length::Fixed(3.)), button(text(panel_text)).on_press(Message::ToggleRightPanel) ] .spacing(10) @@ -659,7 +686,9 @@ impl LalaMusicBar { Space::with_width(Length::Fill), buttons, sound_slider, - Space::with_width(Length::Fixed(10.)), + Space::with_width(Length::Fixed(3.)), + self.date_widget(), + Space::with_width(Length::Fixed(3.)), button(text(panel_text)).on_press(Message::ToggleRightPanel) ] .spacing(10) @@ -700,6 +729,7 @@ impl MultiApplication for LalaMusicBar { sender, receiver: Arc::new(Mutex::new(receiver)), quite_mode: false, + datetime: Utc::now(), }, Command::perform(get_metadata_initial(), Message::DBusInfoUpdate), ) @@ -1174,6 +1204,9 @@ impl MultiApplication for LalaMusicBar { Message::CloseErrorNotification(id) => { return Command::single(Action::Window(WindowAction::Close(id))); } + Message::RequestUpdateTime => { + self.datetime = Utc::now(); + } } Command::none() } @@ -1241,6 +1274,8 @@ impl MultiApplication for LalaMusicBar { iced::subscription::Subscription::batch([ iced::time::every(std::time::Duration::from_secs(1)) .map(|_| Message::RequestDBusInfoUpdate), + iced::time::every(std::time::Duration::from_secs(60)) + .map(|_| Message::RequestUpdateTime), iced::time::every(std::time::Duration::from_secs(5)).map(|_| Message::UpdateBalance), iced::event::listen() .map(|event| Message::LauncherInfo(LaunchMessage::IcedEvent(event))),