Skip to content

Commit

Permalink
fix: scrollable list bar in right side
Browse files Browse the repository at this point in the history
  • Loading branch information
ckaznable committed Nov 8, 2023
1 parent dc024c1 commit d236153
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ fn main() -> Result<(), Box<dyn Error>> {
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;

let pokemon_list = PokemonListState::new(Rc::new(bundle));

// create app and run it
let app = AppState {
pokemon_list: PokemonListState {
bundle: Rc::new(bundle),
..Default::default()
},
pokemon_list,
..Default::default()
};
let res = run_app(&mut terminal, app);
Expand Down
37 changes: 29 additions & 8 deletions src/state/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@ pub struct PokemonListState {
pub profile_page: u8,
}

impl<'a> PokemonListState {
impl PokemonListState {
pub fn new(bundle: Rc<PokemonBundle>) -> Self {
let pokemon_len = bundle.pokemon.len();
let list_scrollbar_state = ScrollbarState::default().content_length(pokemon_len);

let mut list_state = ListState::default();
list_state.select(Some(0));

let filtered_list = Vec::with_capacity(pokemon_len);

Self {
bundle,
list_state,
list_scrollbar_state,
filtered_list,
..Default::default()
}
}

pub fn len(&self) -> usize {
self.bundle.pokemon.len()
}
Expand All @@ -27,11 +45,6 @@ impl<'a> PokemonListState {
self.bundle.ability.clone()
}

pub fn scroll_length(mut self, len: usize) -> Self {
self.list_scrollbar_state = self.list_scrollbar_state.content_length(len);
self
}

pub fn next(&mut self) {
let index = match self.list_state.selected() {
Some(i) => {
Expand Down Expand Up @@ -132,7 +145,7 @@ impl<'a> PokemonListState {
}
}

pub fn profile(&'a self) -> Option<Rc<PokemonEntity>> {
pub fn profile(&self) -> Option<Rc<PokemonEntity>> {
let index = self.list_state.selected()?;
if self.filter_query.is_empty() {
self.bundle.pokemon.get(index).cloned()
Expand All @@ -141,7 +154,7 @@ impl<'a> PokemonListState {
}
}

pub fn profile_with_region_form(&'a self) -> Option<Rc<PokemonEntity>> {
pub fn profile_with_region_form(&self) -> Option<Rc<PokemonEntity>> {
let profile = self.profile()?;
if self.profile_page > 0 {
profile
Expand Down Expand Up @@ -178,4 +191,12 @@ impl<'a> PokemonListState {
self.profile_page = self.profile_page.saturating_sub(1);
}
}

pub fn list_items(&self) -> &Vec<Rc<PokemonEntity>> {
if self.filter_query.is_empty() {
&self.bundle.pokemon
} else {
&self.filtered_list
}
}
}
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn ui(f: &mut Frame, app: &mut AppState) {
f.render_stateful_widget(Filter, chunks[0], app);

// pm list
f.render_stateful_widget(PokemonList, chunks[1], app);
f.render_stateful_widget(PokemonList, chunks[1], &mut app.pokemon_list);

// search input cursor
if let Some((x, y)) = app.tui.cursor {
Expand Down
28 changes: 12 additions & 16 deletions src/widget/pokemon_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use ratatui::{
widgets::{Block, Borders, List, ListItem, Scrollbar, ScrollbarOrientation, StatefulWidget},
};

use crate::{constant::LIST_H_MARGIN, state::AppState};
use crate::{constant::LIST_H_MARGIN, state::PokemonListState };

pub struct PokemonList;

impl StatefulWidget for PokemonList {
type State = AppState;
type State = PokemonListState;

fn render(
self,
Expand All @@ -24,29 +24,25 @@ impl StatefulWidget for PokemonList {
.split(area);

let items: Vec<ListItem> = state
.pokemon_list
.filtered_list
.list_items()
.iter()
.map(|item| ListItem::new(vec![Line::from(item.name_with_no())]))
.collect();

ratatui::widgets::Widget::render(
List::new(items)
.block(Block::default().borders(Borders::LEFT))
.highlight_style(
Style::default()
.bg(Color::LightGreen)
.add_modifier(Modifier::BOLD),
),
layout[0],
buf,
);
List::new(items)
.block(Block::default().borders(Borders::LEFT))
.highlight_style(
Style::default()
.bg(Color::LightGreen)
.add_modifier(Modifier::BOLD),
).render(layout[0], buf, &mut state.list_state);

Scrollbar::new(ScrollbarOrientation::VerticalRight)
.style(Style::default().bg(Color::DarkGray))
.render(
layout[0],
buf,
&mut state.pokemon_list.list_scrollbar_state.clone(),
&mut state.list_scrollbar_state,
);
}
}

0 comments on commit d236153

Please sign in to comment.