Rename init_*_items to register_component

I want to track component structure changes in the window without
generating more code. So use a more generic name for the init_*_items
functions, so that I can add the functionality I need in there.

Also add a register_component to PlatformWindow and call that.
This commit is contained in:
Tobias Hunger 2022-06-29 15:12:50 +02:00 committed by Tobias Hunger
parent 581533f302
commit dd59d41ee6
13 changed files with 27 additions and 11 deletions

View file

@ -126,9 +126,9 @@ public:
} }
template<typename Component, typename ItemArray> template<typename Component, typename ItemArray>
void init_items(Component *c, ItemArray items) const void register_component(Component *c, ItemArray items) const
{ {
cbindgen_private::slint_component_init_items( cbindgen_private::slint_register_component(
vtable::VRef<ComponentVTable> { &Component::static_vtable, c }, items, &inner); vtable::VRef<ComponentVTable> { &Component::static_vtable, c }, items, &inner);
} }

View file

@ -288,7 +288,7 @@ pub mod re_exports {
pub use i_slint_core::animations::EasingCurve; pub use i_slint_core::animations::EasingCurve;
pub use i_slint_core::callbacks::Callback; pub use i_slint_core::callbacks::Callback;
pub use i_slint_core::component::{ pub use i_slint_core::component::{
init_component_items, unregister_component, Component, ComponentRefPin, ComponentVTable, register_component, unregister_component, Component, ComponentRefPin, ComponentVTable,
ComponentWeak, IndexRange, ComponentWeak, IndexRange,
}; };
pub use i_slint_core::graphics::*; pub use i_slint_core::graphics::*;

View file

@ -315,6 +315,8 @@ impl PlatformWindow for GLWindow {
} }
} }
fn register_component(&self) {}
fn unregister_component<'a>( fn unregister_component<'a>(
&self, &self,
component: corelib::component::ComponentRef, component: corelib::component::ComponentRef,

View file

@ -145,6 +145,7 @@ mod the_backend {
fn request_redraw(&self) { fn request_redraw(&self) {
self.backend.with_inner(|inner| inner.post_event(McuEvent::Repaint)) self.backend.with_inner(|inner| inner.post_event(McuEvent::Repaint))
} }
fn register_component(&self) {}
fn unregister_component<'a>( fn unregister_component<'a>(
&self, &self,
_: i_slint_core::component::ComponentRef, _: i_slint_core::component::ComponentRef,

View file

@ -141,6 +141,8 @@ impl PlatformWindow for SimulatorWindow {
} }
} }
fn register_component(&self) {}
fn unregister_component<'a>( fn unregister_component<'a>(
&self, &self,
_: i_slint_core::component::ComponentRef, _: i_slint_core::component::ComponentRef,

View file

@ -1525,6 +1525,8 @@ impl PlatformWindow for QtWindow {
}}; }};
} }
fn register_component(&self) {}
fn unregister_component<'a>( fn unregister_component<'a>(
&self, &self,
component: ComponentRef, component: ComponentRef,

View file

@ -93,6 +93,8 @@ impl PlatformWindow for TestingWindow {
fn request_redraw(&self) {} fn request_redraw(&self) {}
fn register_component(&self) {}
fn unregister_component<'a>( fn unregister_component<'a>(
&self, &self,
_: i_slint_core::component::ComponentRef, _: i_slint_core::component::ComponentRef,

View file

@ -1104,7 +1104,10 @@ fn generate_item_tree(
} }
create_code.extend([ create_code.extend([
format!("{}->m_window.window_handle().init_items(self, self->item_array());", root_access), format!(
"{}->m_window.window_handle().register_component(self, self->item_array());",
root_access
),
format!("self->init({}, self->self_weak, 0, 1 {});", root_access, init_parent_parameters), format!("self->init({}, self->self_weak, 0, 1 {});", root_access, init_parent_parameters),
format!("return slint::ComponentHandle<{0}>{{ self_rc }};", target_struct.name), format!("return slint::ComponentHandle<{0}>{{ self_rc }};", target_struct.name),
]); ]);

View file

@ -1195,7 +1195,7 @@ fn generate_item_tree(
let self_rc = VRc::new(_self); let self_rc = VRc::new(_self);
let _self = self_rc.as_pin_ref(); let _self = self_rc.as_pin_ref();
#init_window #init_window
slint::re_exports::init_component_items(_self, Self::item_array(), #root_token.window.get().unwrap().window_handle()); slint::re_exports::register_component(_self, Self::item_array(), #root_token.window.get().unwrap().window_handle());
Self::init(slint::re_exports::VRc::map(self_rc.clone(), |x| x), #root_token, 0, 1); Self::init(slint::re_exports::VRc::map(self_rc.clone(), |x| x), #root_token, 0, 1);
self_rc self_rc
} }

View file

@ -111,12 +111,13 @@ pub type ComponentRc = vtable::VRc<ComponentVTable, Dyn>;
pub type ComponentWeak = vtable::VWeak<ComponentVTable, Dyn>; pub type ComponentWeak = vtable::VWeak<ComponentVTable, Dyn>;
/// Call init() on the ItemVTable for each item of the component. /// Call init() on the ItemVTable for each item of the component.
pub fn init_component_items<Base>( pub fn register_component<Base>(
base: core::pin::Pin<&Base>, base: core::pin::Pin<&Base>,
item_array: &[vtable::VOffset<Base, ItemVTable, vtable::AllowPin>], item_array: &[vtable::VOffset<Base, ItemVTable, vtable::AllowPin>],
window: &WindowRc, window: &WindowRc,
) { ) {
item_array.iter().for_each(|item| item.apply_pin(base).as_ref().init(window)); item_array.iter().for_each(|item| item.apply_pin(base).as_ref().init(window));
window.register_component();
} }
/// Free the backend graphics resources allocated by the component's items. /// Free the backend graphics resources allocated by the component's items.
@ -137,13 +138,13 @@ pub(crate) mod ffi {
/// Call init() on the ItemVTable of each item in the item array. /// Call init() on the ItemVTable of each item in the item array.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn slint_component_init_items( pub unsafe extern "C" fn slint_register_component(
component: ComponentRefPin, component: ComponentRefPin,
item_array: Slice<vtable::VOffset<u8, ItemVTable, vtable::AllowPin>>, item_array: Slice<vtable::VOffset<u8, ItemVTable, vtable::AllowPin>>,
window_handle: *const crate::window::ffi::WindowRcOpaque, window_handle: *const crate::window::ffi::WindowRcOpaque,
) { ) {
let window = &*(window_handle as *const WindowRc); let window = &*(window_handle as *const WindowRc);
super::init_component_items( super::register_component(
core::pin::Pin::new_unchecked(&*(component.as_ptr() as *const u8)), core::pin::Pin::new_unchecked(&*(component.as_ptr() as *const u8)),
item_array.as_slice(), item_array.as_slice(),
window, window,

View file

@ -1,7 +1,7 @@
// Copyright © SixtyFPS GmbH <info@slint-ui.com> // Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
// cSpell: ignore sharedvector textlayout // cSpell: ignore sharedvector swrenderer textlayout
#![doc = include_str!("README.md")] #![doc = include_str!("README.md")]
#![doc(html_logo_url = "https://slint-ui.com/logo/slint-logo-square-light.svg")] #![doc(html_logo_url = "https://slint-ui.com/logo/slint-logo-square-light.svg")]
@ -144,7 +144,7 @@ pub fn use_modules() -> usize {
+ properties::ffi::slint_property_init as usize + properties::ffi::slint_property_init as usize
+ string::ffi::slint_shared_string_bytes as usize + string::ffi::slint_shared_string_bytes as usize
+ window::ffi::slint_windowrc_drop as usize + window::ffi::slint_windowrc_drop as usize
+ component::ffi::slint_component_init_items as usize + component::ffi::slint_register_component as usize
+ timers::ffi::slint_timer_start as usize + timers::ffi::slint_timer_start as usize
+ graphics::color::ffi::slint_color_brighter as usize + graphics::color::ffi::slint_color_brighter as usize
+ graphics::image::ffi::slint_image_size as usize + graphics::image::ffi::slint_image_size as usize

View file

@ -39,6 +39,9 @@ pub trait PlatformWindow {
/// request. /// request.
fn request_redraw(&self); fn request_redraw(&self);
/// This function is called by the generated code when a component and therefore its tree of items are created.
fn register_component(&self);
/// This function is called by the generated code when a component and therefore its tree of items are destroyed. The /// This function is called by the generated code when a component and therefore its tree of items are destroyed. The
/// implementation typically uses this to free the underlying graphics resources cached via [`crate::graphics::RenderingCache`]. /// implementation typically uses this to free the underlying graphics resources cached via [`crate::graphics::RenderingCache`].
fn unregister_component<'a>( fn unregister_component<'a>(

View file

@ -1189,7 +1189,7 @@ pub fn instantiate(
let instance_ref = component_box.borrow_instance(); let instance_ref = component_box.borrow_instance();
if !component_type.original.is_global() { if !component_type.original.is_global() {
i_slint_core::component::init_component_items( i_slint_core::component::register_component(
instance_ref.instance, instance_ref.instance,
instance_ref.component_type.item_array.as_slice(), instance_ref.component_type.item_array.as_slice(),
eval::window_ref(instance_ref).unwrap(), eval::window_ref(instance_ref).unwrap(),