Skip to content

Commit

Permalink
refactor(widgets): add scroll methods to state structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenMian committed Dec 17, 2024
1 parent f6ccb22 commit 6864324
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
32 changes: 16 additions & 16 deletions src/widgets/object_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ pub struct ObjectInformationState {
geocoder: ReverseGeocoder,
}

impl ObjectInformationState {
pub fn scroll_up(&mut self) {
*self.table_state.offset_mut() = self.table_state.offset().saturating_sub(1);
}

pub fn scroll_down(&mut self) {
let max_offset = self
.items
.len()
.saturating_sub(self.inner_area.height as usize);
*self.table_state.offset_mut() = (self.table_state.offset() + 1).min(max_offset);
}
}

impl Default for ObjectInformationState {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -182,22 +196,8 @@ pub async fn handle_mouse_events(event: MouseEvent, app: &mut App) -> Result<()>
}
}
}
MouseEventKind::ScrollDown => {
let max_offset = app
.object_information_state
.items
.len()
.saturating_sub(inner_area.height as usize);
*app.object_information_state.table_state.offset_mut() =
(*app.object_information_state.table_state.offset_mut() + 1).min(max_offset);
}
MouseEventKind::ScrollUp => {
*app.object_information_state.table_state.offset_mut() = app
.object_information_state
.table_state
.offset()
.saturating_sub(1);
}
MouseEventKind::ScrollUp => app.object_information_state.scroll_up(),
MouseEventKind::ScrollDown => app.object_information_state.scroll_down(),
_ => {}
}
// Highlight the hovered row.
Expand Down
27 changes: 14 additions & 13 deletions src/widgets/satellites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ impl SatellitesState {
}
}
}

pub fn scroll_up(&mut self) {
*self.list_state.offset_mut() = self.list_state.offset().saturating_sub(1);
}

pub fn scroll_down(&mut self) {
let max_offset = self
.items
.len()
.saturating_sub(self.inner_area.height as usize);
*self.list_state.offset_mut() = (self.list_state.offset() + 1).min(max_offset);
}
}

impl Default for SatellitesState {
Expand Down Expand Up @@ -138,19 +150,8 @@ pub async fn handle_mouse_events(event: MouseEvent, app: &mut App) -> Result<()>
app.satellites_state.refresh_objects().await;
}
}
MouseEventKind::ScrollDown => {
let max_offset = app
.satellites_state
.items
.len()
.saturating_sub(inner_area.height as usize);
*app.satellites_state.list_state.offset_mut() =
(app.satellites_state.list_state.offset() + 1).min(max_offset);
}
MouseEventKind::ScrollUp => {
*app.satellites_state.list_state.offset_mut() =
app.satellites_state.list_state.offset().saturating_sub(1);
}
MouseEventKind::ScrollUp => app.satellites_state.scroll_up(),
MouseEventKind::ScrollDown => app.satellites_state.scroll_down(),
_ => {}
}
// Highlight the hovered item.
Expand Down

0 comments on commit 6864324

Please sign in to comment.