cosmic-applets/cosmic-applet-workspaces/src/wayland_subscription.rs
Ashley Wulber 9ebd9b511a
wip: update libcosmic (#93)
* wip: update libcosmic

* fix: damge issue resolved by updating iced

* fix: high cpu usage by time applet and app-list

* refactor subscriptions to produce fewer events

* refactor network applet to use less cpu

* fix: text size

* refactor: i18n for audio applet

* refactor: power applet i18n setup

* fix (battery): always send profile update

* fix (battery): set toggler width to layout correctly

* fix (app-list): backoff for restarts of toplevel subscription

* fix (network): alignment

* feat: ask for comfirmation before applying power applet actions

* wip: integrate cosmic-config

* update zbus

* feat: update to use latest libcosmic

* update iced

* udpate deps

* update deps

* refactor: move applet helpers to this repo, outside of libcosmic.

this should help alleviate some dependency hell

* chore update deps

* update deps

* cleanup
2023-06-01 12:23:12 -04:00

79 lines
2.2 KiB
Rust

use crate::wayland::{self, WorkspaceEvent, WorkspaceList};
use calloop::channel::SyncSender;
use futures::{channel::mpsc, StreamExt};
use std::hash::Hash;
#[derive(Debug, Clone)]
pub enum WorkspacesUpdate {
Workspaces(WorkspaceList),
Started(SyncSender<WorkspaceEvent>),
Errored,
}
pub fn workspaces<I: 'static + Hash + Copy + Send + Sync>(
id: I,
) -> cosmic::iced::Subscription<(I, WorkspacesUpdate)> {
use cosmic::iced::subscription;
subscription::unfold(id, State::Ready, move |state| _workspaces(id, state))
}
async fn _workspaces<I: Copy>(id: I, mut state: State) -> ((I, WorkspacesUpdate), State) {
loop {
let (update, new_state) = match state {
State::Ready => {
if let Ok(watcher) = WorkspacesWatcher::new() {
(
Some((id, WorkspacesUpdate::Started(watcher.get_sender()))),
State::Waiting(watcher),
)
} else {
(Some((id, WorkspacesUpdate::Errored)), State::Error)
}
}
State::Waiting(mut t) => {
if let Some(w) = t.workspaces().await {
(
Some((id, WorkspacesUpdate::Workspaces(w))),
State::Waiting(t),
)
} else {
(Some((id, WorkspacesUpdate::Errored)), State::Error)
}
}
State::Error => cosmic::iced::futures::future::pending().await,
};
state = new_state;
if let Some(update) = update {
return (update, state);
}
}
}
pub enum State {
Ready,
Waiting(WorkspacesWatcher),
Error,
}
pub struct WorkspacesWatcher {
rx: mpsc::Receiver<WorkspaceList>,
tx: SyncSender<WorkspaceEvent>,
}
impl WorkspacesWatcher {
pub fn new() -> anyhow::Result<Self> {
let (tx, rx) = mpsc::channel(20);
let tx = wayland::spawn_workspaces(tx);
Ok(Self { tx, rx })
}
pub fn get_sender(&self) -> SyncSender<WorkspaceEvent> {
self.tx.clone()
}
pub async fn workspaces(&mut self) -> Option<WorkspaceList> {
self.rx.next().await
}
}