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
impl Context
pub fn current(&self) -> Entity
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
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
pub fn environment(&self) -> &Environment
Returns a reference to the Environment model.
pub fn parent_window(&self) -> Entity
pub fn scale_factor(&self) -> f32
pub fn scale_factor(&self) -> f32
Returns the scale factor of the display.
pub fn needs_redraw(&mut self, entity: Entity)
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)
pub fn needs_restyle(&mut self, entity: Entity)
Mark the application as needing to recompute view styles
pub fn needs_relayout(&mut self)
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)
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)
pub fn focus(&mut self)
Sets application focus to the current entity using the previous focus visibility
pub fn remove(&mut self, entity: Entity)
pub fn remove(&mut self, entity: Entity)
Removes the provided entity from the application.
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 add_global_listener<F>(&mut self, listener: F)
pub fn add_global_listener<F>(&mut self, listener: F)
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)
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])
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)
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)
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
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 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()
.
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)
pub fn get_proxy(&self) -> ContextProxy
pub fn resolve_entity_identifier(&self, identity: &str) -> Option<Entity>
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)
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 DataContext for Context
impl DataContext for Context
§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 Context
impl EmitContext for Context
§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)
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> 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.