Trim the Rust ComponentWindow API and implementation a little bit

* Remove the `new` function from the main impl and use the slightly
  less visible From conversion trait
* Make the inner Rc<Window> pub(crate) instead of pub
* Instead, provide a public as_any() accessor that the Qt backend can use
This commit is contained in:
Simon Hausmann 2021-07-20 15:36:27 +02:00 committed by Simon Hausmann
parent 492af0f67c
commit 2553dd1459
6 changed files with 27 additions and 25 deletions

View file

@ -197,7 +197,7 @@ impl Window {
self.mouse_input_state.set(crate::input::process_mouse_input(
component,
event,
&ComponentWindow::new(self.clone()),
&self.clone().into(),
self.mouse_input_state.take(),
));
}
@ -210,8 +210,7 @@ impl Window {
pub fn process_key_input(self: Rc<Self>, event: &KeyEvent) {
let mut item = self.focus_item.borrow().clone();
while let Some(focus_item) = item.upgrade() {
let window = &ComponentWindow::new(self.clone());
if focus_item.borrow().as_ref().key_event(event, window)
if focus_item.borrow().as_ref().key_event(event, &self.clone().into())
== crate::input::KeyEventResult::EventAccepted
{
return;
@ -237,7 +236,7 @@ impl Window {
/// Sets the focus to the item pointed to by item_ptr. This will remove the focus from any
/// currently focused item.
pub fn set_focus_item(self: Rc<Self>, focus_item: &ItemRc) {
let window = ComponentWindow::new(self.clone());
let window = &self.clone().into();
if let Some(old_focus_item) = self.as_ref().focus_item.borrow().upgrade() {
old_focus_item
@ -254,7 +253,7 @@ impl Window {
/// Sets the focus on the window to true or false, depending on the have_focus argument.
/// This results in WindowFocusReceived and WindowFocusLost events.
pub fn set_focus(self: Rc<Self>, have_focus: bool) {
let window = ComponentWindow::new(self.clone());
let window = &self.clone().into();
let event = if have_focus {
crate::input::FocusEvent::WindowReceivedFocus
} else {
@ -309,15 +308,15 @@ impl core::ops::Deref for Window {
/// of components to the screen.
#[repr(C)]
#[derive(Clone)]
pub struct ComponentWindow(pub std::rc::Rc<Window>);
pub struct ComponentWindow(pub(crate) std::rc::Rc<Window>);
impl From<std::rc::Rc<Window>> for ComponentWindow {
fn from(inner: std::rc::Rc<Window>) -> Self {
Self(inner)
}
}
impl ComponentWindow {
/// Creates a new instance of a ComponentWindow based on the given window implementation. Only used
/// internally.
pub fn new(window_impl: std::rc::Rc<Window>) -> Self {
Self(window_impl)
}
/// Registers the window with the windowing system, in order to render the component's items and react
/// to input events once the event loop spins.
pub fn show(&self) {
@ -374,6 +373,11 @@ impl ComponentWindow {
pub fn close_popup(&self) {
self.0.platform_window.get().unwrap().clone().close_popup()
}
/// Return self as any so the backend can upcast
pub fn as_any(&self) -> &dyn core::any::Any {
self.0.as_any()
}
}
/// This module contains the functions needed to interface with the event loop and window traits

View file

@ -20,7 +20,7 @@ use corelib::input::{KeyboardModifiers, MouseEvent};
use corelib::items::ItemRef;
use corelib::layout::Orientation;
use corelib::slice::Slice;
use corelib::window::{ComponentWindow, PlatformWindow};
use corelib::window::PlatformWindow;
use corelib::Property;
use corelib::SharedString;
use sixtyfps_corelib as corelib;
@ -346,7 +346,7 @@ impl GraphicsWindow {
self.mouse_input_state.set(corelib::input::process_mouse_input(
component,
event,
&ComponentWindow::new(self.self_weak.upgrade().unwrap()),
&self.self_weak.upgrade().unwrap().into(),
self.mouse_input_state.take(),
));

View file

@ -1547,12 +1547,12 @@ fn to_femtovg_color(col: &Color) -> femtovg::Color {
#[cfg(target_arch = "wasm32")]
pub fn create_gl_window_with_canvas_id(canvas_id: String) -> ComponentWindow {
let window = sixtyfps_corelib::window::Window::new(|window| {
sixtyfps_corelib::window::Window::new(|window| {
GraphicsWindow::new(window, move |window_builder| {
GLRenderer::new(window_builder, &canvas_id)
})
});
ComponentWindow(window)
})
.into()
}
#[doc(hidden)]
@ -1573,7 +1573,7 @@ thread_local!(pub(crate) static IMAGE_CACHE: RefCell<images::ImageCache> = Defau
pub struct Backend;
impl sixtyfps_corelib::backend::Backend for Backend {
fn create_window(&'static self) -> ComponentWindow {
let window = sixtyfps_corelib::window::Window::new(|window| {
sixtyfps_corelib::window::Window::new(|window| {
GraphicsWindow::new(window, |window_builder| {
GLRenderer::new(
window_builder,
@ -1581,8 +1581,8 @@ impl sixtyfps_corelib::backend::Backend for Backend {
"canvas",
)
})
});
ComponentWindow(window)
})
.into()
}
fn run_event_loop(&'static self, behavior: sixtyfps_corelib::backend::EventLoopQuitBehavior) {

View file

@ -116,9 +116,7 @@ impl sixtyfps_corelib::backend::Backend for Backend {
panic!("The Qt backend needs Qt");
#[cfg(not(no_qt))]
{
let window =
sixtyfps_corelib::window::Window::new(|window| qt_window::QtWindow::new(window));
ComponentWindow::new(window)
sixtyfps_corelib::window::Window::new(|window| qt_window::QtWindow::new(window)).into()
}
}

View file

@ -1496,7 +1496,7 @@ pub(crate) mod ffi {
pub extern "C" fn sixtyfps_qt_get_widget(
window: &sixtyfps_corelib::window::ComponentWindow,
) -> *mut c_void {
<dyn std::any::Any>::downcast_ref(window.0.as_any())
<dyn std::any::Any>::downcast_ref(window.as_any())
.map_or(std::ptr::null_mut(), |win: &QtWindow| {
win.widget_ptr().cast::<c_void>().as_ptr()
})

View file

@ -34,7 +34,7 @@ pub struct TestingBackend {
impl sixtyfps_corelib::backend::Backend for TestingBackend {
fn create_window(&'static self) -> ComponentWindow {
ComponentWindow::new(Window::new(|_| Rc::new(TestingWindow::default())))
Window::new(|_| Rc::new(TestingWindow::default())).into()
}
fn run_event_loop(&'static self, _behavior: sixtyfps_corelib::backend::EventLoopQuitBehavior) {