pub trait Widget {
// Required methods
fn layout(&mut self, ctx: &LayoutCtx<'_>, constraints: Constraints) -> Size;
fn paint(&self, ctx: &mut PaintCtx<'_>, out: &mut dyn Painter);
}Expand description
Legacy immediate layout/paint contract used by experimental LayoutNode code.
This is distinct from crate::component::Widget, which participates in
the retained element tree. Implementations receive logical-pixel geometry;
they are responsible for honoring constraints and for not retaining either
borrowed context.
§Examples
use ailloli_ui_core::{Constraints, Size};
use ailloli_ui_runtime::layout::{LayoutCtx, Widget};
use ailloli_ui_runtime::scene::{PaintCtx, Painter};
struct Fixed;
impl Widget for Fixed {
fn layout(&mut self, _ctx: &LayoutCtx<'_>, constraints: Constraints) -> Size {
constraints.constrain(Size::new(12.0, 8.0))
}
fn paint(&self, _ctx: &mut PaintCtx<'_>, _out: &mut dyn Painter) {}
}Required Methods§
Sourcefn layout(&mut self, ctx: &LayoutCtx<'_>, constraints: Constraints) -> Size
fn layout(&mut self, ctx: &LayoutCtx<'_>, constraints: Constraints) -> Size
Measures the widget within constraints, returning logical pixels.
Implementations should return a size accepted by
[Constraints::constrain]; the trait itself does not enforce that
invariant.
§Examples
use ailloli_ui_core::{Constraints, Scale, Size};
use ailloli_ui_runtime::layout::{LayoutCtx, Widget};
use ailloli_ui_runtime::scene::{PaintCtx, Painter};
struct Fixed;
impl Widget for Fixed {
fn layout(&mut self, _ctx: &LayoutCtx<'_>, c: Constraints) -> Size {
c.constrain(Size::new(30.0, 10.0))
}
fn paint(&self, _ctx: &mut PaintCtx<'_>, _out: &mut dyn Painter) {}
}
let mut widget = Fixed;
let ctx = LayoutCtx::new(Scale::new(1.0));
assert_eq!(widget.layout(&ctx, Constraints::loose(20.0, 20.0)), Size::new(20.0, 10.0));Sourcefn paint(&self, ctx: &mut PaintCtx<'_>, out: &mut dyn Painter)
fn paint(&self, ctx: &mut PaintCtx<'_>, out: &mut dyn Painter)
Emits this widget’s draw commands through out.
The method has no implicit clip or clearing behavior. A widget that emits no commands leaves the painter unchanged.
§Examples
use ailloli_ui_core::{Constraints, Size};
use ailloli_ui_runtime::layout::{LayoutCtx, Widget};
use ailloli_ui_runtime::scene::{PaintCtx, Painter};
struct Invisible;
impl Widget for Invisible {
fn layout(&mut self, _ctx: &LayoutCtx<'_>, _c: Constraints) -> Size { Size::default() }
fn paint(&self, _ctx: &mut PaintCtx<'_>, _out: &mut dyn Painter) {}
}
let widget = Invisible;
let mut ctx = PaintCtx::new();
let mut out = PaintCtx::new();
widget.paint(&mut ctx, &mut out);
assert_eq!(out.layers[0].cmds.len(), 0);