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 UpdateCtx::request_timer and request_anim_frame. #898

Merged
merged 3 commits into from
May 3, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ While some features like the clipboard, menus or file dialogs are not yet availa
- `Label::with_font` and `set_font`. ([#785] by [@thecodewarrior])
- `InternalEvent::RouteTimer` to route timer events. ([#831] by [@sjoshid])
- `MouseButtons` to `MouseEvent` to track which buttons are being held down during an event. ([#843] by [@xStrom])
- `UpdateCtx::request_timer` and `UpdateCtx::request_anim_frame`. ([#898] by [@finnerale])

### Changed

Expand Down Expand Up @@ -72,6 +73,7 @@ While some features like the clipboard, menus or file dialogs are not yet availa

- Improved `Split` accuracy. ([#738] by [@xStrom])
- Built-in widgets no longer stroke outside their `paint_rect`. ([#861] by [@jneem])
- `Switch` toggles with animation when its data changes externally. ([#898] by [@finnerale])

### Docs

Expand Down Expand Up @@ -139,6 +141,7 @@ While some features like the clipboard, menus or file dialogs are not yet availa
[#878]: https://github.com/xi-editor/druid/pull/878
[#889]: https://github.com/xi-editor/druid/pull/899
[#894]: https://github.com/xi-editor/druid/pull/894
[#898]: https://github.com/xi-editor/druid/pull/898

## [0.5.0] - 2020-04-01

Expand Down
4 changes: 3 additions & 1 deletion druid/examples/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use druid::widget::{
Flex, Label, MainAxisAlignment, Padding, Parse, Stepper, Switch, TextBox, WidgetExt,
Checkbox, Flex, Label, MainAxisAlignment, Padding, Parse, Stepper, Switch, TextBox, WidgetExt,
};
use druid::{AppLauncher, Data, Lens, LensExt, LensWrap, LocalizedString, Widget, WindowDesc};

Expand All @@ -27,10 +27,12 @@ fn build_widget() -> impl Widget<DemoState> {
let mut col = Flex::column();
let mut row = Flex::row();
let switch = LensWrap::new(Switch::new(), DemoState::value);
let check_box = LensWrap::new(Checkbox::new(""), DemoState::value);
let switch_label = Label::new("Setting label");

row.add_child(Padding::new(5.0, switch_label));
row.add_child(Padding::new(5.0, switch));
row.add_child(Padding::new(5.0, check_box));

let stepper = LensWrap::new(
Stepper::new()
Expand Down
17 changes: 17 additions & 0 deletions druid/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,23 @@ impl<'a> UpdateCtx<'a> {
self.request_layout();
}

/// Request an animation frame.
pub fn request_anim_frame(&mut self) {
self.base_state.request_anim = true;
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
}

/// Submit a [`Command`] to be run after layout and paint finish.
///
/// **Note:**
Expand Down
4 changes: 2 additions & 2 deletions druid/src/widget/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ impl Widget<bool> for Switch {

ctx.set_active(false);

ctx.request_paint();
self.knob_dragged = false;
self.animation_in_progress = true;
ctx.request_anim_frame();
Expand Down Expand Up @@ -180,7 +179,8 @@ impl Widget<bool> for Switch {

fn update(&mut self, ctx: &mut UpdateCtx, old_data: &bool, data: &bool, _env: &Env) {
if old_data != data {
ctx.request_paint();
self.animation_in_progress = true;
ctx.request_anim_frame();
}
}

Expand Down