From b9330a8bfd138d60a259dceac2bcf1f1436e0daf Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Thu, 2 Nov 2023 00:01:39 +0000 Subject: [PATCH] Add background_color support to TaffyLayout --- src/view/taffy_layout.rs | 15 +++++++++++++-- src/widget/taffy_layout.rs | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/view/taffy_layout.rs b/src/view/taffy_layout.rs index d378cc858..5ef49217d 100644 --- a/src/view/taffy_layout.rs +++ b/src/view/taffy_layout.rs @@ -14,6 +14,8 @@ use std::{any::Any, marker::PhantomData}; +use vello::peniko::Color; + use crate::view::{Id, VecSplice, ViewMarker, ViewSequence}; use crate::widget::{self, ChangeFlags}; use crate::MessageResult; @@ -36,6 +38,7 @@ use super::{Cx, View}; pub struct TaffyLayout> { children: VT, style: taffy::Style, + background_color: Option, phantom: PhantomData (T, A)>, } @@ -63,6 +66,7 @@ impl> TaffyLayout { display, ..Default::default() }, + background_color: None, phantom, } } @@ -77,6 +81,7 @@ impl> TaffyLayout { flex_direction, ..Default::default() }, + background_color: None, phantom, } } @@ -85,6 +90,11 @@ impl> TaffyLayout { style_modifier(&mut self.style); self } + + pub fn with_background_color(mut self, color: impl Into) -> Self { + self.background_color = Some(color.into()); + self + } } impl> ViewMarker for TaffyLayout {} @@ -97,7 +107,7 @@ impl> View for TaffyLayout { fn build(&self, cx: &mut Cx) -> (Id, Self::State, Self::Element) { let mut elements = vec![]; let (id, state) = cx.with_new_id(|cx| self.children.build(cx, &mut elements)); - let column = widget::TaffyLayout::new(elements, self.style.clone()); + let column = widget::TaffyLayout::new(elements, self.style.clone(), self.background_color); (id, state, column) } @@ -119,7 +129,8 @@ impl> View for TaffyLayout { if self.style != prev.style { element.style = self.style.clone(); - flags |= ChangeFlags::LAYOUT; + element.background_color = self.background_color; + flags |= ChangeFlags::LAYOUT | ChangeFlags::PAINT; } flags diff --git a/src/widget/taffy_layout.rs b/src/widget/taffy_layout.rs index d584ee8c1..e5fc8483e 100644 --- a/src/widget/taffy_layout.rs +++ b/src/widget/taffy_layout.rs @@ -17,7 +17,9 @@ use crate::widget::{AccessCx, BoxConstraints, Event}; use ::taffy::style::AvailableSpace; use ::taffy::style_helpers::TaffyMaxContent; use accesskit::NodeId; +use glazier::kurbo::Affine; use vello::kurbo::{Point, Size}; +use vello::peniko::{Brush, Color, Fill}; use vello::SceneBuilder; use super::{contexts::LifeCycleCx, EventCx, LayoutCx, LifeCycle, PaintCx, Pod, UpdateCx, Widget}; @@ -47,10 +49,11 @@ pub struct TaffyLayout { pub child_caches: Vec, pub child_layouts: Vec, pub style: taffy::Style, + pub background_color: Option, } impl TaffyLayout { - pub fn new(children: Vec, style: taffy::Style) -> Self { + pub fn new(children: Vec, style: taffy::Style, background_color: Option) -> Self { let len = children.len(); let mut caches = Vec::new(); caches.resize_with(len, taffy::Cache::new); @@ -59,6 +62,7 @@ impl TaffyLayout { child_caches: caches, child_layouts: vec![taffy::Layout::new(); len], style, + background_color, } } } @@ -390,8 +394,16 @@ impl Widget for TaffyLayout { } fn paint(&mut self, cx: &mut PaintCx, builder: &mut SceneBuilder) { + if let Some(color) = self.background_color { + builder.fill( + Fill::NonZero, + Affine::IDENTITY, + &Brush::Solid(color), + None, + &cx.size().to_rect(), + ); + } for child in &mut self.children { - println!("paint child!"); child.paint(cx, builder); } }