Scroll the podcast card exactly to its end

The card and the share dialog measure their word-wrapped height the
way it is rendered (ratatui's rendered-line-info), and scroll keys stop
at the last line instead of running past it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-20 15:13:07 +02:00
1 parent fd6827126f
commit 17ed8bfcd4
7 files changed
+80 -25

No files matched your search

+48
View File
@@ -245,6 +245,51 @@ mod tests {
assert!(screen.contains("private (not shared"), "{screen}");
}
#[test]
fn the_podcast_card_scrolls_exactly_to_its_last_line() {
use crate::app::state::Popup;
use crate::app::update::podcast_discovery::card_key;
use crossterm::event::KeyCode;
let mut state = search_with_podcasts();
let mut hit = state.search.fed_podcasts[1].clone();
// Long words word-wrap into far more rows than their character
// count suggests (one 50-character word per 90-column row).
let word = "x".repeat(50);
hit.hit.card.description = Some(vec![word.as_str(); 30].join(" "));
state.popup = Some(Popup::PodcastCard {
hit: Box::new(hit),
preview: None,
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
let last_line = "e — прочитать ленту";
let screen = render(&state, 110, 30);
assert!(!screen.contains(last_line), "below the fold:\n{screen}");
// Paging far past the end stops at the last line ...
for _ in 0..20 {
let popup = state.popup.take().unwrap();
assert_eq!(card_key(&mut state, popup, KeyCode::PageDown), None);
}
let screen = render(&state, 110, 30);
assert!(screen.contains(last_line), "{screen}");
let Some(Popup::PodcastCard {
scroll, max_scroll, ..
}) = &state.popup
else {
panic!("the card is open");
};
assert_eq!(*scroll, max_scroll.get());
let end = *scroll;
// ... and one step back moves the view at once.
let popup = state.popup.take().unwrap();
card_key(&mut state, popup, KeyCode::Up);
assert!(matches!(
&state.popup,
Some(Popup::PodcastCard { scroll, .. }) if *scroll == end - 1
));
}
/// Search view with a federated artist and two public podcast cards;
/// the first podcast is subscribed on this device.
fn search_with_podcasts() -> AppState {
@@ -308,6 +353,7 @@ mod tests {
hit: Box::new(hit.clone()),
preview: None,
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
let screen = render(&state, 110, 40);
assert!(screen.contains("[podcast] Other Show"), "{screen}");
@@ -354,6 +400,7 @@ mod tests {
subscribed_as: None,
})),
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
let screen = render(&state, 110, 40);
assert!(
@@ -375,6 +422,7 @@ mod tests {
hit: Box::new(state.search.fed_podcasts[0].clone()),
preview: None,
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
let screen = render(&state, 110, 40);
assert!(screen.contains("✓ Уже подписан"), "{screen}");
+10 -12
View File
@@ -23,6 +23,7 @@ pub(super) fn draw(
hit: &PodcastHit,
preview: Option<&Loadable<FeedPreview>>,
scroll: u16,
max_scroll: &std::cell::Cell<u16>,
) {
let subscribed =
crate::app::update::podcast_discovery::subscribed_id(state, hit, preview).is_some();
@@ -31,11 +32,12 @@ pub(super) fn draw(
let width = 92.min(frame.area().width.saturating_sub(4)).max(30);
let inner_width = width.saturating_sub(2).max(1);
let body_height = lines
.iter()
.map(|line| (line.width().max(1) as u16).div_ceil(inner_width))
.sum::<u16>();
let height = (body_height + 4).min(frame.area().height.saturating_sub(2));
let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false });
// The height of the word-wrapped text, as it is rendered.
let body_height = u16::try_from(paragraph.line_count(inner_width)).unwrap_or(u16::MAX);
let height = body_height
.saturating_add(4)
.min(frame.area().height.saturating_sub(2));
let area = centered(frame.area(), width, height);
let block = Block::bordered()
.title(" Подкаст · публичная карточка ")
@@ -46,13 +48,9 @@ pub(super) fn draw(
frame.render_widget(block, area);
let [body, footer_area] =
Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(inner);
let max_scroll = body_height.saturating_sub(body.height);
frame.render_widget(
Paragraph::new(lines)
.wrap(Wrap { trim: false })
.scroll((scroll.min(max_scroll), 0)),
body,
);
let last = body_height.saturating_sub(body.height);
max_scroll.set(last);
frame.render_widget(paragraph.scroll((scroll.min(last), 0)), body);
let footer = if subscribed {
"enter открыть · e выпуски · ↑↓ прокрутка · esc закрыть"
} else {
+10 -11
View File
@@ -32,7 +32,8 @@ pub fn draw(frame: &mut Frame, state: &AppState) {
hit,
preview,
scroll,
}) => super::podcast_card::draw(frame, state, hit, preview.as_ref(), *scroll),
max_scroll,
}) => super::podcast_card::draw(frame, state, hit, preview.as_ref(), *scroll, max_scroll),
Some(Popup::ConfirmPodcastShare {
title,
urls,
@@ -1386,14 +1387,12 @@ fn draw_confirm_podcast_share(
"enter/y опубликовать · esc/n отмена"
};
let inner_width = width.saturating_sub(2).max(1);
let body_height = lines
.iter()
.map(|line| {
let text_width = line.width().max(1) as u16;
text_width.div_ceil(inner_width)
})
.sum::<u16>();
let height = (body_height + 4).min(frame.area().height.saturating_sub(2));
let body = Paragraph::new(lines).wrap(Wrap { trim: false });
// The height of the word-wrapped text, as it is rendered.
let body_height = u16::try_from(body.line_count(inner_width)).unwrap_or(u16::MAX);
let height = body_height
.saturating_add(4)
.min(frame.area().height.saturating_sub(2));
let area = centered(frame.area(), width, height);
let block = Block::bordered()
.title(" Поделиться в федерации ")
@@ -1402,9 +1401,9 @@ fn draw_confirm_podcast_share(
let inner = block.inner(area);
frame.render_widget(Clear, area);
frame.render_widget(block, area);
let [body, footer_area] =
let [body_area, footer_area] =
Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(inner);
frame.render_widget(Paragraph::new(lines).wrap(Wrap { trim: false }), body);
frame.render_widget(body, body_area);
frame.render_widget(
Paragraph::new(Line::styled(footer, theme::dim())).alignment(Alignment::Center),
footer_area,