Show vertical scrollbar only if height is lower than total monitors

This commit is contained in:
Marco De Araujo 2026-01-08 09:25:26 -04:00
parent d4e3ce6e27
commit 142f76684b
3 changed files with 105 additions and 63 deletions

View file

@ -16,43 +16,29 @@ use ratatui::{
const STATUS_LINE_LENGTH: usize = 100;
const MAX_NAME_LENGTH: usize = 30;
pub fn render_monitor_list(frame: &mut Frame, area: Rect, state: &DashboardViewState) {
let group_areas = layout_groups(area, &state.groups);
pub fn render_monitor_list(main_frame: &mut Frame, area: Rect, state: &DashboardViewState) {
let constraints: Vec<Constraint> = state
.groups
.iter()
.map(|g| {
let height_neeed = 3 + g.monitors.len();
Constraint::Length(height_neeed as u16)
})
.collect();
if constraints.is_empty() {
return;
}
let group_areas = Layout::vertical(constraints).split(area);
for (i, (group, &group_area)) in state.groups.iter().zip(group_areas.iter()).enumerate() {
render_group(frame, group_area, group, i == 0);
if group_area.height > 0 {
render_group(main_frame, group_area, group, i == 0);
}
}
}
fn layout_groups(area: Rect, groups: &[GroupViewState]) -> Vec<Rect> {
let total_lines: usize = groups.iter().map(|g| g.monitors.len() + 1).sum();
if total_lines == 0 {
return Vec::new();
}
let content_height = area.height.saturating_sub(2);
let line_height = content_height as usize / total_lines;
let mut current_y = area.y + 1;
let mut areas = Vec::with_capacity(groups.len());
for group in groups {
let group_lines = group.monitors.len() + 2;
let group_height = (group_lines + line_height).max(1);
areas.push(Rect {
x: area.x,
y: current_y,
width: area.width,
height: group_height as u16,
});
current_y += group_height as u16;
}
areas
}
fn render_group(frame: &mut Frame, area: Rect, group: &GroupViewState, is_first: bool) {
let chunks = Layout::default()
.direction(Direction::Vertical)
@ -60,6 +46,10 @@ fn render_group(frame: &mut Frame, area: Rect, group: &GroupViewState, is_first:
.constraints([Constraint::Length(1), Constraint::Min(1)])
.split(area);
if chunks[0].height <= 0 || chunks[1].height <= 0 || group.monitors.is_empty() {
return;
}
let group_title = Line::from(vec![
Span::styled(format!("{} ", group.name), title_style()),
Span::styled(