Struct druid::EventCtx

source ·
pub struct EventCtx<'a, 'b> { /* private fields */ }
Expand description

A mutable context provided to event handling methods of widgets.

Widgets should call request_paint whenever an event causes a change in the widget’s appearance, to schedule a repaint.

Implementations§

get the WidgetId of the current widget.

Returns a reference to the current WindowHandle.

Get the WindowId of the current window.

Get an object which can create text layouts.

The current window’s Scale.

The returned Scale is a copy and thus its information will be stale after the platform changes the window’s scale. This means you can only rely on it until the next Event::WindowScale event happens.

The layout size.

This is the layout size as ultimately determined by the parent container, on the previous layout pass.

Generally it will be the same as the size returned by the child widget’s layout method.

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

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.

Convert a point from the widget’s coordinate space to the screen’s. See the Screen module

Query the “hot” state of the widget.

See WidgetPod::is_hot for additional information.

Query the “active” state of the widget.

See WidgetPod::is_active for additional information.

The focus status of a widget.

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

Focus means that the widget receives keyboard events.

A widget can request focus using the request_focus method. It’s also possible to register for automatic focus via register_for_focus.

If a widget gains or loses focus it will get a LifeCycle::FocusChanged event.

Only one widget at a time is focused. However due to the way events are routed, all ancestors of that widget will also receive keyboard events.

The (tree) focus status of a widget.

Returns true if either this specific widget or any one of its descendants is focused. To check if only this specific widget is focused use is_focused,

The disabled state of a widget.

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

Disabled means that this widget should not change the state of the application. What that means is not entirely clear but in any it should not change its data. Therefore others can use this as a safety mechanism to prevent the application from entering an illegal state. For an example the decrease button of a counter of type usize should be disabled if the value is 0.

Set the cursor icon.

This setting will be retained until clear_cursor is called, but it will only take effect when this widget is either hot or active. If a child widget also sets a cursor, the child widget’s cursor will take precedence. (If that isn’t what you want, use override_cursor instead.)

Override the cursor icon.

This setting will be retained until clear_cursor is called, but it will only take effect when this widget is either hot or active. This will override the cursor preferences of a child widget. (If that isn’t what you want, use set_cursor instead.)

Clear the cursor icon.

This undoes the effect of set_cursor and override_cursor.

Indicate that your ViewContext has changed.

This event is sent after layout is done and before paint is called. Note that you can still receive this event even if there was no prior call to layout.

Widgets must call this method after changing the clip region of their children. Changes to the other parts of ViewContext (cursor position and global origin) are tracked internally.

Request a paint pass. This is equivalent to calling request_paint_rect for the widget’s paint_rect.

Request a paint pass for redrawing a rectangle, which is given relative to our layout rectangle.

Request a layout pass.

A Widget’s layout method is always called when the widget tree changes, or the window is resized.

If your widget would like to have layout called at any other time, (such as if it would like to change the layout of children in response to some event) it must call this method.

Request an AnimFrame event.

Receiving AnimFrame does not inherently mean a paint invocation will follow. If you want something actually painted you need to explicitly call request_paint or request_paint_rect when handling the AnimFrame event.

Note that not requesting paint when handling the AnimFrame event and then recursively requesting another AnimFrame can lead to rapid event fire, which is probably not what you want and would most likely be wasted compute cycles.

Indicate that your children have changed.

Widgets must call this method after adding a new child, removing a child or changing which children are hidden (see should_propagate_to_hidden).

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.

Calling this method during LifeCycle::DisabledChanged has no effect.

Indicate that text input state has changed.

A widget that accepts text input should call this anytime input state (such as the text or the selection) changes as a result of a non text-input event.

Create a new sub-window.

The sub-window will have its app data synchronised with caller’s nearest ancestor WidgetPod. ‘U’ must be the type of the nearest surrounding WidgetPod. The ‘data’ argument should be the current value of data for that widget.

Scrolls this widget into view.

If this widget is only partially visible or not visible at all because of Scrolls it is wrapped in, they will do the minimum amount of scrolling necessary to bring this widget fully into view.

If the widget is hidden, this method has no effect.

This functionality is achieved by sending a SCROLL_TO_VIEW notification.

Submit a Command to be run after this event is handled.

Commands are run in the order they are submitted; all commands submitted during the handling of an event are executed before the update method is called; events submitted during update are handled after painting.

Target::Auto commands will be sent to the window containing the widget.

Returns an ExtEventSink that can be moved between threads, and can be used to submit commands back to the application.

Request a timer event.

The return value is a token, which can be used to associate the request with the event.

Submit a Notification.

The provided argument can be a Selector or a Command; this lets us work with the existing API for adding a payload to a Selector.

If the argument is a Command, the command’s target will be ignored.

Examples
const IMPORTANT_EVENT: Selector<String> = Selector::new("druid-example.important-event");

fn check_event(ctx: &mut EventCtx, event: &Event) {
    if is_this_the_event_we_were_looking_for(event) {
        ctx.submit_notification(IMPORTANT_EVENT.with("That's the one".to_string()))
    }
}

Submit a Notification without warning.

In contrast to submit_notification, calling this method will not result in an “unhandled notification” warning.

Set the “active” state of the widget.

See EventCtx::is_active.

Create a new window. T must be the application’s root Data type (the type provided to AppLauncher::launch).

Show the context menu in the window containing the current widget. T must be the application’s root Data type (the type provided to AppLauncher::launch).

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

Determine whether the event has been handled by some other widget.

Request keyboard 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.

See is_focused for more information about focus.

Transfer focus to the widget with the given WidgetId.

See is_focused for more information about focus.

Transfer focus to the next focusable widget.

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

See is_focused for more information about focus.

Transfer focus to the previous focusable widget.

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

See is_focused for more information about focus.

Give up focus.

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

See is_focused for more information about focus.

Request an update cycle.

After this, update will be called on the widget in the next update cycle, even if there’s not a data change.

The use case for this method is when a container widget synthesizes data for its children. This is appropriate in specialized cases, but before reaching for this method, consider whether it might be better to refactor to be more idiomatic, in particular to make that data available in the app state.

Scrolls the area into view.

If the area is only partially visible or not visible at all because of Scrolls this widget is wrapped in, they will do the minimum amount of scrolling necessary to bring the area fully into view.

If the widget is hidden, this method has no effect.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

Performs the conversion.
Performs the conversion.
Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more