pub struct CachedWidget { /* private fields */ }
Expand description
A Singleton wrapper widget that caches and reuses its child widget across multiple instances.
CachedWidget
is designed to optimize performance and memory usage by ensuring
that only one instance of a child widget is created and shared across multiple
uses in the UI. This is particularly useful for complex widgets that are used
in different parts of the UI but should maintain a single state.
§Usage
In the DSL, you can use CachedWidget
as follows:
<CachedWidget> {
my_widget = <MyWidget> {}
}
The child widget will be created once and cached.
Subsequent uses of this CachedWidget
with the same child id (mid_widget
) will reuse the cached instance.
Note that only one child is supported per CachedWidget
.
CachedWidget supports Makepad’s widget finding mechanism, allowing child widgets to be located as expected.
§Implementation Details
- Uses a global
WidgetWrapperCache
to store cached widgets - Handles widget creation and caching in the
after_apply
hook - Delegates most widget operations (like event handling and drawing) to the cached child widget
§Note
While CachedWidget
can significantly improve performance for complex, frequently used widgets,
it should be used judiciously. Overuse of caching can lead to unexpected behavior if not managed properly.
Trait Implementations§
Source§impl LiveApply for CachedWidget
impl LiveApply for CachedWidget
Source§impl LiveApplyReset for CachedWidget
impl LiveApplyReset for CachedWidget
Source§impl LiveApplyValue for CachedWidget
impl LiveApplyValue for CachedWidget
Source§impl LiveHook for CachedWidget
impl LiveHook for CachedWidget
Source§fn apply_value_instance(
&mut self,
cx: &mut Cx,
apply: &mut Apply<'_, '_, '_>,
index: usize,
nodes: &[LiveNode],
) -> usize
fn apply_value_instance( &mut self, cx: &mut Cx, apply: &mut Apply<'_, '_, '_>, index: usize, nodes: &[LiveNode], ) -> usize
Handles the application of instance properties to this CachedWidget.
In the case of CachedWidget
This method is responsible for setting up the template
for the child widget, and applying any changes to an existing widget instance.
Source§fn after_apply(
&mut self,
cx: &mut Cx,
_apply: &mut Apply<'_, '_, '_>,
_index: usize,
_nodes: &[LiveNode],
)
fn after_apply( &mut self, cx: &mut Cx, _apply: &mut Apply<'_, '_, '_>, _index: usize, _nodes: &[LiveNode], )
Handles the creation or retrieval of the cached widget after applying changes.
This method is called after all properties have been applied to the widget. It ensures that the child widget is properly cached and retrieved from the global cache.