-
Notifications
You must be signed in to change notification settings - Fork 567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the LifeCycle::Size event. #953
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,13 +188,37 @@ impl<T, W: Widget<T>> WidgetPod<T, W> { | |
self.state.id | ||
} | ||
|
||
/// Set layout rectangle. | ||
/// Set the layout [`Rect`]. | ||
/// | ||
/// Intended to be called on child widget in container's `layout` | ||
/// implementation. | ||
/// A container widget should call the [`Widget::layout`] method on its children in | ||
/// its own [`Widget::layout`] implementation, then possibly modify the returned [`Size`], and | ||
/// finally call this `set_layout_rect` method on the child to set the final layout [`Rect`]. | ||
/// | ||
/// The child will receive the [`LifeCycle::Size`] event informing them of the final [`Size`]. | ||
/// | ||
/// [`Widget::layout`]: trait.Widget.html#tymethod.layout | ||
/// [`Rect`]: struct.Rect.html | ||
/// [`Size`]: struct.Size.html | ||
/// [`LifeCycle::Size`]: enum.LifeCycle.html#variant.Size | ||
pub fn set_layout_rect(&mut self, ctx: &mut LayoutCtx, data: &T, env: &Env, layout_rect: Rect) { | ||
let mut needs_merge = false; | ||
|
||
let old_size = self.state.layout_rect.map(|r| r.size()); | ||
let new_size = layout_rect.size(); | ||
|
||
self.state.layout_rect = Some(layout_rect); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this feels like an opportunity for |
||
|
||
if old_size.is_none() || old_size.unwrap() != new_size { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and I'd have written this as |
||
let mut child_ctx = LifeCycleCtx { | ||
command_queue: ctx.command_queue, | ||
base_state: &mut self.state, | ||
window_id: ctx.window_id, | ||
}; | ||
let size_event = LifeCycle::Size(new_size); | ||
self.inner.lifecycle(&mut child_ctx, &size_event, data, env); | ||
needs_merge = true; | ||
} | ||
|
||
if WidgetPod::set_hot_state( | ||
&mut self.inner, | ||
ctx.command_queue, | ||
|
@@ -205,6 +229,10 @@ impl<T, W: Widget<T>> WidgetPod<T, W> { | |
data, | ||
env, | ||
) { | ||
needs_merge = true; | ||
} | ||
|
||
if needs_merge { | ||
ctx.base_state.merge_up(&self.state); | ||
} | ||
} | ||
|
@@ -215,9 +243,12 @@ impl<T, W: Widget<T>> WidgetPod<T, W> { | |
self.layout_rect() | ||
} | ||
|
||
/// The layout rectangle. | ||
/// Returns the layout [`Rect`]. | ||
/// | ||
/// This will be the same [`Rect`] that was set by [`set_layout_rect`]. | ||
/// | ||
/// This will be same value as set by `set_layout_rect`. | ||
/// [`Rect`]: struct.Rect.html | ||
/// [`set_layout_rect`]: #method.set_layout_rect | ||
pub fn layout_rect(&self) -> Rect { | ||
self.state.layout_rect.unwrap_or_default() | ||
} | ||
|
@@ -790,6 +821,11 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> { | |
|
||
true | ||
} | ||
LifeCycle::Size(_) => { | ||
// We are a descendant of a widget that received the Size event. | ||
// This event was meant only for our parent, so don't recurse. | ||
false | ||
} | ||
//NOTE: this is not sent here, but from the special set_hot_state method | ||
LifeCycle::HotChanged(_) => false, | ||
LifeCycle::FocusChanged(_) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
surprised clippy didn't want
.map(Rect::size)
.