Skip to main content

EventCtx

Struct EventCtx 

Source
pub struct EventCtx<'a> { /* private fields */ }
Expand description

A context provided to event-handling Widget methods.

Implementations§

Source§

impl EventCtx<'_>

Source

pub fn widget_id(&self) -> WidgetId

The WidgetId of the current widget.

Source

pub fn transform(&self) -> Affine

The current (local) transform of this widget.

Source§

impl EventCtx<'_>

Source

pub fn text_contexts( &mut self, ) -> (&mut FontContext, &mut LayoutContext<BrushIndex>)

Get the Parley contexts needed to build and paint text sections.

Most users should embed a pre-built label widget (such as masonry::widgets::Label) as a child for non-interactive text. These contexts could however be useful for custom text editing, such as for rich text editing.

Any cached text layouts should be invalidated in the layout pass when Self::fonts_changed returns true.

Source

pub fn fonts_changed(&mut self) -> bool

Whether the set of loaded fonts has changed since layout was most recently called.

Any cached text layouts should be invalidated in the layout pass when this is true.

Source§

impl EventCtx<'_>

Source

pub fn capture_pointer(&mut self)

Capture the pointer in the current widget.

Pointer capture is only allowed during a Down event. It is a logic error to capture the pointer during any other event.

A widget normally only receives pointer events when the pointer is inside the widget’s layout box. Pointer capture causes widget layout boxes to be ignored: when the pointer is captured by a widget, that widget will continue receiving pointer events when the pointer is outside the widget’s layout box. Other widgets the pointer is over will not receive events. Events that are not marked as handled by the capturing widget, bubble up to the widget’s ancestors, ignoring their layout boxes as well.

The pointer cannot be captured by multiple widgets at the same time. If a widget has captured the pointer and another widget captures it, the first widget loses the pointer capture.

§Releasing the pointer

Any widget can release the pointer during any event. The pointer is automatically released after handling of a Up or Cancel event completes. A widget holding the pointer capture will be the target of these events.

If pointer capture is lost for external reasons (the widget is disabled, the window lost focus, etc), the widget will still get a Cancel event.

Source

pub fn release_pointer(&mut self)

Release the pointer previously captured through capture_pointer.

Source

pub fn request_scroll_to_this(&mut self)

Send a signal to parent widgets to scroll this widget into view.

Source

pub fn request_scroll_to(&mut self, rect: Rect)

Send a signal to parent widgets to scroll this area into view.

rect is in local coordinates.

Source

pub fn set_handled(&mut self)

Set the event as “handled”, which stops its propagation to parent widgets.

Source

pub fn is_handled(&self) -> bool

Determine whether the event has been handled.

Source

pub fn target(&self) -> WidgetId

The widget originally targeted by the event.

This will be different from widget_id during event bubbling.

Source

pub fn request_focus(&mut self)

Request text focus.

Because only one widget can be focused at a time, multiple focus requests from different widgets during a single event cycle means that the last widget that requests focus will override the previous requests.

Source

pub fn set_focus(&mut self, target: WidgetId)

Transfer text focus to the widget with the given WidgetId.

Source

pub fn resign_focus(&mut self)

Give up text focus.

This should only be called by a widget that currently has focus.

Source

pub fn local_position(&self, p: PhysicalPosition<f64>) -> Point

Translate window position to widget local position.

Source§

impl EventCtx<'_>

Source

pub fn size(&self) -> Size

The layout size.

This is roughly the layout size returned by the layout method on the previous layout pass, with some adjustment for pixel snapping.

Source

pub fn baseline_offset(&self) -> f64

The offset of the baseline relative to the bottom of the widget.

Source

pub fn window_origin(&self) -> Point

The origin of the widget in window coordinates, relative to the top left corner of the content area.

Source

pub fn window_transform(&self) -> Affine

Global transform of this widget in the window coordinate space.

Computed from all transform and scroll_translation values from this to the root widget.

Source

pub fn bounding_rect(&self) -> Rect

The bounding rect of the widget in window coordinates.

See bounding rect documentation for details.

Source

pub fn paint_rect(&self) -> Rect

The paint rect of the widget.

Covers the area we expect to be invalidated when the widget is painted.

Source

pub fn clip_path(&self) -> Option<Rect>

The clip path of the widget, if any was set.

For more information, see LayoutCtx::set_clip_path.

Source

pub fn to_window(&self, widget_point: Point) -> Point

Convert a point from the widget’s coordinate space to the window’s.

The returned point is relative to the content area; it excludes window chrome.

Source§

impl EventCtx<'_>

Source

