Struct EventContext
pub struct EventContext<'a> {
pub cache: &'a mut CachedData,
pub windows: &'a mut HashMap<Entity, WindowState>,
/* private fields */
}
Expand description
A context used when handling events.
The EventContext
is provided by the event
method in View
, or the event
method in Model
, and can be used to mutably access the
desired style and layout properties of the current view.
§Example
pub struct CustomView {}
impl CustomView {
pub fn new(cx: &mut Context) -> Handle<Self> {
Self{}.build(cx, |_|{})
}
}
impl View for CustomView {
fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|window_event, _| match window_event {
WindowEvent::Press{..} => {
// Change the view background color to red when pressed.
cx.set_background_color(Color::red());
}
_=> {}
});
}
}
Fields§
§cache: &'a mut CachedData
§windows: &'a mut HashMap<Entity, WindowState>
Implementations§
§impl<'a> EventContext<'a>
impl<'a> EventContext<'a>
pub fn new(cx: &'a mut Context) -> EventContext<'a>
pub fn new_with_current( cx: &'a mut Context, current: Entity, ) -> EventContext<'a>
pub fn get_view<V>(&self) -> Option<&V>where
V: View,
pub fn close_window(&mut self)
pub fn window_position(&self) -> WindowPosition
pub fn window_size(&self) -> WindowSize
pub fn resolve_entity_identifier(&self, id: &str) -> Option<Entity>
pub fn resolve_entity_identifier(&self, id: &str) -> Option<Entity>
Returns the [Entity] id associated with the given identifier.
pub fn current(&self) -> Entity
pub fn current(&self) -> Entity
Returns the [Entity] id of the current view.
pub fn mouse(&self) -> &MouseState<Entity>
pub fn mouse(&self) -> &MouseState<Entity>
Returns a reference to the mouse state.
pub fn nth_child(&self, n: usize) -> Option<Entity>
pub fn last_child(&self) -> Option<Entity>
pub fn with_current<T>( &mut self, entity: Entity, f: impl FnOnce(&mut EventContext<'a>) -> T, ) -> T
pub fn has_drop_data(&self) -> bool
pub fn bounds(&self) -> BoundingBox
pub fn bounds(&self) -> BoundingBox
Returns the bounds of the current view.
pub fn set_bounds(&mut self, bounds: BoundingBox)
pub fn scale_factor(&self) -> f32
pub fn scale_factor(&self) -> f32
Returns the scale factor.
pub fn logical_to_physical(&self, logical: f32) -> f32
pub fn logical_to_physical(&self, logical: f32) -> f32
Converts logical points to physical pixels.
pub fn physical_to_logical(&self, physical: f32) -> f32
pub fn physical_to_logical(&self, physical: f32) -> f32
Convert physical pixels to logical points.
pub fn clip_region(&self) -> BoundingBox
pub fn clip_region(&self) -> BoundingBox
Returns the clip bounds of the current view.
pub fn play_animation(
&mut self,
anim_id: impl AnimId,
duration: Duration,
delay: Duration,
)
pub fn play_animation( &mut self, anim_id: impl AnimId, duration: Duration, delay: Duration, )
Trigger an animation with the given id to play on the current view.
pub fn play_animation_for(
&mut self,
anim_id: impl AnimId,
target: &str,
duration: Duration,
delay: Duration,
)
pub fn play_animation_for( &mut self, anim_id: impl AnimId, target: &str, duration: Duration, delay: Duration, )
Trigger an animation with the given id to play on a target view.
pub fn is_animating(&self, anim_id: impl AnimId) -> bool
pub fn is_animating(&self, anim_id: impl AnimId) -> bool
Returns true if the current view is currently animating with the given animation id.
pub fn add_listener<F, W>(&mut self, listener: F)
pub fn add_listener<F, W>(&mut self, listener: F)
Add a listener to an entity.
A listener can be used to handle events which would not normally propagate to the entity. For example, mouse events when a different entity has captured them. Useful for things like closing a popup when clicking outside of its bounding box.
pub fn set_language(&mut self, lang: LanguageIdentifier)
pub fn set_language(&mut self, lang: LanguageIdentifier)
Sets the language used by the application for localization.
pub fn capture(&mut self)
pub fn capture(&mut self)
Capture mouse input for the current view.
pub fn release(&mut self)
pub fn release(&mut self)
Release mouse input capture for the current view.
pub fn focus_with_visibility(&mut self, focus_visible: bool)
pub fn focus_with_visibility(&mut self, focus_visible: bool)
Sets application focus to the current view with the specified focus visibility.
pub fn focus(&mut self)
pub fn focus(&mut self)
Sets application focus to the current view using the previous focus visibility.
Focused elements receive keyboard input events and can be selected with the :focus
CSS pseudo-class selector.
pub fn focus_next(&mut self)
pub fn focus_next(&mut self)
Moves the keyboard focus to the next navigable view.
pub fn focus_prev(&mut self)
pub fn hovered(&self) -> Entity
pub fn hovered(&self) -> Entity
Returns the currently hovered view.
pub fn focused(&self) -> Entity
pub fn focused(&self) -> Entity
Returns the currently focused view.
pub fn is_hovered(&self) -> bool
pub fn is_hovered(&self) -> bool
Returns true if the current view is being hovered.
pub fn is_focused(&self) -> bool
pub fn is_focused(&self) -> bool
Returns true if the current view is focused.
pub fn is_draggable(&self) -> bool
pub fn is_disabled(&self) -> bool
pub fn is_disabled(&self) -> bool
Returns true if the current view is disabled.
pub fn is_checked(&self) -> bool
pub fn is_checked(&self) -> bool
Returns true if the current view is checked.
pub fn is_read_only(&self) -> bool
pub fn is_read_only(&self) -> bool
Returns true if the view is in a read-only state.
pub fn lock_cursor_icon(&mut self)
pub fn lock_cursor_icon(&mut self)
Prevents the cursor icon from changing until the lock is released.
pub fn unlock_cursor_icon(&mut self)
pub fn unlock_cursor_icon(&mut self)
Releases any cursor icon lock, allowing the cursor icon to be changed.
pub fn is_cursor_icon_locked(&self) -> bool
pub fn is_cursor_icon_locked(&self) -> bool
Returns true if the cursor icon is locked.
pub fn set_drop_data(&mut self, data: impl Into<DropData>)
pub fn get_clipboard(&mut self) -> Result<String, Box<dyn Error + Sync + Send>>
pub fn get_clipboard(&mut self) -> Result<String, Box<dyn Error + Sync + Send>>
Get the contents of the system clipboard.
This may fail for a variety of backend-specific reasons.
pub fn set_clipboard(
&mut self,
text: String,
) -> Result<(), Box<dyn Error + Sync + Send>>
pub fn set_clipboard( &mut self, text: String, ) -> Result<(), Box<dyn Error + Sync + Send>>
Set the contents of the system clipboard.
This may fail for a variety of backend-specific reasons.
pub fn toggle_class(&mut self, class_name: &str, applied: bool)
pub fn toggle_class(&mut self, class_name: &str, applied: bool)
Toggles the addition/removal of a class name for the current view.
§Example
cx.toggle_class("foo", true);
pub fn environment(&self) -> &Environment
pub fn environment(&self) -> &Environment
Returns a reference to the Environment model.
pub fn set_theme_mode(&mut self, theme_mode: ThemeMode)
pub fn set_theme_mode(&mut self, theme_mode: ThemeMode)
Sets the current theme mode.
pub fn needs_redraw(&mut self)
pub fn needs_redraw(&mut self)
Marks the current view as needing to be redrawn.
pub fn needs_relayout(&mut self)
pub fn needs_relayout(&mut self)
Marks the current view as needing a layout computation.
pub fn needs_restyle(&mut self)
pub fn needs_restyle(&mut self)
Marks the current view as needing to be restyled.
pub fn reload_styles(&mut self) -> Result<(), Error>
pub fn reload_styles(&mut self) -> Result<(), Error>
Reloads the stylesheets linked to the application.
pub fn spawn<F>(&self, target: F)
pub fn spawn<F>(&self, target: F)
Spawns a thread and provides a ContextProxy for sending events back to the main UI thread.
pub fn get_proxy(&self) -> ContextProxy
pub fn get_proxy(&self) -> ContextProxy
Returns a ContextProxy which can be moved between threads and used to send events back to the main UI thread.
pub fn modify<V>(&mut self, f: impl FnOnce(&mut V))where
V: View,
pub fn background_color(&mut self) -> Color
pub fn background_color(&mut self) -> Color
Returns the background color of the view.
Returns a transparent color if the view does not have a background color.
pub fn set_id(&mut self, id: &str)
pub fn set_hover(&mut self, flag: bool)
pub fn set_hover(&mut self, flag: bool)
Sets the hover state of the current view.
Hovered elements can be selected with the :hover
CSS pseudo-class selector:
element:hover {
background-color: red;
}
Typically this is set by the hover system and should not be set manually.
pub fn set_active(&mut self, active: bool)
pub fn set_active(&mut self, active: bool)
Set the active state for the current view.
Active elements can be selected with the :active
CSS pseudo-class selector:
element:active {
background-color: red;
}
pub fn set_read_only(&mut self, flag: bool)
pub fn set_read_write(&mut self, flag: bool)
pub fn set_checked(&mut self, flag: bool)
pub fn set_checked(&mut self, flag: bool)
Sets the checked state of the current view.
Checked elements can be selected with the :checked
CSS pseudo-class selector:
element:checked {
background-color: red;
}
pub fn set_valid(&mut self, flag: bool)
pub fn set_valid(&mut self, flag: bool)
Sets the valid state of the current view.
Checked elements can be selected with the :checked
CSS pseudo-class selector:
element:checked {
background-color: red;
}
pub fn set_placeholder_shown(&mut self, flag: bool)
pub fn is_valid(&self) -> bool
pub fn is_placeholder_shown(&self) -> bool
pub fn set_role(&mut self, role: Role)
pub fn set_role(&mut self, role: Role)
Sets the accessibility role of the view.
pub fn set_default_action_verb(
&mut self,
default_action_verb: DefaultActionVerb,
)
pub fn set_default_action_verb( &mut self, default_action_verb: DefaultActionVerb, )
Sets the accessibility default action verb of the view.
pub fn set_live(&mut self, live: Live)
pub fn set_live(&mut self, live: Live)
Sets the view to be an accessibility live region.
pub fn labelled_by(&mut self, id: &str)
pub fn labelled_by(&mut self, id: &str)
Sets the view, by id name, which labels the current view for accessibility.
Sets whether the view should be explicitely hidden from accessibility.
pub fn text_value(&mut self, text: &str)
pub fn text_value(&mut self, text: &str)
Sets a text value used for accessbility for the current view.
pub fn numeric_value(&mut self, value: f64)
pub fn numeric_value(&mut self, value: f64)
Sets a numeric value used for accessibility for the current view.
pub fn set_display(&mut self, display: Display)
pub fn set_display(&mut self, display: Display)
Sets the display type of the current view.
A display value of Display::None
causes the view to be ignored by both layout and rendering.
pub fn set_visibility(&mut self, visibility: Visibility)
pub fn set_visibility(&mut self, visibility: Visibility)
Sets the visibility of the current view.
The layout system will still compute the size and position of an invisible (hidden) view.
pub fn set_opacity(&mut self, opacity: f32)
pub fn set_opacity(&mut self, opacity: f32)
Sets the opacity of the current view.
Expects a number between 0.0 (transparent) and 1.0 (opaque).
pub fn set_z_index(&mut self, z_index: i32)
pub fn set_z_index(&mut self, z_index: i32)
Sets the z-index of the current view.
pub fn set_clip_path(&mut self, clip_path: ClipPath)
pub fn set_clip_path(&mut self, clip_path: ClipPath)
Sets the clip path of the current view.
pub fn set_overflowx(&mut self, overflowx: impl Into<Overflow>)
pub fn set_overflowx(&mut self, overflowx: impl Into<Overflow>)
Sets the overflow type on the horizontal axis of the current view.
pub fn set_overflowy(&mut self, overflowy: impl Into<Overflow>)
pub fn set_overflowy(&mut self, overflowy: impl Into<Overflow>)
Sets the overflow type on the vertical axis of the current view.
pub fn set_transform(&mut self, transform: impl Into<Vec<Transform>>)
pub fn set_transform(&mut self, transform: impl Into<Vec<Transform>>)
Sets the transform of the current view.
pub fn set_transform_origin(&mut self, transform_origin: Translate)
pub fn set_transform_origin(&mut self, transform_origin: Translate)
Sets the transform origin of the current view.
pub fn set_translate(&mut self, translate: impl Into<Translate>)
pub fn set_translate(&mut self, translate: impl Into<Translate>)
Sets the translation of the current view.
pub fn set_rotate(&mut self, angle: impl Into<Angle>)
pub fn set_rotate(&mut self, angle: impl Into<Angle>)
Sets the rotation of the current view.
pub fn set_backdrop_filter(&mut self, filter: Filter)
pub fn set_backdrop_filter(&mut self, filter: Filter)
Sets the backdrop filter of the current view.
pub fn set_background_color(&mut self, background_color: Color)
pub fn set_width(&mut self, width: Units)
pub fn set_height(&mut self, height: Units)
pub fn set_max_height(&mut self, height: Units)
pub fn set_left(&mut self, left: Units)
pub fn set_top(&mut self, left: Units)
pub fn set_right(&mut self, left: Units)
pub fn set_bottom(&mut self, left: Units)
pub fn set_pointer_events(&mut self, pointer_events: impl Into<PointerEvents>)
pub fn border_width(&self) -> f32
pub fn border_width(&self) -> f32
Returns the border width of the current view in physical pixels.
pub fn add_timer(
&mut self,
interval: Duration,
duration: Option<Duration>,
callback: impl Fn(&mut EventContext<'_>, TimerAction) + 'static,
) -> Timer
pub fn add_timer( &mut self, interval: Duration, duration: Option<Duration>, callback: impl Fn(&mut EventContext<'_>, TimerAction) + 'static, ) -> Timer
Adds a timer to the application.
interval
- The time between ticks of the timer.
duration
- An optional duration for the timer. Pass None
for a continuos timer.
callback
- A callback which is called on when the timer is started, ticks, and stops. Disambiguated by the TimerAction
parameter of the callback.
Returns a Timer
id which can be used to start and stop the timer.
§Example
Creates a timer which calls the provided callback every second for 5 seconds:
let timer = cx.add_timer(Duration::from_secs(1), Some(Duration::from_secs(5)), |cx, reason|{
match reason {
TimerAction::Start => {
debug!("Start timer");
}
TimerAction::Tick(delta) => {
debug!("Tick timer: {:?}", delta);
}
TimerAction::Stop => {
debug!("Stop timer");
}
}
});
pub fn start_timer(&mut self, timer: Timer)
pub fn start_timer(&mut self, timer: Timer)
Starts a timer with the provided timer id.
Events sent within the timer callback provided in add_timer()
will target the current view.
pub fn modify_timer(
&mut self,
timer: Timer,
timer_function: impl Fn(&mut TimerState),
)
pub fn modify_timer( &mut self, timer: Timer, timer_function: impl Fn(&mut TimerState), )
Modifies the state of an existing timer with the provided Timer
id.
pub fn query_timer<T>( &mut self, timer: Timer, timer_function: impl Fn(&TimerState) -> T, ) -> Option<T>
pub fn timer_is_running(&mut self, timer: Timer) -> bool
pub fn timer_is_running(&mut self, timer: Timer) -> bool
Returns true if the timer with the provided timer id is currently running.
pub fn stop_timer(&mut self, timer: Timer)
pub fn stop_timer(&mut self, timer: Timer)
Stops the timer with the given timer id.
Any events emitted in response to the timer stopping, as determined by the callback provided in add_timer()
, will target the view which called start_timer()
.
Trait Implementations§
§impl DataContext for EventContext<'_>
impl DataContext for EventContext<'_>
§fn data<T>(&self) -> Option<&T>where
T: 'static,
fn data<T>(&self) -> Option<&T>where
T: 'static,
None
if the data does not exist.fn as_context(&self) -> Option<LocalizationContext<'_>>
§impl EmitContext for EventContext<'_>
impl EmitContext for EventContext<'_>
§fn emit<M>(&mut self, message: M)
fn emit<M>(&mut self, message: M)
§fn emit_to<M>(&mut self, target: Entity, message: M)
fn emit_to<M>(&mut self, target: Entity, message: M)
§fn emit_custom(&mut self, event: Event)
fn emit_custom(&mut self, event: Event)
§fn schedule_emit<M>(&mut self, message: M, at: Instant) -> TimedEventHandle
fn schedule_emit<M>(&mut self, message: M, at: Instant) -> TimedEventHandle
§fn schedule_emit_to<M>(
&mut self,
target: Entity,
message: M,
at: Instant,
) -> TimedEventHandle
fn schedule_emit_to<M>( &mut self, target: Entity, message: M, at: Instant, ) -> TimedEventHandle
§fn schedule_emit_custom(
&mut self,
event: Event,
at: Instant,
) -> TimedEventHandle
fn schedule_emit_custom( &mut self, event: Event, at: Instant, ) -> TimedEventHandle
§fn cancel_scheduled(&mut self, handle: TimedEventHandle)
fn cancel_scheduled(&mut self, handle: TimedEventHandle)
source§impl ModifyWindow for EventContext<'_>
impl ModifyWindow for EventContext<'_>
fn modify_window<T>(&mut self, f: impl FnOnce(&Window) -> T) -> Option<T>
§impl TreeProps for EventContext<'_>
impl TreeProps for EventContext<'_>
§fn first_child(&self) -> Entity
fn first_child(&self) -> Entity
fn parent_window(&self) -> Option<Entity>
Auto Trait Implementations§
impl<'a> Freeze for EventContext<'a>
impl<'a> !RefUnwindSafe for EventContext<'a>
impl<'a> !Send for EventContext<'a>
impl<'a> !Sync for EventContext<'a>
impl<'a> Unpin for EventContext<'a>
impl<'a> !UnwindSafe for EventContext<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.