Skip to content
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

Add the request_timer method to LifeCycleCtx. #954

Merged
merged 2 commits into from
May 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -196,6 +197,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
[#943]: https://github.com/xi-editor/druid/pull/943
[#951]: https://github.com/xi-editor/druid/pull/951
[#953]: https://github.com/xi-editor/druid/pull/953
[#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
14 changes: 13 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 @@ -213,6 +213,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
command_queue: ctx.command_queue,
base_state: &mut self.state,
window_id: ctx.window_id,
window: ctx.window,
};
let size_event = LifeCycle::Size(new_size);
self.inner.lifecycle(&mut child_ctx, &size_event, data, env);
Expand All @@ -224,6 +225,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 @@ -334,6 +336,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 @@ -350,6 +353,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 @@ -515,6 +519,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 @@ -600,6 +605,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 @@ -647,6 +653,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 @@ -663,6 +670,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 @@ -679,6 +687,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 @@ -695,6 +704,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 @@ -839,6 +849,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 @@ -1005,6 +1016,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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I think I'm missing the reason we can't reuse the context here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the borrow checker. LifeCycleCtx holds an immutable reference to self while do_anim_frame wants a mutable reference to self.

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