From 70a0c73fb106abf5b9bf90974130e48e4610305c Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 12 Jul 2024 16:23:26 +0200 Subject: [PATCH] refactor(tui): widgets (besides clockw) are now visible in small horizontal terminals #11 --- src/clock.rs | 54 ++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/clock.rs b/src/clock.rs index 2cc82e9..e2f0c30 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -325,7 +325,7 @@ impl Clock { data: &UiData, ) -> anyhow::Result<()> { let clockw = tui_big_text::BigText::builder() - .style(Style::new().red().on_light_blue()) + .style(Style::new().red()) .lines(vec![data.ftime().into()]) .alignment(Alignment::Center) .build() @@ -335,10 +335,10 @@ impl Clock { let root = frame.size(); let space = Block::bordered() .padding(Padding::new( - root.width / 8, - root.width / 8, - root.height / 8, - root.height / 8, + root.width / 16, + root.width / 16, + root.height / 16, + root.height / 16, )) .title(env!("CARGO_PKG_NAME")) .title_bottom(env!("CARGO_PKG_VERSION")) @@ -365,7 +365,8 @@ impl Clock { } } - let tmp = LineGauge::default() + let widget = LineGauge::default() + .on_red() .filled_style(if self.did_notify { Style::default() .slow_blink() @@ -376,16 +377,10 @@ impl Clock { } else { Style::default().blue() }) - .on_cyan() .unfilled_style(Style::default()) - .block(Block::new().padding(Padding::new( - parts[2].left() / 10, - parts[2].right() / 6, - 0, - 0, - ))) + .block(Block::default().padding(Padding::right(parts[2].width / 5))) .ratio(ratio); - Some(tmp) + Some(widget) } else { None }; @@ -393,14 +388,9 @@ impl Clock { // render the small date let datew = Paragraph::new(data.fdate()) .blue() - .alignment(Alignment::Left) - .on_gray() - .block(Block::new().padding(Padding::new( - parts[1].left(), - parts[1].right() / 3, - 0, - 0, - ))); + .on_yellow() + .block(Block::default().padding(Padding::right(2))) + .alignment(Alignment::Right); frame.render_widget(&timebarw, parts[2]); frame.render_widget(datew, parts[1]); // render the clock @@ -482,15 +472,29 @@ impl Clock { Ok(()) } fn partition(r: Rect) -> Vec { + const OFFSET: u16 = 24; let part = Layout::default() .direction(Direction::Vertical) - .constraints([Constraint::Length(8), Constraint::Min(0)]) + .constraints([Constraint::Length(8), Constraint::Length(3)]) .split(r); let subparts = Layout::default() .direction(Direction::Horizontal) - .constraints([Constraint::Min(10), Constraint::Ratio(1, 2)]) - .split(part[1]); + .constraints([ + Constraint::Max(OFFSET), + Constraint::Max(part[0].width - OFFSET), + ]) + .split(Self::centered_rect(part[1], 65)); vec![part[0], subparts[0], subparts[1]] } + fn centered_rect(r: Rect, percent_x: u16) -> Rect { + Layout::default() + .direction(Direction::Horizontal) + .constraints([ + Constraint::Percentage((100 - percent_x) / 2), + Constraint::Percentage(percent_x), + Constraint::Percentage((100 - percent_x) / 2), + ]) + .split(r)[1] + } }