masonry-rs
masonry-rs copied to clipboard
Improve Widget trait
I want to make some pretty deep changes to the Widget trait. The ultimate trait would look something like this:
pub trait Widget2: Any {
fn on_event(&mut self, ctx: &mut EventCtx, event: &Event);
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle);
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size;
fn compute_max_intrinsic(&mut self, axis: Axis, ctx: &mut LayoutCx, bc: &BoxConstraints) -> f64;
fn accessibility(&mut self, cx: &mut AccessCx);
// Maybe some other arguments after the switch to Vello
fn paint(&mut self, ctx: &mut PaintCtx);
fn children(&self) -> SmallVec<[WidgetRef<'_, dyn Widget>; 16]>;
// Got to figure out what argument the callback takes
fn call_on_children(&mut self, callback: impl FnMut(&mut impl Widget));
fn get_debug_text(&self) -> Option<String>;
// --- Auto-generated implementations ---
fn make_trace_span(&self) -> Span;
fn get_child_at_pos(&self, pos: Point) -> Option<WidgetRef<'_, dyn Widget>>;
fn get_child_with_id(&self, id: WidgetId) -> Option<WidgetRef<'_, dyn Widget>>;
fn call_on_child_at_pos(&mut self, pos: Point, callback: impl FnOnce(&mut impl Widget));
fn call_on_child_with_id(&mut self, id: WidgetId, callback: impl FnOnce(&mut impl Widget));
fn type_name(&self) -> &'static str;
fn short_type_name(&self) -> &'static str;
}
Notable changes:
- Accessibility support is baked in. I'm not sure this is the right way though.
- We add compute_max_intrinsic method to layout pass.
- We add call_on_children method. That method should replace the boilerplate code in container widgets that propagates events. I'm not sure how to make it work type-system-wise.
- Generally speaking, we give up on the idea of Widget being completely dyn-safe. This mean we'll need an ErasedWidget type, same idea as in Panoramix.