Struct vizia_core::context::EventContext
source · 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§
source§impl<'a> EventContext<'a>
impl<'a> EventContext<'a>
pub fn new(cx: &'a mut Context) -> Self
pub fn new_with_current(cx: &'a mut Context, current: Entity) -> Self
pub fn get_view<V: View>(&self) -> Option<&V>
pub fn close_window(&mut self)
pub fn window_position(&self) -> WindowPosition
pub fn window_size(&self) -> WindowSize
sourcepub 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.
sourcepub 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 Self) -> T, ) -> T
pub fn has_drop_data(&self) -> bool
sourcepub fn bounds(&self) -> BoundingBox
pub fn bounds(&self) -> BoundingBox
Returns the bounds of the current view.
pub fn set_bounds(&mut self, bounds: BoundingBox)
sourcepub fn scale_factor(&self) -> f32
pub fn scale_factor(&self) -> f32
Returns the scale factor.
sourcepub fn logical_to_physical(&self, logical: f32) -> f32
pub fn logical_to_physical(&self, logical: f32) -> f32
Converts logical points to physical pixels.
sourcepub fn physical_to_logical(&self, physical: f32) -> f32
pub fn physical_to_logical(&self, physical: f32) -> f32
Convert physical pixels to logical points.
sourcepub fn clip_region(&self) -> BoundingBox
pub fn clip_region(&self) -> BoundingBox
Returns the clip bounds of the current view.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub fn set_language(&mut self, lang: LanguageIdentifier)
pub fn set_language(&mut self, lang: LanguageIdentifier)
Sets the language used by the application for localization.
sourcepub 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.
sourcepub 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.
sourcepub 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)
sourcepub fn is_hovered(&self) -> bool
pub fn is_hovered(&self) -> bool
Returns true if the current view is being hovered.
sourcepub 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
sourcepub fn is_disabled(&self) -> bool
pub fn is_disabled(&self) -> bool
Returns true if the current view is disabled.
sourcepub fn is_checked(&self) -> bool
pub fn is_checked(&self) -> bool
Returns true if the current view is checked.
sourcepub fn is_read_only(&self) -> bool
pub fn is_read_only(&self) -> bool
Returns true if the view is in a read-only state.
sourcepub fn lock_cursor_icon(&mut self)
pub fn lock_cursor_icon(&mut self)
Prevents the cursor icon from changing until the lock is released.
sourcepub 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.
sourcepub 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>)
sourcepub fn get_clipboard(
&mut self,
) -> Result<String, Box<dyn Error + Send + Sync + 'static>>
pub fn get_clipboard( &mut self, ) -> Result<String, Box<dyn Error + Send + Sync + 'static>>
Get the contents of the system clipboard.
This may fail for a variety of backend-specific reasons.
sourcepub fn set_clipboard(
&mut self,
text: String,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>>
pub fn set_clipboard( &mut self, text: String, ) -> Result<(), Box<dyn Error + Send + Sync + 'static>>
Set the contents of the system clipboard.
This may fail for a variety of backend-specific reasons.
sourcepub 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);
sourcepub fn environment(&self) -> &Environment
pub fn environment(&self) -> &Environment
Returns a reference to the Environment model.
sourcepub fn set_theme_mode(&mut self, theme_mode: ThemeMode)
pub fn set_theme_mode(&mut self, theme_mode: ThemeMode)
Sets the current theme mode.
sourcepub fn needs_redraw(&mut self)
pub fn needs_redraw(&mut self)
Marks the current view as needing to be redrawn.
sourcepub fn needs_relayout(&mut self)
pub fn needs_relayout(&mut self)
Marks the current view as needing a layout computation.
sourcepub fn needs_restyle(&mut self)
pub fn needs_restyle(&mut self)
Marks the current view as needing to be restyled.
sourcepub fn reload_styles(&mut self) -> Result<(), Error>
pub fn reload_styles(&mut self) -> Result<(), Error>
Reloads the stylesheets linked to the application.
sourcepub 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.
sourcepub 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: View>(&mut self, f: impl FnOnce(&mut V))
sourcepub 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)
sourcepub 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.
sourcepub 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)
sourcepub 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;
}
sourcepub 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
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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).
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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.
sourcepub 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>)
sourcepub fn border_width(&self) -> f32
pub fn border_width(&self) -> f32
Returns the border width of the current view in physical pixels.
sourcepub 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");
}
}
});
sourcepub 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.
sourcepub 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>
sourcepub 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.
sourcepub 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§
source§impl<'a> DataContext for EventContext<'a>
impl<'a> DataContext for EventContext<'a>
source§fn data<T: 'static>(&self) -> Option<&T>
fn data<T: 'static>(&self) -> Option<&T>
None
if the data does not exist.fn as_context(&self) -> Option<LocalizationContext<'_>>
source§impl<'a> EmitContext for EventContext<'a>
impl<'a> EmitContext for EventContext<'a>
source§fn emit<M: Any + Send>(&mut self, message: M)
fn emit<M: Any + Send>(&mut self, message: M)
source§fn emit_to<M: Any + Send>(&mut self, target: Entity, message: M)
fn emit_to<M: Any + Send>(&mut self, target: Entity, message: M)
source§fn emit_custom(&mut self, event: Event)
fn emit_custom(&mut self, event: Event)
source§fn schedule_emit<M: Any + Send>(
&mut self,
message: M,
at: Instant,
) -> TimedEventHandle
fn schedule_emit<M: Any + Send>( &mut self, message: M, at: Instant, ) -> TimedEventHandle
source§fn schedule_emit_to<M: Any + Send>(
&mut self,
target: Entity,
message: M,
at: Instant,
) -> TimedEventHandle
fn schedule_emit_to<M: Any + Send>( &mut self, target: Entity, message: M, at: Instant, ) -> TimedEventHandle
source§fn schedule_emit_custom(
&mut self,
event: Event,
at: Instant,
) -> TimedEventHandle
fn schedule_emit_custom( &mut self, event: Event, at: Instant, ) -> TimedEventHandle
source§fn cancel_scheduled(&mut self, handle: TimedEventHandle)
fn cancel_scheduled(&mut self, handle: TimedEventHandle)
source§impl<'a> TreeProps for EventContext<'a>
impl<'a> TreeProps for EventContext<'a>
source§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.