Skip to content

Commit 3846a83

Browse files
authored
Add UpdateCtx::request_timer and request_anim_frame. (#898)
1 parent 2b10ce7 commit 3846a83

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ While some features like the clipboard, menus or file dialogs are not yet availa
2929
- `Label::with_font` and `set_font`. ([#785] by [@thecodewarrior])
3030
- `InternalEvent::RouteTimer` to route timer events. ([#831] by [@sjoshid])
3131
- `MouseButtons` to `MouseEvent` to track which buttons are being held down during an event. ([#843] by [@xStrom])
32+
- `UpdateCtx::request_timer` and `UpdateCtx::request_anim_frame`. ([#898] by [@finnerale])
3233

3334
### Changed
3435

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

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

7678
### Docs
7779

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

143146
## [0.5.0] - 2020-04-01
144147

druid/examples/switch.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
use druid::widget::{
16-
Flex, Label, MainAxisAlignment, Padding, Parse, Stepper, Switch, TextBox, WidgetExt,
16+
Checkbox, Flex, Label, MainAxisAlignment, Padding, Parse, Stepper, Switch, TextBox, WidgetExt,
1717
};
1818
use druid::{AppLauncher, Data, Lens, LensExt, LensWrap, LocalizedString, Widget, WindowDesc};
1919

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

3233
row.add_child(Padding::new(5.0, switch_label));
3334
row.add_child(Padding::new(5.0, switch));
35+
row.add_child(Padding::new(5.0, check_box));
3436

3537
let stepper = LensWrap::new(
3638
Stepper::new()

druid/src/contexts.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,23 @@ impl<'a> UpdateCtx<'a> {
540540
self.request_layout();
541541
}
542542

543+
/// Request an animation frame.
544+
pub fn request_anim_frame(&mut self) {
545+
self.base_state.request_anim = true;
546+
self.request_paint();
547+
}
548+
549+
/// Request a timer event.
550+
///
551+
/// The return value is a token, which can be used to associate the
552+
/// request with the event.
553+
pub fn request_timer(&mut self, deadline: Duration) -> TimerToken {
554+
self.base_state.request_timer = true;
555+
let timer_token = self.window.request_timer(deadline);
556+
self.base_state.add_timer(timer_token);
557+
timer_token
558+
}
559+
543560
/// Submit a [`Command`] to be run after layout and paint finish.
544561
///
545562
/// **Note:**

druid/src/widget/switch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ impl Widget<bool> for Switch {
137137

138138
ctx.set_active(false);
139139

140-
ctx.request_paint();
141140
self.knob_dragged = false;
142141
self.animation_in_progress = true;
143142
ctx.request_anim_frame();
@@ -180,7 +179,8 @@ impl Widget<bool> for Switch {
180179

181180
fn update(&mut self, ctx: &mut UpdateCtx, old_data: &bool, data: &bool, _env: &Env) {
182181
if old_data != data {
183-
ctx.request_paint();
182+
self.animation_in_progress = true;
183+
ctx.request_anim_frame();
184184
}
185185
}
186186

0 commit comments

Comments
 (0)