Add subscription for updating time

This commit is contained in:
Jeremy Soller 2023-11-21 15:48:22 -07:00
parent 0e7964bcad
commit 49ee2c63b7
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
3 changed files with 42 additions and 12 deletions

View file

@ -4,12 +4,12 @@
use cosmic::app::{message, Command, Core, Settings};
use cosmic::{
executor,
iced::{self, alignment, Length},
iced::{self, alignment, futures::SinkExt, subscription, Length},
style, widget, Element,
};
use greetd_ipc::{codec::SyncCodec, AuthMessageType, Request, Response};
use std::{collections::HashMap, env, fs, io, path::Path, process, sync::Arc};
use tokio::net::UnixStream;
use tokio::{net::UnixStream, time};
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
// The pwd::Passwd method is unsafe (but not labelled as such) due to using global state (libc pwent functions).
@ -660,4 +660,21 @@ impl cosmic::Application for App {
.content_fit(iced::ContentFit::Cover)
.into()
}
fn subscription(&self) -> subscription::Subscription<Self::Message> {
struct HeartbeatSubscription;
subscription::channel(
std::any::TypeId::of::<HeartbeatSubscription>(),
16,
|mut msg_tx| async move {
loop {
// Send heartbeat once a second to update time
//TODO: only send this when needed
msg_tx.send(Message::None).await.unwrap();
time::sleep(time::Duration::new(1, 0)).await;
}
},
)
}
}