cosmic-applets/cosmic-app-list/src/toplevel_subscription.rs
2023-10-11 19:46:07 -04:00

89 lines
2.8 KiB
Rust

//! # DBus interface proxy for: `org.freedesktop.UPower.KbdBacklight`
//!
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
//! Source: `Interface '/org/freedesktop/UPower/KbdBacklight' from service 'org.freedesktop.UPower' on system bus`.
use cctk::sctk::reexports::{calloop, client::protocol::wl_seat::WlSeat};
use cctk::toplevel_info::ToplevelInfo;
use cosmic::iced;
use cosmic::iced::subscription;
use cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1;
use futures::{
channel::mpsc::{unbounded, UnboundedReceiver},
SinkExt, StreamExt,
};
use std::{fmt::Debug, hash::Hash, thread::JoinHandle};
use crate::toplevel_handler::toplevel_handler;
pub fn toplevel_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,
) -> iced::Subscription<ToplevelUpdate> {
subscription::channel(id, 50, move |mut output| async move {
let mut state = State::Ready;
loop {
state = start_listening(state, &mut output).await;
}
})
}
pub enum State {
Ready,
Waiting(
UnboundedReceiver<ToplevelUpdate>,
calloop::channel::Sender<ToplevelRequest>,
JoinHandle<()>,
),
Finished,
}
async fn start_listening(
state: State,
output: &mut futures::channel::mpsc::Sender<ToplevelUpdate>,
) -> State {
match state {
State::Ready => {
let (calloop_tx, calloop_rx) = calloop::channel::channel();
let (toplevel_tx, toplevel_rx) = unbounded();
let handle = std::thread::spawn(move || {
toplevel_handler(toplevel_tx, calloop_rx);
});
let tx = calloop_tx.clone();
_ = output.send(ToplevelUpdate::Init(tx)).await;
State::Waiting(toplevel_rx, calloop_tx, handle)
}
State::Waiting(mut rx, tx, handle) => {
if handle.is_finished() {
_ = output.send(ToplevelUpdate::Finished).await;
return State::Finished;
}
match rx.next().await {
Some(u) => {
_ = output.send(u).await;
State::Waiting(rx, tx, handle)
}
None => {
_ = output.send(ToplevelUpdate::Finished).await;
return State::Finished;
}
}
}
State::Finished => iced::futures::future::pending().await,
}
}
#[derive(Clone, Debug)]
pub enum ToplevelUpdate {
Finished,
AddToplevel(ZcosmicToplevelHandleV1, ToplevelInfo),
UpdateToplevel(ZcosmicToplevelHandleV1, ToplevelInfo),
RemoveToplevel(ZcosmicToplevelHandleV1),
Init(calloop::channel::Sender<ToplevelRequest>),
}
#[derive(Debug, Clone)]
pub enum ToplevelRequest {
Activate(ZcosmicToplevelHandleV1),
Quit(ZcosmicToplevelHandleV1),
Exit,
}