The app list counted its own header as a row, and the cursor paid for it
Uninstall came back from the Windows box saying the thing it had already said on macOS: once there are more apps than a page, paging down leaves the list with no highlight anywhere. Two consoles that have spent this branch being taught what a `█` costs agree with each other about nothing, so a complaint that survives both of them is not standing in the console. It was standing in `draw_uninstall`, and it had been there since the panel was written. The panel pushes its column header — `size name version source`, plus a filter chip while a filter is standing — into the same `List` widget that holds the apps, and paints the highlight by asking whether `row == cursor`, where `row` counts the apps that `visible()` returned. `ListState::selected` is handed that same `app.uninstall.cursor`: a number in the space of visible apps, in a widget whose rows are that space plus one, plus two with the chip. ratatui spends `selected` on exactly one thing in this configuration, because `highlight_style` is empty on purpose — the row paints its own highlight so that a cursor survives a terminal with no colour at all — so the disagreement cannot mispaint a row. What it can do is decide which rows exist on screen: the scroll window is positioned around the row one above the cursor. While the list fits in the card, or while the cursor is high enough in a list that does not, the cursor is inside the window either way and the header costs nothing. That is why this needed a machine with more installed apps than a fixture has, and why the same binary lied the same way on both platforms. `ScanPanel::lines` has never shown the symptom, and the reason is worth naming rather than admiring: it holds entries and nothing else, because the heading of that card lives on the `Block`. One widget, one index space, nothing to drift. The uninstall *plan* was taught the same lesson from the other direction — it used to swap item zero for a bundle heading, which held only while the `.app` was the largest thing in the plan, until a container outgrew it and every row under the heading sat one line away from its cursor. The app list was the third place a header had been allowed to spend a row, and the only one nobody had asked about. The fix declines to count anything. `chrome` and the entries are two constraints of one vertical `Layout`: the `Paragraph` takes the first rectangle, the `List` gets a rectangle holding entries only, and `selected` and `band()` are back to describing the same rows. Nowhere in the file is `cursor + header_lines` written down, because an offset is the thing that just went wrong, and the next line of chrome added to this header would need one of its own. `the_app_under_the_cursor_is_the_row_that_is_on_screen` is the regression test, and it loops over every cursor position rather than picking a deep one, because whether one deep cursor fails depends on how tall the card happens to be where the test is running — which is a measurement of a machine, not an invariant. For each position it draws 140x34 and asks the buffer two questions: is the app under the cursor on the screen at all, and is the cell it landed on wearing the accent background and the bold that a highlight paints. The series runs twice, once with the filter chip on screen, because the chip is the second row of the two index spaces that were drifting apart. Before the fix it stops at cursor 25 with "the app under the cursor is off screen". Nothing on the screen had been drawn wrongly, which is the shape of this bug: every row was correct, and the only thing missing was the cursor. 68 tests in the interface crate, one of them this one, green; `cargo fmt --check` and `cargo clippy --workspace --all-targets` clean for the host.main
parent
bf04f2c105
commit
f9f86f6103
|
|
@ -995,7 +995,14 @@ fn draw_uninstall(frame: &mut Frame, area: Rect, app: &App) {
|
|||
let source_w = width
|
||||
.saturating_sub(2 + size_w + 1 + name_w + 1 + version_w + 1)
|
||||
.max(8);
|
||||
let mut lines = vec![Line::from(Span::styled(
|
||||
// The column header and the filter chip are chrome: they label the columns,
|
||||
// they are not rows the cursor can rest on. They used to be pushed into the
|
||||
// `List` ahead of the entries, which handed the widget one or two rows the
|
||||
// app never counted. ratatui uses `selected` for nothing but scrolling here
|
||||
// (the row paints its own highlight), so from the first page turn onwards it
|
||||
// centred the window on the row above the cursor and pushed the highlighted
|
||||
// row out of view.
|
||||
let mut chrome = vec![Line::from(Span::styled(
|
||||
format!(
|
||||
"{} {} {} {}",
|
||||
right(&app.strings.t("size", "大小"), size_w),
|
||||
|
|
@ -1006,7 +1013,7 @@ fn draw_uninstall(frame: &mut Frame, area: Rect, app: &App) {
|
|||
Palette::muted(),
|
||||
))];
|
||||
if !app.uninstall.filter.is_empty() {
|
||||
lines.push(Line::from(Span::styled(
|
||||
chrome.push(Line::from(Span::styled(
|
||||
format!(
|
||||
" {}{}",
|
||||
app.strings.t("filter", "筛选"),
|
||||
|
|
@ -1015,66 +1022,77 @@ fn draw_uninstall(frame: &mut Frame, area: Rect, app: &App) {
|
|||
Style::default().fg(Palette::warn()).bg(Palette::card_bg()),
|
||||
)));
|
||||
}
|
||||
lines.extend(visible.iter().enumerate().map(|(row, index)| {
|
||||
let app_info = &app.uninstall.apps[*index];
|
||||
let highlight = row == app.uninstall.cursor;
|
||||
let running = app_info.running;
|
||||
let left = vec![
|
||||
Span::styled(
|
||||
if running { "▶ " } else { " " },
|
||||
dim(highlight, Palette::warn()),
|
||||
),
|
||||
Span::styled(
|
||||
right(&app_info.size.human(), size_w),
|
||||
if highlight {
|
||||
Palette::selected()
|
||||
} else {
|
||||
Palette::size_color(app_info.size)
|
||||
},
|
||||
),
|
||||
Span::raw(" "),
|
||||
Span::styled(
|
||||
pad(&truncate(&app_info.name, name_w, false), name_w),
|
||||
if highlight {
|
||||
Palette::selected()
|
||||
} else {
|
||||
Style::default().fg(Palette::text())
|
||||
},
|
||||
),
|
||||
Span::raw(" "),
|
||||
Span::styled(
|
||||
pad(app_info.version.as_deref().unwrap_or("—"), version_w),
|
||||
dim(highlight, Palette::muted_fg()),
|
||||
),
|
||||
];
|
||||
let note = if running {
|
||||
app.strings.t("running", "运行中")
|
||||
} else {
|
||||
app_info.source.clone()
|
||||
};
|
||||
joined(
|
||||
left,
|
||||
vec![Span::styled(
|
||||
truncate(¬e, source_w, false),
|
||||
dim(
|
||||
highlight,
|
||||
if running {
|
||||
Palette::warn()
|
||||
// Chrome above, entries below: one widget, one index space, so
|
||||
// `ListState::selected` and the highlight style count the same rows. The
|
||||
// cleanup list has always done this — its heading lives in the card
|
||||
// block — which is why paging never lost a cursor there.
|
||||
let strip = Layout::vertical([Constraint::Length(chrome.len() as u16), Constraint::Min(1)])
|
||||
.split(inner);
|
||||
frame.render_widget(Paragraph::new(chrome).style(Palette::list()), strip[0]);
|
||||
let lines: Vec<Line<'static>> = visible
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(row, index)| {
|
||||
let app_info = &app.uninstall.apps[*index];
|
||||
let highlight = row == app.uninstall.cursor;
|
||||
let running = app_info.running;
|
||||
let left = vec![
|
||||
Span::styled(
|
||||
if running { "▶ " } else { " " },
|
||||
dim(highlight, Palette::warn()),
|
||||
),
|
||||
Span::styled(
|
||||
right(&app_info.size.human(), size_w),
|
||||
if highlight {
|
||||
Palette::selected()
|
||||
} else {
|
||||
Palette::muted_fg()
|
||||
Palette::size_color(app_info.size)
|
||||
},
|
||||
),
|
||||
)],
|
||||
width,
|
||||
)
|
||||
.style(band(row, highlight))
|
||||
}));
|
||||
Span::raw(" "),
|
||||
Span::styled(
|
||||
pad(&truncate(&app_info.name, name_w, false), name_w),
|
||||
if highlight {
|
||||
Palette::selected()
|
||||
} else {
|
||||
Style::default().fg(Palette::text())
|
||||
},
|
||||
),
|
||||
Span::raw(" "),
|
||||
Span::styled(
|
||||
pad(app_info.version.as_deref().unwrap_or("—"), version_w),
|
||||
dim(highlight, Palette::muted_fg()),
|
||||
),
|
||||
];
|
||||
let note = if running {
|
||||
app.strings.t("running", "运行中")
|
||||
} else {
|
||||
app_info.source.clone()
|
||||
};
|
||||
joined(
|
||||
left,
|
||||
vec![Span::styled(
|
||||
truncate(¬e, source_w, false),
|
||||
dim(
|
||||
highlight,
|
||||
if running {
|
||||
Palette::warn()
|
||||
} else {
|
||||
Palette::muted_fg()
|
||||
},
|
||||
),
|
||||
)],
|
||||
width,
|
||||
)
|
||||
.style(band(row, highlight))
|
||||
})
|
||||
.collect();
|
||||
let list = List::new(lines)
|
||||
.style(Palette::list())
|
||||
.highlight_symbol("")
|
||||
.highlight_style(Style::default());
|
||||
let mut state = ListState::default().with_selected(Some(app.uninstall.cursor));
|
||||
frame.render_stateful_widget(list, inner, &mut state);
|
||||
frame.render_stateful_widget(list, strip[1], &mut state);
|
||||
}
|
||||
|
||||
fn draw_uninstall_plan(frame: &mut Frame, area: Rect, app: &App) {
|
||||
|
|
@ -2520,4 +2538,96 @@ mod tests {
|
|||
"the toast should float inside the card, not over its edge: {carrier:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// The app list prints a column header above its rows, and while the user
|
||||
/// is filtering it prints a filter chip below that header. Both are drawn
|
||||
/// *inside* the `List` widget, which means they spend row numbers of the
|
||||
/// list, while the highlight style is picked in the space of
|
||||
/// the visible-apps list — two index spaces one (or two) apart.
|
||||
/// ratatui only uses `selected` to decide how far to scroll
|
||||
/// (`highlight_style` is deliberately empty, the row paints itself), so the
|
||||
/// damage stayed invisible until the list grew past one page: the window is
|
||||
/// positioned for the row above the cursor, the highlighted row falls off
|
||||
/// the bottom, and the user pages down into a list with no cursor at all.
|
||||
///
|
||||
/// Cleanup never showed this because `ScanPanel::lines` holds nothing but
|
||||
/// entries — its heading lives in the card block. Same widget, one index
|
||||
/// space, no drift.
|
||||
#[test]
|
||||
fn the_app_under_the_cursor_is_the_row_that_is_on_screen() {
|
||||
crate::theme::force_color_mode(ColorMode::TrueColor);
|
||||
use ratatui::backend::TestBackend;
|
||||
use ratatui::Terminal;
|
||||
use squid_core::AppInfo;
|
||||
|
||||
fn render(app: &App) -> Terminal<TestBackend> {
|
||||
let mut term = Terminal::new(TestBackend::new(140, 34)).expect("terminal");
|
||||
term.draw(|frame| draw(frame, app)).expect("draw");
|
||||
term
|
||||
}
|
||||
/// Where a needle sits, if it sits anywhere at all.
|
||||
fn locate(term: &Terminal<TestBackend>, needle: &str) -> Option<(u16, u16)> {
|
||||
let buf = term.backend().buffer();
|
||||
for row in 0..buf.area().height {
|
||||
let mut text = String::new();
|
||||
let mut columns = Vec::new();
|
||||
for col in 0..buf.area().width {
|
||||
for ch in buf[(col, row)].symbol().chars() {
|
||||
text.push(ch);
|
||||
columns.push(col);
|
||||
}
|
||||
}
|
||||
if let Some(offset) = text.find(needle) {
|
||||
return Some((row, columns[text[..offset].chars().count()]));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
let mut app = App::new().expect("app");
|
||||
app.toast = None;
|
||||
app.panel = Panel::Uninstall;
|
||||
app.uninstall.view = UninstallView::List;
|
||||
app.uninstall.apps = (0..30)
|
||||
.map(|index| AppInfo {
|
||||
name: format!("Sq{index:02}"),
|
||||
path: format!("/Applications/Sq{index:02}.app").into(),
|
||||
source: "Applications".into(),
|
||||
size: Bytes(1 << 20),
|
||||
..Default::default()
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Every cursor position, so the test cannot pass by being lucky about
|
||||
// how tall the card happens to be: what matters is that the window
|
||||
// always covers the row the cursor is on.
|
||||
app.uninstall.filter = "sq1".into();
|
||||
let filtered = app.uninstall.visible().len();
|
||||
app.uninstall.filter.clear();
|
||||
assert!(filtered > 1, "the filter should still match a page or so");
|
||||
let cases: Vec<(&str, usize)> = (0..app.uninstall.apps.len())
|
||||
.map(|cursor| ("", cursor))
|
||||
.chain((0..filtered).map(|cursor| ("sq1", cursor)))
|
||||
.collect();
|
||||
for (filter, cursor) in cases {
|
||||
app.uninstall.filter = filter.to_string();
|
||||
app.uninstall.cursor = cursor;
|
||||
let visible = app.uninstall.visible();
|
||||
let name = app.uninstall.apps[visible[cursor]].name.clone();
|
||||
let term = render(&app);
|
||||
let label = format!("filter={filter:?} cursor={cursor} ({name})");
|
||||
let (row, col) = locate(&term, &name)
|
||||
.unwrap_or_else(|| panic!("the app under the cursor is off screen: {label}"));
|
||||
let style = term.backend().buffer()[(col, row)].style();
|
||||
assert_eq!(
|
||||
style.bg,
|
||||
Some(Palette::accent()),
|
||||
"the row on screen is not the row the cursor is on: {label}"
|
||||
);
|
||||
assert!(
|
||||
style.add_modifier.contains(Modifier::BOLD),
|
||||
"the cursor row lost its emphasis: {label}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -136,6 +136,7 @@ ratatui + crossterm。六个面板 `Cleanup / Uninstall / Purge / Storage / Stat
|
|||
与细体 `○` 两条冗余通道。`close_gaps` 负责让高亮行成为连续的一整条:列分隔用的是无样式
|
||||
`Span::raw(" ")`,反白时会在行里留下一道道暗缝,所以只在「整行非空白 span 样式一致」时把空白也染上;
|
||||
路径列只截断不补齐,因此 `item_row` 末尾还会按显示宽度补一段同色空格,让高亮条一直顶到卡片右边框。
|
||||
- **一个 widget 只有一套下标**:`ListState::selected` 是 ratatui 用来定位滚动窗口的行号,高亮却由每一行自己画,两者必须数同一批行。卸载列表把「大小/名称/版本」表头(筛选时还多一行 filter chip)和条目一起塞进同一个 `List`,`selected` 就此比样式多算了一两行:第一屏看不出来,列表一超过一页,窗口按「光标上面那一行」定位,高亮行被挤出可视区,用户看到的是「下翻之后没有高亮了」——Mac 与 Windows 同因,因为它跟终端无关。清理面板从不出现这个现象,`ScanPanel::lines` 里只有条目,标题挂在 card 的 block 上。现在的写法把表头和 filter chip 当作 chrome 单独走一段 `Layout`:`Paragraph` 拿上面,`List` 只拿条目。`the_app_under_the_cursor_is_the_row_that_is_on_screen` 遍历每一个光标位置(含筛选态)断言「光标那一行就是屏幕上那一行、且带着强调色与加粗」,因为它无法靠猜某个卡片高度通过。
|
||||
|
||||
### squid-cli
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue