-
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
Keep hot state consistent with mouse position. #841
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0051dca
Keep hot state consistent with mouse position.
xStrom 6cbffc2
Remove double referencing of env.
xStrom def0fb9
Restore iff comment.
xStrom 8a21f9a
Improve timer example box colors.
xStrom 2e1c601
Unify hot state determination to prevent it going out of sync.
xStrom 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
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 |
---|---|---|
|
@@ -73,7 +73,7 @@ pub(crate) struct BaseState { | |
/// The insets applied to the layout rect to generate the paint rect. | ||
/// In general, these will be zero; the exception is for things like | ||
/// drop shadows or overflowing text. | ||
paint_insets: Insets, | ||
pub(crate) paint_insets: Insets, | ||
|
||
// TODO: consider using bitflags for the booleans. | ||
|
||
|
@@ -182,8 +182,25 @@ impl<T, W: Widget<T>> WidgetPod<T, W> { | |
/// | ||
/// Intended to be called on child widget in container's `layout` | ||
/// implementation. | ||
pub fn set_layout_rect(&mut self, layout_rect: Rect) { | ||
pub fn set_layout_rect(&mut self, ctx: &mut LayoutCtx, data: &T, env: &Env, layout_rect: 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. I wish there were an alternative, but I can't think of one. |
||
self.state.layout_rect = Some(layout_rect); | ||
|
||
let had_hot = self.state.is_hot; | ||
self.state.is_hot = match ctx.mouse_pos { | ||
Some(pos) => layout_rect.winding(pos) != 0, | ||
None => false, | ||
}; | ||
if had_hot != self.state.is_hot { | ||
let hot_changed_event = LifeCycle::HotChanged(self.state.is_hot); | ||
let mut child_ctx = LifeCycleCtx { | ||
command_queue: ctx.command_queue, | ||
base_state: &mut self.state, | ||
window_id: ctx.window_id, | ||
}; | ||
self.inner | ||
.lifecycle(&mut child_ctx, &hot_changed_event, data, env); | ||
ctx.base_state.merge_up(&child_ctx.base_state); | ||
} | ||
} | ||
|
||
#[deprecated(since = "0.5.0", note = "use layout_rect() instead")] | ||
|
@@ -268,7 +285,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> { | |
base_state: &self.state, | ||
focus_widget: ctx.focus_widget, | ||
}; | ||
self.inner.paint(&mut inner_ctx, data, &env); | ||
self.inner.paint(&mut inner_ctx, data, env); | ||
ctx.z_ops.append(&mut inner_ctx.z_ops); | ||
|
||
if env.get(Env::DEBUG_PAINT) { | ||
|
@@ -313,7 +330,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> { | |
let layout_origin = self.layout_rect().origin().to_vec2(); | ||
ctx.transform(Affine::translate(layout_origin)); | ||
let visible = ctx.region().to_rect() - layout_origin; | ||
ctx.with_child_ctx(visible, |ctx| self.paint(ctx, data, &env)); | ||
ctx.with_child_ctx(visible, |ctx| self.paint(ctx, data, env)); | ||
}); | ||
} | ||
|
||
|
@@ -325,26 +342,36 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> { | |
/// [`layout`]: trait.Widget.html#tymethod.layout | ||
pub fn layout( | ||
&mut self, | ||
layout_ctx: &mut LayoutCtx, | ||
ctx: &mut LayoutCtx, | ||
bc: &BoxConstraints, | ||
data: &T, | ||
env: &Env, | ||
) -> Size { | ||
layout_ctx.paint_insets = Insets::ZERO; | ||
let size = self.inner.layout(layout_ctx, bc, data, &env); | ||
self.state.needs_layout = false; | ||
|
||
let child_mouse_pos = match ctx.mouse_pos { | ||
Some(pos) => Some(pos - self.layout_rect().origin().to_vec2()), | ||
None => None, | ||
}; | ||
let mut child_ctx = LayoutCtx { | ||
command_queue: ctx.command_queue, | ||
base_state: &mut self.state, | ||
window_id: ctx.window_id, | ||
text_factory: ctx.text_factory, | ||
mouse_pos: child_mouse_pos, | ||
}; | ||
let size = self.inner.layout(&mut child_ctx, bc, data, env); | ||
|
||
ctx.base_state.merge_up(&child_ctx.base_state); | ||
|
||
if size.width.is_infinite() { | ||
let name = self.widget().type_name(); | ||
log::warn!("Widget `{}` has an infinite width.", name); | ||
} | ||
|
||
if size.height.is_infinite() { | ||
let name = self.widget().type_name(); | ||
log::warn!("Widget `{}` has an infinite height.", name); | ||
} | ||
|
||
self.state.paint_insets = layout_ctx.paint_insets; | ||
self.state.needs_layout = false; | ||
size | ||
} | ||
|
||
|
@@ -496,11 +523,11 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> { | |
let hot_changed_event = LifeCycle::HotChanged(is_hot); | ||
let mut lc_ctx = child_ctx.make_lifecycle_ctx(); | ||
self.inner | ||
.lifecycle(&mut lc_ctx, &hot_changed_event, data, &env); | ||
.lifecycle(&mut lc_ctx, &hot_changed_event, data, env); | ||
} | ||
if recurse { | ||
child_ctx.base_state.has_active = false; | ||
self.inner.event(&mut child_ctx, &child_event, data, &env); | ||
self.inner.event(&mut child_ctx, &child_event, data, env); | ||
child_ctx.base_state.has_active |= child_ctx.base_state.is_active; | ||
}; | ||
|
||
|
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
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
Oops, something went wrong.
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.
I think it might make sense to have the 'last mouse position' also be in base state? I could imagine it being useful for lots of things, like calculating deltas when you see
MouseMoved
.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.
Having it in base state wouldn't help with this particular PR, because the base state value is going to be stale in
layout
. This could be observed in the timer example but it's not limited to that.If we end up wanting the last position in base state, then this
layout
pass would just be another update source for the base state's cached value.Given this, I think it's best to go with YAGNI here. When we eventually need it in base state, then that's the moment to add it.