mirror of
https://github.com/slint-ui/slint.git
synced 2025-11-01 04:18:14 +00:00
Create the window adapter lazily in C++
This commit is contained in:
parent
d48e7192d8
commit
cd9994306e
5 changed files with 29 additions and 19 deletions
|
|
@ -21,21 +21,21 @@ template<typename Component>
|
|||
inline void send_mouse_click(const Component *component, float x, float y)
|
||||
{
|
||||
auto crc = *component->self_weak.into_dyn().lock();
|
||||
cbindgen_private::slint_send_mouse_click(&crc, x, y, &component->m_window.window_handle());
|
||||
cbindgen_private::slint_send_mouse_click(&crc, x, y, &component->window().window_handle());
|
||||
}
|
||||
|
||||
template<typename Component>
|
||||
inline void send_keyboard_char(const Component *component, const slint::SharedString &str,
|
||||
bool pressed)
|
||||
{
|
||||
cbindgen_private::slint_send_keyboard_char(&str, pressed, &component->m_window.window_handle());
|
||||
cbindgen_private::slint_send_keyboard_char(&str, pressed, &component->window().window_handle());
|
||||
}
|
||||
|
||||
template<typename Component>
|
||||
inline void send_keyboard_string_sequence(const Component *component,
|
||||
const slint::SharedString &str)
|
||||
{
|
||||
cbindgen_private::send_keyboard_string_sequence(&str, &component->m_window.window_handle());
|
||||
cbindgen_private::send_keyboard_string_sequence(&str, &component->window().window_handle());
|
||||
}
|
||||
|
||||
#define assert_eq(A, B) \
|
||||
|
|
|
|||
|
|
@ -29,6 +29,15 @@ pub unsafe extern "C" fn slint_windowrc_init(out: *mut WindowAdapterRcOpaque) {
|
|||
core::ptr::write(out as *mut Rc<dyn WindowAdapter>, win);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn slint_ensure_backend() {
|
||||
i_slint_backend_selector::with_platform(|_b| {
|
||||
// Nothing to do, just make sure a backend was created
|
||||
Ok(())
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn slint_run_event_loop() {
|
||||
i_slint_backend_selector::with_platform(|b| b.run_event_loop()).unwrap();
|
||||
|
|
|
|||
|
|
@ -732,9 +732,8 @@ fn generate_public_component(file: &mut File, component: &llr::PublicComponent)
|
|||
// FIXME: many of the different component bindings need to access this
|
||||
Access::Public,
|
||||
Declaration::Var(Var {
|
||||
ty: "slint::Window".into(),
|
||||
ty: "std::optional<slint::Window>".into(),
|
||||
name: "m_window".into(),
|
||||
init: Some("slint::Window{slint::private_api::WindowAdapterRc()}".into()),
|
||||
..Default::default()
|
||||
}),
|
||||
));
|
||||
|
|
@ -780,7 +779,7 @@ fn generate_public_component(file: &mut File, component: &llr::PublicComponent)
|
|||
Declaration::Function(Function {
|
||||
name: "show".into(),
|
||||
signature: "()".into(),
|
||||
statements: Some(vec!["m_window.show();".into()]),
|
||||
statements: Some(vec!["window().show();".into()]),
|
||||
..Default::default()
|
||||
}),
|
||||
));
|
||||
|
|
@ -790,7 +789,7 @@ fn generate_public_component(file: &mut File, component: &llr::PublicComponent)
|
|||
Declaration::Function(Function {
|
||||
name: "hide".into(),
|
||||
signature: "()".into(),
|
||||
statements: Some(vec!["m_window.hide();".into()]),
|
||||
statements: Some(vec!["window().hide();".into()]),
|
||||
..Default::default()
|
||||
}),
|
||||
));
|
||||
|
|
@ -800,10 +799,15 @@ fn generate_public_component(file: &mut File, component: &llr::PublicComponent)
|
|||
Declaration::Function(Function {
|
||||
name: "window".into(),
|
||||
signature: "() const -> slint::Window&".into(),
|
||||
statements: Some(vec![format!(
|
||||
"return const_cast<{} *>(this)->m_window;",
|
||||
component_struct.name
|
||||
)]),
|
||||
statements: Some(vec![
|
||||
format!("auto self = const_cast<{} *>(this);", component_struct.name),
|
||||
"if (!m_window.has_value()) {".into(),
|
||||
" auto &window = self->m_window.emplace(slint::private_api::WindowAdapterRc());"
|
||||
.into(),
|
||||
" window.window_handle().set_component(*self);".into(),
|
||||
"}".into(),
|
||||
"return self->m_window.value();".into(),
|
||||
]),
|
||||
..Default::default()
|
||||
}),
|
||||
));
|
||||
|
|
@ -1225,15 +1229,12 @@ fn generate_item_tree(
|
|||
];
|
||||
|
||||
if parent_ctx.is_none() {
|
||||
create_code.extend([format!(
|
||||
"{}->m_window.window_handle().set_component(*self_rc);",
|
||||
root_access
|
||||
)]);
|
||||
create_code.push("slint::cbindgen_private::slint_ensure_backend();".into());
|
||||
}
|
||||
|
||||
create_code.extend([
|
||||
format!(
|
||||
"{}->m_window.window_handle().register_component(self, self->item_array());",
|
||||
"if (auto &window = {}->m_window) window->window_handle().register_component(self, self->item_array());",
|
||||
root_access
|
||||
),
|
||||
format!("self->init({}, self->self_weak, 0, 1 {});", root_access, init_parent_parameters),
|
||||
|
|
@ -1266,7 +1267,7 @@ fn generate_item_tree(
|
|||
let mut destructor = vec!["auto self = this;".to_owned()];
|
||||
|
||||
destructor.push(format!(
|
||||
"{}->m_window.window_handle().unregister_component(self, item_array());",
|
||||
"if (auto &window = {}->m_window) window->window_handle().unregister_component(self, item_array());",
|
||||
root_access
|
||||
));
|
||||
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ assert(instance.get_test_zero());
|
|||
assert(instance.get_test());
|
||||
|
||||
ratio = 2.;
|
||||
instance.m_window.window_handle().set_scale_factor(ratio);
|
||||
instance.window().window_handle().set_scale_factor(ratio);
|
||||
assert_eq(instance.get_l1(), 12.);
|
||||
assert_eq(instance.get_l2(), 12. * ratio);
|
||||
assert_eq(instance.get_l3(), 100. + 12. * ratio);
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ assert(instance.get_test());
|
|||
|
||||
assert_eq(instance.get_phx_to_rem(), 2.);
|
||||
assert_eq(instance.get_px_to_rem(), 2.);
|
||||
instance.m_window.window_handle().set_scale_factor(2.0);
|
||||
instance.window().window_handle().set_scale_factor(2.0);
|
||||
assert_eq(instance.get_phys_pixel_size(), 20.);
|
||||
assert_eq(instance.get_phx_to_rem(), 1.);
|
||||
assert_eq(instance.get_px_to_rem(), 2.);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue