Skip to content

Commit

Permalink
Add the request_timer method to LifeCycleCtx.
Browse files Browse the repository at this point in the history
  • Loading branch information
xStrom committed May 17, 2020
1 parent a480919 commit fe2f89e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
- `MouseButtons` to `MouseEvent` to track which buttons are being held down during an event. ([#843] by [@xStrom])
- `Env` and `Key` gained methods for inspecting an `Env` at runtime ([#880] by [@Zarenor])
- `UpdateCtx::request_timer` and `UpdateCtx::request_anim_frame`. ([#898] by [@finnerale])
- `LifeCycleCtx::request_timer`. ([#954] by [@xStrom])
- `UpdateCtx::size` and `LifeCycleCtx::size`. ([#917] by [@jneem])
- `WidgetExt::debug_widget_id`, for displaying widget ids on hover. ([#876] by [@cmyr])
- `im` feature, with `Data` support for the [`im` crate](https://docs.rs/im/) collections. ([#924] by [@cmyr])
Expand Down Expand Up @@ -192,6 +193,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
[#940]: https://github.com/xi-editor/druid/pull/940
[#942]: https://github.com/xi-editor/druid/pull/942
[#943]: https://github.com/xi-editor/druid/pull/943
[#954]: https://github.com/xi-editor/druid/pull/954

## [0.5.0] - 2020-04-01

Expand Down
13 changes: 13 additions & 0 deletions druid/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct LifeCycleCtx<'a> {
pub(crate) command_queue: &'a mut CommandQueue,
pub(crate) base_state: &'a mut BaseState,
pub(crate) window_id: WindowId,
pub(crate) window: &'a WindowHandle,
}

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

Expand Down Expand Up @@ -548,6 +550,17 @@ impl<'a> LifeCycleCtx<'a> {
self.request_paint();
}

/// 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: Duration) -> TimerToken {
self.base_state.request_timer = true;
let timer_token = self.window.request_timer(deadline);
self.base_state.add_timer(timer_token);
timer_token
}

/// The layout size.
///
/// This is the layout size as ultimately determined by the parent
Expand Down
12 changes: 11 additions & 1 deletion druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::piet::{
use crate::{
BoxConstraints, Color, Command, Data, Env, Event, EventCtx, InternalEvent, InternalLifeCycle,
LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Region, Target, TimerToken, UpdateCtx, Widget,
WidgetId, WindowId,
WidgetId, WindowHandle, WindowId,
};

/// Our queue type
Expand Down Expand Up @@ -200,6 +200,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
ctx.command_queue,
&mut self.state,
ctx.window_id,
ctx.window,
layout_rect,
ctx.mouse_pos,
data,
Expand Down Expand Up @@ -303,6 +304,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
command_queue: &mut CommandQueue,
child_state: &mut BaseState,
window_id: WindowId,
window: &WindowHandle,
rect: Rect,
mouse_pos: Option<Point>,
data: &T,
Expand All @@ -319,6 +321,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
command_queue,
base_state: child_state,
window_id,
window,
};
child.lifecycle(&mut child_ctx, &hot_changed_event, data, env);
// if hot changes and we're showing widget ids, always repaint
Expand Down Expand Up @@ -484,6 +487,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
command_queue: ctx.command_queue,
base_state: &mut self.state,
window_id: ctx.window_id,
window: ctx.window,
text_factory: ctx.text_factory,
mouse_pos: child_mouse_pos,
};
Expand Down Expand Up @@ -569,6 +573,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
child_ctx.command_queue,
child_ctx.base_state,
child_ctx.window_id,
child_ctx.window,
rect,
None,
data,
Expand Down Expand Up @@ -616,6 +621,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
child_ctx.command_queue,
child_ctx.base_state,
child_ctx.window_id,
child_ctx.window,
rect,
Some(mouse_event.pos),
data,
Expand All @@ -632,6 +638,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
child_ctx.command_queue,
child_ctx.base_state,
child_ctx.window_id,
child_ctx.window,
rect,
Some(mouse_event.pos),
data,
Expand All @@ -648,6 +655,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
child_ctx.command_queue,
child_ctx.base_state,
child_ctx.window_id,
child_ctx.window,
rect,
Some(mouse_event.pos),
data,
Expand Down Expand Up @@ -793,6 +801,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
command_queue: ctx.command_queue,
base_state: &mut self.state,
window_id: ctx.window_id,
window: ctx.window,
};

if recurse {
Expand Down Expand Up @@ -959,6 +968,7 @@ mod tests {
command_queue: &mut command_queue,
base_state: &mut state,
window_id: WindowId::next(),
window: &WindowHandle::default(),
};

let env = Env::default();
Expand Down
30 changes: 20 additions & 10 deletions druid/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,33 @@ impl<T: Data> Window<T> {
env: &Env,
process_commands: bool,
) {
let mut base_state = BaseState::new(self.root.id());
let mut ctx = LifeCycleCtx {
command_queue: queue,
window_id: self.id,
base_state: &mut base_state,
};

if let LifeCycle::AnimFrame(_) = event {
self.do_anim_frame(&mut ctx, data, env)
self.do_anim_frame(queue, data, env)
} else {
let mut base_state = BaseState::new(self.root.id());
let mut ctx = LifeCycleCtx {
command_queue: queue,
window_id: self.id,
window: &self.handle,
base_state: &mut base_state,
};

self.root.lifecycle(&mut ctx, event, data, env);
}

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

/// AnimFrame has special logic, so we implement it separately.
fn do_anim_frame(&mut self, ctx: &mut LifeCycleCtx, data: &T, env: &Env) {
fn do_anim_frame(&mut self, queue: &mut CommandQueue, data: &T, env: &Env) {
let mut base_state = BaseState::new(self.root.id());
let mut ctx = LifeCycleCtx {
command_queue: queue,
window_id: self.id,
window: &self.handle,
base_state: &mut base_state,
};

// TODO: this calculation uses wall-clock time of the paint call, which
// potentially has jitter.
//
Expand All @@ -277,7 +286,7 @@ impl<T: Data> Window<T> {
let elapsed_ns = last.map(|t| now.duration_since(t).as_nanos()).unwrap_or(0) as u64;

let event = LifeCycle::AnimFrame(elapsed_ns);
self.root.lifecycle(ctx, &event, data, env);
self.root.lifecycle(&mut ctx, &event, data, env);
if ctx.base_state.request_anim {
self.last_anim = Some(now);
}
Expand Down Expand Up @@ -340,6 +349,7 @@ impl<T: Data> Window<T> {
base_state: &mut base_state,
text_factory: piet.text(),
window_id: self.id,
window: &self.handle,
mouse_pos: self.last_mouse_pos,
};
let bc = BoxConstraints::tight(self.size);
Expand Down

0 comments on commit fe2f89e

Please sign in to comment.