elio 1.1.0

Snappy, batteries-included terminal file manager with rich previews, inline images, bulk actions, and trash support.
Documentation
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
use super::*;

#[derive(Clone, Copy)]
pub(in crate::app) struct WheelTuning {
    queue_limit: isize,
    medium_threshold: u8,
    fast_threshold: u8,
    medium_divisor: isize,
    fast_divisor: isize,
}

pub(in crate::app) const ENTRY_WHEEL_TUNING: WheelTuning = WheelTuning {
    queue_limit: WHEEL_SCROLL_QUEUE_LIMIT,
    medium_threshold: 4,
    fast_threshold: 8,
    medium_divisor: 2,
    fast_divisor: 3,
};
const ENTRY_HORIZONTAL_WHEEL_TUNING: WheelTuning = WheelTuning {
    queue_limit: WHEEL_SCROLL_QUEUE_LIMIT_HORIZONTAL,
    medium_threshold: 2,
    fast_threshold: 4,
    medium_divisor: 2,
    fast_divisor: 3,
};
const PREVIEW_WHEEL_TUNING: WheelTuning = WheelTuning {
    queue_limit: WHEEL_SCROLL_QUEUE_LIMIT,
    medium_threshold: 4,
    fast_threshold: 8,
    medium_divisor: 2,
    fast_divisor: 4,
};
const PREVIEW_HORIZONTAL_WHEEL_TUNING: WheelTuning = WheelTuning {
    queue_limit: WHEEL_SCROLL_QUEUE_LIMIT_PREVIEW_HORIZONTAL,
    medium_threshold: 2,
    fast_threshold: 4,
    medium_divisor: 2,
    fast_divisor: 3,
};
const SEARCH_WHEEL_TUNING: WheelTuning = WheelTuning {
    queue_limit: WHEEL_SCROLL_QUEUE_LIMIT_SEARCH,
    medium_threshold: 2,
    fast_threshold: 4,
    medium_divisor: 2,
    fast_divisor: 3,
};
const HIGH_FREQUENCY_ENTRY_WHEEL_TUNING: WheelTuning = WheelTuning {
    queue_limit: 16,
    medium_threshold: 4,
    fast_threshold: 8,
    medium_divisor: 1,
    fast_divisor: 1,
};
const HIGH_FREQUENCY_ENTRY_HORIZONTAL_WHEEL_TUNING: WheelTuning = WheelTuning {
    queue_limit: 2,
    medium_threshold: 2,
    fast_threshold: 3,
    medium_divisor: 3,
    fast_divisor: 5,
};

impl App {
    pub fn process_pending_scroll(&mut self) -> bool {
        let mut dirty = false;

        if self.overlays.search.is_some() {
            self.input.wheel_scroll.vertical.pending = 0;
            self.input.wheel_scroll.horizontal.pending = 0;
            self.input.wheel_scroll.preview.pending = 0;
            self.input.wheel_scroll.preview_horizontal.pending = 0;
            dirty |= self.flush_search_scroll();
        } else {
            self.input.wheel_scroll.search.pending = 0;
            dirty |= self.flush_entry_vertical_scroll();
            dirty |= self.flush_preview_scroll();
            dirty |= self.flush_preview_horizontal_scroll();
            if self.navigation.view_mode == ViewMode::Grid {
                dirty |= self.flush_entry_horizontal_scroll();
            } else {
                self.input.wheel_scroll.horizontal.pending = 0;
            }
        }

        dirty
    }

    pub fn has_pending_scroll(&self) -> bool {
        self.input.wheel_scroll.vertical.pending != 0
            || self.input.wheel_scroll.horizontal.pending != 0
            || self.input.wheel_scroll.preview.pending != 0
            || self.input.wheel_scroll.preview_horizontal.pending != 0
            || self.input.wheel_scroll.search.pending != 0
    }

