Remove the component parameter from GenericWindow::process_key_input

This commit is contained in:
Simon Hausmann 2020-11-11 19:16:26 +01:00
parent 516680ad5d
commit a2dadf8fe8
6 changed files with 36 additions and 65 deletions

View file

@ -20,16 +20,15 @@ template<typename Component>
inline void send_mouse_click(const Component &component, float x, float y) inline void send_mouse_click(const Component &component, float x, float y)
{ {
cbindgen_private::sixtyfps_send_mouse_click( cbindgen_private::sixtyfps_send_mouse_click(
{ &Component::component_type, const_cast<Component *>(&component) }, { &Component::component_type, const_cast<Component *>(&component) }, x, y,
x, y, &component.window); &component.window);
} }
template<typename Component> template<typename Component>
inline void send_keyboard_string_sequence(const Component &component, const sixtyfps::SharedString &str) inline void send_keyboard_string_sequence(const Component &component,
const sixtyfps::SharedString &str)
{ {
cbindgen_private::send_keyboard_string_sequence( cbindgen_private::send_keyboard_string_sequence(&str, &component.window);
{ &Component::component_type, const_cast<Component *>(&component) },
&str, &component.window);
} }
#define assert_eq(A, B) \ #define assert_eq(A, B) \

View file

@ -454,7 +454,7 @@ declare_types! {
let comp = this.borrow(&lock).0.clone(); let comp = this.borrow(&lock).0.clone();
let component = comp.ok_or(()).or_else(|()| cx.throw_error("Invalid type"))?; let component = comp.ok_or(()).or_else(|()| cx.throw_error("Invalid type"))?;
run_scoped(&mut cx,this.downcast().unwrap(), || { run_scoped(&mut cx,this.downcast().unwrap(), || {
sixtyfps_corelib::tests::send_keyboard_string_sequence(component.borrow(), &sequence.into(), &component.window()); sixtyfps_corelib::tests::send_keyboard_string_sequence(&sequence.into(), &component.window());
Ok(()) Ok(())
})?; })?;
Ok(JsUndefined::new().as_value(&mut cx)) Ok(JsUndefined::new().as_value(&mut cx))

View file

@ -335,7 +335,6 @@ pub mod testing {
key_codes: &[crate::re_exports::KeyCode], key_codes: &[crate::re_exports::KeyCode],
) { ) {
sixtyfps_corelib::tests::sixtyfps_send_key_clicks( sixtyfps_corelib::tests::sixtyfps_send_key_clicks(
vtable::VRef::new_pin(component),
&crate::re_exports::Slice::from_slice(key_codes), &crate::re_exports::Slice::from_slice(key_codes),
component.component_window(), component.component_window(),
) )
@ -349,7 +348,6 @@ pub mod testing {
sequence: &str, sequence: &str,
) { ) {
sixtyfps_corelib::tests::send_keyboard_string_sequence( sixtyfps_corelib::tests::send_keyboard_string_sequence(
vtable::VRef::new_pin(component),
&super::SharedString::from(sequence), &super::SharedString::from(sequence),
component.component_window(), component.component_window(),
) )

View file

@ -59,11 +59,7 @@ pub trait GenericWindow {
/// Arguments: /// Arguments:
/// * `event`: The key event received by the windowing system. /// * `event`: The key event received by the windowing system.
/// * `component`: The SixtyFPS compiled component that provides the tree of items. /// * `component`: The SixtyFPS compiled component that provides the tree of items.
fn process_key_input( fn process_key_input(self: Rc<Self>, event: &KeyEvent);
self: Rc<Self>,
event: &KeyEvent,
component: core::pin::Pin<crate::component::ComponentRef>,
);
/// Calls the `callback` function with the underlying winit::Window that this /// Calls the `callback` function with the underlying winit::Window that this
/// GenericWindow backs. /// GenericWindow backs.
fn with_platform_window(&self, callback: &dyn Fn(&winit::window::Window)); fn with_platform_window(&self, callback: &dyn Fn(&winit::window::Window));
@ -190,12 +186,8 @@ impl ComponentWindow {
self.0.clone().current_keyboard_modifiers() self.0.clone().current_keyboard_modifiers()
} }
pub(crate) fn process_key_input( pub(crate) fn process_key_input(&self, event: &KeyEvent) {
&self, self.0.clone().process_key_input(event)
event: &KeyEvent,
component: core::pin::Pin<crate::component::ComponentRef>,
) {
self.0.clone().process_key_input(event, component)
} }
/// Clears the focus on any previously focused item and makes the provided /// Clears the focus on any previously focused item and makes the provided
@ -434,7 +426,7 @@ impl EventLoop {
if let Some(ref key_event) = if let Some(ref key_event) =
(input, window.current_keyboard_modifiers()).try_into().ok() (input, window.current_keyboard_modifiers()).try_into().ok()
{ {
window.clone().process_key_input(key_event, component); window.clone().process_key_input(key_event);
// FIXME: remove this, it should be based on actual changes rather than this // FIXME: remove this, it should be based on actual changes rather than this
window.request_redraw(); window.request_redraw();
} }
@ -458,7 +450,7 @@ impl EventLoop {
unicode_scalar: ch.into(), unicode_scalar: ch.into(),
modifiers, modifiers,
}; };
window.clone().process_key_input(&key_event, component); window.clone().process_key_input(&key_event);
// FIXME: remove this, it should be based on actual changes rather than this // FIXME: remove this, it should be based on actual changes rather than this
window.request_redraw(); window.request_redraw();
} }

View file

@ -631,12 +631,11 @@ impl<Backend: GraphicsBackend> crate::eventloop::GenericWindow for GraphicsWindo
); );
} }
fn process_key_input( fn process_key_input(self: Rc<Self>, event: &KeyEvent) {
self: Rc<Self>, let component = self.component.borrow().upgrade().unwrap();
event: &KeyEvent, ComponentRc::borrow_pin(&component)
component: core::pin::Pin<crate::component::ComponentRef>, .as_ref()
) { .key_event(event, &crate::eventloop::ComponentWindow::new(self.clone()));
component.as_ref().key_event(event, &crate::eventloop::ComponentWindow::new(self.clone()));
} }
fn with_platform_window(&self, callback: &dyn Fn(&winit::window::Window)) { fn with_platform_window(&self, callback: &dyn Fn(&winit::window::Window)) {

View file

@ -65,32 +65,24 @@ pub extern "C" fn sixtyfps_set_keyboard_modifiers(
/// Simulate a key down event. /// Simulate a key down event.
#[no_mangle] #[no_mangle]
pub extern "C" fn sixtyfps_send_key_clicks( pub extern "C" fn sixtyfps_send_key_clicks(
component: core::pin::Pin<crate::component::ComponentRef>,
key_codes: &crate::slice::Slice<crate::input::KeyCode>, key_codes: &crate::slice::Slice<crate::input::KeyCode>,
window: &crate::eventloop::ComponentWindow, window: &crate::eventloop::ComponentWindow,
) { ) {
for key_code in key_codes.iter() { for key_code in key_codes.iter() {
window.process_key_input( window.process_key_input(&crate::input::KeyEvent::KeyPressed {
&crate::input::KeyEvent::KeyPressed { code: *key_code,
code: *key_code, modifiers: window.current_keyboard_modifiers(),
modifiers: window.current_keyboard_modifiers(), });
}, window.process_key_input(&crate::input::KeyEvent::KeyReleased {
component, code: *key_code,
); modifiers: window.current_keyboard_modifiers(),
window.process_key_input( });
&crate::input::KeyEvent::KeyReleased {
code: *key_code,
modifiers: window.current_keyboard_modifiers(),
},
component,
);
} }
} }
/// Simulate a character input event. /// Simulate a character input event.
#[no_mangle] #[no_mangle]
pub extern "C" fn send_keyboard_string_sequence( pub extern "C" fn send_keyboard_string_sequence(
component: core::pin::Pin<crate::component::ComponentRef>,
sequence: &crate::SharedString, sequence: &crate::SharedString,
window: &crate::eventloop::ComponentWindow, window: &crate::eventloop::ComponentWindow,
) { ) {
@ -98,25 +90,19 @@ pub extern "C" fn send_keyboard_string_sequence(
let key_down = |maybe_code: &Option<crate::input::KeyCode>| { let key_down = |maybe_code: &Option<crate::input::KeyCode>| {
maybe_code.clone().map(|code| { maybe_code.clone().map(|code| {
window.process_key_input( window.process_key_input(&crate::input::KeyEvent::KeyPressed {
&crate::input::KeyEvent::KeyPressed { code: code,
code: code, modifiers: window.current_keyboard_modifiers(),
modifiers: window.current_keyboard_modifiers(), });
},
component,
);
}); });
}; };
let key_up = |maybe_code: &Option<crate::input::KeyCode>| { let key_up = |maybe_code: &Option<crate::input::KeyCode>| {
maybe_code.clone().map(|code| { maybe_code.clone().map(|code| {
window.process_key_input( window.process_key_input(&crate::input::KeyEvent::KeyReleased {
&crate::input::KeyEvent::KeyReleased { code: code,
code: code, modifiers: window.current_keyboard_modifiers(),
modifiers: window.current_keyboard_modifiers(), });
},
component,
);
}); });
}; };
@ -131,13 +117,10 @@ pub extern "C" fn send_keyboard_string_sequence(
key_down(&maybe_key_code); key_down(&maybe_key_code);
window.process_key_input( window.process_key_input(&crate::input::KeyEvent::CharacterInput {
&crate::input::KeyEvent::CharacterInput { unicode_scalar: ch.into(),
unicode_scalar: ch.into(), modifiers: window.current_keyboard_modifiers(),
modifiers: window.current_keyboard_modifiers(), });
},
component,
);
key_up(&maybe_key_code); key_up(&maybe_key_code);