1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! In-memory scroll-position memory for recently visited notes.
//!
//! Remembers the scroll offset of the last few notes the user left, so
//! navigating back to one — via a link or the picker, not just the back/forward
//! history — resumes where they were instead of jumping to the top. This is
//! deliberately not persisted: it only needs to survive within a session.
/// How many notes' scroll positions are retained.
const CAPACITY: usize = 10;
#[derive(Default)]
pub struct ScrollMemory {
/// (note name, scroll position), most-recently-remembered first.
entries: Vec<(String, i32)>,
}
impl ScrollMemory {
pub fn new() -> Self {
Self::default()
}
/// Record `pos` for `note`, promoting it to most-recent and evicting the
/// least-recently-remembered note once more than [`CAPACITY`] are tracked.
pub fn remember(&mut self, note: &str, pos: i32) {
self.entries.retain(|(name, _)| name != note);
self.entries.insert(0, (note.to_string(), pos));
self.entries.truncate(CAPACITY);
}
/// The remembered scroll position for `note`, if it is still tracked.
pub fn get(&self, note: &str) -> Option<i32> {
self.entries
.iter()
.find(|(name, _)| name == note)
.map(|(_, pos)| *pos)
}
/// Rename a tracked note in place (used when a note is renamed), preserving
/// its remembered position and recency. No-op if `old` is not tracked.
pub fn rename(&mut self, old: &str, new: &str) {
if let Some((name, _)) = self.entries.iter_mut().find(|(name, _)| name == old) {
*name = new.to_string();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn remembers_and_returns_position() {
let mut m = ScrollMemory::new();
assert_eq!(m.get("a"), None);
m.remember("a", 42);
assert_eq!(m.get("a"), Some(42));
}
#[test]
fn updates_existing_position() {
let mut m = ScrollMemory::new();
m.remember("a", 10);
m.remember("a", 99);
assert_eq!(m.get("a"), Some(99));
}
#[test]
fn evicts_least_recently_remembered_beyond_capacity() {
let mut m = ScrollMemory::new();
for i in 0..CAPACITY {
m.remember(&format!("p{i}"), i as i32);
}
// All CAPACITY notes are still tracked.
assert_eq!(m.get("p0"), Some(0));
// One more evicts the oldest ("p0"); the newest survives.
m.remember("new", 123);
assert_eq!(m.get("p0"), None);
assert_eq!(m.get("new"), Some(123));
assert_eq!(m.get("p1"), Some(1));
}
#[test]
fn rename_preserves_position() {
let mut m = ScrollMemory::new();
m.remember("old", 42);
m.rename("old", "new");
assert_eq!(m.get("old"), None);
assert_eq!(m.get("new"), Some(42));
}
#[test]
fn rename_unknown_note_is_noop() {
let mut m = ScrollMemory::new();
m.remember("a", 1);
m.rename("missing", "new");
assert_eq!(m.get("new"), None);
assert_eq!(m.get("a"), Some(1));
}
#[test]
fn re_remembering_refreshes_recency() {
let mut m = ScrollMemory::new();
for i in 0..CAPACITY {
m.remember(&format!("p{i}"), i as i32);
}
// Touch the oldest so it is no longer the eviction candidate.
m.remember("p0", 7);
m.remember("new", 1);
assert_eq!(m.get("p0"), Some(7)); // survived
assert_eq!(m.get("p1"), None); // evicted instead
}
}