From b07d940f3f511c728ef9ddc82d9c81de5a3023a2 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Mon, 14 Dec 2020 14:39:15 +0300 Subject: [PATCH 1/2] widget: Add InitializingWidgetExt Expose init_template() on initializing widgets. --- gtk4/src/prelude.rs | 2 +- gtk4/src/widget.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/gtk4/src/prelude.rs b/gtk4/src/prelude.rs index 2ae276a9e360..96738fd0f16b 100644 --- a/gtk4/src/prelude.rs +++ b/gtk4/src/prelude.rs @@ -21,7 +21,7 @@ pub use crate::spin_button::SpinButtonExtManual; pub use crate::text_buffer::TextBufferExtManual; pub use crate::tree_sortable::TreeSortableExtManual; pub use crate::tree_store::TreeStoreExtManual; -pub use crate::widget::WidgetExtManual; +pub use crate::widget::{InitializingWidgetExt, WidgetExtManual}; #[doc(hidden)] pub use glib::prelude::*; diff --git a/gtk4/src/widget.rs b/gtk4/src/widget.rs index a1ae296adc55..a611bb5a148a 100644 --- a/gtk4/src/widget.rs +++ b/gtk4/src/widget.rs @@ -1,6 +1,8 @@ // Take a look at the license at the top of the repository in the LICENSE file. +use crate::prelude::WidgetExt; use crate::Widget; + use glib::object::{Cast, IsA, WeakRef}; use glib::translate::*; use glib::ObjectExt; @@ -84,3 +86,15 @@ impl TickCallbackId { } } } + +pub trait InitializingWidgetExt { + fn init_template(&self); +} + +impl> InitializingWidgetExt for glib::subclass::InitializingObject { + fn init_template(&self) { + unsafe { + self.as_ref().init_template(); + } + } +} From f04cf3a6a192fcf8ea13013038d613d525fcac45 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Mon, 14 Dec 2020 14:39:49 +0300 Subject: [PATCH 2/2] examples: Update composite_template --- examples/src/bin/composite_template.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/src/bin/composite_template.rs b/examples/src/bin/composite_template.rs index ab5a1d390996..c40ccefa11e1 100644 --- a/examples/src/bin/composite_template.rs +++ b/examples/src/bin/composite_template.rs @@ -54,11 +54,15 @@ mod imp { klass.set_template(template); Self::bind_template_children(klass); } + + // You must call `Widget`'s `init_template()` within `instance_init()`. + fn instance_init(obj: &glib::subclass::InitializingObject) { + obj.init_template(); + } } impl ObjectImpl for ExApplicationWindow { fn constructed(&self, obj: &Self::Type) { - obj.init_template(); obj.init_label(); self.parent_constructed(obj); }