Struct vizia::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>

source

pub fn new(cx: &'a mut Context) -> EventContext<'a>

source

pub fn new_with_current( cx: &'a mut Context, current: Entity, ) -> EventContext<'a>

source

pub fn get_view<V>(&self) -> Option<&V>
where V: View,

source

pub fn close_window(&mut self)

source

pub fn window_position(&self) -> WindowPosition

source

pub fn window_size(&self) -> WindowSize

source

pub fn resolve_entity_identifier(&self, id: &str) -> Option<Entity>

Returns the [Entity] id associated with the given identifier.

source

pub fn current(&self) -> Entity

Returns the [Entity] id of the current view.

source

pub fn modifiers(&self) -> &Modifiers

Returns a reference to the keyboard modifiers state.

source

pub fn mouse(&self) -> &MouseState<Entity>

Returns a reference to the mouse state.

source

pub fn nth_child(&self, n: usize) -> Option<Entity>

source

pub fn last_child(&self) -> Option<Entity>

source

pub fn with_current<T>( &mut self, entity: Entity, f: impl FnOnce(&mut EventContext<'a>) -> T, ) -> T

source

pub fn has_drop_data(&self) -> bool

source

pub fn bounds(&self) -> BoundingBox

Returns the bounds of the current view.

source

pub fn set_bounds(&mut self, bounds: BoundingBox)

source

pub fn scale_factor(&self) -> f32

Returns the scale factor.

source

pub fn logical_to_physical(&self, logical: f32) -> f32

Converts logical points to physical pixels.

source

pub fn physical_to_logical(&self, physical: f32) -> f32

Convert physical pixels to logical points.

source

pub fn clip_region(&self) -> BoundingBox

Returns the clip bounds of the current view.

source

pub fn transform(&self) -> Matrix

Returns the 2D transform of the current view.

source

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.

source

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.

source

pub fn is_animating(&self, anim_id: impl AnimId) -> bool

Returns true if the current view is currently animating with the given animation id.

source

pub fn add_listener<F, W>(&mut self, listener: F)
where W: View, F: 'static + Fn(&mut W, &mut EventContext<'_>, &mut Event),

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.

source

pub fn set_language(&mut self, lang: LanguageIdentifier)

Sets the language used by the application for localization.

source

pub fn capture(&mut self)

Capture mouse input for the current view.

source

pub fn release(&mut self)

Release mouse input capture for the current view.

source

pub fn focus_with_visibility(&mut self, focus_visible: bool)

Sets application focus to the current view with the specified focus visibility.

source

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.

source

pub fn focus_next(&mut self)

Moves the keyboard focus to the next navigable view.

source

pub fn focus_prev(&mut self)

source

pub fn hovered(&self) -> Entity

Returns the currently hovered view.

source

pub fn focused(&self) -> Entity

Returns the currently focused view.

source

pub fn is_hovered(&self) -> bool

Returns true if the current view is being hovered.

source

pub fn is_active(&self) -> bool

Returns true if the current view is active.

source

pub fn is_over(&self) -> bool

Returns true if the mouse cursor is over the current view.

source

pub fn is_focused(&self) -> bool

Returns true if the current view is focused.

source

pub fn is_draggable(&self) -> bool

source

pub fn is_disabled(&self) -> bool

Returns true if the current view is disabled.

source

pub fn is_checked(&self) -> bool

Returns true if the current view is checked.

source

pub fn is_read_only(&self) -> bool

Returns true if the view is in a read-only state.

source

pub fn lock_cursor_icon(&mut self)

Prevents the cursor icon from changing until the lock is released.

source

pub fn unlock_cursor_icon(&mut self)

Releases any cursor icon lock, allowing the cursor icon to be changed.

source

pub fn is_cursor_icon_locked(&self) -> bool

Returns true if the cursor icon is locked.

source

pub fn set_drop_data(&mut self, data: impl Into<DropData>)

source

pub fn get_clipboard(&mut self) -> Result<String, Box<dyn Error + Send + Sync>>

Get the contents of the system clipboard.

This may fail for a variety of backend-specific reasons.

source

pub fn set_clipboard( &mut self, text: String, ) -> Result<(), Box<dyn Error + Send + Sync>>

Set the contents of the system clipboard.

This may fail for a variety of backend-specific reasons.

source

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);
source

pub fn environment(&self) -> &Environment

Returns a reference to the Environment model.

source

pub fn set_theme_mode(&mut self, theme_mode: ThemeMode)

Sets the current theme mode.

source

pub fn needs_redraw(&mut self)

Marks the current view as needing to be redrawn.

source

pub fn needs_relayout(&mut self)

Marks the current view as needing a layout computation.

source

pub fn needs_restyle(&mut self)

Marks the current view as needing to be restyled.

source

pub fn reload_styles(&mut self) -> Result<(), Error>

Reloads the stylesheets linked to the application.

source

pub fn spawn<F>(&self, target: F)
where F: 'static + Send + FnOnce(&mut ContextProxy),

Spawns a thread and provides a ContextProxy for sending events back to the main UI thread.

source

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.

source

pub fn modify<V>(&mut self, f: impl FnOnce(&mut V))
where V: View,

source

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.

source

pub fn set_id(&mut self, id: &str)

source

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.

source

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;
}
source

pub fn set_read_only(&mut self, flag: bool)

source

pub fn set_read_write(&mut self, flag: bool)

source

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;
}
source

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;
}
source

