remove fixed widget widths

This commit is contained in:
emilis 2023-01-23 00:38:08 +00:00
parent 9471d47ade
commit 3a5d8e929d
2 changed files with 13 additions and 33 deletions

View File

@ -91,26 +91,9 @@ pub struct Line {
components: Vec<Component>, components: Vec<Component>,
} }
#[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)] #[derive(Debug, Clone, Eq)]
pub struct Widget { pub struct Widget {
want_width: SectionWidth, want_width: u8,
per_line: Vec<Token>, per_line: Vec<Token>,
} }
@ -126,12 +109,9 @@ impl PartialEq for Widget {
} }
impl Widget { impl Widget {
pub fn new( pub fn new(width_pct: u8, tokens_per_line: Vec<Token>) -> Self {
width: SectionWidth,
tokens_per_line: Vec<Token>,
) -> Self {
Self { Self {
want_width: width, want_width: width_pct,
per_line: tokens_per_line, per_line: tokens_per_line,
} }
} }
@ -199,7 +179,9 @@ impl Instruction {
.into_iter() .into_iter()
.map(|w| { .map(|w| {
let (width, token) = ( 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()), w.get_line(line).map(|t| t.clone()),
); );
let mut result = match token { let mut result = match token {
@ -268,7 +250,7 @@ impl Plan {
fn fit_check(widgets: &Vec<Widget>) { fn fit_check(widgets: &Vec<Widget>) {
if widgets if widgets
.into_iter() .into_iter()
.map(|wd| wd.want_width.abs_size(100)) .map(|wd| wd.want_width as usize)
.sum::<usize>() .sum::<usize>()
> 100 > 100
{ {
@ -355,21 +337,19 @@ impl Plan {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
const THIRD: u8 = 100 / 3;
fn test_widgets() -> (Widget, Widget, Widget) { fn test_widgets() -> (Widget, Widget, Widget) {
( (
Widget::new(THIRD, vec![Token::text("hello").centered()]),
Widget::new( Widget::new(
SectionWidth::Third, THIRD,
vec![Token::text("hello").centered()],
),
Widget::new(
SectionWidth::Third,
vec![Token::text("hello") vec![Token::text("hello")
.pad_char('*', 16) .pad_char('*', 16)
.bg(Color::RED)], .bg(Color::RED)],
), ),
Widget::new( Widget::new(
SectionWidth::Third, THIRD,
vec![Token::text("hello").limited(16).padded(20)], vec![Token::text("hello").limited(16).padded(20)],
), ),
) )

View File

@ -24,7 +24,7 @@ impl Default for App {
Plan::start().fill(vec![]).fixed( Plan::start().fill(vec![]).fixed(
4, 4,
vec![Widget::new( vec![Widget::new(
kkdisp::component::SectionWidth::Full, 100,
vec![ vec![
Token::End, Token::End,
Token::text("click me") Token::text("click me")
@ -36,7 +36,7 @@ impl Default for App {
Plan::start().fill(vec![]).fixed( Plan::start().fill(vec![]).fixed(
4, 4,
vec![Widget::new( vec![Widget::new(
kkdisp::component::SectionWidth::Full, 100,
vec![ vec![
Token::End, Token::End,
Token::text( Token::text(