Skip to content

Commit

Permalink
clock
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
  • Loading branch information
ryanabx committed Aug 22, 2024
1 parent 2b9cfdf commit c400649
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 5 deletions.
44 changes: 44 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 19 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,13 +31,15 @@ fn main() -> Result<(), iced::Error> {
struct Panel<'a> {
_panel_config: PanelConfig,
app_tray: AppTray<'a>,
settings_tray: SettingsTray,
}

impl<'a> Default for Panel<'a> {
fn default() -> Self {
Self {
_panel_config: PanelConfig::default(),
app_tray: AppTray::new(AppTrayConfig::default()),
settings_tray: SettingsTray::new(),
}
}
}
Expand All @@ -45,6 +48,7 @@ impl<'a> Default for Panel<'a> {
pub enum Message {
Panic,
AppTray(AppTrayMessage),
SettingsTray(SettingsTrayMessage),
}

impl<'a> Application for Panel<'a> {
Expand All @@ -63,6 +67,7 @@ impl<'a> Application for Panel<'a> {
}

fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
println!("Received message: {:?}", message);
match message {
Message::Panic => {
panic!("Panic button pressed hehe");
Expand All @@ -71,14 +76,21 @@ 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),
}
}

fn view(
&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,
Expand All @@ -94,7 +106,10 @@ impl<'a> Application for Panel<'a> {
}

fn subscription(&self) -> iced::Subscription<Self::Message> {
self.app_tray.subscription().map(Message::AppTray)
Subscription::batch(vec![
self.settings_tray.subscription().map(Message::SettingsTray),
self.app_tray.subscription().map(Message::AppTray),
])
}
}

Expand Down
94 changes: 94 additions & 0 deletions src/settings_tray/clock_subscription.rs
Original file line number Diff line number Diff line change
@@ -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<ClockMessage> {
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<ClockMessage> {
iced::widget::container(column![iced::widget::text!("{}", self.time).style(
|theme| {
Style {
color: Some(Color::WHITE),
}
}
)])
.into()
}

pub fn subscription(&self) -> iced::Subscription<ClockMessage> {
struct ClockWorker;
iced::subscription::channel(
std::any::TypeId::of::<ClockWorker>(),
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);
}
};
}
},
)
}
}
43 changes: 42 additions & 1 deletion src/settings_tray/mod.rs
Original file line number Diff line number Diff line change
@@ -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<SettingsTrayMessage> {
match message {
SettingsTrayMessage::Clock(clock_msg) => self
.clock
.handle_message(clock_msg)
.map(SettingsTrayMessage::Clock),
}
}

pub fn view(&self) -> iced::Element<SettingsTrayMessage> {
iced::widget::container(row![self.clock.view().map(SettingsTrayMessage::Clock)]).into()
}

pub fn subscription(&self) -> iced::Subscription<SettingsTrayMessage> {
self.clock.subscription().map(SettingsTrayMessage::Clock)
}
}

0 comments on commit c400649

Please sign in to comment.