Core Concepts

Core Widget Traits

WidgetNode Trait

1pub trait WidgetNode: LiveApply {
2    // Main methods
3    fn uid_to_widget(&self, uid: WidgetUid) -> WidgetRef;
4    fn find_widgets(&self, path: &[LiveId], cached: WidgetCache, results: &mut WidgetSet);
5    fn walk(&mut self, cx: &mut Cx) -> Walk;
6    fn area(&self) -> Area;
7    fn redraw(&mut self, cx: &mut Cx);
8    // Other default implementations
9}

This is the foundational trait for all Widgets:

  • Find Widget based on UID
  • Find Widgets based on path
  • Calculate layout position and size
  • Manage drawing areas
  • Trigger redraws

Widget Trait

1pub type DrawStep = Result<(), WidgetRef>;
2
3pub trait Widget: WidgetNode {
4    fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope);
5    fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep;
6
7    // Other default implementations
8}

This trait extends WidgetNode, adding:

  • Event handling system
  • Drawing process control

Widget References and Management

WidgetRef

WidgetRef is a smart pointer wrapper for Widgets.

1pub struct WidgetRefInner {
2    pub widget: Box<dyn Widget>,
3}
4
5pub struct WidgetRef(Rc<RefCell<Option<WidgetRefInner>>>);
6
7// Key methods
8impl WidgetRef {
9    // Other methods
10    // Event handling
11    pub fn handle_event(&self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
12        // ...
13    }
14
15    // Drawing
16    pub fn draw(&mut self, cx: &mut Cx2d, scope: &mut Scope) -> DrawStep {
17        // ...
18    }
19
20    // Get mutable reference to underlying Widget
21    pub fn borrow_mut<T: 'static + Widget>(&self) -> Option<RefMut<'_, T>> {
22        // ...
23    }
24
25    // Basic find method
26    pub fn find_widgets(&self, path: &[LiveId], cached: WidgetCache, results: &mut WidgetSet) {
27        if let Some(inner) = self.0.borrow().as_ref() {
28            inner.widget.find_widgets(path, cached, results)
29        }
30    }
31}
  • Uses Rc<RefCell> to provide reference counting and interior mutability
  • Supports Clone and thread safety
  • Safe Widget access through borrow/borrow_mut

WidgetCache

WidgetCache is an important performance optimization mechanism in the Makepad UI framework. It's a parameter that traverses the entire component tree, controlling whether components enable, clear, or bypass caching at each layer.

This is important for improving large UI performance because it can:

  • Avoid repeated component tree traversal
  • Force latest state retrieval when needed
  • Clear invalid caches when component states change
1// Typical call chain:
2widget.widget(&path)
3  -> widget.find_widgets(path, WidgetCache::Yes)  // High-level API defaults to using cache
4    -> child_widget.find_widgets(path, cached)    // Continue passing cache parameter down
5      -> grandchild.find_widgets(path, cached)    // Cache strategy needs to be consistent

WidgetSet

WidgetSet is a collection of WidgetRefs.

1pub struct WidgetSet(pub SmallVec<[WidgetRef;2]>);
  • Efficiently manages multiple Widget references
  • Supports batch operations and lookups
  • Uses SmallVec to optimize memory usage in small-scale scenarios