Skip to content

Commit 702db96

Browse files
committed
fixed doc comments
1 parent 7e630ec commit 702db96

File tree

7 files changed

+68
-39
lines changed

7 files changed

+68
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ You can find its changes [documented below](#070---2021-01-01).
7474
- Added `compute_max_intrinsic` method to the `Widget` trait, which determines the maximum useful dimension of the widget ([#2172] by [@sjoshid])
7575
- Windows: Dark mode support for the title bar ([#2196] by [@dristic])
7676
- `ZStack` widget ([#2235] by [@xarvic])
77-
- `Lifecycle::ViewStateChanged`, `InternalLifecycle::RouteViewStateChanged`, `ChangeCtx`, and `RequestCtx` ([2149] by [@xarvic])
77+
- `Lifecycle::ViewStateChanged`, `InternalLifecycle::RouteViewStateChanged`, `ChangeCtx`, and `RequestCtx` ([#2149] by [@xarvic])
7878

7979
### Changed
8080

druid/src/contexts.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ pub struct State<'a, 'b> {
161161

162162
/// Convenience trait for code generic over contexts.
163163
///
164-
/// Methods to do with commands and timers.
165-
/// Available to all contexts but PaintCtx.
164+
/// Methods that deal with commands and timers.
165+
/// Available to all contexts but [`PaintCtx`].
166+
///
167+
/// [`PaintCtx`](PaintCtx)
166168
pub trait ChangeCtx<'b> {
167169
/// Submit a [`Command`] to be run after this event is handled. See [`submit_command`].
168170
///
@@ -446,7 +448,7 @@ impl_context_method!(
446448
/// Returns `true` if either this specific widget or any one of its descendants is focused.
447449
/// To check if only this specific widget is focused use [`is_focused`],
448450
///
449-
/// [`is_focused`]: #method.is_focused
451+
/// [`is_focused`]: crate::EventCtx::is_focused
450452
pub fn has_focus(&self) -> bool {
451453
self.widget_state.has_focus
452454
}
@@ -463,7 +465,7 @@ impl_context_method!(
463465
/// For an example the decrease button of a counter of type `usize` should be disabled if the
464466
/// value is `0`.
465467
///
466-
/// [`set_disabled`]: EventCtx::set_disabled
468+
/// [`set_disabled`]: crate::EventCtx::set_disabled
467469
pub fn is_disabled(&self) -> bool {
468470
self.widget_state.is_disabled()
469471
}
@@ -478,10 +480,10 @@ impl_context_method!(EventCtx<'_, '_>, UpdateCtx<'_, '_>, {
478480
/// cursor, the child widget's cursor will take precedence. (If that isn't what you want, use
479481
/// [`override_cursor`] instead.)
480482
///
481-
/// [`clear_cursor`]: EventCtx::clear_cursor
482-
/// [`override_cursor`]: EventCtx::override_cursor
483-
/// [`hot`]: EventCtx::is_hot
484-
/// [`active`]: EventCtx::is_active
483+
/// [`clear_cursor`]: crate::EventCtx::clear_cursor
484+
/// [`override_cursor`]: crate::EventCtx::override_cursor
485+
/// [`hot`]: crate::EventCtx::is_hot
486+
/// [`active`]: crate::EventCtx::is_active
485487
pub fn set_cursor(&mut self, cursor: &Cursor) {
486488
trace!("set_cursor {:?}", cursor);
487489
self.widget_state.cursor_change = CursorChange::Set(cursor.clone());
@@ -493,10 +495,10 @@ impl_context_method!(EventCtx<'_, '_>, UpdateCtx<'_, '_>, {
493495
/// effect when this widget is either [`hot`] or [`active`]. This will override the cursor
494496
/// preferences of a child widget. (If that isn't what you want, use [`set_cursor`] instead.)
495497
///
496-
/// [`clear_cursor`]: EventCtx::clear_cursor
497-
/// [`set_cursor`]: EventCtx::override_cursor
498-
/// [`hot`]: EventCtx::is_hot
499-
/// [`active`]: EventCtx::is_active
498+
/// [`clear_cursor`]: crate::EventCtx::clear_cursor
499+
/// [`set_cursor`]: crate::EventCtx::set_cursor
500+
/// [`hot`]: crate::EventCtx::is_hot
501+
/// [`active`]: crate::EventCtx::is_active
500502
pub fn override_cursor(&mut self, cursor: &Cursor) {
501503
trace!("override_cursor {:?}", cursor);
502504
self.widget_state.cursor_change = CursorChange::Override(cursor.clone());
@@ -506,20 +508,26 @@ impl_context_method!(EventCtx<'_, '_>, UpdateCtx<'_, '_>, {
506508
///
507509
/// This undoes the effect of [`set_cursor`] and [`override_cursor`].
508510
///
509-
/// [`override_cursor`]: EventCtx::override_cursor
510-
/// [`set_cursor`]: EventCtx::set_cursor
511+
/// [`override_cursor`]: crate::EventCtx::override_cursor
512+
/// [`set_cursor`]: crate::EventCtx::set_cursor
511513
pub fn clear_cursor(&mut self) {
512514
trace!("clear_cursor");
513515
self.widget_state.cursor_change = CursorChange::Default;
514516
}
515517
});
516518

517-
//methods on event, update and layout.
519+
// methods on event, update and layout.
518520
impl_context_method!(EventCtx<'_, '_>, UpdateCtx<'_, '_>, LayoutCtx<'_, '_>, {
519-
/// Indicate that your view_context has changed.
521+
/// Indicate that your [`ViewContext`] has changed.
522+
///
523+
/// This event is sent after layout is done and before paint is called. Note that you can still
524+
/// receive this event even if there was no prior call to layout.
525+
///
526+
/// Widgets must call this method after changing the clip region of their children.
527+
/// Changes to the other parts of [`ViewContext`] (cursor position and global origin) are tracked
528+
/// internally.
520529
///
521-
/// Widgets must call this method after changing the clip region of thier children.
522-
/// The other parts of view_context (cursor_position and global origin) are tracked internally.
530+
/// [`ViewContext`]: crate::ViewContext
523531
pub fn view_context_changed(&mut self) {
524532
self.widget_state.view_context_changed = true;
525533
}

druid/src/core.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
//! The fundamental druid types.
1616
17-
use druid::contexts::ChangeCtx;
1817
use std::collections::VecDeque;
1918
use tracing::{trace, trace_span, warn};
2019

2120
use crate::bloom::Bloom;
2221
use crate::command::sys::{CLOSE_WINDOW, SUB_WINDOW_HOST_TO_PARENT, SUB_WINDOW_PARENT_TO_HOST};
2322
use crate::commands::SCROLL_TO_VIEW;
24-
use crate::contexts::ContextState;
23+
use crate::contexts::{ChangeCtx, ContextState};
2524
use crate::kurbo::{Affine, Insets, Point, Rect, Shape, Size};
2625
use crate::sub_window::SubWindowUpdate;
2726
use crate::{
@@ -82,6 +81,10 @@ pub struct WidgetState {
8281
/// `size` these constitute the child's layout rect.
8382
origin: Point,
8483
/// The origin of the parent in the window coordinate space;
84+
//TODO: decide whether we should remove this. With this PR (https://github.com/linebender/druid/pull/2149)
85+
// the position of a widget can change during event, but its global position only gets updated
86+
// after layout while calling some_pod.layout_rect() gives you the correct local position every
87+
// time.
8588
pub(crate) parent_window_origin: Point,
8689
/// A flag used to track and debug missing calls to set_origin.
8790
is_expecting_set_origin_call: bool,
@@ -125,7 +128,12 @@ pub struct WidgetState {
125128
/// Some of our children have the `view_context_changed` flag set.
126129
pub(crate) children_view_context_changed: bool,
127130

131+
/// indicate that the [`ViewContext`] changed.
128132
///
133+
/// When this flag is set, the associated widget will receive a `LifeCycle::ViewContextChanged
134+
/// event`.
135+
///
136+
/// [`ViewContext`]: crate::ViewContext
129137
pub(crate) view_context_changed: bool,
130138

131139
/// Any descendant is active.
@@ -354,12 +362,12 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
354362
self.state.baseline_offset
355363
}
356364

357-
/// Determines if the provided `mouse_pos` is inside `rect`
365+
/// Determines if the provided `mouse_pos` is inside the widget's `layout_rect`
358366
/// and if so updates the hot state and sends `LifeCycle::HotChanged`.
359367
///
360368
/// Returns `true` if the hot state changed.
361369
///
362-
/// The provided `child_state` should be merged up if this returns `true`.
370+
/// The WidgetPod should merge up its state when `true` is returned.
363371
fn set_hot_state(
364372
&mut self,
365373
state: &mut ContextState,
@@ -1070,7 +1078,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
10701078
));
10711079

10721080
self.set_hot_state(ctx.state, view_context.last_mouse_position, data, env);
1073-
self.state.parent_window_origin = view_context.parent_window_origin;
1081+
self.state.parent_window_origin = view_context.window_origin;
10741082

10751083
self.state.children_view_context_changed = false;
10761084
self.state.view_context_changed = false;

druid/src/event.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
//! Events.
1616
17-
use crate::kurbo::{Rect, Size};
1817
use std::ops::{Add, Sub};
1918

2019
use druid_shell::{Clipboard, KeyEvent, TimerToken};
2120

21+
use crate::kurbo::{Rect, Size};
2222
use crate::mouse::MouseEvent;
2323
use crate::{Command, Notification, Point, WidgetId};
2424

@@ -304,7 +304,12 @@ pub enum LifeCycle {
304304
///
305305
/// [`EventCtx::is_focused`]: struct.EventCtx.html#method.is_focused
306306
FocusChanged(bool),
307+
/// Called when the [`ViewContext`] of this widget changed.
307308
///
309+
/// See [`view_context_changed`] on how and when to request this event.
310+
///
311+
/// [`view_context_changed`]: crate::EventCtx::view_context_changed
312+
/// [`ViewContext`]: ViewContext
308313
ViewContextChanged(ViewContext),
309314
/// Internal druid lifecycle event.
310315
///
@@ -334,7 +339,8 @@ pub enum InternalLifeCycle {
334339
},
335340
/// Used to route the `DisabledChanged` event to the required widgets.
336341
RouteDisabledChanged,
337-
/// The parents widget origin in window coordinate space has changed.
342+
343+
/// Used to route the `ViewContextChanged` event to the required widgets.
338344
RouteViewContextChanged(ViewContext),
339345
/// For testing: request the `WidgetState` of a specific widget.
340346
///
@@ -364,18 +370,25 @@ pub enum InternalLifeCycle {
364370
DebugInspectState(StateCheckFn),
365371
}
366372

367-
/// Information about the widgets surroundings.
373+
/// Information about the widget's surroundings.
374+
///
375+
/// The global origin is also saved in the widget state.
376+
///
377+
/// When the `ViewContext` of a widget changes it receives a `ViewContextChanged` event.
368378
#[derive(Debug, Copy, Clone)]
369379
pub struct ViewContext {
370-
/// The origin of this widget's parent relative to the window.
371-
pub parent_window_origin: Point,
380+
/// The origin of this widget relative to the window.
381+
///
382+
/// This is written from the perspective of the Widget and not the Pod.
383+
/// For the Pod this is its parent's window origin.
384+
pub window_origin: Point,
372385

373-
/// The last position the cursor was on, relative to the widget.
386+
/// The last position the cursor was at, relative to the widget.
374387
pub last_mouse_position: Option<Point>,
375388

376389
/// The visible area, this widget is contained in, relative to the widget.
377390
///
378-
/// The area may be larger than the widgets paint_rect.
391+
/// The area may be larger than the widgets `paint_rect`.
379392
pub clip: Rect,
380393
}
381394

@@ -444,7 +457,7 @@ impl LifeCycle {
444457

445458
/// Returns an event for a widget which maybe is overlapped by another widget.
446459
///
447-
/// When ignore is set to `true` the widget will set its hot state to `false` even if the cursor
460+
/// When `ignore` is set to `true` the widget will set its hot state to `false` even if the cursor
448461
/// is inside its bounds.
449462
pub fn ignore_hot(&self, ignore: bool) -> Self {
450463
if ignore {
@@ -490,11 +503,11 @@ impl InternalLifeCycle {
490503
}
491504

492505
impl ViewContext {
493-
/// Transforms the `ViewContext` into the coordinate_space of its child.
506+
/// Transforms the `ViewContext` into the coordinate space of its child.
494507
pub(crate) fn for_child_widget(&self, child_origin: Point) -> Self {
495508
let child_origin = child_origin.to_vec2();
496509
ViewContext {
497-
parent_window_origin: self.parent_window_origin.add(child_origin),
510+
window_origin: self.window_origin.add(child_origin),
498511
last_mouse_position: self.last_mouse_position.map(|pos| pos.sub(child_origin)),
499512
clip: self.clip.sub(child_origin),
500513
}

druid/src/widget/clip_box.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ impl Viewport {
8787
}
8888
}
8989

90-
/// Sets the component selected by axis of viewport origin to `pos`, while trying to keep the
90+
/// Sets the component selected by `axis` of viewport origin to `pos`, while trying to keep the
9191
/// view rectangle inside the content rectangle.
9292
///
93-
/// Returns true if the position changed. Note that the valid values for the viewport origin
93+
/// Returns `true` if the position changed. Note that the valid values for the viewport origin
9494
/// are constrained by the size of the child, and so the origin might not get set to exactly
9595
/// `pos`.
9696
pub fn pan_to_on_axis(&mut self, axis: Axis, pos: f64) -> bool {
@@ -374,7 +374,7 @@ impl<T, W: Widget<T>> ClipBox<T, W> {
374374
})
375375
}
376376

377-
/// Pans the minimal distance to show the target rect.
377+
/// Pans the minimal distance to show the `region`.
378378
///
379379
/// If the target region is larger than the viewport, we will display the
380380
/// portion that fits, prioritizing the portion closest to the origin.

druid/src/widget/scroll.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<T, W: Widget<T>> Scroll<T, W> {
6565
self.clip.pan_by(ctx, data, env, delta)
6666
}
6767

68-
/// Scroll the minimal distance to show the target rect.
68+
/// Scroll the minimal distance to show the target `region`.
6969
///
7070
/// If the target region is larger than the viewport, we will display the
7171
/// portion that fits, prioritizing the portion closest to the origin.

druid/src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<T: Data> Window<T> {
185185
if self.root.state().children_view_context_changed && !self.root.state().needs_layout {
186186
let event =
187187
LifeCycle::Internal(InternalLifeCycle::RouteViewContextChanged(ViewContext {
188-
parent_window_origin: Point::ORIGIN,
188+
window_origin: Point::ORIGIN,
189189
last_mouse_position: self.last_mouse_pos,
190190
clip: self.size.to_rect(),
191191
}));

0 commit comments

Comments
 (0)