pub trait Interact<C: Context>: Copy + 'static {
type State: Copy + Default + 'static;
type Response: Copy + 'static;
// Required method
fn update(
&self,
state: &mut Self::State,
cx: &mut InteractCx<'_, C::Scalar>,
) -> Self::Response;
}Expand description
handle input for your widget
keep settings on this value and pass it to crate::Ui::interact each build
§execution
interactions run before each build using the last build’s settings and layout. children run before parents, siblings in build order, and interactions on the same widget in registration order.
§keyboard and text
InteractCx::keyboard delivers keyboard and text input to the focused widget,
then its ancestors. without focus, all mounted handlers are eligible.
call InteractCx::claim_keyboard when handled to stop it from reaching another
interaction. ignored events keep bubbling. the route uses focus at the start
of the update, so changing focus does not redirect the current event.
InteractCx::input bypasses routing and claims.
§sharing contacts
Contact::claim: keep the press for this update. call it each update, or stop to let a parent take over.Contact::capture: keep the press until it ends, even outside your widget. use this when a drag starts.
both need a free contact that is not already captured.
they return true on success. neither can take another widget’s claim.
interactions on the same widget share captures.
§following a contact
save Contact::id in your state and find it in InteractCx::contacts on
later updates. the handle itself only lasts one update.
to start tracking a press with an Option<ContactId> state:
*state = cx.contacts().find_map(|mut contact| {
(contact.phase == Phase::Started && contact.claim()).then_some(contact.id)
});on later updates:
- cancel if the contact is missing or
Contact::freeisfalse - cancel on
Phase::Cancelled - finish on
Phase::Ended
after capture, just follow the contact without claiming or capturing it again
Required Associated Types§
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".