use std::any::Any;
use std::sync::{Arc, Mutex, PoisonError};
use iced_core::widget::operation::{Focusable, Outcome, Scrollable, TextInput};
use iced_core::widget::{Id, Operation};
use iced_core::{Rectangle, Vector, window};
use icedwright_protocol::{RawTree, WindowId};
use crate::RawRecorder;
pub type Recording = (RawTree, Vec<(window::Id, WindowId)>);
pub type Slot = Arc<Mutex<Option<Recording>>>;
#[derive(Debug)]
pub struct Collect {
recorder: Mutex<Option<RawRecorder>>,
slot: Slot,
}
impl Collect {
#[must_use]
pub fn new(slot: Slot) -> Self {
Self {
recorder: Mutex::new(Some(RawRecorder::new())),
slot,
}
}
fn with(&mut self, f: impl FnOnce(&mut RawRecorder)) {
let recorder = self
.recorder
.get_mut()
.unwrap_or_else(PoisonError::into_inner);
if let Some(recorder) = recorder.as_mut() {
f(recorder);
}
}
}
pub fn take(slot: &Slot) -> Option<Recording> {
slot.lock().unwrap_or_else(PoisonError::into_inner).take()
}
impl Operation for Collect {
fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation)) {
self.with(|recorder| recorder.traverse(operate));
}
fn container(&mut self, id: Option<&Id>, bounds: Rectangle) {
self.with(|recorder| recorder.container(id, bounds));
}
fn scrollable(
&mut self,
id: Option<&Id>,
bounds: Rectangle,
content_bounds: Rectangle,
translation: Vector,
state: &mut dyn Scrollable,
) {
self.with(|recorder| recorder.scrollable(id, bounds, content_bounds, translation, state));
}
fn focusable(&mut self, id: Option<&Id>, bounds: Rectangle, state: &mut dyn Focusable) {
self.with(|recorder| recorder.focusable(id, bounds, state));
}
fn text_input(&mut self, id: Option<&Id>, bounds: Rectangle, state: &mut dyn TextInput) {
self.with(|recorder| recorder.text_input(id, bounds, state));
}
fn text(&mut self, id: Option<&Id>, bounds: Rectangle, text: &str) {
self.with(|recorder| recorder.text(id, bounds, text));
}
fn custom(&mut self, id: Option<&Id>, bounds: Rectangle, state: &mut dyn Any) {
self.with(|recorder| recorder.custom(id, bounds, state));
}
fn finish(&self) -> Outcome<()> {
let recorder = self
.recorder
.lock()
.unwrap_or_else(PoisonError::into_inner)
.take();
let Some(recorder) = recorder else {
return Outcome::None;
};
let ids: Vec<_> = recorder
.window_ids()
.iter()
.map(|(window, id)| (*window, *id))
.collect();
let tree = recorder.into_tree();
*self.slot.lock().unwrap_or_else(PoisonError::into_inner) = Some((tree, ids));
Outcome::Some(())
}
}
#[cfg(test)]
mod tests {
use iced_core::widget::operation::Outcome;
use super::*;
use crate::WindowMarker;
#[test]
fn two_windows_with_markers_give_two_recordings_in_the_slot() {
let slot: Slot = Arc::default();
let mut collect = Collect::new(Arc::clone(&slot));
let first = window::Id::unique();
let second = window::Id::unique();
let bounds = Rectangle::new(iced_core::Point::ORIGIN, iced_core::Size::new(10.0, 10.0));
collect.custom(None, bounds, &mut WindowMarker(first));
collect.text(None, bounds, "one");
collect.custom(None, bounds, &mut WindowMarker(second));
collect.text(None, bounds, "two");
assert!(matches!(collect.finish(), Outcome::Some(())));
let Some((tree, ids)) = take(&slot) else {
panic!("nothing recorded");
};
assert_eq!(tree.windows.len(), 2);
let mut ids = ids;
ids.sort_by_key(|(_, id)| *id);
assert_eq!(ids, vec![(first, WindowId(0)), (second, WindowId(1))]);
assert!(matches!(collect.finish(), Outcome::None));
}
}