vizia::context

Struct Context

pub struct Context {
    pub tree: Tree<Entity>,
    pub windows: HashMap<Entity, WindowState>,
    pub mouse: MouseState<Entity>,
    pub text_context: TextContext,
    pub ignore_default_theme: bool,
    pub window_has_focus: bool,
    /* private fields */
}
Expand description

The main storage and control object for a Vizia application.

Fields§

§tree: Tree<Entity>§windows: HashMap<Entity, WindowState>§mouse: MouseState<Entity>§text_context: TextContext§ignore_default_theme: bool§window_has_focus: bool

Implementations§

§

impl Context

pub fn new() -> Context

Creates a new context.

pub fn current(&self) -> Entity

The “current” entity, generally the entity which is currently being built or the entity which is currently having an event dispatched to it.

pub fn with_current<T>( &mut self, current: Entity, f: impl FnOnce(&mut Context) -> T, ) -> T

Makes the above black magic more explicit

pub fn environment(&self) -> &Environment

Returns a reference to the Environment model.

pub fn parent_window(&self) -> Entity

pub fn scale_factor(&self) -> f32

Returns the scale factor of the display.

pub fn needs_redraw(&mut self, entity: Entity)

Mark the application as needing to rerun the draw method

pub fn needs_restyle(&mut self, entity: Entity)

Mark the application as needing to recompute view styles

pub fn needs_relayout(&mut self)

Mark the application as needing to rerun layout computations

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

Sets application focus to the current entity with the specified focus visiblity

pub fn focus(&mut self)

Sets application focus to the current entity using the previous focus visibility

pub fn remove(&mut self, entity: Entity)

Removes the provided entity from the application.

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.

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

Adds a global listener to the application.

Global listeners have the first opportunity to handle every event that is sent in an application. They will never be removed. If you need a listener tied to the lifetime of a view, use add_listener.

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

Sets the language used by the application for localization.

pub fn add_font_mem(&mut self, data: impl AsRef<[u8]>)

pub fn set_default_font(&mut self, names: &[&str])

Sets the global default font for the application.

pub fn add_stylesheet(&mut self, style: impl IntoCssStr) -> Result<(), Error>

pub fn remove_user_themes(&mut self)

Remove all user themes from the application.

pub fn add_animation(&mut self, animation: AnimationBuilder<'_>) -> Animation

pub fn set_image_loader<F>(&mut self, loader: F)
where F: 'static + Fn(&mut ResourceContext<'_>, &str),

pub fn add_translation(&mut self, lang: LanguageIdentifier, ftl: impl ToString)

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)

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

Modifies the state of an existing timer with the provided Timer id.

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)

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

pub fn load_image( &mut self, path: &str, data: &'static [u8], policy: ImageRetentionPolicy, )

pub fn load_svg( &mut self, path: &str, data: &[u8], policy: ImageRetentionPolicy, ) -> ImageId

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

pub fn get_proxy(&self) -> ContextProxy

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

Finds the entity that identifier identifies

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

Trait Implementations§

§

impl<V> AsMut<Context> for Handle<'_, V>

§

fn as_mut(&mut self) -> &mut Context

Converts this type into a mutable reference of the (usually inferred) input type.
§

impl DataContext for Context

§

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

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

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

§

impl Default for Context

§

fn default() -> Context

Returns the “default value” for a type. Read more
§

impl EmitContext for Context

§

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
§

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
§

fn emit_custom(&mut self, event: Event)

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

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
§

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
§

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
§

fn cancel_scheduled(&mut self, handle: TimedEventHandle)

Cancel a scheduled event before it is sent. Read more

Auto Trait Implementations§

§

impl Freeze for Context

§

impl !RefUnwindSafe for Context

§

impl !Send for Context

§

impl !Sync for Context

§

impl Unpin for Context

§

impl !UnwindSafe for Context

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

source§

type Output = T

Should always be Self
source§

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

source§

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

source§

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