-
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
Associate timers with widget ids #831
Changes from 11 commits
1d099bd
3f47c3f
1bdc803
dd7892e
12cbde9
6349aa4
e6b8496
3d4870a
2e6b493
9a91913
0b77763
38210fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,7 @@ pub enum InternalEvent { | |
MouseLeave, | ||
/// A command still in the process of being dispatched. | ||
TargetedCommand(Target, Command), | ||
RouteTimer(TimerToken, WidgetId), | ||
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. Add a comment here describing this variant. Something simple like Used to route timer events. would work. |
||
} | ||
|
||
/// Application life cycle events. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
//! Management of multiple windows. | ||
|
||
use std::collections::HashMap; | ||
use std::mem; | ||
|
||
// Automatically defaults to std::time::Instant on non Wasm platforms | ||
|
@@ -28,8 +29,8 @@ use crate::widget::LabelText; | |
use crate::win_handler::RUN_COMMANDS_TOKEN; | ||
use crate::{ | ||
BoxConstraints, Command, Data, Env, Event, EventCtx, InternalEvent, InternalLifeCycle, | ||
LayoutCtx, LifeCycle, LifeCycleCtx, MenuDesc, PaintCtx, UpdateCtx, Widget, WidgetId, WidgetPod, | ||
WindowDesc, | ||
LayoutCtx, LifeCycle, LifeCycleCtx, MenuDesc, PaintCtx, TimerToken, UpdateCtx, Widget, | ||
WidgetId, WidgetPod, WindowDesc, | ||
}; | ||
|
||
/// A unique identifier for a window. | ||
|
@@ -48,6 +49,7 @@ pub struct Window<T> { | |
pub(crate) last_mouse_pos: Option<Point>, | ||
pub(crate) focus: Option<WidgetId>, | ||
pub(crate) handle: WindowHandle, | ||
pub(crate) timers: HashMap<TimerToken, WidgetId>, | ||
// delegate? | ||
} | ||
|
||
|
@@ -64,6 +66,7 @@ impl<T> Window<T> { | |
last_mouse_pos: None, | ||
focus: None, | ||
handle, | ||
timers: HashMap::new(), | ||
} | ||
} | ||
} | ||
|
@@ -171,6 +174,14 @@ impl<T: Data> Window<T> { | |
self.size = Size::new(size.width * scale, size.height * scale); | ||
Event::WindowSize(self.size) | ||
} | ||
Event::Timer(token) => { | ||
if let Some(widget_id) = self.timers.get(&token) { | ||
Event::Internal(InternalEvent::RouteTimer(token, *widget_id)) | ||
} else { | ||
log::error!("No widget found for timer {:?}", token); | ||
return false; | ||
} | ||
} | ||
other => other, | ||
}; | ||
|
||
|
@@ -215,6 +226,17 @@ impl<T: Data> Window<T> { | |
|
||
self.post_event_processing(queue, data, env, false); | ||
|
||
//In some platforms, timer tokens are reused. So it is necessary to remove token from | ||
//window's timer before adding base state's timers to it. | ||
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.
|
||
if let Event::Internal(InternalEvent::RouteTimer(token, _)) = event { | ||
self.timers.remove(&token); | ||
} | ||
|
||
//If at least one widget requested timer, collect those timers from widgets and add to window's timers map. | ||
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. If at least one widget requested a timer, |
||
if base_state.request_timer { | ||
self.timers.extend(base_state.timers); | ||
} | ||
|
||
is_handled | ||
} | ||
|
||
|
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.
Add
recurse = false;
here because if we get this then we're already a descendant of the targeted widget.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.
Good catch. Missed this important change. :(
Thanks.