    pub(in crate::app) fn handle_wheel_event(&mut self, mouse: MouseEvent, delta: isize) {
        // In HighFrequency mode (Alacritty, Ghostty, VTE), scroll event coordinates can be
        // unreliable. hover_panel is tracked exclusively from MouseEventKind::Moved events
        // (via ?1003h any-event tracking), which always carry the true cursor position.
        // Use it as the primary routing source; fall back to scroll event coords, then auto-focus.
        let target = if self.input.wheel_profile == WheelProfile::HighFrequency {
            self.input
                .hover_panel
                .or_else(|| self.resolve_wheel_target(mouse.column, mouse.row))
                .or_else(|| self.preview_auto_focus_target(false))
        } else {
            self.resolve_wheel_target(mouse.column, mouse.row)
                .or_else(|| self.preview_auto_focus_target(false))
        };
        match target {
            Some(WheelTarget::Preview) => {
                self.focus_preview_scroll();
                if self.pdf_page_wheel_navigation_active() && self.step_pdf_page(delta) {
                    return;
                }
                if self.comic_page_wheel_navigation_active()
                    && self.step_comic_page_with_preview_mode(delta, PreviewRefreshMode::Deferred)
                {
                    return;
                }
                if self.step_epub_section_from_preview_wheel(delta) {
                    return;
                }
                if mouse.modifiers.contains(KeyModifiers::SHIFT)
                    && self.preview_allows_horizontal_scroll()
                {
                    if self.input.wheel_profile == WheelProfile::HighFrequency {
                        let _ = self.scroll_preview_columns_immediately(delta);
                    } else {
                        Self::queue_scroll(
                            &mut self.input.wheel_scroll.preview_horizontal,
                            delta,
                            PREVIEW_HORIZONTAL_WHEEL_TUNING,
                        );
                    }
                } else if self.input.wheel_profile == WheelProfile::HighFrequency {
                    let _ = self.scroll_preview_immediately(delta);
                } else {
                    Self::queue_scroll(
                        &mut self.input.wheel_scroll.preview,
                        delta,
                        PREVIEW_WHEEL_TUNING,
                    );
                }
            }
            Some(WheelTarget::Entries) | None => {
                self.focus_entry_scroll();
                if self.navigation.view_mode == ViewMode::Grid
                    && mouse.modifiers.contains(KeyModifiers::SHIFT)
                {
                    let tuning = self.entry_horizontal_wheel_tuning();
                    Self::queue_scroll(&mut self.input.wheel_scroll.horizontal, delta, tuning);
                } else if self.input.wheel_profile == WheelProfile::HighFrequency {
                    let _ = self.scroll_entry_immediately(delta);
                } else {
                    let tuning = self.entry_wheel_tuning();
                    Self::queue_scroll(&mut self.input.wheel_scroll.vertical, delta, tuning);
                }
            }
        }
    }

    pub(in crate::app) fn handle_horizontal_wheel_event(
        &mut self,
        mouse: MouseEvent,
        delta: isize,
    ) {
        let target = if self.input.wheel_profile == WheelProfile::HighFrequency {
            self.input
                .hover_panel
                .or_else(|| self.resolve_wheel_target(mouse.column, mouse.row))
                .or_else(|| self.preview_auto_focus_target(true))
        } else {
            self.resolve_wheel_target(mouse.column, mouse.row)
                .or_else(|| self.preview_auto_focus_target(true))
        };
        match target {
            Some(WheelTarget::Preview) => {
                self.focus_preview_scroll();
                if self.preview_allows_horizontal_scroll() {
                    if self.input.wheel_profile == WheelProfile::HighFrequency {
                        let _ = self.scroll_preview_columns_immediately(delta);
                    } else {
                        Self::queue_scroll(
                            &mut self.input.wheel_scroll.preview_horizontal,
                            delta,
                            PREVIEW_HORIZONTAL_WHEEL_TUNING,
                        );
                    }
                }
            }
            Some(WheelTarget::Entries) | None => {
                if self.navigation.view_mode != ViewMode::Grid {
                    return;
                }
                self.focus_entry_scroll();
                let tuning = self.entry_horizontal_wheel_tuning();
                Self::queue_scroll(&mut self.input.wheel_scroll.horizontal, delta, tuning);
            }
        }
    }

    pub(in crate::app) fn queue_search_wheel(&mut self, delta: isize) {
        Self::queue_scroll(
            &mut self.input.wheel_scroll.search,
            delta,
            SEARCH_WHEEL_TUNING,
        );
    }