pub fn get_scale_factor(&self) -> f64

Get DPI scaling factor.

This is not required for most widgets, and should be used only for precise rendering, such as rendering single pixel lines or selecting image variants. This is currently only provided in the render stages, as these are the only passes which are re-run when the scale factor changes, except EventCtx where it is necessary to translate pointer events which are currently in physical coordinates.

Note that accessibility nodes and paint results will automatically be scaled by Masonry. This also doesn’t account for the widget’s current transform, which cannot currently be accessed by widgets directly.

Source§

impl EventCtx<'_>

Source

pub fn is_hovered(&self) -> bool

The “hovered” status of a widget.

A widget is “hovered” when a pointer is hovering over it. Widgets will often change their appearance as a visual indication that they will respond to pointer (usually mouse) interaction.

If the pointer is captured, then only the capturing widget can have hovered status. If the pointer is captured but not hovering over the captured widget, then no widget has the hovered status.

Source

pub fn has_hovered(&self) -> bool

Whether this widget or any of its descendants are hovered.

To check if only this specific widget is hovered use is_hovered.

Source

pub fn is_active(&self) -> bool

The “active” status of a widget.

A widget is “active” when the user is “actively” interacting with it. Currently, a widget is determined to be active when it has captured a pointer, but this may change in the future to account for e.g. keyboard interactions.

Source

pub fn has_active(&self) -> bool

Whether this widget or any of its descendants are active.

To check if only this specific widget is active use is_active.

Source

pub fn is_pointer_capture_target(&self) -> bool

Whether a pointer is captured by this widget.

The pointer will usually be the mouse. In future versions, this function will take a pointer id as input to test a specific pointer.

Source

pub fn is_focus_target(&self) -> bool

The text focus status of a widget.

The focused widget is the one that receives keyboard events.

Returns true if this specific widget is focused. To check if any descendants are focused use has_focus_target.

Source

pub fn has_focus_target(&self) -> bool

Whether this widget or any of its descendants are focused.

To check if only this specific widget is focused use is_focus_target.

Source

pub fn is_window_focused(&self) -> bool

Whether the window is focused.

Source

pub fn accepts_pointer_interaction(&self) -> bool

Whether this widget gets pointer events and hovered status.

Source

pub fn accepts_focus(&self) -> bool

Whether this widget gets text focus.

Source

pub fn accepts_text_input(&self) -> bool

Whether this widget gets IME events.

Source

pub fn is_disabled(&self) -> bool

Whether this widget is disabled.

Returns true if this widget or any of its ancestors is explicitly disabled. To make this widget explicitly disabled use set_disabled.

Source

pub fn is_stashed(&self) -> bool

Whether this widget is stashed.

Source§

impl EventCtx<'_>

Source

pub fn request_render(&mut self)

Request a paint and an accessibility pass.

Source

pub fn request_paint_only(&mut self)

Request a paint pass, specifically for the paint method.

Unlike request_render, this does not request an accessibility pass or a call to post_paint.

Use request_render unless you’re sure neither is needed.

Source

pub fn request_post_paint(&mut self)

Request a paint pass for the post_paint method.

Source

pub fn request_accessibility_update(&mut self)

Request an accessibility pass.

This doesn’t request a paint pass. If you want to request both an accessibility pass and a paint pass, use request_render.

Source

pub fn request_layout(&mut self)

Request a layout pass.

Call this method if the widget has changed in a way that requires a layout pass.

Source

pub fn request_compose(&mut self)

Request a compose pass.

The compose pass is often cheaper than the layout pass, because it can only transform individual widgets’ position.

Source

pub fn request_anim_frame(&mut self)

Request an animation frame.

Source

pub fn request_cursor_icon_change(&mut self)

Notifies Masonry that the cursor returned by Widget::get_cursor has changed.

This is mostly meant for cases where the cursor changes even if the pointer doesn’t move, because the nature of the widget has changed somehow.

Source

pub fn children_changed(&mut self)

Indicate that your children have changed.

Widgets must call this method after adding a new child.

Source

pub fn remove_child(&mut self, child: WidgetPod<impl Widget + ?Sized>)

Indicate that a child is about to be removed from the tree.

Container widgets should avoid dropping WidgetPods. Instead, they should pass them to this method.

Source

pub fn set_disabled(&mut self, disabled: bool)

Set the disabled state for this widget.

Setting this to false does not mean a widget is not still disabled; for instance it may still be disabled by an ancestor. See is_disabled for more information.

Source

pub fn set_transform(&mut self, transform: Affine)

Set the transform for this widget.

It behaves similarly as CSS transforms

Source§

