agg_gui/widgets/text_area/scroll.rs
1//! Internal vertical scrolling for [`TextArea`], split out of `text_area.rs`
2//! to keep that file under the 800-line cap.
3//!
4//! Rather than embedding a `ScrollView`, `TextArea` composes the same
5//! [`ScrollbarAxis`](crate::widgets::scrollbar::ScrollbarAxis) primitive that
6//! `ScrollView` uses. That keeps the overflow math, thumb geometry, drag/hover
7//! interaction and painting pixel-consistent with every other scroll bar in the
8//! app, and lets the widget read the global scroll style/visibility so the
9//! Appearance demo's live tweaks apply here too.
10//!
11//! The scroll offset (`vbar.offset`) is folded into
12//! [`TextArea::content_top_y`], so all existing geometry (paint, hit-test,
13//! cursor overlay) inherits scrolling for free. `0` shows the first line;
14//! larger offsets reveal lower lines.
15//!
16//! The vertical bar is a *floating overlay* (it does not shrink the text wrap
17//! width). This matches the default [`ScrollBarKind::Floating`] and avoids a
18//! wrap/overflow feedback loop; the thin dormant bar rarely occludes glyphs and
19//! grows only while hovered/dragged.
20
21use super::*;
22use crate::geometry::Point;
23use crate::widgets::scroll_view::{
24 current_scroll_style, current_scroll_visibility, ScrollBarStyle, ScrollBarVisibility,
25};
26use crate::widgets::scrollbar::{
27 paint_prepared_scrollbar, ScrollbarGeometry, ScrollbarOrientation, DEFAULT_GRAB_MARGIN,
28};
29
30/// Outcome of routing a `MouseMove` through the scroll bar first.
31pub(crate) enum ScrollMove {
32 /// The bar thumb is being dragged; `bool` = offset changed this move.
33 Dragging(bool),
34 /// Not dragging; `bool` = the bar's hover state changed this move.
35 Hover(bool),
36}
37
38impl TextArea {
39 /// Inner content height (widget height minus vertical padding).
40 pub(crate) fn inner_height(&self) -> f64 {
41 (self.bounds.height - self.padding * 2.0).max(0.0)
42 }
43
44 /// Total wrapped-content height at the current line count.
45 pub(crate) fn content_height(&self) -> f64 {
46 self.cached_lines.len() as f64 * self.cached_line_h
47 }
48
49 /// Furthest the content can scroll (0 when everything fits).
50 pub(crate) fn max_scroll_y(&self) -> f64 {
51 (self.content_height() - self.inner_height()).max(0.0)
52 }
53
54 /// Current vertical scroll offset (0 = top). Exposed for tests/inspectors.
55 pub fn scroll_offset(&self) -> f64 {
56 self.vbar.offset
57 }
58
59 /// Number of whole visual lines that fit in the viewport — the distance
60 /// PageUp/PageDown move the caret. Always at least 1 so the caret advances
61 /// even in a viewport shorter than one line.
62 pub(crate) fn page_lines(&self) -> usize {
63 if self.cached_line_h <= 0.0 {
64 return 1;
65 }
66 ((self.inner_height() / self.cached_line_h).floor() as usize).max(1)
67 }
68
69 fn scroll_style(&self) -> ScrollBarStyle {
70 current_scroll_style()
71 }
72 fn scroll_visibility(&self) -> ScrollBarVisibility {
73 current_scroll_visibility()
74 }
75
76 /// Refresh the axis with the current content height and re-clamp the offset.
77 /// Called each layout after the wrap cache is rebuilt.
78 pub(crate) fn sync_scroll(&mut self) {
79 self.vbar.enabled = true;
80 self.vbar.content = self.content_height();
81 self.vbar.clamp_offset(self.inner_height());
82 }
83
84 /// Vertical track geometry in widget-local Y-up coords. The bar hugs the
85 /// right inner edge; the track spans the padded inner height.
86 fn v_scrollbar_geometry(&self) -> ScrollbarGeometry {
87 ScrollbarGeometry {
88 orientation: ScrollbarOrientation::Vertical,
89 track_start: self.padding,
90 track_end: (self.bounds.height - self.padding).max(self.padding),
91 cross_end: (self.bounds.width - 2.0).max(0.0),
92 hit_margin: DEFAULT_GRAB_MARGIN,
93 }
94 }
95
96 /// Scroll so the caret's visual line is fully visible. Re-wraps first (the
97 /// cache may be dirty after an edit) so the line index and content height
98 /// are current. Called after every cursor-affecting key.
99 pub(crate) fn ensure_cursor_visible(&mut self) {
100 let inner_w = self.inner_width();
101 self.refresh_wrap(inner_w);
102 let line_h = self.cached_line_h;
103 if line_h <= 0.0 {
104 return;
105 }
106 let inner_h = self.inner_height();
107 let cursor = self.edit.borrow().cursor;
108 let line = self.line_for_cursor(cursor);
109 // Line `i` occupies content-space [i·h, (i+1)·h], measured downward from
110 // the content top. The visible window is [offset, offset + inner_h].
111 let top = line as f64 * line_h;
112 let bottom = top + line_h;
113 let mut off = self.vbar.offset;
114 if top < off {
115 off = top; // caret above the window → scroll up to its line
116 } else if bottom > off + inner_h {
117 off = bottom - inner_h; // caret below → scroll down just enough
118 }
119 self.vbar.offset = off.clamp(0.0, self.max_scroll_y());
120 }
121
122 /// Wheel handler. Positive `delta_y` means "see content above" (decrease
123 /// offset), matching the [`Event::MouseWheel`](crate::event::Event) sign
124 /// convention. Returns `true` when the offset actually moved.
125 pub(crate) fn scroll_by_wheel(&mut self, delta_y: f64) -> bool {
126 if self.max_scroll_y() <= 0.0 {
127 return false;
128 }
129 // Windows convention: one wheel notch scrolls 3 lines. `delta_y` arrives
130 // in line-units (1.0 == one notch; the platform adapter pre-scales
131 // trackpad pixel deltas into fractional line-units), so scaling by
132 // 3 * line-height gives a crisp 3-line notch while keeping pixel-precise
133 // trackpad panning smooth. This is more content-aware than ScrollView's
134 // flat 40 px/line, which felt slow for the editors' larger line pitch.
135 let line_h = if self.cached_line_h > 0.0 {
136 self.cached_line_h
137 } else {
138 self.font_size * 1.35
139 };
140 self.vbar
141 .scroll_by(-delta_y * 3.0 * line_h, self.inner_height())
142 }
143
144 /// Begin a thumb drag if `pos` is on the thumb. Returns `true` when a drag
145 /// started (caller must then suppress text selection).
146 pub(crate) fn scrollbar_begin_drag(&mut self, pos: Point) -> bool {
147 if self.max_scroll_y() <= 0.0 {
148 return false;
149 }
150 let geom = self.v_scrollbar_geometry();
151 self.vbar
152 .begin_drag(pos, self.inner_height(), self.scroll_style(), geom)
153 }
154
155 /// Route a `MouseMove` through the bar: continue an in-progress drag, or
156 /// update hover state.
157 pub(crate) fn scrollbar_on_mouse_move(&mut self, pos: Point) -> ScrollMove {
158 let geom = self.v_scrollbar_geometry();
159 let vp = self.inner_height();
160 let style = self.scroll_style();
161 if self.vbar.dragging {
162 ScrollMove::Dragging(self.vbar.drag_to(pos, vp, style, geom))
163 } else {
164 ScrollMove::Hover(self.vbar.update_hover(pos, vp, style, geom))
165 }
166 }
167
168 /// End any thumb drag. Returns `true` if a drag was in progress.
169 pub(crate) fn scrollbar_end_drag(&mut self) -> bool {
170 let was = self.vbar.dragging;
171 self.vbar.dragging = false;
172 was
173 }
174
175 /// Paint the vertical bar (thumb + track) using the global scroll style, so
176 /// it looks identical to `ScrollView`'s bars.
177 pub(crate) fn paint_scrollbar(&mut self, ctx: &mut dyn DrawCtx) {
178 let vp = self.inner_height();
179 let style = self.scroll_style();
180 let vis = self.scroll_visibility();
181 let geom = self.v_scrollbar_geometry();
182 if let Some(bar) = self.vbar.prepare_paint(vp, style, vis, geom) {
183 paint_prepared_scrollbar(ctx, bar);
184 }
185 }
186
187 /// `true` while the bar has a running hover/visibility animation, so the
188 /// widget keeps requesting frames until it settles.
189 pub(crate) fn scrollbar_animating(&self) -> bool {
190 self.vbar.animation_active()
191 }
192}