Add background color to Tree

This commit is contained in:
Joscha 2024-03-08 17:24:16 +01:00
parent c9dd8051dd
commit 859fdb4bfe
2 changed files with 8 additions and 11 deletions

View file

@ -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<f32>) -> Vec2 {
Vec2::new(point.x as i32, point.y as i32)
@ -13,12 +14,14 @@ fn size_to_vec2(size: Size<f32>) -> Vec2 {
pub struct Tree<C> {
tree: TaffyTree<BoxedWidget<C>>,
background: Srgb,
}
impl<C> Tree<C> {
pub fn new() -> Self {
pub fn new(background: Srgb) -> Self {
Self {
tree: TaffyTree::new(),
background,
}
}
@ -94,15 +97,9 @@ impl<C> Tree<C> {
// 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<C> Default for Tree<C> {
fn default() -> Self {
Self::new()
}
}

View file

@ -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::<Context>::new();
let mut tree = Tree::<Context>::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)?;