impl EventCtx<'_>

Source

pub fn set_stashed( &mut self, child: &mut WidgetPod<impl Widget + ?Sized>, stashed: bool, )

Mark child widget as stashed.

If stashed is true, the child will not be painted or listed in the accessibility tree.

This will not trigger a layout pass.

Source

pub fn mutate_self_later( &mut self, f: impl FnOnce(WidgetMut<'_, dyn Widget>) + Send + 'static, )

Queue a callback that will be called with a WidgetMut for this widget.

The callbacks will be run in the order they were submitted during the mutate pass.

Source

pub fn mutate_later<W: Widget + FromDynWidget + ?Sized>( &mut self, child: &mut WidgetPod<W>, f: impl FnOnce(WidgetMut<'_, W>) + Send + 'static, )

Queue a callback that will be called with a WidgetMut for the given child widget.

The callbacks will be run in the order they were submitted during the mutate pass.

Source

pub fn get_raw<Child: Widget + FromDynWidget + ?Sized>( &mut self, child: &mut WidgetPod<Child>, ) -> (&Child, RawCtx<'_>)

Get direct reference to the stored child, and a context handle for that child.

Source

pub fn get_raw_mut<Child: Widget + FromDynWidget + AllowRawMut + ?Sized>( &mut self, child: &mut WidgetPod<Child>, ) -> (&mut Child, RawCtx<'_>)

Get direct mutable reference to the stored child, and a context handle for that child.

This context lets you set pass flags for the child widget, which may be tricky. In general, you should avoid setting the flags of a pass that runs before the pass you’re currently in. Not doing so might lead to performance cliffs and, hypothetically, panics.

This method is an escape hatch for cases where a parent widget completely controls their child, but needs it to be a separate widget for user interaction to behave as expected. As such, the child widget must opt-in using the AllowRawMut trait.

See pass documentation for the pass order.

Source

pub fn submit_action<Action: AnyDebug + Send>( &mut self, action: impl Into<Action>, )

Submit an action, which indicates that this widget requires something be handled by the application, such as user input.

The Action type parameter should always be the Self::Action associated type of the widget you’re calling this method from. Masonry will validate this, and this method may panic if this isn’t the case.

For further details see ErasedAction.

Source

pub fn submit_untyped_action(&mut self, action: ErasedAction)

Submit a type-erased action.

Unlike Self::submit_action, this method lets you submit an action with an arbitrary type, which may not match Self::Action. This may act as an escape hatch in some situations.

For further details see ErasedAction.

Source

pub fn set_ime_area(&mut self, ime_area: Rect)

Set the IME cursor area.

When this widget is focused and accepts text input, the reported IME area is sent to the platform. The area can be used by the platform to, for example, place a candidate box near that area, while ensuring the area is not obscured.

If no IME area is set, the platform will use the widget’s layout rect.

Source

pub fn clear_ime_area(&mut self)

Remove the IME cursor area.

See LayoutCtx::set_ime_area for more details.

Source

pub fn set_clipboard(&mut self, contents: String)

Set the contents of the platform clipboard.

For example, text widgets should call this for “cut” and “copy” user interactions. Note that we currently don’t support the “Primary” selection buffer on X11/Wayland.

Source

pub fn drag_window(&mut self)

Start a window drag.

Moves the window with the left mouse button until the button is released.

Source

pub fn drag_resize_window(&mut self, direction: ResizeDirection)

Start a window resize.

Resizes the window with the left mouse button until the button is released.

Source

pub fn toggle_maximized(&mut self)

Toggle the maximized state of the window.

Source

pub fn minimize(&mut self)

Minimize the window.

Source

pub fn exit(&mut self)

Exit the application.

Source

pub fn show_window_menu(&mut self, position: LogicalPosition<f64>)

Show the window menu at a specified position.

Source

pub fn create_layer<W: Widget + ?Sized>( &mut self, root_widget: NewWidget<W>, position: Point, )

Create a new layer at a specified position.

Source

pub fn remove_layer(&mut self, root_widget_id: WidgetId)

Remove the layer with the specified widget as root.

Source

pub fn reposition_layer(&mut self, root_widget_id: WidgetId, position: Point)

Reposition the layer with the specified widget as root.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for EventCtx<'a>

§

impl<'a> !Send for EventCtx<'a>

§

impl<'a> !Sync for EventCtx<'a>

§

impl<'a> !UnwindSafe for EventCtx<'a>

§

impl<'a> Freeze for EventCtx<'a>

§

impl<'a> Unpin for EventCtx<'a>

§

impl<'a> UnsafeUnpin for EventCtx<'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
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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