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 2, 2023
1 parent c18bb85 commit b9330a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
15 changes: 13 additions & 2 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 @@ -36,6 +38,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 @@ -63,6 +66,7 @@ impl<T, A, VT: ViewSequence<T, A>> TaffyLayout<T, A, VT> {
display,
..Default::default()
},
background_color: None,
phantom,
}
}
Expand All @@ -77,6 +81,7 @@ impl<T, A, VT: ViewSequence<T, A>> TaffyLayout<T, A, VT> {
flex_direction,
..Default::default()
},
background_color: None,
phantom,
}
}
Expand All @@ -85,6 +90,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 @@ -97,7 +107,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 @@ -119,7 +129,8 @@ impl<T, A, VT: ViewSequence<T, A>> View<T, A> for TaffyLayout<T, A, VT> {

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
Expand Down
16 changes: 14 additions & 2 deletions src/widget/taffy_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -47,10 +49,11 @@ pub struct TaffyLayout {
pub child_caches: Vec<taffy::Cache>,
pub child_layouts: Vec<taffy::Layout>,
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);
Expand All @@ -59,6 +62,7 @@ impl TaffyLayout {
child_caches: caches,
child_layouts: vec![taffy::Layout::new(); len],
style,
background_color,
}
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit b9330a8

Please sign in to comment.