Struct druid::LifeCycleCtx
source · pub struct LifeCycleCtx<'a, 'b> { /* private fields */ }
Expand description
A mutable context provided to the lifecycle
method on widgets.
Certain methods on this context are only meaningful during the handling of
specific lifecycle events; for instance register_child
should only be called while handling LifeCycle::WidgetAdded
.
Implementations§
source§impl LifeCycleCtx<'_, '_>
impl LifeCycleCtx<'_, '_>
sourcepub fn window(&self) -> &WindowHandle
pub fn window(&self) -> &WindowHandle
Returns a reference to the current WindowHandle
.
source§impl LifeCycleCtx<'_, '_>
impl LifeCycleCtx<'_, '_>
sourcepub fn size(&self) -> Size
pub fn size(&self) -> Size
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.
sourcepub fn window_origin(&self) -> Point
pub fn window_origin(&self) -> Point
The origin of the widget in window coordinates, relative to the top left corner of the content area.
sourcepub fn to_window(&self, widget_point: Point) -> Point
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.
sourcepub fn to_screen(&self, widget_point: Point) -> Point
pub fn to_screen(&self, widget_point: Point) -> Point
Convert a point from the widget’s coordinate space to the screen’s.
See the Screen
module
sourcepub fn is_hot(&self) -> bool
pub fn is_hot(&self) -> bool
Query the “hot” state of the widget.
See WidgetPod::is_hot
for additional information.
sourcepub fn is_active(&self) -> bool
pub fn is_active(&self) -> bool
Query the “active” state of the widget.
See WidgetPod::is_active
for additional information.
sourcepub fn is_focused(&self) -> bool
pub fn is_focused(&self) -> bool
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.
sourcepub fn has_focus(&self) -> bool
pub fn has_focus(&self) -> bool
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
,
sourcepub fn is_disabled(&self) -> bool
pub fn is_disabled(&self) -> bool
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
.
source§impl LifeCycleCtx<'_, '_>
impl LifeCycleCtx<'_, '_>
sourcepub fn request_paint(&mut self)
pub fn request_paint(&mut self)
Request a paint
pass. This is equivalent to calling
request_paint_rect
for the widget’s paint_rect
.
Examples found in repository?
More examples
sourcepub fn request_paint_rect(&mut self, rect: Rect)
pub fn request_paint_rect(&mut self, rect: Rect)
Request a paint
pass for redrawing a rectangle, which is given
relative to our layout rectangle.
sourcepub fn request_layout(&mut self)
pub fn request_layout(&mut self)
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.
sourcepub fn request_anim_frame(&mut self)
pub fn request_anim_frame(&mut self)
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.
sourcepub fn children_changed(&mut self)
pub fn children_changed(&mut self)
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
).
sourcepub fn set_disabled(&mut self, disabled: bool)
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.
Calling this method during LifeCycle::DisabledChanged
has no effect.
sourcepub fn invalidate_text_input(&mut self, event: ImeInvalidation)
pub fn invalidate_text_input(&mut self, event: ImeInvalidation)
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.
sourcepub fn new_sub_window<W: Widget<U> + 'static, U: Data>(
&mut self,
window_config: WindowConfig,
widget: W,
data: U,
env: Env
) -> WindowId
pub fn new_sub_window<W: Widget<U> + 'static, U: Data>( &mut self, window_config: WindowConfig, widget: W, data: U, env: Env ) -> WindowId
sourcepub fn scroll_to_view(&mut self)
pub fn scroll_to_view(&mut self)
Scrolls this widget into view.
If this widget is only partially visible or not visible at all because of Scroll
s
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.
source§impl LifeCycleCtx<'_, '_>
impl LifeCycleCtx<'_, '_>
sourcepub fn submit_command(&mut self, cmd: impl Into<Command>)
pub fn submit_command(&mut self, cmd: impl Into<Command>)
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.
Examples found in repository?
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
fn lifecycle(
&mut self,
child: &mut W,
ctx: &mut LifeCycleCtx,
event: &LifeCycle,
data: &T,
env: &Env,
) {
if let LifeCycle::HotChanged(false) = event {
if let TooltipState::Showing(win_id) = self.state {
ctx.submit_command(CLOSE_WINDOW.to(win_id));
}
self.state = TooltipState::Fresh;
}
child.lifecycle(ctx, event, data, env)
}
sourcepub fn get_external_handle(&self) -> ExtEventSink
pub fn get_external_handle(&self) -> ExtEventSink
Returns an ExtEventSink
that can be moved between threads,
and can be used to submit commands back to the application.
sourcepub fn request_timer(&mut self, deadline: Duration) -> TimerToken
pub fn request_timer(&mut self, deadline: Duration) -> TimerToken
Request a timer event.
The return value is a token, which can be used to associate the request with the event.
source§impl LifeCycleCtx<'_, '_>
impl LifeCycleCtx<'_, '_>
sourcepub fn register_child(&mut self, child_id: WidgetId)
pub fn register_child(&mut self, child_id: WidgetId)
Registers a child widget.
This should only be called in response to a LifeCycle::WidgetAdded
event.
In general, you should not need to call this method; it is handled by
the WidgetPod
.
sourcepub fn register_for_focus(&mut self)
pub fn register_for_focus(&mut self)
Register this widget to be eligible to accept focus automatically.
This should only be called in response to a LifeCycle::BuildFocusChain
event.
See EventCtx::is_focused
for more information about focus.
sourcepub fn register_text_input(&mut self, document: impl ImeHandlerRef + 'static)
pub fn register_text_input(&mut self, document: impl ImeHandlerRef + 'static)
Register this widget as accepting text input.