From 859fdb4bfefbe404c7fc5a191099f0350468485d Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 8 Mar 2024 17:24:16 +0100 Subject: [PATCH] Add background color to Tree --- showbits-common/src/tree.rs | 15 ++++++--------- showbits-thermal-printer/src/drawer.rs | 4 ++-- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/showbits-common/src/tree.rs b/showbits-common/src/tree.rs index e2a6d5f..ba09055 100644 --- a/showbits-common/src/tree.rs +++ b/showbits-common/src/tree.rs @@ -1,7 +1,8 @@ use image::RgbImage; +use palette::Srgb; use taffy::{AvailableSpace, NodeId, Point, Size, TaffyResult, TaffyTree}; -use crate::{BoxedWidget, Rect, Vec2, View}; +use crate::{color, BoxedWidget, Rect, Vec2, View}; fn point_to_vec2(point: Point) -> Vec2 { Vec2::new(point.x as i32, point.y as i32) @@ -13,12 +14,14 @@ fn size_to_vec2(size: Size) -> Vec2 { pub struct Tree { tree: TaffyTree>, + background: Srgb, } impl Tree { - pub fn new() -> Self { + pub fn new(background: Srgb) -> Self { Self { tree: TaffyTree::new(), + background, } } @@ -94,15 +97,9 @@ impl Tree { // TODO Check how taffy treats the border? let (width, height) = size_to_vec2(layout.size).to_u32(); - let mut image = RgbImage::new(width, height); + let mut image = RgbImage::from_pixel(width, height, color::to_image_rgb(self.background)); self.render_to_view(ctx, root, &mut View::new(&mut image))?; Ok(image) } } - -impl Default for Tree { - fn default() -> Self { - Self::new() - } -} diff --git a/showbits-thermal-printer/src/drawer.rs b/showbits-thermal-printer/src/drawer.rs index 733a59a..255ff4d 100644 --- a/showbits-thermal-printer/src/drawer.rs +++ b/showbits-thermal-printer/src/drawer.rs @@ -1,6 +1,7 @@ use cosmic_text::{Attrs, Metrics}; use palette::Srgb; use showbits_common::{ + color, widgets::{FontStuff, HasFontStuff, Text}, Tree, WidgetExt, }; @@ -66,7 +67,7 @@ impl Drawer { } fn on_test(&mut self) -> anyhow::Result<()> { - let mut tree = Tree::::new(); + let mut tree = Tree::::new(Srgb::new(1.0, 1.0, 1.0)); let root = Text::simple( &mut self.ctx.font_stuff, @@ -74,7 +75,6 @@ impl Drawer { Attrs::new(), "Hello world!", ) - .color(Srgb::new(1.0, 1.0, 1.0)) .node() .margin_all(length(10.0)) .register(&mut tree)?;