    pub(in crate::app) fn queue_scroll(lane: &mut ScrollLane, delta: isize, tuning: WheelTuning) {
        let burst_count = Self::register_scroll_input(lane, delta);

        let divisor = if burst_count >= tuning.fast_threshold {
            tuning.fast_divisor
        } else if burst_count >= tuning.medium_threshold {
            tuning.medium_divisor
        } else {
            1
        };

        if divisor <= 1 {
            lane.pending = (lane.pending + delta).clamp(-tuning.queue_limit, tuning.queue_limit);
            return;
        }

        lane.remainder += delta;
        while lane.remainder.abs() >= divisor {
            let step = lane.remainder.signum();
            lane.pending = (lane.pending + step).clamp(-tuning.queue_limit, tuning.queue_limit);
            lane.remainder -= step * divisor;
        }
    }

    fn register_scroll_input(lane: &mut ScrollLane, delta: isize) -> u8 {
        let now = Instant::now();
        let direction = delta.signum();
        let continuing_burst = lane.last_input_direction == direction
            && lane
                .last_input_at
                .is_some_and(|at| now.duration_since(at) <= WHEEL_SCROLL_BURST_WINDOW);

        if continuing_burst {
            lane.burst_count = lane.burst_count.saturating_add(1);
        } else {
            lane.remainder = 0;
            lane.burst_count = 1;
        }
        lane.last_input_at = Some(now);
        lane.last_input_direction = direction;
        lane.burst_count
    }

    fn consume_scroll_step(lane: &mut ScrollLane, cooldown: Duration) -> Option<isize> {
        let now = Instant::now();
        if lane.pending == 0 {
            return None;
        }
        if lane
            .last_step_at
            .is_some_and(|at| now.duration_since(at) < cooldown)
        {
            return None;
        }

        let step = lane.pending.signum();
        lane.pending -= step;
        lane.last_step_at = Some(now);
        Some(step)
    }

