From 3a5d8e929dcb1d3b53704b7302679ed825c513fa Mon Sep 17 00:00:00 2001 From: emilis Date: Mon, 23 Jan 2023 00:38:08 +0000 Subject: [PATCH] remove fixed widget widths --- kkdisp/src/component.rs | 42 +++++++++++------------------------------ kkx/src/main.rs | 4 ++-- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/kkdisp/src/component.rs b/kkdisp/src/component.rs index bc1a9b7..86ef006 100644 --- a/kkdisp/src/component.rs +++ b/kkdisp/src/component.rs @@ -91,26 +91,9 @@ pub struct Line { components: Vec, } -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum SectionWidth { - Full, - Third, - TwoThirds, -} - -impl SectionWidth { - pub fn abs_size(&self, max_width: usize) -> usize { - match self { - SectionWidth::Full => max_width, - SectionWidth::Third => max_width / 3, - SectionWidth::TwoThirds => (max_width / 3) * 2, - } - } -} - #[derive(Debug, Clone, Eq)] pub struct Widget { - want_width: SectionWidth, + want_width: u8, per_line: Vec, } @@ -126,12 +109,9 @@ impl PartialEq for Widget { } impl Widget { - pub fn new( - width: SectionWidth, - tokens_per_line: Vec, - ) -> Self { + pub fn new(width_pct: u8, tokens_per_line: Vec) -> Self { Self { - want_width: width, + want_width: width_pct, per_line: tokens_per_line, } } @@ -199,7 +179,9 @@ impl Instruction { .into_iter() .map(|w| { let (width, token) = ( - w.want_width.abs_size(line_width), + (w.want_width as usize * line_width) + / 100 + + 1, // Feels wrong? w.get_line(line).map(|t| t.clone()), ); let mut result = match token { @@ -268,7 +250,7 @@ impl Plan { fn fit_check(widgets: &Vec) { if widgets .into_iter() - .map(|wd| wd.want_width.abs_size(100)) + .map(|wd| wd.want_width as usize) .sum::() > 100 { @@ -355,21 +337,19 @@ impl Plan { #[cfg(test)] mod tests { use super::*; + const THIRD: u8 = 100 / 3; fn test_widgets() -> (Widget, Widget, Widget) { ( + Widget::new(THIRD, vec![Token::text("hello").centered()]), Widget::new( - SectionWidth::Third, - vec![Token::text("hello").centered()], - ), - Widget::new( - SectionWidth::Third, + THIRD, vec![Token::text("hello") .pad_char('*', 16) .bg(Color::RED)], ), Widget::new( - SectionWidth::Third, + THIRD, vec![Token::text("hello").limited(16).padded(20)], ), ) diff --git a/kkx/src/main.rs b/kkx/src/main.rs index 6952c48..a067998 100644 --- a/kkx/src/main.rs +++ b/kkx/src/main.rs @@ -24,7 +24,7 @@ impl Default for App { Plan::start().fill(vec![]).fixed( 4, vec![Widget::new( - kkdisp::component::SectionWidth::Full, + 100, vec![ Token::End, Token::text("click me") @@ -36,7 +36,7 @@ impl Default for App { Plan::start().fill(vec![]).fixed( 4, vec![Widget::new( - kkdisp::component::SectionWidth::Full, + 100, vec![ Token::End, Token::text(