-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
- Loading branch information
Showing
5 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} | ||
}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |