Give widget access to taffy layout

This commit is contained in:
Joscha 2024-03-09 00:07:59 +01:00
parent f4a6435e6f
commit 4f14d70152
3 changed files with 26 additions and 8 deletions

View file

@ -55,13 +55,13 @@ impl<C> Tree<C> {
node: NodeId,
view: &mut View<'_>,
) -> anyhow::Result<()> {
let layout = self.tree.layout(node)?;
let layout = *self.tree.layout(node)?;
let area = Rect::from_nw(point_to_vec2(layout.location), size_to_vec2(layout.size));
let mut view = view.dup().zoom(area);
// First pass
if let Some(widget) = self.tree.get_node_context_mut(node) {
widget.draw_below(ctx, &mut view)?;
widget.draw_below(ctx, &mut view, &layout)?;
}
// Render children
@ -77,7 +77,7 @@ impl<C> Tree<C> {
// Second pass
if let Some(widget) = self.tree.get_node_context_mut(node) {
widget.draw_above(ctx, &mut view)?;
widget.draw_above(ctx, &mut view, &layout)?;
}
Ok(())

View file

@ -1,4 +1,4 @@
use taffy::{AvailableSpace, Size};
use taffy::{AvailableSpace, Layout, Size};
use crate::{Node, View};
@ -19,13 +19,23 @@ pub trait Widget<C> {
///
/// Prefer this over [`Self::draw_above`] when implementing a leaf widget.
#[allow(unused_variables)]
fn draw_below(&mut self, ctx: &mut C, view: &mut View<'_>) -> anyhow::Result<()> {
fn draw_below(
&mut self,
ctx: &mut C,
view: &mut View<'_>,
layout: &Layout,
) -> anyhow::Result<()> {
Ok(())
}
/// Called after all children are drawn.
#[allow(unused_variables)]
fn draw_above(&mut self, ctx: &mut C, view: &mut View<'_>) -> anyhow::Result<()> {
fn draw_above(
&mut self,
ctx: &mut C,
view: &mut View<'_>,
layout: &Layout,
) -> anyhow::Result<()> {
Ok(())
}
}

View file

@ -1,6 +1,9 @@
use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping, SwashCache};
use palette::Srgba;
use taffy::prelude::{AvailableSpace, Size};
use taffy::{
prelude::{AvailableSpace, Size},
Layout,
};
use crate::{color, Rect, Vec2, View, Widget};
@ -120,7 +123,12 @@ impl<C: HasFontStuff> Widget<C> for Text {
Size { width, height }
}
fn draw_below(&mut self, ctx: &mut C, view: &mut View<'_>) -> anyhow::Result<()> {
fn draw_below(
&mut self,
ctx: &mut C,
view: &mut View<'_>,
_layout: &Layout,
) -> anyhow::Result<()> {
let size = view.size();
let FontStuff {