2022-12-14 16:51:22 -05:00
|
|
|
use iced::{
|
|
|
|
|
futures::{
|
|
|
|
|
channel::mpsc::{unbounded, UnboundedReceiver},
|
|
|
|
|
StreamExt,
|
|
|
|
|
},
|
|
|
|
|
subscription, Rectangle,
|
|
|
|
|
};
|
|
|
|
|
use std::{fmt::Debug, hash::Hash, collections::HashMap};
|
|
|
|
|
|
|
|
|
|
use super::RectangleTracker;
|
|
|
|
|
|
|
|
|
|
pub fn rectangle_tracker_subscription<
|
|
|
|
|
I: 'static + Hash + Copy + Send + Sync + Debug,
|
|
|
|
|
R: 'static + Hash + Copy + Send + Sync + Debug + Eq,
|
|
|
|
|
>(
|
|
|
|
|
id: I,
|
|
|
|
|
) -> iced::Subscription<(I, RectangleUpdate<R>)> {
|
|
|
|
|
subscription::unfold(id, State::Ready, move |state| start_listening(id, state))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub enum State<I> {
|
|
|
|
|
Ready,
|
|
|
|
|
Waiting(UnboundedReceiver<(I, Rectangle)>, HashMap<I, Rectangle>),
|
|
|
|
|
Finished,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn start_listening<I: Copy, R: 'static + Hash + Copy + Send + Sync + Debug + Eq>(
|
|
|
|
|
id: I,
|
|
|
|
|
state: State<R>,
|
|
|
|
|
) -> (Option<(I, RectangleUpdate<R>)>, State<R>) {
|
|
|
|
|
match state {
|
|
|
|
|
State::Ready => {
|
|
|
|
|
let (tx, rx) = unbounded();
|
|
|
|
|
|
2022-12-23 15:10:13 +01:00
|
|
|
(
|
2022-12-14 16:51:22 -05:00
|
|
|
Some((id, RectangleUpdate::Init(RectangleTracker { tx }))),
|
|
|
|
|
State::Waiting(rx, HashMap::new()),
|
2022-12-23 15:10:13 +01:00
|
|
|
)
|
2022-12-14 16:51:22 -05:00
|
|
|
}
|
|
|
|
|
State::Waiting(mut rx, mut map) => match rx.next().await {
|
2022-12-23 15:10:13 +01:00
|
|
|
Some(u) =>
|
2022-12-14 16:51:22 -05:00
|
|
|
{
|
|
|
|
|
if let Some(prev) = map.get(&u.0) {
|
|
|
|
|
let new = u.1;
|
|
|
|
|
if prev.width != new.width || prev.height != new.height || prev.x != new.x || prev.y != new.y {
|
|
|
|
|
map.insert(u.0, new);
|
|
|
|
|
return (
|
|
|
|
|
Some((id, RectangleUpdate::Rectangle(u))),
|
|
|
|
|
State::Waiting(rx, map),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
map.insert(u.0, u.1);
|
|
|
|
|
return (
|
|
|
|
|
Some((id, RectangleUpdate::Rectangle(u))),
|
|
|
|
|
State::Waiting(rx, map),
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-12-23 15:10:13 +01:00
|
|
|
(None, State::Waiting(rx, map))
|
|
|
|
|
|
2022-12-14 16:51:22 -05:00
|
|
|
},
|
|
|
|
|
None => (None, State::Finished),
|
|
|
|
|
},
|
|
|
|
|
State::Finished => iced::futures::future::pending().await,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub enum RectangleUpdate<I>
|
|
|
|
|
where
|
|
|
|
|
I: 'static + Hash + Copy + Send + Sync + Debug,
|
|
|
|
|
{
|
|
|
|
|
Rectangle((I, Rectangle)),
|
|
|
|
|
Init(RectangleTracker<I>),
|
|
|
|
|
}
|