Skip to content

Commit

Permalink
Scroll episodes list on selection
Browse files Browse the repository at this point in the history
  • Loading branch information
FakeMichau committed Jul 4, 2023
1 parent baf021d commit 5cdfb86
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lma_tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn handle_main_menu_key<B: Backend, T: Service + Send>(
app.list_state
.move_progress(&SelectionDirection::Previous, &mut app.anime_list, rt)?;
} else if key.code == key_binds.forwards || key.code == key_binds.confirmation {
app.list_state.select()?;
app.list_state.select(app.list_state.last_height)?;
} else if key.code == key_binds.backwards {
app.list_state.unselect();
} else if key.code == key_binds.delete {
Expand Down
11 changes: 10 additions & 1 deletion lma_tui/src/ui/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct StatefulList {
selected_local_id: usize,
list_cache: Vec<Show>,
scroll_progress: usize,
pub last_height: u16,
}

impl StatefulList {
Expand All @@ -33,6 +34,7 @@ impl StatefulList {
selected_local_id: 0,
list_cache,
scroll_progress: 0,
last_height: 0,
})
}

Expand Down Expand Up @@ -120,7 +122,7 @@ impl StatefulList {
Ok(())
}

pub fn select(&mut self) -> Result<(), String> {
pub fn select(&mut self, height: u16) -> Result<(), String> {
if self.selecting_episode {
// navigating inside the episodes tab
let selected_episode = self.episodes_state.selected().unwrap_or_default();
Expand Down Expand Up @@ -150,12 +152,18 @@ impl StatefulList {
.map_or(0, |pos| (pos + 1) % show.episodes.len());

self.episodes_state.select(Some(index));
let height = height.checked_sub(1).unwrap_or_default() as usize;
if show.episodes.len() > height {
*self.episodes_state.offset_mut() =
index.checked_sub(height / 2).unwrap_or_default();
}
self.set_selecting_episode(true);
}
}
}
Ok(())
}

pub fn unselect(&mut self) {
self.episodes_state.select(None);
self.set_selecting_episode(false);
Expand Down Expand Up @@ -469,6 +477,7 @@ mod tests {
selected_local_id: 0,
list_cache: generate_test_shows(count),
scroll_progress: 0,
last_height: 0,
}
}

Expand Down
1 change: 1 addition & 0 deletions lma_tui/src/ui/main_menu/episodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn render<B: Backend, T: Service>(app: &mut App<T>, area: Rect, frame: &mut
let mut header = app.config.headers().episodes.clone();

let (table_area, scrollbar_area) = get_inner_layout(area);
app.list_state.last_height = table_area.height;

let selected_show = app.list_state.selected_show().cloned();
#[allow(clippy::cast_precision_loss)]
Expand Down

0 comments on commit 5cdfb86

Please sign in to comment.