Enable text selection and mouse scrolling in the warning viewer (#57271)

## What changed

- Allow selecting warning text while preserving wrapped URLs and hard line breaks when copying.
- Add mouse-wheel scrolling within the warning body and keep page navigation working after selection.
- Use the shared menu surface and show configured scroll hints only for directions with more content.
- Preserve painted backgrounds when rendering text selections.

## Testing

Add coverage for warning selection without changing the draft, paging after selection, wheel scrolling and directional hints, and selection boundaries in scrolled text containing tabs and wrapped words. Update warning panel snapshots.

GitOrigin-RevId: c4b77277a60b7b4af92043cb3761769ddc636454
This commit is contained in:
Ian MacLeod
2026-10-26 21:36:05 +00:00
committed by copyberry
parent 51e3f94b2b
commit 7077b1edb9
23 changed files with 308 additions and 87 deletions
+28 -4
View File
@@ -41,7 +41,7 @@ use logical::logical_lines;
use ratatui::text::Line;
/// One cell's logical text and its width-dependent display rows.
pub(super) struct TextLayout {
pub(crate) struct TextLayout {
pub(super) image: Option<std::sync::Arc<crate::transcript_image::TranscriptImage>>,
logical: Vec<LogicalLine>,
text: String,
@@ -127,11 +127,35 @@ impl TextLayout {
ranges
}
pub(super) fn new(lines: Vec<HyperlinkLine>, width: u16) -> Self {
pub(crate) fn new(lines: Vec<HyperlinkLine>, width: u16) -> Self {
let logical = logical_lines(&lines);
Self::from_logical(logical, width)
}
/// Freeze the exact painted rows of a plain-text viewport, including tab stops.
/// Trim the selectable source so word and keyboard selection stay inside the viewport.
pub(crate) fn visible_plaintext(mut self, start: usize, height: u16) -> Self {
if start == 0 && self.rows.len() <= usize::from(height) {
return self;
}
self.rows.truncate(start + usize::from(height));
self.rows.drain(..start.min(self.rows.len()));
let from = self.rows.first().map_or(0, |row| row.source.start);
let to = self.rows.last().map_or(from, |row| row.source.end);
self.text = self.text[from..to].to_owned();
let lines = self
.text
.split('\n')
.map(|line| line.to_owned().into())
.collect::<Vec<_>>();
self.logical = logical_lines(&lines);
for row in &mut self.rows {
row.source = row.source.start - from..row.source.end - from;
row.line_end = row.line_end.map(|end| end.min(to) - from);
}
self
}
/// Reflow the same displayed content revision, retaining styles, links and disclosure label.
pub(super) fn rewrap(&self, width: u16) -> Self {
let mut layout = Self::from_logical(self.logical.clone(), width);
@@ -292,7 +316,7 @@ impl TextLayout {
&self.text
}
pub(super) fn row_count(&self) -> usize {
pub(crate) fn row_count(&self) -> usize {
// Images occupy visual space, but have no searchable or selectable text rows.
self.text_row_count()
+ self
@@ -327,7 +351,7 @@ impl TextLayout {
}
/// Paint visible rows, including inherited styles on empty rows and trailing columns.
pub(super) fn render(&self, area: Rect, buf: &mut Buffer, start_row: usize) {
pub(crate) fn render(&self, area: Rect, buf: &mut Buffer, start_row: usize) {
for (screen_row, row) in self.visible_rows(area, start_row) {
let mut row_area =
Rect::new(area.x, area.y + screen_row, area.width, /*height*/ 1);
@@ -61,6 +61,31 @@ fn copying_wrapped_text_preserves_only_hard_newlines() {
assert_eq!(view.selected_text(&cells).as_deref(), Some(expected.text()));
}
#[test]
fn scrolled_plaintext_selection_matches_painted_tab_and_word_boundaries() {
let layout = TextLayout::new(
vec![
"hidden".into(),
" A\tBCD".into(),
"These few words will unfortunately not wrap nicely.".into(),
],
/*width*/ 15,
);
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 15, /*height*/ 3,
);
let mut buffer = Buffer::empty(area);
layout.render(area, &mut buffer, /*start_row*/ 1);
assert_eq!(buffer[(8, 0)].symbol(), "B");
assert_eq!(buffer[(0, 2)].symbol(), "w");
let visible = layout.visible_plaintext(/*start*/ 1, area.height);
let start = visible.position_at(/*row*/ 0, /*column*/ 2);
let end = visible.position_at(/*row*/ 2, /*column*/ 4);
assert_eq!(&visible.text()[start..end], "A\tBCD\nThese few words will");
assert_eq!(visible.text(), " A\tBCD\nThese few words will");
}
#[test]
fn hit_testing_uses_graphemes_and_does_not_select_padding() {
let layout = TextLayout::new(vec!["a界e\u{301}👩‍💻".into()], /*width*/ 12);