Skip to content

Commit c9927cf

Browse files
gtk/templates: Add missing traits for TemplateChild
Fixes #1842
1 parent 1626356 commit c9927cf

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-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: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,12 +1211,13 @@ 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)]
1214+
#[repr(C)]
12151215
pub struct TemplateChild<T>
12161216
where
12171217
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
12181218
{
12191219
ptr: *mut <T as ObjectType>::GlibType,
1220+
should_drop: bool,
12201221
}
12211222

12221223
impl<T> Default for TemplateChild<T>
@@ -1228,6 +1229,7 @@ where
12281229

12291230
Self {
12301231
ptr: std::ptr::null_mut(),
1232+
should_drop: false,
12311233
}
12321234
}
12331235
}
@@ -1245,6 +1247,44 @@ where
12451247
}
12461248
}
12471249

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

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

0 commit comments

Comments
 (0)