Skip to main content

egui_kit/history/
memory.rs

1//! [`MemoryHistory`]: a history stack in memory.
2
3use super::{EntryKey, History, HistoryEntry};
4
5/// An in-memory history stack, used on native targets and in tests.
6#[derive(Debug, Clone)]
7pub struct MemoryHistory {
8    entries: Vec<HistoryEntry>,
9    index: usize,
10    next_key: u64,
11    moved: bool,
12}
13
14impl MemoryHistory {
15    pub fn new(initial_url: impl Into<String>) -> Self {
16        Self {
17            entries: vec![HistoryEntry {
18                url: initial_url.into(),
19                key: EntryKey(0),
20            }],
21            index: 0,
22            next_key: 1,
23            moved: false,
24        }
25    }
26
27    pub fn entries(&self) -> &[HistoryEntry] {
28        &self.entries
29    }
30}
31
32impl History for MemoryHistory {
33    fn current(&self) -> HistoryEntry {
34        self.entries[self.index].clone()
35    }
36
37    fn push(&mut self, url: &str) -> EntryKey {
38        let key = EntryKey(self.next_key);
39        self.next_key += 1;
40        self.entries.truncate(self.index + 1);
41        self.entries.push(HistoryEntry {
42            url: url.to_owned(),
43            key,
44        });
45        self.index += 1;
46        key
47    }
48
49    fn replace(&mut self, url: &str) {
50        self.entries[self.index].url = url.to_owned();
51    }
52
53    fn back(&mut self) {
54        if self.can_go_back() {
55            self.index -= 1;
56            self.moved = true;
57        }
58    }
59
60    fn forward(&mut self) {
61        if self.can_go_forward() {
62            self.index += 1;
63            self.moved = true;
64        }
65    }
66
67    fn can_go_back(&self) -> bool {
68        self.index > 0
69    }
70
71    fn can_go_forward(&self) -> bool {
72        self.index + 1 < self.entries.len()
73    }
74
75    fn poll(&mut self, _ctx: &egui::Context) -> Option<HistoryEntry> {
76        core::mem::take(&mut self.moved).then(|| self.current())
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn back_and_forward() {
86        let ctx = egui::Context::default();
87        let mut h = MemoryHistory::new("/");
88        let a = h.push("/a");
89        h.push("/b");
90        h.back();
91        h.back();
92        assert_eq!(h.poll(&ctx).map(|e| e.url).as_deref(), Some("/"));
93        assert_eq!(h.poll(&ctx), None);
94        h.forward();
95        assert_eq!(
96            h.poll(&ctx),
97            Some(HistoryEntry {
98                url: "/a".into(),
99                key: a
100            })
101        );
102        h.replace("/a2");
103        assert_eq!(h.current().key, a);
104        h.push("/c");
105        assert!(!h.can_go_forward());
106        let urls: Vec<_> = h.entries().iter().map(|e| e.url.as_str()).collect();
107        assert_eq!(urls, ["/", "/a2", "/c"]);
108    }
109
110    #[test]
111    fn blocking_changes_nothing() {
112        let ctx = egui::Context::default();
113        let mut h = MemoryHistory::new("/");
114        h.push("/a");
115        let before = h.clone();
116        h.set_blocking(true);
117        h.back();
118        assert_eq!(h.poll(&ctx).map(|e| e.url).as_deref(), Some("/"));
119        h.set_blocking(false);
120        assert_eq!(h.entries(), before.entries());
121    }
122}