refactor(tui): widgets (besides clockw) are now visible in small horizontal terminals #11
cargo devel CI / cargo CI (push) Successful in 1m50s Details

This commit is contained in:
Christoph J. Scherr 2024-07-12 16:23:26 +02:00
parent 9a2629c273
commit b25c3f4c46
1 changed files with 27 additions and 25 deletions

View File

@ -325,7 +325,7 @@ impl Clock {
data: &UiData, data: &UiData,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let clockw = tui_big_text::BigText::builder() let clockw = tui_big_text::BigText::builder()
.style(Style::new().red().on_light_blue()) .style(Style::new().red())
.lines(vec![data.ftime().into()]) .lines(vec![data.ftime().into()])
.alignment(Alignment::Center) .alignment(Alignment::Center)
.build() .build()
@ -335,10 +335,10 @@ impl Clock {
let root = frame.size(); let root = frame.size();
let space = Block::bordered() let space = Block::bordered()
.padding(Padding::new( .padding(Padding::new(
root.width / 8, root.width / 16,
root.width / 8, root.width / 16,
root.height / 8, root.height / 16,
root.height / 8, root.height / 16,
)) ))
.title(env!("CARGO_PKG_NAME")) .title(env!("CARGO_PKG_NAME"))
.title_bottom(env!("CARGO_PKG_VERSION")) .title_bottom(env!("CARGO_PKG_VERSION"))
@ -365,7 +365,7 @@ impl Clock {
} }
} }
let tmp = LineGauge::default() let widget = LineGauge::default()
.filled_style(if self.did_notify { .filled_style(if self.did_notify {
Style::default() Style::default()
.slow_blink() .slow_blink()
@ -376,16 +376,10 @@ impl Clock {
} else { } else {
Style::default().blue() Style::default().blue()
}) })
.on_cyan()
.unfilled_style(Style::default()) .unfilled_style(Style::default())
.block(Block::new().padding(Padding::new( .block(Block::default().padding(Padding::right(parts[2].width / 5)))
parts[2].left() / 10,
parts[2].right() / 6,
0,
0,
)))
.ratio(ratio); .ratio(ratio);
Some(tmp) Some(widget)
} else { } else {
None None
}; };
@ -393,14 +387,8 @@ impl Clock {
// render the small date // render the small date
let datew = Paragraph::new(data.fdate()) let datew = Paragraph::new(data.fdate())
.blue() .blue()
.alignment(Alignment::Left) .block(Block::default().padding(Padding::right(2)))
.on_gray() .alignment(Alignment::Right);
.block(Block::new().padding(Padding::new(
parts[1].left(),
parts[1].right() / 3,
0,
0,
)));
frame.render_widget(&timebarw, parts[2]); frame.render_widget(&timebarw, parts[2]);
frame.render_widget(datew, parts[1]); frame.render_widget(datew, parts[1]);
// render the clock // render the clock
@ -482,15 +470,29 @@ impl Clock {
Ok(()) Ok(())
} }
fn partition(r: Rect) -> Vec<Rect> { fn partition(r: Rect) -> Vec<Rect> {
const OFFSET: u16 = 24;
let part = Layout::default() let part = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints([Constraint::Length(8), Constraint::Min(0)]) .constraints([Constraint::Length(8), Constraint::Length(3)])
.split(r); .split(r);
let subparts = Layout::default() let subparts = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints([Constraint::Min(10), Constraint::Ratio(1, 2)]) .constraints([
.split(part[1]); Constraint::Max(OFFSET),
Constraint::Max(part[0].width - OFFSET),
])
.split(Self::centered_rect(part[1], 65));
vec![part[0], subparts[0], subparts[1]] 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]
}
} }