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>,
}
#[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<Token>,
}
@ -126,12 +109,9 @@ impl PartialEq for Widget {
}
impl Widget {
pub fn new(
width: SectionWidth,
tokens_per_line: Vec<Token>,
) -> Self {
pub fn new(width_pct: u8, tokens_per_line: Vec<Token>) -> 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<Widget>) {
if widgets
.into_iter()
.map(|wd| wd.want_width.abs_size(100))
.map(|wd| wd.want_width as usize)
.sum::<usize>()
> 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)],
),
)

View File

@ -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(