Skip to content

Commit 4af2709

Browse files
authored
Add the request_timer method to LifeCycleCtx. (#954)
1 parent ff4c588 commit 4af2709

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
4242
- `MouseButtons` to `MouseEvent` to track which buttons are being held down during an event. ([#843] by [@xStrom])
4343
- `Env` and `Key` gained methods for inspecting an `Env` at runtime ([#880] by [@Zarenor])
4444
- `UpdateCtx::request_timer` and `UpdateCtx::request_anim_frame`. ([#898] by [@finnerale])
45+
- `LifeCycleCtx::request_timer`. ([#954] by [@xStrom])
4546
- `UpdateCtx::size` and `LifeCycleCtx::size`. ([#917] by [@jneem])
4647
- `WidgetExt::debug_widget_id`, for displaying widget ids on hover. ([#876] by [@cmyr])
4748
- `im` feature, with `Data` support for the [`im` crate](https://docs.rs/im/) collections. ([#924] by [@cmyr])
@@ -196,6 +197,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
196197
[#943]: https://github.com/xi-editor/druid/pull/943
197198
[#951]: https://github.com/xi-editor/druid/pull/951
198199
[#953]: https://github.com/xi-editor/druid/pull/953
200+
[#954]: https://github.com/xi-editor/druid/pull/954
199201

200202
## [0.5.0] - 2020-04-01
201203

druid/src/contexts.rs

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub struct LifeCycleCtx<'a> {
6262
pub(crate) command_queue: &'a mut CommandQueue,
6363
pub(crate) base_state: &'a mut BaseState,
6464
pub(crate) window_id: WindowId,
65+
pub(crate) window: &'a WindowHandle,
6566
}
6667

6768
/// A mutable context provided to data update methods of widgets.
@@ -91,6 +92,7 @@ pub struct LayoutCtx<'a, 'b: 'a> {
9192
pub(crate) base_state: &'a mut BaseState,
9293
pub(crate) text_factory: &'a mut Text<'b>,
9394
pub(crate) window_id: WindowId,
95+
pub(crate) window: &'a WindowHandle,
9496
pub(crate) mouse_pos: Option<Point>,
9597
}
9698

@@ -548,6 +550,17 @@ impl<'a> LifeCycleCtx<'a> {
548550
self.request_paint();
549551
}
550552

553+
/// Request a timer event.
554+
///
555+
/// The return value is a token, which can be used to associate the
556+
/// request with the event.
557+
pub fn request_timer(&mut self, deadline: Duration) -> TimerToken {
558+
self.base_state.request_timer = true;
559+
let timer_token = self.window.request_timer(deadline);
560+
self.base_state.add_timer(timer_token);
561+
timer_token
562+
}
563+
551564
/// The layout size.
552565
///
553566
/// This is the layout size as ultimately determined by the parent

druid/src/core.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::piet::{
2424
use crate::{
2525
BoxConstraints, Color, Command, Data, Env, Event, EventCtx, InternalEvent, InternalLifeCycle,
2626
LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Region, Target, TimerToken, UpdateCtx, Widget,
27-
WidgetId, WindowId,
27+
WidgetId, WindowHandle, WindowId,
2828
};
2929

3030
/// Our queue type
@@ -213,6 +213,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
213213
command_queue: ctx.command_queue,
214214
base_state: &mut self.state,
215215
window_id: ctx.window_id,
216+
window: ctx.window,
216217
};
217218
let size_event = LifeCycle::Size(new_size);
218219
self.inner.lifecycle(&mut child_ctx, &size_event, data, env);
@@ -224,6 +225,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
224225
ctx.command_queue,
225226
&mut self.state,
226227
ctx.window_id,
228+
ctx.window,
227229
layout_rect,
228230
ctx.mouse_pos,
229231
data,
@@ -334,6 +336,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
334336
command_queue: &mut CommandQueue,
335337
child_state: &mut BaseState,
336338
window_id: WindowId,
339+
window: &WindowHandle,
337340
rect: Rect,
338341
mouse_pos: Option<Point>,
339342
data: &T,
@@ -350,6 +353,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
350353
command_queue,
351354
base_state: child_state,
352355
window_id,
356+
window,
353357
};
354358
child.lifecycle(&mut child_ctx, &hot_changed_event, data, env);
355359
// if hot changes and we're showing widget ids, always repaint
@@ -515,6 +519,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
515519
command_queue: ctx.command_queue,
516520
base_state: &mut self.state,
517521
window_id: ctx.window_id,
522+
window: ctx.window,
518523
text_factory: ctx.text_factory,
519524
mouse_pos: child_mouse_pos,
520525
};
@@ -600,6 +605,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
600605
child_ctx.command_queue,
601606
child_ctx.base_state,
602607
child_ctx.window_id,
608+
child_ctx.window,
603609
rect,
604610
None,
605611
data,
@@ -647,6 +653,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
647653
child_ctx.command_queue,
648654
child_ctx.base_state,
649655
child_ctx.window_id,
656+
child_ctx.window,
650657
rect,
651658
Some(mouse_event.pos),
652659
data,
@@ -663,6 +670,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
663670
child_ctx.command_queue,
664671
child_ctx.base_state,
665672
child_ctx.window_id,
673+
child_ctx.window,
666674
rect,
667675
Some(mouse_event.pos),
668676
data,
@@ -679,6 +687,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
679687
child_ctx.command_queue,
680688
child_ctx.base_state,
681689
child_ctx.window_id,
690+
child_ctx.window,
682691
rect,
683692
Some(mouse_event.pos),
684693
data,
@@ -695,6 +704,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
695704
child_ctx.command_queue,
696705
child_ctx.base_state,
697706
child_ctx.window_id,
707+
child_ctx.window,
698708
rect,
699709
Some(mouse_event.pos),
700710
data,
@@ -839,6 +849,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
839849
command_queue: ctx.command_queue,
840850
base_state: &mut self.state,
841851
window_id: ctx.window_id,
852+
window: ctx.window,
842853
};
843854

844855
if recurse {
@@ -1005,6 +1016,7 @@ mod tests {
10051016
command_queue: &mut command_queue,
10061017
base_state: &mut state,
10071018
window_id: WindowId::next(),
1019+
window: &WindowHandle::default(),
10081020
};
10091021

10101022
let env = Env::default();

druid/src/window.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -250,24 +250,33 @@ impl<T: Data> Window<T> {
250250
env: &Env,
251251
process_commands: bool,
252252
) {
253-
let mut base_state = BaseState::new(self.root.id());
254-
let mut ctx = LifeCycleCtx {
255-
command_queue: queue,
256-
window_id: self.id,
257-
base_state: &mut base_state,
258-
};
259-
260253
if let LifeCycle::AnimFrame(_) = event {
261-
self.do_anim_frame(&mut ctx, data, env)
254+
self.do_anim_frame(queue, data, env)
262255
} else {
256+
let mut base_state = BaseState::new(self.root.id());
257+
let mut ctx = LifeCycleCtx {
258+
command_queue: queue,
259+
window_id: self.id,
260+
window: &self.handle,
261+
base_state: &mut base_state,
262+
};
263+
263264
self.root.lifecycle(&mut ctx, event, data, env);
264265
}
265266

266267
self.post_event_processing(queue, data, env, process_commands);
267268
}
268269

269270
/// AnimFrame has special logic, so we implement it separately.
270-
fn do_anim_frame(&mut self, ctx: &mut LifeCycleCtx, data: &T, env: &Env) {
271+
fn do_anim_frame(&mut self, queue: &mut CommandQueue, data: &T, env: &Env) {
272+
let mut base_state = BaseState::new(self.root.id());
273+
let mut ctx = LifeCycleCtx {
274+
command_queue: queue,
275+
window_id: self.id,
276+
window: &self.handle,
277+
base_state: &mut base_state,
278+
};
279+
271280
// TODO: this calculation uses wall-clock time of the paint call, which
272281
// potentially has jitter.
273282
//
@@ -277,7 +286,7 @@ impl<T: Data> Window<T> {
277286
let elapsed_ns = last.map(|t| now.duration_since(t).as_nanos()).unwrap_or(0) as u64;
278287

279288
let event = LifeCycle::AnimFrame(elapsed_ns);
280-
self.root.lifecycle(ctx, &event, data, env);
289+
self.root.lifecycle(&mut ctx, &event, data, env);
281290
if ctx.base_state.request_anim {
282291
self.last_anim = Some(now);
283292
}
@@ -340,6 +349,7 @@ impl<T: Data> Window<T> {
340349
base_state: &mut base_state,
341350
text_factory: piet.text(),
342351
window_id: self.id,
352+
window: &self.handle,
343353
mouse_pos: self.last_mouse_pos,
344354
};
345355
let bc = BoxConstraints::tight(self.size);

0 commit comments

Comments
 (0)