diff --git a/CHANGELOG.md b/CHANGELOG.md index fc23d9a..f69d0a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 +## Unreleased + +### Added + +- `AnimationRecorder::animate_keypress` is a new helper that animates a single + key press. + ## v0.3.0 (2024-05-12) ### Breaking Changes diff --git a/examples/plotters.rs b/examples/plotters.rs index 2c495fe..0fb6d35 100644 --- a/examples/plotters.rs +++ b/examples/plotters.rs @@ -1,8 +1,11 @@ +use std::time::Duration; + use cushy::value::{Dynamic, Source}; use cushy::widget::MakeWidget; use cushy::widgets::slider::Slidable; use cushy::widgets::Canvas; use cushy::Run; +use kludgine::app::winit::keyboard::{Key, NamedKey}; use plotters::prelude::*; // This is copied from the sierpinski.rs example in the plotters repository. @@ -30,9 +33,8 @@ where Ok(()) } -fn main() -> cushy::Result<()> { +fn plotters() -> impl MakeWidget { let depth = Dynamic::new(1); - "Depth" .and(depth.clone().slider_between(1, 5)) .and( @@ -45,5 +47,51 @@ fn main() -> cushy::Result<()> { .expand(), ) .into_rows() - .run() +} + +fn main() -> cushy::Result<()> { + plotters().run() +} + +#[test] +fn runs() { + cushy::example!(plotters).animated(|r| { + r.wait_for(Duration::from_millis(500)).unwrap(); + r.animate_keypress( + kludgine::app::winit::keyboard::PhysicalKey::Code( + kludgine::app::winit::keyboard::KeyCode::ArrowRight, + ), + Key::Named(NamedKey::ArrowRight), + None, + Duration::from_millis(250), + ) + .unwrap(); + r.animate_keypress( + kludgine::app::winit::keyboard::PhysicalKey::Code( + kludgine::app::winit::keyboard::KeyCode::ArrowRight, + ), + Key::Named(NamedKey::ArrowRight), + None, + Duration::from_millis(250), + ) + .unwrap(); + r.animate_keypress( + kludgine::app::winit::keyboard::PhysicalKey::Code( + kludgine::app::winit::keyboard::KeyCode::ArrowRight, + ), + Key::Named(NamedKey::ArrowRight), + None, + Duration::from_millis(250), + ) + .unwrap(); + r.animate_keypress( + kludgine::app::winit::keyboard::PhysicalKey::Code( + kludgine::app::winit::keyboard::KeyCode::ArrowRight, + ), + Key::Named(NamedKey::ArrowRight), + None, + Duration::from_secs(1), + ) + .unwrap(); + }); } diff --git a/src/window.rs b/src/window.rs index d289478..930a8eb 100644 --- a/src/window.rs +++ b/src/window.rs @@ -2581,6 +2581,7 @@ impl CushyWindowBuilder { #[must_use] pub fn finish_virtual(self, device: &wgpu::Device, queue: &wgpu::Queue) -> VirtualWindow { let mut state = VirtualState::new(); + state.size = self.initial_size; let mut cushy = self.finish(&mut state, device, queue); cushy.set_focused(true); @@ -3499,6 +3500,36 @@ where self.wait_for(over) } + /// Simulates a key down and key up event with the given information. + pub fn animate_keypress( + &mut self, + physical_key: PhysicalKey, + logical_key: Key, + text: Option<&str>, + duration: Duration, + ) -> Result<(), VirtualRecorderError> { + let text = text.map(SmolStr::new); + let half_duration = duration / 2; + let mut event = KeyEvent { + physical_key, + logical_key, + text, + state: ElementState::Pressed, + repeat: false, + location: KeyLocation::Standard, + }; + self.recorder + .window + .keyboard_input(DeviceId::Virtual(0), event.clone(), true); + self.wait_for(half_duration)?; + event.state = ElementState::Released; + self.recorder + .window + .keyboard_input(DeviceId::Virtual(0), event.clone(), true); + + self.wait_for(half_duration) + } + /// Animates entering the graphemes from `text` over `duration`. pub fn animate_text_input( &mut self,