Skip to content

Commit

Permalink
Add public methods to access clip path
Browse files Browse the repository at this point in the history
This adds public getters for clip path to non-`LayoutCtx` contexts, and
also to `WidgetState` as it seems relevant for painting.

The motivation is to remove private field accesses from
`Widget::get_child_at_pos` as mentioned in
#565 (comment).

This leaves one private field access, namely `WidgetState::is_stashed`.
As per the docs of `WidgetState` ("widgets will generally not interact
with it directly"), that field perhaps should not be publicly accessible
from `WidgetState`, but then it should be accessible from `WidgetRef`.
  • Loading branch information
tomcur committed Oct 1, 2024
1 parent 67f0043 commit 0beb3e7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions masonry/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ impl_context_method!(
self.widget_state.window_origin()
}

/// The clip path of the widget, if any was set.
///
/// For more information, see
/// [`LayoutCtx::set_clip_path`](crate::LayoutCtx::set_clip_path).
pub fn clip_path(&self) -> Option<Rect> {
self.widget_state.clip_path()
}

/// 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.
Expand Down
5 changes: 2 additions & 3 deletions masonry/src/widget/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,9 @@ pub trait Widget: AsAny {
ctx: QueryCtx<'c>,
pos: Point,
) -> Option<WidgetRef<'c, dyn Widget>> {
let relative_pos = pos - ctx.widget_state.window_origin().to_vec2();
let relative_pos = pos - ctx.window_origin().to_vec2();
if !ctx
.widget_state
.clip
.clip_path()
.map_or(true, |clip| clip.contains(relative_pos))
{
return None;
Expand Down
7 changes: 7 additions & 0 deletions masonry/src/widget/widget_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ impl WidgetState {
Rect::from_origin_size(self.window_origin(), self.size)
}

/// The clip path of the widget, if any was set.
///
/// For more information, see [`LayoutCtx::set_clip_path`](crate::LayoutCtx::set_clip_path).
pub fn clip_path(&self) -> Option<Rect> {
self.clip
}

/// Returns the area being edited by an IME, in global coordinates.
///
/// By default, returns the same as [`Self::window_layout_rect`].
Expand Down

0 comments on commit 0beb3e7

Please sign in to comment.