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

+1 -1
View File
@@ -33,7 +33,7 @@ lofty = "0.22"
# P2P federation: library index in a shared DHT + audio streaming between
# peers (same protocol as furumi-fd).
music-dht = { version = "0.6.0", features = ["podcast-feed"] }
ratatui = "0.30.1"
ratatui = { version = "0.30.1", features = ["unstable-rendered-line-info"] }
reqwest = { version = "0.12.28", default-features = false, features = ["rustls-tls", "stream", "blocking"] }
rhai = { version = "1", features = ["sync"] }
rodio = { version = "0.22.2", default-features = false, features = ["playback", "mp3", "flac", "vorbis", "wav", "symphonia-aac", "symphonia-isomp4", "symphonia-alac"] }
+4
View File
@@ -789,6 +789,10 @@ pub enum Popup {
preview: Option<Loadable<crate::podcasts::discovery::FeedPreview>>,
/// First body line shown.
scroll: u16,
/// Largest useful `scroll` at the last draw (the wrapped body
/// height minus the visible rows); scroll keys stop there.
/// `u16::MAX` until the card was drawn.
max_scroll: std::cell::Cell<u16>,
},
/// Enabling network similarity reveals an embedding query to peers.
SimilarityPrivacyConsent { enable_federation: bool },
+6
View File
@@ -26,6 +26,7 @@ pub fn open_card(state: &mut AppState, index: usize) {
hit: Box::new(hit.clone()),
preview: None,
scroll: 0,
max_scroll: std::cell::Cell::new(u16::MAX),
});
}
@@ -37,6 +38,7 @@ pub fn card_key(state: &mut AppState, popup: Popup, key: KeyCode) -> Option<Effe
hit,
mut preview,
mut scroll,
max_scroll,
} = popup
else {
state.popup = Some(popup);
@@ -68,10 +70,14 @@ pub fn card_key(state: &mut AppState, popup: Popup, key: KeyCode) -> Option<Effe
KeyCode::Home | KeyCode::Char('g') => scroll = 0,
_ => {}
}
// Never past the end the last draw measured, so scrolling back moves
// the view at once.
scroll = scroll.min(max_scroll.get());
state.popup = Some(Popup::PodcastCard {
hit,
preview,
scroll,
max_scroll,
});
effect
}
+1 -1
View File
@@ -2594,7 +2594,7 @@ mod podcast_discovery {
assert!(state.player.queue.is_empty());
assert!(matches!(
&state.popup,
Some(Popup::PodcastCard { hit, preview: None, scroll: 0 }) if hit.title() == "Rust Radio"
Some(Popup::PodcastCard { hit, preview: None, scroll: 0, .. }) if hit.title() == "Rust Radio"
));
// The music rows keep their behavior.
let mut state = search_state(Vec::new(), vec![claimed_hit()]);
+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,