pub fn set_placeholder_shown(&mut self, flag: bool)

source

pub fn is_valid(&self) -> bool

source

pub fn is_placeholder_shown(&self) -> bool

source

pub fn set_name(&mut self, name: &str)

Sets the accessibility name of the view.

source

pub fn set_role(&mut self, role: Role)

Sets the accessibility role of the view.

source

pub fn set_default_action_verb( &mut self, default_action_verb: DefaultActionVerb, )

Sets the accessibility default action verb of the view.

source

pub fn set_live(&mut self, live: Live)

Sets the view to be an accessibility live region.

source

pub fn labelled_by(&mut self, id: &str)

Sets the view, by id name, which labels the current view for accessibility.

source

pub fn set_hidden(&mut self, hidden: bool)

Sets whether the view should be explicitely hidden from accessibility.

source

pub fn text_value(&mut self, text: &str)

Sets a text value used for accessbility for the current view.

source

pub fn numeric_value(&mut self, value: f64)

Sets a numeric value used for accessibility for the current view.

source

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.

source

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.

source

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).

source

pub fn set_z_index(&mut self, z_index: i32)

Sets the z-index of the current view.

source

pub fn set_clip_path(&mut self, clip_path: ClipPath)

Sets the clip path of the current view.

source

pub fn set_overflowx(&mut self, overflowx: impl Into<Overflow>)

Sets the overflow type on the horizontal axis of the current view.

source

pub fn set_overflowy(&mut self, overflowy: impl Into<Overflow>)

Sets the overflow type on the vertical axis of the current view.

source

pub fn set_transform(&mut self, transform: impl Into<Vec<Transform>>)

Sets the transform of the current view.

source

pub fn set_transform_origin(&mut self, transform_origin: Translate)

Sets the transform origin of the current view.

source

pub fn set_translate(&mut self, translate: impl Into<Translate>)

Sets the translation of the current view.

source

pub fn set_rotate(&mut self, angle: impl Into<Angle>)

Sets the rotation of the current view.

source

pub fn set_scale(&mut self, scale: impl Into<Scale>)

Sets the scale of the current view.

source

pub fn set_backdrop_filter(&mut self, filter: Filter)

Sets the backdrop filter of the current view.

source

pub fn set_background_color(&mut self, background_color: Color)

source

pub fn set_width(&mut self, width: Units)

source

pub fn set_height(&mut self, height: Units)

source

pub fn set_max_height(&mut self, height: Units)

source

pub fn set_left(&mut self, left: Units)

source

pub fn set_top(&mut self, left: Units)

source

pub fn set_right(&mut self, left: Units)

source

pub fn set_bottom(&mut self, left: Units)

source

pub fn set_text(&mut self, text: &str)

Sets the text of the current view.

source

pub fn set_pointer_events(&mut self, pointer_events: impl Into<PointerEvents>)

source

pub fn border_width(&self) -> f32

Returns the border width of the current view in physical pixels.

source

pub fn font_size(&self) -> f32

Returns the font-size of the current view in physical pixels.

source

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");
        }
    }
});
source

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.

source

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.

source

pub fn query_timer<T>( &mut self, timer: Timer, timer_function: impl Fn(&TimerState) -> T, ) -> Option<T>

source

pub fn timer_is_running(&mut self, timer: Timer) -> bool

Returns true if the timer with the provided timer id is currently running.

source

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>

source§

fn data<T>(&self) -> Option<&T>
where T: 'static,

Get model/view data from the context. Returns None if the data does not exist.
source§

fn as_context(&self) -> Option<LocalizationContext<'_>>

source§

impl<'a> EmitContext for EventContext<'a>

source§

fn emit<M>(&mut self, message: M)
where M: Any + Send,

Send an event containing the provided message up the tree from the current entity. Read more
source§

fn emit_to<M>(&mut self, target: Entity, message: M)
where M: Any + Send,

Send an event containing the provided message directly to a specified entity from the current entity. Read more
source§

fn emit_custom(&mut self, event: Event)

Send a custom event with custom origin and propagation information. Read more
source§

fn schedule_emit<M>(&mut self, message: M, at: Instant) -> TimedEventHandle
where M: Any + Send,

Send an event containing the provided message up the tree at a particular time instant. Read more
source§

fn schedule_emit_to<M>( &mut self, target: Entity, message: M, at: Instant, ) -> TimedEventHandle
where M: Any + Send,

Send an event containing the provided message directly to a specified view at a particular time instant. Read more
source§

fn schedule_emit_custom( &mut self, event: Event, at: Instant, ) -> TimedEventHandle

Send a custom event with custom origin and propagation information at a particular time instant. Read more
source§

fn cancel_scheduled(&mut self, handle: TimedEventHandle)

Cancel a scheduled event before it is sent. Read more
source§

impl<'a> ModifyWindow for EventContext<'a>

source§

fn modify_window<T>(&mut self, f: impl FnOnce(&Window) -> T) -> Option<T>

source§

impl<'a> TreeProps for EventContext<'a>

source§

fn parent(&self) -> Entity

Returns the id of the parent of the current view.
source§

fn first_child(&self) -> Entity

Returns the id of the first_child of the current view.
source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more