feat: dock settings pages

This commit is contained in:
Ashley Wulber 2023-09-11 17:15:59 -04:00 committed by Ashley Wulber
parent 1c6ed73165
commit 87de348d86
11 changed files with 901 additions and 553 deletions

View file

@ -1,4 +1,7 @@
use cosmic::iced::subscription;
use cosmic::{
iced::subscription,
iced_futures::futures::{self, SinkExt},
};
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::fmt::Debug;
use std::hash::Hash;
@ -21,19 +24,20 @@ pub enum DesktopFileEvent {
pub fn desktop_files<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,
) -> cosmic::iced::Subscription<(I, DesktopFileEvent)> {
subscription::unfold(id, State::Ready, move |mut state| async move {
) -> cosmic::iced::Subscription<DesktopFileEvent> {
subscription::channel(id, 50, move |mut output| async move {
let mut state = State::Ready;
loop {
let (event, new_state) = start_watching(id, state).await;
state = new_state;
if let Some(event) = event {
return (event, state);
}
state = start_watching(state, &mut output).await;
}
})
}
async fn start_watching<I: Copy>(id: I, state: State) -> (Option<(I, DesktopFileEvent)>, State) {
async fn start_watching(
state: State,
output: &mut futures::channel::mpsc::Sender<DesktopFileEvent>,
) -> State {
match state {
State::Ready => {
let paths = freedesktop_desktop_entry::default_paths();
@ -42,22 +46,19 @@ async fn start_watching<I: Copy>(id: I, state: State) -> (Option<(I, DesktopFile
for path in paths {
let _ = watcher.watch(path.as_ref(), RecursiveMode::Recursive);
}
(
Some((id, DesktopFileEvent::Changed)),
State::Waiting { watcher, rx },
)
_ = output.send(DesktopFileEvent::Changed).await;
State::Waiting { watcher, rx }
} else {
(None, State::Finished)
State::Finished
}
}
State::Waiting { watcher, rx } => {
if let Some(rx) = async_watch(rx).await {
(
Some((id, DesktopFileEvent::Changed)),
State::Waiting { watcher, rx },
)
_ = output.send(DesktopFileEvent::Changed).await;
State::Waiting { watcher, rx }
} else {
(None, State::Finished)
State::Finished
}
}
State::Finished => cosmic::iced::futures::future::pending().await,