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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Screen state and transition logic for the viewer.
//!
//! `Viewport` is the user's interactive view into a document build — a value-typed
//! state machine whose `apply` method is a pure transition function returning
//! `(Self, Vec<RenderOp>)`.
use super::display_state::DisplayState;
use super::effect::{Effect, ExitReason, RenderOp, ScreenRestore, ViewerMode};
use super::layout::ScrollState;
use super::mode_grep::LastSearch;
use super::mode_log::LogState;
use super::mode_url::{self, UrlPickerState};
use super::query::DocumentQuery;
use super::session::JumpEntry;
/// The user's interactive view into a document build.
///
/// Contains all mutable state for the inner event loop: interaction mode,
/// scroll position, tile cache, and transient UI state. Created fresh
/// for each document build; destroyed when the build is replaced.
pub(super) struct Viewport {
pub mode: ViewerMode,
pub scroll: ScrollState,
pub display: DisplayState,
pub flash: Option<String>,
pub dirty: bool,
pub last_search: Option<LastSearch>,
pub highlights_visible: bool,
}
impl Default for Viewport {
fn default() -> Self {
Self {
mode: ViewerMode::Normal,
scroll: ScrollState::new(0, 0, 0, 0, crate::config::ScrollAnimation::default()),
display: DisplayState::new(0),
flash: None,
dirty: false,
last_search: None,
highlights_visible: true,
}
}
}
/// Read-only environment for effect application.
///
/// Bundles references to layout, document content, and navigation state
/// that `Viewport::apply` needs but must not modify.
pub(super) struct ViewContext<'a> {
pub layout: &'a super::layout::Layout,
pub acc_value: Option<u32>,
pub filename: &'a str,
pub jump_stack: &'a [JumpEntry],
pub doc: &'a DocumentQuery<'a>,
pub log_buffer: &'a crate::log::LogBuffer,
}
impl Viewport {
/// Pure state transition. Returns updated viewport and render ops.
///
/// No Result, no &mut self — takes ownership and returns new state.
/// Exit conditions are signaled via `RenderOp::Exit`.
pub(super) fn apply(mut self, effect: Effect, ctx: &ViewContext) -> (Self, Vec<RenderOp>) {
let mut ops = Vec::new();
match effect {
Effect::ScrollImpulse {
delta_px,
direction,
} => {
let now = std::time::Instant::now();
// restart_ramp captures whether the animation was settled
// BEFORE this impulse — used by ExpDecay's ease-in.
let was_settled = !self.scroll.animator.is_animating(
self.scroll.anchor,
&self.scroll.input_history,
now,
);
// Push to history; convolve evicted entries into the
// permanent anchor using their actual contribution at
// this instant (exact for Kinetic, full δ for ExpDecay).
let evicted = self.scroll.input_history.record(direction, delta_px);
for r in &evicted {
self.scroll.anchor += self.scroll.animator.eviction_contribution(r, now);
}
self.scroll.animator.restart_ease_in_if_settled(was_settled);
self.dirty = true;
}
Effect::ScrollAnchor(y) => {
// iOS scroll-to-top semantics: cancel residual momentum,
// pin the anchor to the current sub-pixel position, then
// push a single (y - current) impulse so velocity-based
// animators glide smoothly to y. ExpDecay variants chase
// (anchor + Σδ = y) the same way.
let now = std::time::Instant::now();
let current = self
.scroll
.current_position(now)
.clamp(0.0, self.scroll.img_h as f64);
let was_settled = !self.scroll.animator.is_animating(
self.scroll.anchor,
&self.scroll.input_history,
now,
);
self.scroll.input_history.drain();
self.scroll.anchor = current;
let delta = (y as i32) - (current.round() as i32);
let direction = if delta >= 0 {
super::input_history::ScrollDirection::Down
} else {
super::input_history::ScrollDirection::Up
};
let _ = self.scroll.input_history.record(direction, delta);
self.scroll.animator.restart_ease_in_if_settled(was_settled);
self.dirty = true;
}
Effect::MarkDirty => {
self.dirty = true;
}
Effect::Flash(msg) => {
self.flash = Some(msg);
}
Effect::RedrawStatusBar => {
ops.push(RenderOp::DrawStatusBar);
}
Effect::RedrawGrep => {
if matches!(self.mode, ViewerMode::Grep(_)) {
ops.push(RenderOp::DrawModeScreen);
}
}
Effect::RedrawCommandBar => {
if matches!(self.mode, ViewerMode::Command(_)) {
ops.push(RenderOp::DrawModeScreen);
}
}
Effect::RedrawUrlPicker => {
if matches!(self.mode, ViewerMode::UrlPicker(_)) {
ops.push(RenderOp::DrawModeScreen);
}
}
Effect::RedrawToc => {
if matches!(self.mode, ViewerMode::Toc(_)) {
ops.push(RenderOp::DrawModeScreen);
}
}
Effect::RedrawInlineSearch => {
if matches!(self.mode, ViewerMode::InlineSearch(_)) {
ops.push(RenderOp::DrawStatusBar);
}
}
Effect::RedrawLog => {
if matches!(self.mode, ViewerMode::Log(_)) {
ops.push(RenderOp::DrawModeScreen);
}
}
Effect::Yank(text) => {
ops.push(RenderOp::CopyToClipboard(text));
}
Effect::OpenExternalUrl(url) => {
ops.push(RenderOp::OpenExternalUrl(url));
}
Effect::SetMode(m) => {
match &m {
ViewerMode::InlineSearch(_) => {
ops.push(RenderOp::DrawStatusBar);
}
ViewerMode::Normal => {
unreachable!("use ExitToNormal to return to Normal mode");
}
_ => {
ops.push(RenderOp::DrawModeScreen);
}
}
self.mode = m;
}
Effect::ExitToNormal(restore) => {
match restore {
ScreenRestore::FullRefresh => {
ops.push(RenderOp::ClearScreen);
ops.push(RenderOp::DeleteAllImages);
self.display.clear_all();
}
ScreenRestore::StatusBarRefresh => {
ops.push(RenderOp::DrawStatusBar);
}
}
self.mode = ViewerMode::Normal;
self.dirty = true;
}
Effect::SetLastSearch(ls) => {
self.last_search = Some(ls);
self.highlights_visible = true;
self.display.clear_overlay_state();
ops.push(RenderOp::DeleteOverlayPlacements);
self.dirty = true;
}
Effect::HideHighlights => {
self.highlights_visible = false;
self.display.clear_overlay_state();
ops.push(RenderOp::DeleteOverlayPlacements);
self.dirty = true;
}
Effect::ShowHighlights => {
self.highlights_visible = true;
self.display.clear_overlay_state();
ops.push(RenderOp::DeleteOverlayPlacements);
self.dirty = true;
}
Effect::DeletePlacements => {
ops.push(RenderOp::DeletePlacements);
}
Effect::InvalidateOverlays => {
self.display.clear_overlay_state();
ops.push(RenderOp::DeleteOverlayPlacements);
self.dirty = true;
}
Effect::EnterUrlPickerAll => {
let entries = mode_url::collect_all_url_entries(ctx.doc);
if entries.is_empty() {
self.flash = Some("No URLs in document".into());
if !matches!(self.mode, ViewerMode::Normal) {
ops.push(RenderOp::ClearScreen);
ops.push(RenderOp::DeleteAllImages);
self.display.clear_all();
self.mode = ViewerMode::Normal;
self.dirty = true;
} else {
ops.push(RenderOp::DrawStatusBar);
}
} else {
ops.push(RenderOp::DeletePlacements);
ops.push(RenderOp::ClearScreen);
// NO DeleteAllImages — original code doesn't call it here
let up = UrlPickerState::new(entries);
self.mode = ViewerMode::UrlPicker(up);
ops.push(RenderOp::DrawModeScreen);
}
}
Effect::EnterLog => {
let ls = LogState::new(ctx.log_buffer);
self.mode = ViewerMode::Log(ls);
ops.push(RenderOp::DrawModeScreen);
}
Effect::GoBack => {
if ctx.jump_stack.is_empty() {
self.flash = Some("No previous file".into());
ops.push(RenderOp::DrawStatusBar);
} else {
ops.push(RenderOp::Exit(ExitReason::GoBack));
}
}
Effect::ToggleWatch => {
// Handled in mod.rs effect loop (needs Session access)
}
Effect::Exit(reason) => {
ops.push(RenderOp::Exit(reason));
}
}
(self, ops)
}
}