Make recorder widget produce Interaction
This commit is contained in:
parent
2532099efc
commit
8ca25d627f
3 changed files with 45 additions and 53 deletions
|
|
@ -8,7 +8,7 @@ use crate::core::Length::Fill;
|
||||||
use crate::core::alignment::Horizontal::Right;
|
use crate::core::alignment::Horizontal::Right;
|
||||||
use crate::core::border;
|
use crate::core::border;
|
||||||
use crate::core::window;
|
use crate::core::window;
|
||||||
use crate::core::{Element, Event, Font, Size, Theme};
|
use crate::core::{Element, Font, Size, Theme};
|
||||||
use crate::executor;
|
use crate::executor;
|
||||||
use crate::futures::futures::channel::mpsc;
|
use crate::futures::futures::channel::mpsc;
|
||||||
use crate::icon;
|
use crate::icon;
|
||||||
|
|
@ -75,7 +75,7 @@ pub enum Message {
|
||||||
pub enum Tick<P: Program> {
|
pub enum Tick<P: Program> {
|
||||||
Tester(Message),
|
Tester(Message),
|
||||||
Program(P::Message),
|
Program(P::Message),
|
||||||
Recorder(Event),
|
Recorder(instruction::Interaction),
|
||||||
Emulator(emulator::Event<P>),
|
Emulator(emulator::Event<P>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -316,9 +316,8 @@ impl<P: Program + 'static> Tester<P> {
|
||||||
|
|
||||||
Task::none()
|
Task::none()
|
||||||
}
|
}
|
||||||
Tick::Recorder(event) => {
|
Tick::Recorder(interaction) => {
|
||||||
let mut interaction =
|
let mut interaction = Some(interaction);
|
||||||
instruction::Interaction::from_event(event);
|
|
||||||
|
|
||||||
while let Some(new_interaction) = interaction.take() {
|
while let Some(new_interaction) = interaction.take() {
|
||||||
if let Some(Instruction::Interact(last_interaction)) =
|
if let Some(Instruction::Interact(last_interaction)) =
|
||||||
|
|
@ -441,7 +440,7 @@ impl<P: Program + 'static> Tester<P> {
|
||||||
|
|
||||||
Element::from(
|
Element::from(
|
||||||
recorder(themer(theme, view))
|
recorder(themer(theme, view))
|
||||||
.on_event(Tick::Recorder),
|
.on_record(Tick::Recorder),
|
||||||
)
|
)
|
||||||
.map(emulate)
|
.map(emulate)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use crate::core::{
|
||||||
self, Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell,
|
self, Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell,
|
||||||
Size, Vector, Widget,
|
Size, Vector, Widget,
|
||||||
};
|
};
|
||||||
|
use crate::test::instruction::Interaction;
|
||||||
|
|
||||||
pub fn recorder<'a, Message, Theme, Renderer>(
|
pub fn recorder<'a, Message, Theme, Renderer>(
|
||||||
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
|
|
@ -18,7 +19,7 @@ pub fn recorder<'a, Message, Theme, Renderer>(
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Recorder<'a, Message, Theme, Renderer> {
|
pub struct Recorder<'a, Message, Theme, Renderer> {
|
||||||
content: Element<'a, Message, Theme, Renderer>,
|
content: Element<'a, Message, Theme, Renderer>,
|
||||||
on_event: Option<Box<dyn Fn(Event) -> Message + 'a>>,
|
on_record: Option<Box<dyn Fn(Interaction) -> Message + 'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> Recorder<'a, Message, Theme, Renderer> {
|
impl<'a, Message, Theme, Renderer> Recorder<'a, Message, Theme, Renderer> {
|
||||||
|
|
@ -27,15 +28,15 @@ impl<'a, Message, Theme, Renderer> Recorder<'a, Message, Theme, Renderer> {
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
on_event: None,
|
on_record: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_event(
|
pub fn on_record(
|
||||||
mut self,
|
mut self,
|
||||||
on_event: impl Fn(Event) -> Message + 'a,
|
on_record: impl Fn(Interaction) -> Message + 'a,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.on_event = Some(Box::new(on_event));
|
self.on_record = Some(Box::new(on_record));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -64,8 +65,8 @@ where
|
||||||
state, event, layout, cursor, renderer, clipboard, shell, viewport,
|
state, event, layout, cursor, renderer, clipboard, shell, viewport,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(on_event) = &self.on_event {
|
if let Some(on_record) = &self.on_record {
|
||||||
record(event, cursor, shell, layout.bounds(), on_event);
|
record(event, cursor, shell, layout.bounds(), on_record);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,7 +158,7 @@ where
|
||||||
overlay::Element::new(Box::new(Overlay {
|
overlay::Element::new(Box::new(Overlay {
|
||||||
raw,
|
raw,
|
||||||
bounds: layout.bounds(),
|
bounds: layout.bounds(),
|
||||||
on_event: self.on_event.as_deref(),
|
on_record: self.on_record.as_deref(),
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -178,7 +179,7 @@ where
|
||||||
struct Overlay<'a, Message, Theme, Renderer> {
|
struct Overlay<'a, Message, Theme, Renderer> {
|
||||||
raw: overlay::Element<'a, Message, Theme, Renderer>,
|
raw: overlay::Element<'a, Message, Theme, Renderer>,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
on_event: Option<&'a dyn Fn(Event) -> Message>,
|
on_record: Option<&'a dyn Fn(Interaction) -> Message>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> core::Overlay<Message, Theme, Renderer>
|
impl<'a, Message, Theme, Renderer> core::Overlay<Message, Theme, Renderer>
|
||||||
|
|
@ -233,7 +234,7 @@ where
|
||||||
.as_overlay_mut()
|
.as_overlay_mut()
|
||||||
.update(event, layout, cursor, renderer, clipboard, shell);
|
.update(event, layout, cursor, renderer, clipboard, shell);
|
||||||
|
|
||||||
if let Some(on_event) = &self.on_event {
|
if let Some(on_event) = &self.on_record {
|
||||||
record(event, cursor, shell, self.bounds, on_event);
|
record(event, cursor, shell, self.bounds, on_event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -261,7 +262,7 @@ where
|
||||||
overlay::Element::new(Box::new(Overlay {
|
overlay::Element::new(Box::new(Overlay {
|
||||||
raw,
|
raw,
|
||||||
bounds: self.bounds,
|
bounds: self.bounds,
|
||||||
on_event: self.on_event,
|
on_record: self.on_record,
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -276,34 +277,24 @@ fn record<Message>(
|
||||||
cursor: mouse::Cursor,
|
cursor: mouse::Cursor,
|
||||||
shell: &mut Shell<'_, Message>,
|
shell: &mut Shell<'_, Message>,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
on_event: impl Fn(Event) -> Message,
|
on_record: impl Fn(Interaction) -> Message,
|
||||||
) {
|
) {
|
||||||
match event {
|
if let Event::Mouse(_) = event
|
||||||
Event::Mouse(event) => {
|
&& !cursor.is_over(bounds)
|
||||||
if !cursor.is_over(bounds) {
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
match event {
|
let interaction =
|
||||||
mouse::Event::ButtonPressed(_)
|
if let Event::Mouse(mouse::Event::CursorMoved { position }) = event {
|
||||||
| mouse::Event::ButtonReleased(_)
|
Interaction::from_event(&Event::Mouse(mouse::Event::CursorMoved {
|
||||||
| mouse::Event::WheelScrolled { .. } => {
|
position: *position - (bounds.position() - Point::ORIGIN),
|
||||||
shell.publish(on_event(Event::Mouse(*event)));
|
}))
|
||||||
}
|
} else {
|
||||||
mouse::Event::CursorMoved { position } => {
|
Interaction::from_event(event)
|
||||||
shell.publish(on_event(Event::Mouse(
|
};
|
||||||
mouse::Event::CursorMoved {
|
|
||||||
position: *position
|
if let Some(interaction) = interaction {
|
||||||
- (bounds.position() - Point::ORIGIN),
|
shell.publish(on_record(interaction));
|
||||||
},
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Event::Keyboard(event) => {
|
|
||||||
shell.publish(on_event(Event::Keyboard(event.clone())));
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,18 +33,20 @@ pub enum Interaction {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Interaction {
|
impl Interaction {
|
||||||
pub fn from_event(event: Event) -> Option<Self> {
|
pub fn from_event(event: &Event) -> Option<Self> {
|
||||||
Some(match event {
|
Some(match event {
|
||||||
Event::Mouse(mouse) => Self::Mouse(match mouse {
|
Event::Mouse(mouse) => Self::Mouse(match mouse {
|
||||||
mouse::Event::CursorMoved { position } => {
|
mouse::Event::CursorMoved { position } => {
|
||||||
Mouse::Move(Target::Point(position))
|
Mouse::Move(Target::Point(*position))
|
||||||
}
|
|
||||||
mouse::Event::ButtonPressed(button) => {
|
|
||||||
Mouse::Press { button, at: None }
|
|
||||||
}
|
|
||||||
mouse::Event::ButtonReleased(button) => {
|
|
||||||
Mouse::Release { button, at: None }
|
|
||||||
}
|
}
|
||||||
|
mouse::Event::ButtonPressed(button) => Mouse::Press {
|
||||||
|
button: *button,
|
||||||
|
at: None,
|
||||||
|
},
|
||||||
|
mouse::Event::ButtonReleased(button) => Mouse::Release {
|
||||||
|
button: *button,
|
||||||
|
at: None,
|
||||||
|
},
|
||||||
_ => None?,
|
_ => None?,
|
||||||
}),
|
}),
|
||||||
Event::Keyboard(keyboard) => Self::Keyboard(match keyboard {
|
Event::Keyboard(keyboard) => Self::Keyboard(match keyboard {
|
||||||
|
|
@ -61,7 +63,7 @@ impl Interaction {
|
||||||
keyboard::Key::Named(keyboard::key::Named::Backspace) => {
|
keyboard::Key::Named(keyboard::key::Named::Backspace) => {
|
||||||
Keyboard::Press(Key::Backspace)
|
Keyboard::Press(Key::Backspace)
|
||||||
}
|
}
|
||||||
_ => Keyboard::Typewrite(text?.to_string()),
|
_ => Keyboard::Typewrite(text.as_ref()?.to_string()),
|
||||||
},
|
},
|
||||||
keyboard::Event::KeyReleased { key, .. } => match key {
|
keyboard::Event::KeyReleased { key, .. } => match key {
|
||||||
keyboard::Key::Named(keyboard::key::Named::Enter) => {
|
keyboard::Key::Named(keyboard::key::Named::Enter) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue