pub struct AdaptiveView { /* private fields */ }
Expand description
A widget that adapts its content based on the current context.
AdaptiveView
allows you to define different layouts for various conditions, like display context,
parent size or platform variations, (e.g., desktop vs. mobile) and automatically switches
between them based on a selector function.
Optionally retains unused variants to preserve their state
§Example
live_design! {
// ...
adaptive = <AdaptiveView> {
Desktop = <CustomView> {
label = { text: "Desktop View" } // override specific values of the same widget
}
Mobile = <CustomView> {
label = { text: "Mobile View" }
}
}
// ...
}
fn setup_adaptive_view(cx: &mut Cx) {;
self.adaptive_view(id!(adaptive)).set_variant_selector(|cx, parent_size| {
if cx.display_context.screen_size.x >= 1280.0 {
live_id!(Desktop)
} else {
live_id!(Mobile)
}
});
}
In this example, the AdaptiveView
switches between Desktop and Mobile layouts
based on the screen width. The set_variant_selector
method allows you to define
custom logic for choosing the appropriate layout variant.
AdaptiveView
implements a default variant selector based on the screen width for different
device layouts (Currently Desktop
and Mobile
). You can override this through the set_variant_selector
method.
Check out VariantSelector for more information on how to define custom selectors, and what information is available to them.
Implementations§
Source§impl AdaptiveView
impl AdaptiveView
Sourcepub fn set_variant_selector(
&mut self,
selector: impl FnMut(&mut Cx, &DVec2) -> LiveId + 'static,
)
pub fn set_variant_selector( &mut self, selector: impl FnMut(&mut Cx, &DVec2) -> LiveId + 'static, )
Set a variant selector for this widget.
The selector is a closure that takes a DisplayContext
and returns a LiveId
, corresponding to the template to use.