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::app::{message, Command, Core, Settings};
use cosmic::{ use cosmic::{
executor, executor,
iced::{self, alignment, Length}, iced::{self, alignment, futures::SinkExt, subscription, Length},
style, widget, Element, style, widget, Element,
}; };
use greetd_ipc::{codec::SyncCodec, AuthMessageType, Request, Response}; use greetd_ipc::{codec::SyncCodec, AuthMessageType, Request, Response};
use std::{collections::HashMap, env, fs, io, path::Path, process, sync::Arc}; 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>> { 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). // 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) .content_fit(iced::ContentFit::Cover)
.into() .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;
}
},
)
}
} }

View file

@ -122,7 +122,13 @@ where
viewport: &Rectangle, viewport: &Rectangle,
) { ) {
match &self.image_opt { 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 => {} None => {}
} }

View file

@ -9,12 +9,7 @@ use cosmic::{
event::wayland::{Event as WaylandEvent, OutputEvent, SessionLockEvent}, event::wayland::{Event as WaylandEvent, OutputEvent, SessionLockEvent},
futures::{self, SinkExt}, futures::{self, SinkExt},
subscription, subscription,
wayland::session_lock::{ wayland::session_lock::{destroy_lock_surface, get_lock_surface, lock, unlock},
get_lock_surface,
lock,
destroy_lock_surface,
unlock,
},
Length, Subscription, Length, Subscription,
}, },
iced_runtime::core::window::Id as SurfaceId, iced_runtime::core::window::Id as SurfaceId,
@ -594,13 +589,13 @@ impl cosmic::Application for App {
.into() .into()
} }
//TODO: subscription for date/time
fn subscription(&self) -> Subscription<Self::Message> { fn subscription(&self) -> Subscription<Self::Message> {
if self.exited { if self.exited {
return Subscription::none(); return Subscription::none();
} }
struct SomeWorker; struct HeartbeatSubscription;
struct PamSubscription;
//TODO: how to avoid cloning this on every time subscription is called? //TODO: how to avoid cloning this on every time subscription is called?
let username = self.flags.current_user.name.clone(); let username = self.flags.current_user.name.clone();
@ -618,7 +613,19 @@ impl cosmic::Application for App {
_ => None, _ => None,
}), }),
subscription::channel( subscription::channel(
std::any::TypeId::of::<SomeWorker>(), 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;
}
},
),
subscription::channel(
std::any::TypeId::of::<PamSubscription>(),
16, 16,
|mut msg_tx| async move { |mut msg_tx| async move {
loop { loop {