pub trait WidgetQuery<'w, 's> {
type WidgetView<'a>
where Self: 'a;
// Required methods
fn find_by_id<'a>(
&'a mut self,
target_id: &str,
) -> Option<Self::WidgetView<'a>>;
fn find_by_entity<'a>(
&'a mut self,
entity: Entity,
) -> Option<Self::WidgetView<'a>>;
fn find_by_class(&self, target_class: &str) -> Vec<Entity>;
fn get_components<'a>(
&'a mut self,
entity: Entity,
) -> Option<Self::WidgetView<'a>>;
}Expand description
Trait used for widgets system param.
§Navigation Patterns
When using widget queries in event handlers that may navigate, use the navigate! macro
to automatically exit the function after navigation:
fn on_button_click(
_click: On<Clicked>,
mut text_q: TextQuery,
mut router: ResMut<Router>
) {
if let Some(mut text) = text_q.find_by_id("status") {
text.set_text("Navigating...");
}
navigate!(router, "next-page", ()); // Function exits here automatically
// This code will never execute
}This prevents common issues where loops or code continue executing after navigation:
fn mark_items_complete(
_click: On<Clicked>,
mut btn_q: ButtonQuery,
mut router: ResMut<Router>
) {
for entity in btn_q.find_by_class("item-btn") {
if let Some(mut btn) = btn_q.find_by_entity(entity) {
if btn.text.value.0 == "target_item" {
btn.class.add_class("completed");
navigate!(router, "home", ()); // Exits function and loop
}
}
}
}Required Associated Types§
Sourcetype WidgetView<'a>
where
Self: 'a
type WidgetView<'a> where Self: 'a
The specific view struct this query returns (Ex, ButtonWidget, TextWidget).
Required Methods§
Sourcefn find_by_id<'a>(&'a mut self, target_id: &str) -> Option<Self::WidgetView<'a>>
fn find_by_id<'a>(&'a mut self, target_id: &str) -> Option<Self::WidgetView<'a>>
Method to find widget by ID.
Sourcefn find_by_entity<'a>(
&'a mut self,
entity: Entity,
) -> Option<Self::WidgetView<'a>>
fn find_by_entity<'a>( &'a mut self, entity: Entity, ) -> Option<Self::WidgetView<'a>>
Method to find widget by Entity.
Sourcefn find_by_class(&self, target_class: &str) -> Vec<Entity>
fn find_by_class(&self, target_class: &str) -> Vec<Entity>
Method to find entities that match with provided classes.
Sourcefn get_components<'a>(
&'a mut self,
entity: Entity,
) -> Option<Self::WidgetView<'a>>
fn get_components<'a>( &'a mut self, entity: Entity, ) -> Option<Self::WidgetView<'a>>
Get related components of an entity.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".