tui: Reuse widget state in diff and files
90c19c26cfaffde21b4f34eae7b4e49530807aa1
Use shared Badge, ActionBar, ListState, and Viewport plumbing so the diff and file-browser surfaces lean on tui widgets instead of carrying bespoke state and render glue. Assisted-by: Codex:gpt-5.5
1 parent
82c3f69f
crates/diff/src/viewer.rs
+39 -33
| 8 | 8 | use std::path::{Path, PathBuf}; |
|
| 9 | 9 | ||
| 10 | 10 | use rustik_highlight as syntax; |
|
| 11 | 11 | use rustik_tui as tui; |
|
| 12 | 12 | use tui::{ |
|
| 13 | - | Buffer, Cell, Color, Constraint, Control, Event, HStack, Key, Label, Line, Mouse, MouseButton, |
|
| 14 | - | Placement, Rect, Scroll, ScrollView, ScrollbarStyle, Size, Style, Table, TableOptions, Text, |
|
| 15 | - | VStack, View, Viewport, Widget, colors, |
|
| 13 | + | Badge, Buffer, Cell, Color, Constraint, Control, Event, HStack, Key, Label, Line, Mouse, |
|
| 14 | + | MouseButton, Placement, Rect, Scroll, ScrollView, ScrollbarStyle, Size, Style, Table, |
|
| 15 | + | TableOptions, Text, VStack, View, Viewport, Widget, colors, |
|
| 16 | 16 | }; |
|
| 17 | 17 | ||
| 18 | 18 | use crate::context::{self, ContextChange, ContextState}; |
|
| 19 | 19 | use crate::highlight::{DiffHighlighter, HighlightKeyInput, HighlightResult}; |
|
| 20 | 20 | use crate::word; |
| 77 | 77 | const EMPTY_CARD_ROWS: usize = FILE_DIVIDER_ROW + 1; |
|
| 78 | 78 | ||
| 79 | 79 | /// Mutable read-only diff viewport state. |
|
| 80 | 80 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
|
| 81 | 81 | pub struct DiffState { |
|
| 82 | - | /// First visible row in the rendered document. |
|
| 83 | - | pub scroll: usize, |
|
| 82 | + | /// Vertical viewport over the rendered document. |
|
| 83 | + | pub scroll: Viewport, |
|
| 84 | 84 | /// Currently focused file or hunk. |
|
| 85 | 85 | pub focus: Option<DiffFocus>, |
|
| 86 | 86 | /// Collapsed file sections. |
|
| 87 | 87 | pub collapsed_files: BTreeSet<FileId>, |
|
| 88 | 88 | } |
| 538 | 538 | .collect() |
|
| 539 | 539 | } |
|
| 540 | 540 | ||
| 541 | 541 | /// Sets the first visible rendered row. |
|
| 542 | 542 | pub fn set_scroll(&mut self, scroll: usize) { |
|
| 543 | - | self.state.scroll = scroll; |
|
| 543 | + | self.state.scroll = Viewport::from_offset(scroll); |
|
| 544 | 544 | } |
|
| 545 | 545 | ||
| 546 | 546 | /// Sets the focused file or hunk. |
|
| 547 | 547 | pub fn set_focus(&mut self, focus: Option<DiffFocus>) { |
|
| 548 | 548 | self.state.focus = focus; |
| 567 | 567 | } |
|
| 568 | 568 | } |
|
| 569 | 569 | ||
| 570 | 570 | /// Returns the file occupying the first visible diff row. |
|
| 571 | 571 | fn visible_file(&self) -> Option<FileId> { |
|
| 572 | - | self.file_row_location(self.state.scroll) |
|
| 572 | + | self.file_row_location(self.state.scroll.offset) |
|
| 573 | 573 | .map(|location| location.file.id) |
|
| 574 | 574 | .or_else(|| self.document.files.first().map(|file| file.id)) |
|
| 575 | 575 | } |
|
| 576 | 576 | ||
| 577 | 577 | /// Returns rendered rows plus trailing space needed to top-anchor the final file. |
| 596 | 596 | (candidate.id == file).then_some(indexed.start) |
|
| 597 | 597 | }) else { |
|
| 598 | 598 | return; |
|
| 599 | 599 | }; |
|
| 600 | 600 | self.state.focus = Some(DiffFocus::File(file)); |
|
| 601 | - | self.state.scroll = View::new(row, self.scroll_rows(visible_rows), visible_rows).offset; |
|
| 601 | + | self.state |
|
| 602 | + | .scroll |
|
| 603 | + | .set_offset(row, self.scroll_rows(visible_rows), visible_rows); |
|
| 602 | 604 | } |
|
| 603 | 605 | ||
| 604 | 606 | /// Updates the file-tree cursor to the visible diff file. |
|
| 605 | 607 | fn sync_file_tree(&mut self) { |
|
| 606 | 608 | if let Some(file) = self.visible_file() { |
| 661 | 663 | self.preamble = Self::preamble(self.document.commit.as_ref(), body_cols); |
|
| 662 | 664 | ||
| 663 | 665 | if self.preamble.len() != preamble_rows { |
|
| 664 | 666 | self.rebuild_index(); |
|
| 665 | 667 | } |
|
| 666 | - | let scroll = self.state.scroll; |
|
| 668 | + | let scroll = self.state.scroll.offset; |
|
| 667 | 669 | let sticky = self.sticky_rows(scroll, area.rows, body_cols); |
|
| 668 | 670 | // Body offset skips sticky rows so the main diff starts below the fixed |
|
| 669 | 671 | // header strip. |
|
| 670 | 672 | let body_offset = scroll.saturating_add(sticky.len().min(area.rows)); |
|
| 671 | 673 | let render_rows = self.render_rows_for(scroll, area.rows); |
| 702 | 704 | return ViewChange::Unchanged; |
|
| 703 | 705 | } |
|
| 704 | 706 | ||
| 705 | 707 | match action { |
|
| 706 | 708 | DiffAction::Scroll(scroll) => { |
|
| 707 | - | let mut viewport = Viewport::from_offset(self.state.scroll); |
|
| 708 | - | let previous = self.state.scroll; |
|
| 709 | - | viewport.scroll(scroll, self.scroll_rows(diff.rows), diff.rows); |
|
| 710 | - | self.state.scroll = viewport.offset; |
|
| 711 | - | if self.state.scroll != previous |
|
| 709 | + | let previous = self.state.scroll.offset; |
|
| 710 | + | self.state |
|
| 711 | + | .scroll |
|
| 712 | + | .scroll(scroll, self.scroll_rows(diff.rows), diff.rows); |
|
| 713 | + | if self.state.scroll.offset != previous |
|
| 712 | 714 | && layout.tree.is_some() |
|
| 713 | 715 | && let Some(file) = self.visible_file() |
|
| 714 | 716 | { |
|
| 715 | 717 | self.state.focus = Some(DiffFocus::File(file)); |
|
| 716 | 718 | self.file_tree.select_file(file); |
|
| 717 | 719 | } |
|
| 718 | 720 | } |
|
| 719 | 721 | DiffAction::Resize => { |
|
| 720 | - | let mut viewport = Viewport::from_offset(self.state.scroll); |
|
| 721 | - | viewport.clamp(self.scroll_rows(diff.rows), diff.rows); |
|
| 722 | - | self.state.scroll = viewport.offset; |
|
| 722 | + | self.state |
|
| 723 | + | .scroll |
|
| 724 | + | .clamp(self.scroll_rows(diff.rows), diff.rows); |
|
| 723 | 725 | if layout.tree.is_some() { |
|
| 724 | 726 | self.sync_file_tree(); |
|
| 725 | 727 | } |
|
| 726 | 728 | } |
|
| 727 | 729 | DiffAction::FocusNext => { |
| 792 | 794 | } |
|
| 793 | 795 | } |
|
| 794 | 796 | ||
| 795 | 797 | if before.invalidates_index(&self.state) { |
|
| 796 | 798 | self.rebuild_index(); |
|
| 797 | - | self.state.scroll = |
|
| 798 | - | View::new(self.state.scroll, self.scroll_rows(diff.rows), diff.rows).offset; |
|
| 799 | + | self.state |
|
| 800 | + | .scroll |
|
| 801 | + | .clamp(self.scroll_rows(diff.rows), diff.rows); |
|
| 799 | 802 | } |
|
| 800 | 803 | if self.state == before |
|
| 801 | 804 | && self.file_tree_visibility == before_visibility |
|
| 802 | 805 | && *self.file_tree.state() == before_file_tree |
|
| 803 | 806 | { |
| 880 | 883 | /// Returns highlight requests for visible source rows plus configured lookahead. |
|
| 881 | 884 | pub fn highlight_requests(&mut self, area: Rect) -> Vec<HighlightRequest> { |
|
| 882 | 885 | let diff = self.layout(area).diff; |
|
| 883 | 886 | let take = diff.rows.saturating_add(self.options.highlight_lookahead); |
|
| 884 | 887 | let view = View::new( |
|
| 885 | - | self.state.scroll, |
|
| 886 | - | self.render_rows_for(self.state.scroll, diff.rows), |
|
| 888 | + | self.state.scroll.offset, |
|
| 889 | + | self.render_rows_for(self.state.scroll.offset, diff.rows), |
|
| 887 | 890 | diff.rows, |
|
| 888 | 891 | ); |
|
| 889 | 892 | let start = view |
|
| 890 | 893 | .offset |
|
| 891 | 894 | .saturating_add(self.sticky_row_count(view.offset, diff.rows)); |
| 1339 | 1342 | return; |
|
| 1340 | 1343 | }; |
|
| 1341 | 1344 | ||
| 1342 | 1345 | self.state.focus = Some(target.focus); |
|
| 1343 | 1346 | if filter == FocusFilter::File { |
|
| 1344 | - | self.state.scroll = |
|
| 1345 | - | View::new(target.row, self.scroll_rows(visible_rows), visible_rows).offset; |
|
| 1347 | + | self.state |
|
| 1348 | + | .scroll |
|
| 1349 | + | .set_offset(target.row, self.scroll_rows(visible_rows), visible_rows); |
|
| 1346 | 1350 | return; |
|
| 1347 | 1351 | } |
|
| 1348 | 1352 | ||
| 1349 | - | let mut viewport = Viewport::from_offset(self.state.scroll); |
|
| 1350 | 1353 | let margin = visible_rows / 4; |
|
| 1351 | - | viewport.scroll_to_row( |
|
| 1354 | + | let before = margin.max(self.sticky_row_count(self.state.scroll.offset, visible_rows)); |
|
| 1355 | + | self.state.scroll.scroll_to_row( |
|
| 1352 | 1356 | target.row, |
|
| 1353 | 1357 | self.scroll_rows(visible_rows), |
|
| 1354 | 1358 | visible_rows, |
|
| 1355 | 1359 | Placement::Margin { |
|
| 1356 | - | before: margin.max(self.sticky_row_count(self.state.scroll, visible_rows)), |
|
| 1360 | + | before, |
|
| 1357 | 1361 | after: margin, |
|
| 1358 | 1362 | }, |
|
| 1359 | 1363 | ); |
|
| 1360 | - | self.state.scroll = viewport.offset; |
|
| 1361 | 1364 | } |
|
| 1362 | 1365 | ||
| 1363 | 1366 | /// Returns focusable targets in rendered row order. |
|
| 1364 | 1367 | fn focus_targets(&self, filter: FocusFilter) -> Vec<FocusTarget> { |
|
| 1365 | 1368 | let mut targets = Vec::new(); |
| 1658 | 1661 | left.push(Label::space()); |
|
| 1659 | 1662 | left = left.extend(self.path_line()); |
|
| 1660 | 1663 | ||
| 1661 | 1664 | if let Some(badge) = self.badge(palette) { |
|
| 1662 | 1665 | left.push(Label::space()); |
|
| 1663 | - | left.push(badge); |
|
| 1666 | + | left = left.extend(badge.line()); |
|
| 1664 | 1667 | } |
|
| 1665 | 1668 | let mut header = HStack::default().child(left).spacer(); |
|
| 1666 | 1669 | let stats = self.stats(); |
|
| 1667 | 1670 | ||
| 1668 | 1671 | if let Some(side) = Self::diffstat_line(stats.additions, stats.deletions, palette) { |
| 1700 | 1703 | } |
|
| 1701 | 1704 | Some(line) |
|
| 1702 | 1705 | } |
|
| 1703 | 1706 | ||
| 1704 | 1707 | /// Returns a file-kind badge. |
|
| 1705 | - | fn badge(&self, palette: DiffPalette) -> Option<Label> { |
|
| 1708 | + | fn badge(&self, palette: DiffPalette) -> Option<Badge> { |
|
| 1706 | 1709 | match self.header.kind { |
|
| 1707 | 1710 | FileKind::Added => Some(Self::change_badge( |
|
| 1708 | 1711 | "created", |
|
| 1709 | 1712 | palette.positive_fg, |
|
| 1710 | 1713 | palette.positive_row_bg, |
| 1737 | 1740 | FileKind::Modified => None, |
|
| 1738 | 1741 | } |
|
| 1739 | 1742 | } |
|
| 1740 | 1743 | ||
| 1741 | 1744 | /// Returns a small colored badge. |
|
| 1742 | - | fn change_badge(text: &str, foreground: Color, background: Color) -> Label { |
|
| 1743 | - | Label::new(&format!(" {text} ")) |
|
| 1744 | - | .fg(foreground) |
|
| 1745 | - | .bg(background) |
|
| 1745 | + | fn change_badge(text: &str, foreground: Color, background: Color) -> Badge { |
|
| 1746 | + | let style = Style::new(foreground).bg(background); |
|
| 1747 | + | ||
| 1748 | + | Badge::new(text) |
|
| 1749 | + | .caps(" ", " ") |
|
| 1750 | + | .cap_style(style) |
|
| 1751 | + | .style(style) |
|
| 1746 | 1752 | } |
|
| 1747 | 1753 | ||
| 1748 | 1754 | /// Returns a display path or placeholder. |
|
| 1749 | 1755 | fn display_path(path: Option<&PathBuf>) -> String { |
|
| 1750 | 1756 | path.map_or_else(|| "<unknown>".to_owned(), |path| path.display().to_string()) |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
src/app/a.rs
+0 -0
src/lib/b.rs
+0 -0
README.md
+0 -0
assets/css/style.css
+0 -0
assets/css/style.css
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
crates/files/src/main.rs
+54 -107
| 4 | 4 | use std::collections::HashMap; |
|
| 5 | 5 | use std::ffi::OsStr; |
|
| 6 | 6 | use std::fs; |
|
| 7 | 7 | use std::io; |
|
| 8 | 8 | use std::path::{Path, PathBuf}; |
|
| 9 | - | use std::sync::atomic::{AtomicUsize, Ordering}; |
|
| 10 | 9 | use std::sync::{Mutex, PoisonError}; |
|
| 11 | 10 | use std::time::SystemTime; |
|
| 12 | 11 | ||
| 13 | 12 | use rustik::fmt; |
|
| 14 | 13 | use rustik_tui as tui; |
|
| 15 | 14 | use rustik_tui_termion as tui_termion; |
|
| 16 | 15 | use tui::text; |
|
| 17 | 16 | use tui::{ |
|
| 18 | - | App, Block, Border, Cell, Color, Control, Direction, Event, Frame, Key, Label, Length, Line, |
|
| 19 | - | List, Mouse, MouseButton, Paint, Rect, ScrollMetrics, ScrollView, ScrollbarStyle, Selection, |
|
| 20 | - | Size, Style, Table, TableOptions, TextArea, VStack, Widget, colors, |
|
| 17 | + | ActionBar, ActionList, App, Block, Border, Cell, Color, Control, Direction, Event, Frame, Key, |
|
| 18 | + | Label, Length, Line, List, ListState, Mouse, MouseButton, Paint, Rect, ScrollMetrics, |
|
| 19 | + | ScrollView, ScrollbarStyle, Size, StatefulWidget, Style, Table, TableOptions, TextArea, VStack, |
|
| 20 | + | Widget, colors, |
|
| 21 | 21 | }; |
|
| 22 | 22 | ||
| 23 | 23 | const COLUMN_WIDTH: usize = 30; |
|
| 24 | 24 | const PREVIEW_MIN_WIDTH: usize = 28; |
|
| 25 | 25 | const ACTIVE_BORDER_COLOR: Color = Color::Blue; |
| 30 | 30 | const PREVIEW_TEXT_COLOR: Color = Color::Fixed(252); |
|
| 31 | 31 | const METADATA_LABEL_COLOR: Color = Color::Fixed(240); |
|
| 32 | 32 | const METADATA_VALUE_COLOR: Color = Color::Fixed(244); |
|
| 33 | 33 | const HEADER_COLOR: Color = Color::White; |
|
| 34 | 34 | const ERROR_COLOR: Color = Color::Red; |
|
| 35 | - | const FOOTER_SEPARATOR_COLOR: Color = Color::Fixed(244); |
|
| 36 | 35 | const FOOTER_KEY_COLOR: Color = Color::Cyan; |
|
| 36 | + | const FOOTER_KEY_TEXT_COLOR: Color = Color::Black; |
|
| 37 | 37 | const FOOTER_ACTION_COLOR: Color = Color::Fixed(244); |
|
| 38 | 38 | const SCROLLBAR_TRACK_COLOR: Color = Color::Fixed(238); |
|
| 39 | 39 | const SCROLLBAR_ACTIVE_THUMB_COLOR: Color = Color::Blue; |
|
| 40 | 40 | const SCROLLBAR_INACTIVE_THUMB_COLOR: Color = Color::Fixed(244); |
|
| 41 | 41 |
| 241 | 241 | ||
| 242 | 242 | fn active_column_mut(&mut self) -> Option<&mut Column> { |
|
| 243 | 243 | self.columns.get_mut(self.active) |
|
| 244 | 244 | } |
|
| 245 | 245 | ||
| 246 | - | fn active_column(&self) -> Option<&Column> { |
|
| 247 | - | self.columns.get(self.active) |
|
| 248 | - | } |
|
| 249 | - | ||
| 250 | 246 | fn update_trailing_columns(&mut self) -> bool { |
|
| 251 | 247 | let next = self.active.saturating_add(1); |
|
| 252 | 248 | let Some(entry) = self.selected_entry() else { |
|
| 253 | 249 | let changed = self.columns.len() > next; |
|
| 254 | 250 | self.columns.truncate(next); |
| 298 | 294 | fn scroll(&mut self, delta: isize) -> bool { |
|
| 299 | 295 | if self.focus == Focus::Preview { |
|
| 300 | 296 | return self.move_preview(delta); |
|
| 301 | 297 | } |
|
| 302 | 298 | ||
| 303 | - | self.active_column() |
|
| 299 | + | self.active_column_mut() |
|
| 304 | 300 | .is_some_and(|column| column.scroll(delta)) |
|
| 305 | 301 | } |
|
| 306 | 302 | ||
| 307 | 303 | fn select_first(&mut self) -> bool { |
|
| 308 | 304 | if self.focus == Focus::Preview { |
| 406 | 402 | ||
| 407 | 403 | let chunks = tui::layout::split(area, Direction::Horizontal, &lengths); |
|
| 408 | 404 | for (index, chunk) in chunks.iter().take(visible_columns).enumerate() { |
|
| 409 | 405 | let column_index = hidden + index; |
|
| 410 | 406 | let active = self.focus == Focus::Columns && column_index == self.active; |
|
| 411 | - | if let Some(column) = self.columns.get(column_index) { |
|
| 407 | + | if let Some(column) = self.columns.get_mut(column_index) { |
|
| 412 | 408 | frame.render_widget(ColumnWidget { column, active }, *chunk); |
|
| 413 | 409 | } |
|
| 414 | 410 | } |
|
| 415 | 411 | ||
| 416 | 412 | if show_preview |
| 431 | 427 | } |
|
| 432 | 428 | } |
|
| 433 | 429 | ||
| 434 | 430 | #[derive(Debug)] |
|
| 435 | 431 | struct Column { |
|
| 432 | + | /// Directory path shown by this column. |
|
| 436 | 433 | path: PathBuf, |
|
| 434 | + | /// Directory entries in display order. |
|
| 437 | 435 | entries: Vec<Entry>, |
|
| 438 | - | selection: Selection, |
|
| 439 | - | offset: AtomicUsize, |
|
| 440 | - | visible_rows: AtomicUsize, |
|
| 436 | + | /// Selection and viewport state for the entry list. |
|
| 437 | + | state: ListState, |
|
| 438 | + | /// Rows available to the list in the last render pass. |
|
| 439 | + | visible_rows: usize, |
|
| 441 | 440 | } |
|
| 442 | 441 | ||
| 443 | 442 | impl Column { |
|
| 444 | 443 | fn read(path: PathBuf) -> io::Result<Self> { |
|
| 445 | 444 | let mut entries = Vec::new(); |
| 450 | 449 | entries.sort_by(|a, b| b.is_dir.cmp(&a.is_dir).then_with(|| a.name.cmp(&b.name))); |
|
| 451 | 450 | ||
| 452 | 451 | Ok(Self { |
|
| 453 | 452 | path, |
|
| 454 | 453 | entries, |
|
| 455 | - | selection: Selection::new(), |
|
| 456 | - | offset: AtomicUsize::new(0), |
|
| 457 | - | visible_rows: AtomicUsize::new(1), |
|
| 454 | + | state: ListState::new(), |
|
| 455 | + | visible_rows: 1, |
|
| 458 | 456 | }) |
|
| 459 | 457 | } |
|
| 460 | 458 | ||
| 461 | 459 | fn selected(&self) -> Option<&Entry> { |
|
| 462 | - | self.entries.get(self.selection.selected) |
|
| 463 | - | } |
|
| 464 | - | ||
| 465 | - | fn set_visible_rows(&self, rows: usize) { |
|
| 466 | - | self.visible_rows.store(rows, Ordering::Relaxed); |
|
| 467 | - | self.set_offset(self.offset()); |
|
| 468 | - | } |
|
| 469 | - | ||
| 470 | - | fn offset(&self) -> usize { |
|
| 471 | - | self.offset.load(Ordering::Relaxed) |
|
| 460 | + | self.entries.get(self.state.selected()) |
|
| 472 | 461 | } |
|
| 473 | 462 | ||
| 474 | - | fn set_offset(&self, offset: usize) -> bool { |
|
| 475 | - | let previous = self.offset(); |
|
| 476 | - | let next = offset.min(tui::viewport::max_offset( |
|
| 463 | + | fn scroll(&mut self, delta: isize) -> bool { |
|
| 464 | + | self.state.scroll( |
|
| 465 | + | tui::Scroll::Lines(delta), |
|
| 477 | 466 | self.entries.len(), |
|
| 478 | - | self.visible_rows.load(Ordering::Relaxed), |
|
| 479 | - | )); |
|
| 480 | - | self.offset.store(next, Ordering::Relaxed); |
|
| 481 | - | next != previous |
|
| 482 | - | } |
|
| 483 | - | ||
| 484 | - | fn scroll(&self, delta: isize) -> bool { |
|
| 485 | - | let previous = self.offset(); |
|
| 486 | - | let next = if delta.is_negative() { |
|
| 487 | - | previous.saturating_sub(delta.unsigned_abs()) |
|
| 488 | - | } else { |
|
| 489 | - | previous.saturating_add(delta as usize) |
|
| 490 | - | }; |
|
| 491 | - | self.set_offset(next) |
|
| 492 | - | } |
|
| 493 | - | ||
| 494 | - | fn show_selection(&self) { |
|
| 495 | - | self.set_offset( |
|
| 496 | - | self.selection |
|
| 497 | - | .view( |
|
| 498 | - | self.entries.len(), |
|
| 499 | - | self.visible_rows.load(Ordering::Relaxed), |
|
| 500 | - | tui::viewport::Placement::Margin { |
|
| 501 | - | before: 2, |
|
| 502 | - | after: 2, |
|
| 503 | - | }, |
|
| 504 | - | ) |
|
| 505 | - | .offset, |
|
| 506 | - | ); |
|
| 467 | + | self.visible_rows, |
|
| 468 | + | ) |
|
| 507 | 469 | } |
|
| 508 | 470 | ||
| 509 | 471 | fn select_path(&mut self, path: &Path) { |
|
| 510 | 472 | if let Some(index) = self.entries.iter().position(|entry| entry.path == path) { |
|
| 511 | - | self.selection.select(index, self.entries.len()); |
|
| 512 | - | self.show_selection(); |
|
| 473 | + | self.state.select(index, self.entries.len()); |
|
| 513 | 474 | } |
|
| 514 | 475 | } |
|
| 515 | 476 | ||
| 516 | 477 | fn move_selection(&mut self, delta: isize) -> bool { |
|
| 517 | - | let changed = self.selection.move_by(delta, self.entries.len()); |
|
| 518 | - | if changed { |
|
| 519 | - | self.show_selection(); |
|
| 520 | - | } |
|
| 521 | - | changed |
|
| 478 | + | self.state.move_by(delta, self.entries.len()) |
|
| 522 | 479 | } |
|
| 523 | 480 | ||
| 524 | 481 | fn select_first(&mut self) -> bool { |
|
| 525 | - | let changed = self.selection.select_first(); |
|
| 526 | - | if changed { |
|
| 527 | - | self.show_selection(); |
|
| 528 | - | } |
|
| 529 | - | changed |
|
| 482 | + | self.state.select_first() |
|
| 530 | 483 | } |
|
| 531 | 484 | ||
| 532 | 485 | fn select_last(&mut self) -> bool { |
|
| 533 | - | let changed = self.selection.select_last(self.entries.len()); |
|
| 534 | - | if changed { |
|
| 535 | - | self.show_selection(); |
|
| 536 | - | } |
|
| 537 | - | changed |
|
| 486 | + | self.state.select_last(self.entries.len()) |
|
| 538 | 487 | } |
|
| 539 | 488 | } |
|
| 540 | 489 | ||
| 541 | 490 | #[derive(Debug)] |
|
| 542 | 491 | struct Entry { |
| 570 | 519 | } |
|
| 571 | 520 | } |
|
| 572 | 521 | ||
| 573 | 522 | #[derive(Debug)] |
|
| 574 | 523 | struct ColumnWidget<'a> { |
|
| 575 | - | column: &'a Column, |
|
| 524 | + | column: &'a mut Column, |
|
| 576 | 525 | active: bool, |
|
| 577 | 526 | } |
|
| 578 | 527 | ||
| 579 | 528 | impl Widget for ColumnWidget<'_> { |
|
| 580 | 529 | fn size(&self, parent: tui::Constraint) -> Size { |
| 587 | 536 | } else { |
|
| 588 | 537 | INACTIVE_BORDER_COLOR |
|
| 589 | 538 | }; |
|
| 590 | 539 | let title = title_for(&self.column.path); |
|
| 591 | 540 | let body = ColumnBody { |
|
| 592 | - | column: self.column, |
|
| 541 | + | column: &mut *self.column, |
|
| 593 | 542 | active: self.active, |
|
| 594 | 543 | }; |
|
| 595 | 544 | Block::new(body) |
|
| 596 | 545 | .border_style(Border::new(border)) |
|
| 597 | 546 | .title(Line::new(text::bold(title))) |
| 599 | 548 | } |
|
| 600 | 549 | } |
|
| 601 | 550 | ||
| 602 | 551 | #[derive(Debug)] |
|
| 603 | 552 | struct ColumnBody<'a> { |
|
| 604 | - | column: &'a Column, |
|
| 553 | + | column: &'a mut Column, |
|
| 605 | 554 | active: bool, |
|
| 606 | 555 | } |
|
| 607 | 556 | ||
| 608 | 557 | impl Widget for ColumnBody<'_> { |
|
| 609 | 558 | fn size(&self, parent: tui::Constraint) -> Size { |
| 612 | 561 | ||
| 613 | 562 | fn render(&mut self, area: Rect, buf: &mut tui::Buffer) { |
|
| 614 | 563 | if area.rows == 0 { |
|
| 615 | 564 | return; |
|
| 616 | 565 | } |
|
| 617 | - | self.column.set_visible_rows(area.rows); |
|
| 566 | + | self.column.visible_rows = area.rows; |
|
| 618 | 567 | ||
| 619 | 568 | let lines = self.column.entries.iter().map(entry_line); |
|
| 620 | - | List::new(lines) |
|
| 569 | + | let mut list = List::new(lines) |
|
| 621 | 570 | .active(self.active) |
|
| 622 | 571 | .selected_styles( |
|
| 623 | 572 | Style::default().bg(ACTIVE_SELECTION_BG_COLOR), |
|
| 624 | 573 | Style::default().bg(INACTIVE_SELECTION_BG_COLOR), |
|
| 625 | 574 | ) |
|
| 626 | - | .selected(self.column.selection.selected) |
|
| 627 | - | .offset(self.column.offset()) |
|
| 628 | 575 | .empty(Line::new(text::dim("empty directory")).style(Style::default().italic())) |
|
| 629 | 576 | .placement(tui::viewport::Placement::Margin { |
|
| 630 | 577 | before: 2, |
|
| 631 | 578 | after: 2, |
|
| 632 | 579 | }) |
|
| 633 | - | .scrollbar(finder_scrollbar_style(self.active)) |
|
| 634 | - | .render(area, buf); |
|
| 580 | + | .scrollbar(finder_scrollbar_style(self.active)); |
|
| 581 | + | list.render_with_state(area, buf, &mut self.column.state); |
|
| 635 | 582 | } |
|
| 636 | 583 | } |
|
| 637 | 584 | ||
| 638 | 585 | /// Builds the visible line for one directory entry. |
|
| 639 | 586 | fn entry_line(entry: &Entry) -> Line { |
| 797 | 744 | buf.set_line(x, area.y, &line); |
|
| 798 | 745 | } |
|
| 799 | 746 | } |
|
| 800 | 747 | ||
| 801 | 748 | /// Builds the footer line for current focus and error state. |
|
| 802 | - | fn footer_line(app: &Browser) -> impl Widget { |
|
| 749 | + | fn footer_line(app: &Browser) -> Line { |
|
| 803 | 750 | if let Some(error) = app.error.as_deref() { |
|
| 804 | 751 | return Line::new(Paint::new(error).fg(ERROR_COLOR)); |
|
| 805 | 752 | } |
|
| 806 | 753 | ||
| 807 | - | let mut line = Line::default(); |
|
| 808 | - | footer_action(&mut line, "q", "quit"); |
|
| 754 | + | let mut actions = ActionList::new().action("q", "quit"); |
|
| 809 | 755 | if app.focus == Focus::Preview { |
|
| 810 | - | footer_action(&mut line, "up/down", "scroll"); |
|
| 811 | - | footer_action(&mut line, "space", "half page"); |
|
| 812 | - | footer_action(&mut line, "left/backspace", "columns"); |
|
| 756 | + | actions = actions |
|
| 757 | + | .action("up/down", "scroll") |
|
| 758 | + | .action("space", "half page") |
|
| 759 | + | .action("left/backspace", "columns"); |
|
| 813 | 760 | } else { |
|
| 814 | - | footer_action(&mut line, "up/down", "select"); |
|
| 815 | - | footer_action(&mut line, "right/enter", "open/preview"); |
|
| 816 | - | footer_action(&mut line, "left/backspace", "remove column"); |
|
| 817 | - | } |
|
| 818 | - | footer_action(&mut line, "home/end", "jump"); |
|
| 819 | - | line |
|
| 820 | - | } |
|
| 821 | - | ||
| 822 | - | /// Appends one key/action pair to the footer line. |
|
| 823 | - | fn footer_action(line: &mut Line, key: &'static str, action: &'static str) { |
|
| 824 | - | if line.width() > 0 { |
|
| 825 | - | line.push(Paint::new(" ").fg(FOOTER_SEPARATOR_COLOR)); |
|
| 826 | - | } |
|
| 827 | - | line.push(Paint::new(key).fg(FOOTER_KEY_COLOR)); |
|
| 828 | - | line.push(Paint::new(" ").fg(FOOTER_SEPARATOR_COLOR)); |
|
| 829 | - | line.push(Paint::new(action).fg(FOOTER_ACTION_COLOR)); |
|
| 761 | + | actions = actions |
|
| 762 | + | .action("up/down", "select") |
|
| 763 | + | .action("right/enter", "open/preview") |
|
| 764 | + | .action("left/backspace", "remove column"); |
|
| 765 | + | } |
|
| 766 | + | actions = actions.action("home/end", "jump"); |
|
| 767 | + | ||
| 768 | + | ActionBar::new(actions) |
|
| 769 | + | .cap_style(Style::new(FOOTER_KEY_COLOR)) |
|
| 770 | + | .key_style( |
|
| 771 | + | Style::new(FOOTER_KEY_TEXT_COLOR) |
|
| 772 | + | .bg(FOOTER_KEY_COLOR) |
|
| 773 | + | .bold(), |
|
| 774 | + | ) |
|
| 775 | + | .action_style(Style::new(FOOTER_ACTION_COLOR)) |
|
| 776 | + | .line() |
|
| 830 | 777 | } |
|
| 831 | 778 | ||
| 832 | 779 | /// Returns how many browser columns fit beside the preview pane. |
|
| 833 | 780 | fn visible_column_count(cols: usize, total: usize) -> usize { |
|
| 834 | 781 | if total == 0 { |
crates/tui-widgets/src/list.rs
+16 -1
| 147 | 147 | self.selection.select_last(len) |
|
| 148 | 148 | } |
|
| 149 | 149 | ||
| 150 | 150 | /// Scrolls the viewport manually and pauses selection following. |
|
| 151 | 151 | pub fn scroll_by(&mut self, rows: isize, len: usize) -> bool { |
|
| 152 | - | let changed = self.viewport.scroll(Scroll::Lines(rows), len, 1); |
|
| 152 | + | self.scroll(Scroll::Lines(rows), len, 1) |
|
| 153 | + | } |
|
| 154 | + | ||
| 155 | + | /// Applies manual scrolling for a visible viewport and pauses selection following. |
|
| 156 | + | pub fn scroll(&mut self, scroll: Scroll, len: usize, rows: usize) -> bool { |
|
| 157 | + | let changed = self.viewport.scroll(scroll, len, rows); |
|
| 153 | 158 | self.follow_selection = false; |
|
| 154 | 159 | changed |
|
| 155 | 160 | } |
|
| 156 | 161 | ||
| 157 | 162 | /// Returns a viewport, updating the persistent offset if needed. |
| 358 | 363 | let mut state = ListState::from_offset(20); |
|
| 359 | 364 | ||
| 360 | 365 | assert_eq!(state.view(30, 20, Placement::Minimal).offset, 10); |
|
| 361 | 366 | } |
|
| 362 | 367 | ||
| 368 | + | #[test] |
|
| 369 | + | fn list_state_manual_scroll_uses_visible_rows() { |
|
| 370 | + | let mut state = ListState::from_offset(90); |
|
| 371 | + | ||
| 372 | + | assert!(!state.scroll(Scroll::Lines(1), 100, 10)); |
|
| 373 | + | assert_eq!(state.offset(), 90); |
|
| 374 | + | assert!(state.scroll(Scroll::Lines(1), 100, 1)); |
|
| 375 | + | assert_eq!(state.offset(), 91); |
|
| 376 | + | } |
|
| 377 | + | ||
| 363 | 378 | #[test] |
|
| 364 | 379 | fn list_render_with_state_updates_persistent_offset() { |
|
| 365 | 380 | let mut list = List::new(["zero", "one", "two", "three"].into_iter().map(Line::new)) |
|
| 366 | 381 | .selected_styles(Style::new(Color::Unset), Style::new(Color::Unset)); |
|
| 367 | 382 | let mut state = ListState::new(); |