    fn focus_preview_scroll(&mut self) {
        self.input.last_wheel_target = Some(WheelTarget::Preview);
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.vertical);
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.horizontal);
    }

    fn focus_entry_scroll(&mut self) {
        self.input.last_wheel_target = Some(WheelTarget::Entries);
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.preview);
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.preview_horizontal);
    }

    fn reset_scroll_lane(lane: &mut ScrollLane) {
        lane.pending = 0;
        lane.remainder = 0;
        lane.last_step_at = None;
        lane.last_input_at = None;
        lane.last_input_direction = 0;
        lane.burst_count = 0;
    }

    fn flush_entry_vertical_scroll(&mut self) -> bool {
        let interval = self.entry_scroll_interval();
        let Some(step) = Self::consume_scroll_step(&mut self.input.wheel_scroll.vertical, interval)
        else {
            return false;
        };

        let mut dirty = false;
        let total_steps =
            self.entry_vertical_steps_per_flush(self.input.wheel_scroll.vertical.pending.abs() + 1);
        for step_index in 0..total_steps {
            if step_index > 0 {
                if self.input.wheel_scroll.vertical.pending.signum() != step {
                    break;
                }
                self.input.wheel_scroll.vertical.pending -= step;
            }

            let previous = self.navigation.selected;
            self.move_vertical_with_preview_mode(step, self.entry_wheel_preview_mode());
            dirty |= previous != self.navigation.selected;
        }
        dirty
    }

    fn flush_entry_horizontal_scroll(&mut self) -> bool {
        let Some(step) = Self::consume_scroll_step(
            &mut self.input.wheel_scroll.horizontal,
            WHEEL_SCROLL_INTERVAL_HORIZONTAL,
        ) else {
            return false;
        };

        let previous = self.navigation.selected;
        self.move_by(step);
        previous != self.navigation.selected
    }

    fn scroll_entry_immediately(&mut self, delta: isize) -> bool {
        let burst_count = Self::register_scroll_input(&mut self.input.wheel_scroll.vertical, delta);
        self.input.browser_wheel_post_burst_pending = true;
        self.input.wheel_scroll.vertical.pending = 0;
        self.input.wheel_scroll.vertical.remainder = 0;
        let step = self.high_frequency_entry_step_multiplier(burst_count);
        let preview_mode = if burst_count <= 1 {
            self.entry_wheel_preview_mode()
        } else {
            PreviewRefreshMode::Deferred
        };
        let previous = self.navigation.selected;
        self.move_vertical_with_preview_mode(delta * step, preview_mode);
        previous != self.navigation.selected
    }

    fn entry_wheel_preview_mode(&self) -> PreviewRefreshMode {
        if self.needs_slow_sixel_navigation_workaround() {
            PreviewRefreshMode::Deferred
        } else {
            PreviewRefreshMode::Immediate
        }
    }

    fn flush_search_scroll(&mut self) -> bool {
        let Some(step) = Self::consume_scroll_step(
            &mut self.input.wheel_scroll.search,
            WHEEL_SCROLL_INTERVAL_SEARCH,
        ) else {
            return false;
        };

        let previous = self
            .overlays
            .search
            .as_ref()
            .map(|search| search.selected)
            .unwrap_or(0);
        self.move_search_selection(step);
        self.overlays
            .search
            .as_ref()
            .map(|search| search.selected != previous)
            .unwrap_or(false)
    }

    fn flush_preview_scroll(&mut self) -> bool {
        let Some(step) = Self::consume_scroll_step(
            &mut self.input.wheel_scroll.preview,
            WHEEL_SCROLL_INTERVAL_PREVIEW,
        ) else {
            return false;
        };
        let mut dirty = self.scroll_preview_lines(step);
        if self.input.wheel_scroll.preview.pending.signum() == step {
            self.input.wheel_scroll.preview.pending -= step;
            dirty |= self.scroll_preview_lines(step);
        }
        dirty
    }

    fn flush_preview_horizontal_scroll(&mut self) -> bool {
        let Some(step) = Self::consume_scroll_step(
            &mut self.input.wheel_scroll.preview_horizontal,
            WHEEL_SCROLL_INTERVAL_PREVIEW_HORIZONTAL,
        ) else {
            return false;
        };
        let mut dirty = self.scroll_preview_columns(step);
        if self.input.wheel_scroll.preview_horizontal.pending.signum() == step {
            self.input.wheel_scroll.preview_horizontal.pending -= step;
            dirty |= self.scroll_preview_columns(step);
        }
        dirty
    }

    fn preview_scroll_step(&self) -> usize {
        self.input
            .frame_state
            .preview_rows_visible
            .saturating_div(6)
            .clamp(2, 4)
    }

    fn preview_horizontal_scroll_step(&self) -> usize {
        self.input
            .frame_state
            .preview_cols_visible
            .saturating_div(8)
            .clamp(2, 6)
    }

    pub(in crate::app) fn sync_preview_scroll(&mut self) -> bool {
        let previous = self.preview.state.scroll;
        let previous_horizontal = self.preview.state.horizontal_scroll;
        let visible_rows = self.input.frame_state.preview_rows_visible;
        let visible_cols = self.input.frame_state.preview_cols_visible;
        let max_scroll = self
            .preview_total_lines(visible_cols)
            .saturating_sub(visible_rows.max(1));
        self.preview.state.scroll = self.preview.state.scroll.min(max_scroll);
        let max_horizontal = self.preview_max_horizontal_scroll(visible_cols);
        self.preview.state.horizontal_scroll =
            self.preview.state.horizontal_scroll.min(max_horizontal);
        // When scroll is clamped at the rendered boundary, fire an extension.
        self.maybe_request_code_preview_extension();
        previous != self.preview.state.scroll
            || previous_horizontal != self.preview.state.horizontal_scroll
    }

    /// Fire an incremental extension job when the user has scrolled close
    /// enough to the bottom of the currently-rendered partial preview.
    fn maybe_request_code_preview_extension(&mut self) {
        if !self.preview.state.content.is_incrementally_partial() {
            return;
        }
        if self.preview.state.incremental_render_in_flight {
            return;
        }
        let Some(render_limit) = self.preview.state.content.incremental_render_limit else {
            return;
        };
        let scroll = self.preview.state.scroll;
        let visible_rows = self.input.frame_state.preview_rows_visible;
        let bottom_edge = scroll.saturating_add(visible_rows);
        if bottom_edge + INCREMENTAL_RENDER_LOOKAHEAD < render_limit {
            return;
        }
        // Build and submit the extension request.
        let Some(entry) = self.selected_entry().cloned() else {
            return;
        };
        let variant = self.current_preview_request_options();
        let Some(request) = self.build_code_preview_extension_request(
            entry.clone(),
            variant,
            PreviewPriority::High,
        ) else {
            return;
        };
        let entry_path = entry.path.clone();
        if self.jobs.scheduler.submit_preview(request) {
            self.preview.state.incremental_render_in_flight = true;
            self.preview.state.incremental_render_path = Some(entry_path);
        }
    }

    pub(in crate::app) fn clear_wheel_scroll(&mut self) {
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.vertical);
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.horizontal);
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.preview);
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.preview_horizontal);
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.search);
        self.input.browser_wheel_post_burst_pending = false;
    }

    fn entry_wheel_tuning(&self) -> WheelTuning {
        match self.input.wheel_profile {
            WheelProfile::Default => ENTRY_WHEEL_TUNING,
            WheelProfile::HighFrequency => HIGH_FREQUENCY_ENTRY_WHEEL_TUNING,
        }
    }

    fn entry_horizontal_wheel_tuning(&self) -> WheelTuning {
        match self.input.wheel_profile {
            WheelProfile::Default => ENTRY_HORIZONTAL_WHEEL_TUNING,
            WheelProfile::HighFrequency => HIGH_FREQUENCY_ENTRY_HORIZONTAL_WHEEL_TUNING,
        }
    }

    fn entry_scroll_interval(&self) -> Duration {
        match self.input.wheel_profile {
            WheelProfile::Default => WHEEL_SCROLL_INTERVAL_VERTICAL,
            WheelProfile::HighFrequency => WHEEL_SCROLL_INTERVAL_VERTICAL_HIGH_FREQUENCY,
        }
    }

    fn entry_vertical_steps_per_flush(&self, pending: isize) -> usize {
        if self.input.wheel_profile != WheelProfile::HighFrequency {
            return 1;
        }

        match pending.abs() {
            0..=2 => 1,
            3..=5 => 2,
            6..=10 => 3,
            _ => 4,
        }
    }

    fn high_frequency_entry_step_multiplier(&self, burst_count: u8) -> isize {
        let _ = burst_count;
        1
    }

    pub(in crate::app) fn handle_horizontal_navigation_key(&mut self, delta: isize) -> bool {
        if self.input.last_wheel_target == Some(WheelTarget::Preview) {
            if self.input.wheel_profile == WheelProfile::HighFrequency {
                let _ = self.scroll_preview_columns(delta);
                return true;
            }
            if self.preview_allows_horizontal_scroll()
                && self.preview_max_horizontal_scroll(
                    self.input.frame_state.preview_cols_visible.max(1),
                ) > 0
            {
                return self.scroll_preview_columns(delta);
            }
            self.input.last_wheel_target = Some(WheelTarget::Entries);
        }

        if self.input.wheel_profile == WheelProfile::HighFrequency
            && self.preview_auto_focus_target(true) == Some(WheelTarget::Preview)
            && self.preview_allows_horizontal_scroll()
        {
            self.input.last_wheel_target = Some(WheelTarget::Preview);
            let _ = self.scroll_preview_columns(delta);
            return true;
        }

        if self.input.wheel_profile == WheelProfile::HighFrequency
            && self.navigation.view_mode == ViewMode::Grid
        {
            self.input.last_wheel_target = Some(WheelTarget::Entries);
            self.focus_entry_scroll();
            let tuning = self.entry_horizontal_wheel_tuning();
            Self::queue_scroll(&mut self.input.wheel_scroll.horizontal, delta, tuning);
            return true;
        }

        false
    }

    fn pdf_page_wheel_navigation_active(&self) -> bool {
        self.preview_prefers_pdf_surface()
            || (self.preview_uses_image_overlay() && self.pdf_preview_header_detail().is_some())
    }

    fn comic_page_wheel_navigation_active(&self) -> bool {
        self.comic_preview_wheel_capture_active()
    }

    fn step_epub_section_from_preview_wheel(&mut self, delta: isize) -> bool {
        if !self.epub_section_wheel_navigation_active(delta) {
            return false;
        }

        Self::reset_scroll_lane(&mut self.input.wheel_scroll.preview);
        Self::reset_scroll_lane(&mut self.input.wheel_scroll.preview_horizontal);
        self.step_epub_section(delta)
    }

    fn epub_section_wheel_navigation_active(&self, delta: isize) -> bool {
        if delta == 0 || !self.epub_preview_wheel_capture_active() {
            return false;
        }
        if self.current_epub_section_uses_full_height_image() {
            return true;
        }

        let visible_cols = self.input.frame_state.preview_cols_visible.max(1);
        let visible_rows = self.input.frame_state.preview_rows_visible.max(1);
        let total_lines = self.preview_total_lines(visible_cols);
        if total_lines <= visible_rows {
            return true;
        }

        let max_scroll = total_lines.saturating_sub(visible_rows);
        if delta.is_negative() {
            self.preview.state.scroll == 0
        } else {
            self.preview.state.scroll >= max_scroll
        }
    }

    fn current_epub_section_uses_full_height_image(&self) -> bool {
        self.preview
            .state
            .content
            .preview_visual
            .as_ref()
            .is_some_and(|visual| {
                visual.kind == preview::PreviewVisualKind::PageImage
                    && visual.layout == preview::PreviewVisualLayout::FullHeight
            })
    }

    fn preview_has_vertical_overflow(&self) -> bool {
        let visible_cols = self.input.frame_state.preview_cols_visible.max(1);
        let visible_rows = self.input.frame_state.preview_rows_visible.max(1);
        self.preview_total_lines(visible_cols) > visible_rows
    }

    fn preview_auto_focus_ready(&self) -> bool {
        self.preview_has_vertical_overflow()
            && self.input.last_selection_change_at.elapsed() >= PREVIEW_AUTO_FOCUS_DELAY
    }

    fn preview_horizontal_auto_focus_ready(&self) -> bool {
        self.preview_allows_horizontal_scroll()
            && self
                .preview_max_horizontal_scroll(self.input.frame_state.preview_cols_visible.max(1))
                > 0
            && self.input.last_selection_change_at.elapsed() >= PREVIEW_AUTO_FOCUS_DELAY
    }

    fn preview_auto_focus_target(&self, horizontal: bool) -> Option<WheelTarget> {
        // Fallback only: routes to preview when cursor is outside both panels and the
        // preview has scrollable content. Does NOT use last_wheel_target stickiness —
        // cursor position (via resolve_wheel_target) is always consulted first, so this
        // only fires when the cursor is genuinely ambiguous (e.g. in sidebar/toolbar).
        if self.input.wheel_profile != WheelProfile::HighFrequency {
            return None;
        }

        let preview_ready = if horizontal {
            self.preview_horizontal_auto_focus_ready()
        } else {
            self.preview_auto_focus_ready()
        };

        preview_ready.then_some(WheelTarget::Preview)
    }

    fn scroll_preview_lines(&mut self, delta: isize) -> bool {
        let previous = self.preview.state.scroll;
        let step = self.preview_scroll_step();
        if delta.is_negative() {
            self.preview.state.scroll = self
                .preview
                .state
                .scroll
                .saturating_sub(step.saturating_mul(delta.unsigned_abs()));
        } else {
            self.preview.state.scroll = self
                .preview
                .state
                .scroll
                .saturating_add(step.saturating_mul(delta as usize));
        }
        // sync_preview_scroll already calls maybe_request_code_preview_extension.
        self.sync_preview_scroll();
        previous != self.preview.state.scroll
    }

    pub(in crate::app) fn scroll_preview_columns(&mut self, delta: isize) -> bool {
        let previous = self.preview.state.horizontal_scroll;
        let step = self.preview_horizontal_scroll_step();
        if delta.is_negative() {
            self.preview.state.horizontal_scroll = self
                .preview
                .state
                .horizontal_scroll
                .saturating_sub(step.saturating_mul(delta.unsigned_abs()));
        } else {
            self.preview.state.horizontal_scroll = self
                .preview
                .state
                .horizontal_scroll
                .saturating_add(step.saturating_mul(delta as usize));
        }
        self.sync_preview_scroll();
        previous != self.preview.state.horizontal_scroll
    }

    // Scroll preview immediately without queuing, mirroring scroll_entry_immediately.
    // Used in HighFrequency mode (Alacritty, Ghostty, VTE/Gnome Terminal) where the
    // terminal sends many raw wheel events. The queue system causes lag and stalls in
    // these terminals because it caps pending steps and applies burst throttle divisors.
    fn scroll_preview_immediately(&mut self, delta: isize) -> bool {
        self.input.wheel_scroll.preview.pending = 0;
        self.input.wheel_scroll.preview.remainder = 0;
        self.scroll_preview_lines(delta)
    }

    fn scroll_preview_columns_immediately(&mut self, delta: isize) -> bool {
        self.input.wheel_scroll.preview_horizontal.pending = 0;
        self.input.wheel_scroll.preview_horizontal.remainder = 0;
        self.scroll_preview_columns(delta)
    }
}