Skip to content

Commit 4b9fd2e

Browse files
gtk/templates: Add missing traits for TemplateChild
Fixes #1842
1 parent b16f52b commit 4b9fd2e

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

gtk4-macros/tests/templates.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,50 @@ glib::wrapper! {
303303
fn blueprint_file() {
304304
let _: MyWidget5 = glib::Object::new();
305305
}
306+
307+
mod imp6 {
308+
use super::*;
309+
310+
#[derive(Default, glib::Properties, gtk::CompositeTemplate)]
311+
#[template(string = r#"
312+
<?xml version="1.0" encoding="UTF-8"?>
313+
<interface>
314+
<template class="TestWidget" parent="GtkWidget">
315+
<child>
316+
<object class="GtkWidget" id="widget" />
317+
</child>
318+
</template>
319+
</interface>
320+
"#)]
321+
#[properties(wrapper_type = super::TestWidget)]
322+
pub struct TestWidget {
323+
#[property(get)]
324+
#[template_child]
325+
widget: gtk::TemplateChild<gtk::Widget>,
326+
}
327+
328+
#[glib::object_subclass]
329+
impl ObjectSubclass for TestWidget {
330+
const NAME: &'static str = "TestWidget";
331+
type Type = super::TestWidget;
332+
type ParentType = gtk::Widget;
333+
334+
fn class_init(klass: &mut Self::Class) {
335+
klass.bind_template();
336+
}
337+
338+
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
339+
obj.init_template();
340+
}
341+
}
342+
343+
#[glib::derived_properties]
344+
impl ObjectImpl for TestWidget {}
345+
impl WidgetImpl for TestWidget {}
346+
impl TestWidget {}
347+
}
348+
349+
glib::wrapper! {
350+
pub struct TestWidget(ObjectSubclass<imp6::TestWidget>)
351+
@extends gtk::Widget;
352+
}

gtk4/src/subclass/widget.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,12 +1211,12 @@ pub unsafe trait WidgetClassExt: ClassStruct {
12111211
unsafe impl<T: ClassStruct> WidgetClassExt for T where T::Type: WidgetImpl {}
12121212

12131213
#[derive(Debug, PartialEq, Eq)]
1214-
#[repr(transparent)]
12151214
pub struct TemplateChild<T>
12161215
where
12171216
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
12181217
{
12191218
ptr: *mut <T as ObjectType>::GlibType,
1219+
should_drop: bool,
12201220
}
12211221

12221222
impl<T> Default for TemplateChild<T>
@@ -1228,6 +1228,7 @@ where
12281228

12291229
Self {
12301230
ptr: std::ptr::null_mut(),
1231+
should_drop: false,
12311232
}
12321233
}
12331234
}
@@ -1245,6 +1246,44 @@ where
12451246
}
12461247
}
12471248

1249+
impl<T> ToValue for TemplateChild<T>
1250+
where
1251+
T: ToValue + ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1252+
{
1253+
#[inline]
1254+
fn to_value(&self) -> glib::Value {
1255+
T::to_value(&self.get())
1256+
}
1257+
1258+
#[inline]
1259+
fn value_type(&self) -> glib::Type {
1260+
T::static_type()
1261+
}
1262+
}
1263+
1264+
impl<T> glib::value::ValueType for TemplateChild<T>
1265+
where
1266+
T: glib::value::ValueType + ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1267+
{
1268+
type Type = <T as glib::value::ValueType>::Type;
1269+
}
1270+
1271+
unsafe impl<'a, T> glib::value::FromValue<'a> for TemplateChild<T>
1272+
where
1273+
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1274+
{
1275+
type Checker = glib::value::GenericValueTypeChecker<T>;
1276+
1277+
#[inline]
1278+
unsafe fn from_value(value: &'a glib::Value) -> Self {
1279+
skip_assert_initialized!();
1280+
TemplateChild {
1281+
ptr: T::from_value(value).into_glib_ptr(),
1282+
should_drop: true,
1283+
}
1284+
}
1285+
}
1286+
12481287
impl<T> std::ops::Deref for TemplateChild<T>
12491288
where
12501289
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
@@ -1274,6 +1313,19 @@ where
12741313
}
12751314
}
12761315

1316+
impl<T> Drop for TemplateChild<T>
1317+
where
1318+
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1319+
{
1320+
fn drop(&mut self) {
1321+
if self.should_drop {
1322+
unsafe {
1323+
crate::glib::gobject_ffi::g_object_unref(self.ptr as *mut _);
1324+
}
1325+
}
1326+
}
1327+
}
1328+
12771329
impl<T> TemplateChild<T>
12781330
where
12791331
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,

0 commit comments

Comments
 (0)