Give widget access to taffy layout
This commit is contained in:
parent
f4a6435e6f
commit
4f14d70152
3 changed files with 26 additions and 8 deletions
|
|
@ -55,13 +55,13 @@ impl<C> Tree<C> {
|
||||||
node: NodeId,
|
node: NodeId,
|
||||||
view: &mut View<'_>,
|
view: &mut View<'_>,
|
||||||
) -> anyhow::Result<()> {
|
) -> 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 area = Rect::from_nw(point_to_vec2(layout.location), size_to_vec2(layout.size));
|
||||||
let mut view = view.dup().zoom(area);
|
let mut view = view.dup().zoom(area);
|
||||||
|
|
||||||
// First pass
|
// First pass
|
||||||
if let Some(widget) = self.tree.get_node_context_mut(node) {
|
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
|
// Render children
|
||||||
|
|
@ -77,7 +77,7 @@ impl<C> Tree<C> {
|
||||||
|
|
||||||
// Second pass
|
// Second pass
|
||||||
if let Some(widget) = self.tree.get_node_context_mut(node) {
|
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(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use taffy::{AvailableSpace, Size};
|
use taffy::{AvailableSpace, Layout, Size};
|
||||||
|
|
||||||
use crate::{Node, View};
|
use crate::{Node, View};
|
||||||
|
|
||||||
|
|
@ -19,13 +19,23 @@ pub trait Widget<C> {
|
||||||
///
|
///
|
||||||
/// Prefer this over [`Self::draw_above`] when implementing a leaf widget.
|
/// Prefer this over [`Self::draw_above`] when implementing a leaf widget.
|
||||||
#[allow(unused_variables)]
|
#[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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called after all children are drawn.
|
/// Called after all children are drawn.
|
||||||
#[allow(unused_variables)]
|
#[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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping, SwashCache};
|
use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping, SwashCache};
|
||||||
use palette::Srgba;
|
use palette::Srgba;
|
||||||
use taffy::prelude::{AvailableSpace, Size};
|
use taffy::{
|
||||||
|
prelude::{AvailableSpace, Size},
|
||||||
|
Layout,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{color, Rect, Vec2, View, Widget};
|
use crate::{color, Rect, Vec2, View, Widget};
|
||||||
|
|
||||||
|
|
@ -120,7 +123,12 @@ impl<C: HasFontStuff> Widget<C> for Text {
|
||||||
Size { width, height }
|
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 size = view.size();
|
||||||
|
|
||||||
let FontStuff {
|
let FontStuff {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue