anathema_runtime/runtime/
testing.rs

1use std::time::{Duration, Instant};
2
3use anathema_backend::Backend;
4use anathema_state::{Watched, Watcher, drain_watchers};
5use anathema_store::stack::Stack;
6use anathema_widgets::query::Children;
7
8use crate::Frame;
9use crate::error::Result;
10use crate::events::GlobalEventHandler;
11
12// -----------------------------------------------------------------------------
13//   - Used with test driver -
14//   These functions should not be used outside of testing
15// -----------------------------------------------------------------------------
16impl<'bp, G> Frame<'_, 'bp, G>
17where
18    G: GlobalEventHandler,
19{
20    pub fn components(&mut self) -> anathema_widgets::query::Components<'_, '_, 'bp> {
21        panic!()
22        // anathema_widgets::query::Components::new(
23        //     self.tree.view_mut(),
24        //     self.layout_ctx.attribute_storage,
25        //     self.layout_ctx.dirty_widgets,
26        // )
27    }
28
29    pub fn elements(&mut self) -> Children<'_, 'bp> {
30        Children::new(
31            self.tree.view_mut(),
32            self.layout_ctx.attribute_storage,
33            &mut self.needs_layout,
34        )
35    }
36
37    // TODO: this can't really be called a frame if we can tick it multiple
38    // times. Maybe RuntimeMut or something less mental
39    pub fn wait_for_monitor<B: Backend>(
40        &mut self,
41        backend: &mut B,
42        watcher: Watcher,
43        timeout: Duration,
44    ) -> Result<Watched> {
45        let now = Instant::now();
46
47        let mut watchers = Stack::empty();
48        drain_watchers(&mut watchers);
49
50        if watchers.contains(&watcher) {
51            return Ok(Watched::Triggered);
52        }
53
54        loop {
55            let dur = self.tick(backend)?;
56            self.present(backend);
57            self.cleanup();
58
59            drain_watchers(&mut watchers);
60
61            if watchers.contains(&watcher) {
62                return Ok(Watched::Triggered);
63            }
64
65            if timeout.saturating_sub(now.elapsed()).is_zero() {
66                break Ok(Watched::Timeout);
67            }
68
69            let sleep = self.sleep_micros - dur.as_micros() as u64;
70            std::thread::sleep(Duration::from_micros(sleep));
71        }
72    }
73}