Skip to content

Commit

Permalink
Add background_color support to TaffyLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Nov 23, 2023
1 parent 2874fec commit 633b400
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/view/taffy_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +30,7 @@ use super::{Cx, View};
pub struct TaffyLayout<T, A, VT: ViewSequence<T, A>> {
children: VT,
style: taffy::Style,
background_color: Option<Color>,
phantom: PhantomData<fn() -> (T, A)>,
}

Expand Down Expand Up @@ -55,6 +58,7 @@ impl<T, A, VT: ViewSequence<T, A>> TaffyLayout<T, A, VT> {
display,
..Default::default()
},
background_color: None,
phantom,
}
}
Expand All @@ -69,6 +73,7 @@ impl<T, A, VT: ViewSequence<T, A>> TaffyLayout<T, A, VT> {
flex_direction,
..Default::default()
},
background_color: None,
phantom,
}
}
Expand All @@ -77,6 +82,11 @@ impl<T, A, VT: ViewSequence<T, A>> TaffyLayout<T, A, VT> {
style_modifier(&mut self.style);
self
}

pub fn with_background_color(mut self, color: impl Into<Color>) -> Self {
self.background_color = Some(color.into());
self
}
}

impl<T, A, VT: ViewSequence<T, A>> ViewMarker for TaffyLayout<T, A, VT> {}
Expand All @@ -89,7 +99,7 @@ impl<T, A, VT: ViewSequence<T, A>> View<T, A> for TaffyLayout<T, A, VT> {
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)
}

Expand All @@ -109,10 +119,10 @@ impl<T, A, VT: ViewSequence<T, A>> View<T, A> for TaffyLayout<T, A, VT> {
.rebuild(cx, &prev.children, state, &mut splice)
});

// if self.background_color != prev.background_color {
// element.background_color = self.background_color;
// flags |= ChangeFlags::PAINT
// }
if self.background_color != prev.background_color {
element.background_color = self.background_color;
flags |= ChangeFlags::PAINT
}

if self.style != prev.style {
element.style = self.style.clone();
Expand Down
19 changes: 17 additions & 2 deletions src/widget/taffy_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use crate::geometry::Axis;
use crate::widget::{AccessCx, BoxConstraints, Event};
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};
Expand All @@ -35,14 +37,19 @@ pub struct TaffyLayout {
pub children: Vec<Pod>,
pub cache: taffy::Cache,
pub style: taffy::Style,
pub background_color: Option<Color>,
}

impl TaffyLayout {
pub fn new(children: Vec<Pod>, style: taffy::Style) -> Self {
pub fn new(children: Vec<Pod>, style: taffy::Style, background_color: Option<Color>) -> Self {
let len = children.len();
let mut caches = Vec::new();
caches.resize_with(len, taffy::Cache::new);
TaffyLayout {
children,
cache: taffy::Cache::new(),
style,
background_color,
}
}
}
Expand Down Expand Up @@ -433,8 +440,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);
}
}
Expand Down

0 comments on commit 633b400

Please sign in to comment.