From 17ed8bfcd40e123674e55ad2367727e4f0c7f841 Mon Sep 17 00:00:00 2001 From: Sanapach Date: Fri, 20 Nov 2026 00:04:24 +0200 Subject: [PATCH] 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 --- Cargo.toml | 2 +- src/app/state.rs | 4 +++ src/app/update/podcast_discovery.rs | 6 ++++ src/app/update_tests.rs | 2 +- src/ui/mod.rs | 48 +++++++++++++++++++++++++++++ src/ui/podcast_card.rs | 22 ++++++------- src/ui/popup.rs | 21 ++++++------- 7 files changed, 80 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3804300..993e643 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/app/state.rs b/src/app/state.rs index 7a0b4fb..08c3f07 100644 --- a/src/app/state.rs +++ b/src/app/state.rs @@ -789,6 +789,10 @@ pub enum Popup { preview: Option>, /// 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, }, /// Enabling network similarity reveals an embedding query to peers. SimilarityPrivacyConsent { enable_federation: bool }, diff --git a/src/app/update/podcast_discovery.rs b/src/app/update/podcast_discovery.rs index 3099462..0e6b2ff 100644 --- a/src/app/update/podcast_discovery.rs +++ b/src/app/update/podcast_discovery.rs @@ -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 Option 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 } diff --git a/src/app/update_tests.rs b/src/app/update_tests.rs index a27439d..39be1dc 100644 --- a/src/app/update_tests.rs +++ b/src/app/update_tests.rs @@ -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()]); diff --git a/src/ui/mod.rs b/src/ui/mod.rs index c019256..7e1bba3 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -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}"); diff --git a/src/ui/podcast_card.rs b/src/ui/podcast_card.rs index b2545e9..f9a6606 100644 --- a/src/ui/podcast_card.rs +++ b/src/ui/podcast_card.rs @@ -23,6 +23,7 @@ pub(super) fn draw( hit: &PodcastHit, preview: Option<&Loadable>, scroll: u16, + max_scroll: &std::cell::Cell, ) { 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::(); - 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 { diff --git a/src/ui/popup.rs b/src/ui/popup.rs index 68334ea..5478664 100644 --- a/src/ui/popup.rs +++ b/src/ui/popup.rs @@ -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::(); - 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,