From 49ee2c63b73eda5382565399e746c3d884826f8b Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 21 Nov 2023 15:48:22 -0700 Subject: [PATCH] Add subscription for updating time --- src/greeter.rs | 21 +++++++++++++++++++-- src/image_container.rs | 8 +++++++- src/locker.rs | 25 ++++++++++++++++--------- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/greeter.rs b/src/greeter.rs index 0f61c06..a0b89d6 100644 --- a/src/greeter.rs +++ b/src/greeter.rs @@ -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> { // 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 { + struct HeartbeatSubscription; + + subscription::channel( + std::any::TypeId::of::(), + 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; + } + }, + ) + } } diff --git a/src/image_container.rs b/src/image_container.rs index 769a202..5cec593 100644 --- a/src/image_container.rs +++ b/src/image_container.rs @@ -122,7 +122,13 @@ where viewport: &Rectangle, ) { match &self.image_opt { - Some(image) => draw(renderer, layout, image, self.content_fit, [0.0, 0.0, 0.0, 0.0]), + Some(image) => draw( + renderer, + layout, + image, + self.content_fit, + [0.0, 0.0, 0.0, 0.0], + ), None => {} } diff --git a/src/locker.rs b/src/locker.rs index 8c45594..06ceb34 100644 --- a/src/locker.rs +++ b/src/locker.rs @@ -9,12 +9,7 @@ use cosmic::{ event::wayland::{Event as WaylandEvent, OutputEvent, SessionLockEvent}, futures::{self, SinkExt}, subscription, - wayland::session_lock::{ - get_lock_surface, - lock, - destroy_lock_surface, - unlock, - }, + wayland::session_lock::{destroy_lock_surface, get_lock_surface, lock, unlock}, Length, Subscription, }, iced_runtime::core::window::Id as SurfaceId, @@ -594,13 +589,13 @@ impl cosmic::Application for App { .into() } - //TODO: subscription for date/time fn subscription(&self) -> Subscription { if self.exited { return Subscription::none(); } - struct SomeWorker; + struct HeartbeatSubscription; + struct PamSubscription; //TODO: how to avoid cloning this on every time subscription is called? let username = self.flags.current_user.name.clone(); @@ -618,7 +613,19 @@ impl cosmic::Application for App { _ => None, }), subscription::channel( - std::any::TypeId::of::(), + std::any::TypeId::of::(), + 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; + } + }, + ), + subscription::channel( + std::any::TypeId::of::(), 16, |mut msg_tx| async move { loop {