mirror of https://github.com/linebender/xilem
some additions
This commit is contained in:
parent
a8f3781e38
commit
9616d23454
|
@ -716,8 +716,16 @@ impl_context_method!(
|
|||
///
|
||||
/// The return value is a token, which can be used to associate the
|
||||
/// request with the event.
|
||||
pub fn request_timer(&mut self, deadline: Duration) -> TimerId {
|
||||
pub fn request_timer_rel(&mut self, deadline: Duration) -> TimerId {
|
||||
let deadline = Instant::now() + deadline;
|
||||
self.request_timer(deadline)
|
||||
}
|
||||
|
||||
/// Request a timer event.
|
||||
///
|
||||
/// The return value is a token, which can be used to associate the
|
||||
/// request with the event.
|
||||
pub fn request_timer(&mut self, deadline: Instant) -> TimerId {
|
||||
let timer = Timer::new(self.widget_id(), deadline);
|
||||
let id = timer.id;
|
||||
self.global_state
|
||||
|
|
|
@ -663,6 +663,7 @@ impl MasonryState<'_> {
|
|||
|
||||
// --- MARK: TIMERS ---
|
||||
pub fn handle_new_events(&mut self, _: &ActiveEventLoop, _: winit::event::StartCause) {
|
||||
tracing::trace!("looking for elapsed timers");
|
||||
// check if timers have elapsed and set event loop to wake at next timer deadline
|
||||
let now = Instant::now();
|
||||
loop {
|
||||
|
@ -670,6 +671,7 @@ impl MasonryState<'_> {
|
|||
break;
|
||||
};
|
||||
if next_timer.deadline > now {
|
||||
tracing::trace!("checking timer deadline ({:?}) against current time ({now:?}", next_timer.deadline);
|
||||
break;
|
||||
}
|
||||
// timer has elapsed - remove from heap and handle
|
||||
|
@ -677,14 +679,17 @@ impl MasonryState<'_> {
|
|||
debug_panic!("should be unreachable: peek was Some");
|
||||
break;
|
||||
};
|
||||
tracing::trace!("found elapsed timer with id {elapsed_timer:?}");
|
||||
self.render_root.handle_elapsed_timer(elapsed_timer);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
|
||||
if let Some(next_timer) = self.timers.peek() {
|
||||
tracing::trace!("wait until {:?}", next_timer.deadline);
|
||||
event_loop.set_control_flow(ControlFlow::WaitUntil(next_timer.deadline));
|
||||
} else {
|
||||
tracing::trace!("wait until next event");
|
||||
event_loop.set_control_flow(ControlFlow::Wait);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -148,6 +148,7 @@ pub(crate) fn run_on_elapsed_timer_pass(root: &mut RenderRoot, timer: &Timer) ->
|
|||
&event,
|
||||
false,
|
||||
|widget, ctx, event| {
|
||||
tracing::info!("running on_timer_expired on widget {:?}", timer.widget_id);
|
||||
widget.on_timer_expired(ctx, event);
|
||||
// don't traverse for this event
|
||||
ctx.set_handled();
|
||||
|
|
|
@ -231,14 +231,14 @@ impl Widget for ProgressBar {
|
|||
}
|
||||
|
||||
fn on_timer_expired(&mut self, ctx: &mut EventCtx, _event: &TimerEvent) {
|
||||
println!("on_timer_expired");
|
||||
tracing::debug!("progress_bar: on_timer_expired");
|
||||
self.reset_animation();
|
||||
ctx.request_anim_frame();
|
||||
ctx.request_paint_only();
|
||||
}
|
||||
|
||||
fn on_anim_frame(&mut self, ctx: &mut UpdateCtx, interval: u64) {
|
||||
println!("on_anim_frame");
|
||||
tracing::debug!("progress_bar: on_anim_frame");
|
||||
// pixel per 1ms
|
||||
const NS_PER_PIXEL: f64 = 1_000_000.;
|
||||
const DURATION_BETWEEN_ANIMATIONS: Duration = Duration::from_millis(2_500);
|
||||
|
@ -258,7 +258,7 @@ impl Widget for ProgressBar {
|
|||
// animation finished;
|
||||
self.anim_pixel_position = 0.;
|
||||
self.anim_prev_interval = None;
|
||||
ctx.request_timer(DURATION_BETWEEN_ANIMATIONS);
|
||||
ctx.request_timer_rel(DURATION_BETWEEN_ANIMATIONS);
|
||||
} else {
|
||||
ctx.request_anim_frame();
|
||||
ctx.request_paint_only();
|
||||
|
|
|
@ -90,7 +90,13 @@ pub trait Widget: AsAny {
|
|||
///
|
||||
/// When you create a timer, you can stash the ID returned and compare it against
|
||||
/// `event.id`.
|
||||
fn on_timer_expired(&mut self, ctx: &mut EventCtx, event: &TimerEvent) {}
|
||||
fn on_timer_expired(&mut self, ctx: &mut EventCtx, event: &TimerEvent) {
|
||||
tracing::info!(
|
||||
"default on_timer_expired handler called, id={} debug_text={:?}",
|
||||
ctx.widget_id(),
|
||||
self.get_debug_text()
|
||||
);
|
||||
}
|
||||
|
||||
/// Called at the beginning of a new animation frame.
|
||||
///
|
||||
|
|
Loading…
Reference in New Issue