Skip to content

Commit 66717ef

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

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
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+
test_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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,43 @@ where
12451245
}
12461246
}
12471247

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

0 commit comments

Comments
 (0)