Show vertical scrollbar only if height is lower than total monitors
This commit is contained in:
parent
d4e3ce6e27
commit
142f76684b
3 changed files with 105 additions and 63 deletions
|
|
@ -18,7 +18,7 @@ use ratatui::{
|
|||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
style::{Color, Modifier, Style},
|
||||
text::{Line, Span, Text},
|
||||
widgets::{Block, Borders, Padding, Paragraph},
|
||||
widgets::{Block, Borders, Padding, Paragraph, Scrollbar, ScrollbarOrientation},
|
||||
};
|
||||
|
||||
use std::{
|
||||
|
|
@ -128,14 +128,26 @@ impl App {
|
|||
|
||||
render_header(frame, chunks[0], &self.state);
|
||||
|
||||
let mut main_constraint = Vec::with_capacity(2);
|
||||
main_constraint.push(Constraint::Min(1));
|
||||
|
||||
if self.state.show_vertical_scrollbar(chunks[1].height) {
|
||||
main_constraint.push(Constraint::Length(1));
|
||||
}
|
||||
|
||||
let main_chunks = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints(main_constraint)
|
||||
.split(chunks[1]);
|
||||
|
||||
if self.state.is_loading {
|
||||
Self::render_loading(frame, chunks[1]);
|
||||
Self::render_loading(frame, main_chunks[0]);
|
||||
} else if let Some(error) = &self.state.error_message {
|
||||
Self::render_error(frame, chunks[1], error);
|
||||
Self::render_error(frame, main_chunks[0], error);
|
||||
} else if self.state.groups.is_empty() || self.state.get_all_monitors().is_empty() {
|
||||
Self::render_no_data(frame, chunks[1]);
|
||||
Self::render_no_data(frame, main_chunks[0]);
|
||||
} else {
|
||||
render_monitor_list(frame, chunks[1], &self.state);
|
||||
Self::render_main(frame, main_chunks.to_vec(), &mut self.state);
|
||||
}
|
||||
|
||||
let seconds_until_update = self
|
||||
|
|
@ -148,6 +160,22 @@ impl App {
|
|||
});
|
||||
}
|
||||
|
||||
fn render_main(frame: &mut Frame, area: Vec<Rect>, state: &mut DashboardViewState) {
|
||||
render_monitor_list(frame, area[0], state);
|
||||
|
||||
let total_monitors = state.get_all_monitors().len();
|
||||
|
||||
if area[0].height > total_monitors as u16 {
|
||||
return;
|
||||
}
|
||||
|
||||
let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
|
||||
.begin_symbol(Some("↑"))
|
||||
.end_symbol(Some("↓"));
|
||||
|
||||
frame.render_stateful_widget(scrollbar, area[1], &mut state.scroll_state);
|
||||
}
|
||||
|
||||
fn handle_events(&mut self) -> io::Result<()> {
|
||||
let timeout = Duration::from_secs(1);
|
||||
|
||||
|
|
@ -159,6 +187,12 @@ impl App {
|
|||
|
||||
match key.code {
|
||||
KeyCode::Char('q') | KeyCode::Esc => self.should_quit = true,
|
||||
KeyCode::Up | KeyCode::Char('k') => {
|
||||
self.state.scroll_state.prev();
|
||||
}
|
||||
KeyCode::Down | KeyCode::Char('j') => {
|
||||
self.state.scroll_state.next();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue