Skip to content

Commit

Permalink
gtk/templates: Add missing traits for TemplateChild
Browse files Browse the repository at this point in the history
Fixes #1842
  • Loading branch information
bilelmoussaoui committed Oct 24, 2024
1 parent 1626356 commit c9927cf
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
47 changes: 47 additions & 0 deletions gtk4-macros/tests/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,50 @@ glib::wrapper! {
fn blueprint_file() {
let _: MyWidget5 = glib::Object::new();
}

mod imp6 {
use super::*;

#[derive(Default, glib::Properties, gtk::CompositeTemplate)]
#[template(string = r#"
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="TestWidget" parent="GtkWidget">
<child>
<object class="GtkWidget" id="widget" />
</child>
</template>
</interface>
"#)]
#[properties(wrapper_type = super::TestWidget)]
pub struct TestWidget {
#[property(get)]
#[template_child]
widget: gtk::TemplateChild<gtk::Widget>,
}

#[glib::object_subclass]
impl ObjectSubclass for TestWidget {
const NAME: &'static str = "TestWidget";
type Type = super::TestWidget;
type ParentType = gtk::Widget;

fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}

fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}

#[glib::derived_properties]
impl ObjectImpl for TestWidget {}
impl WidgetImpl for TestWidget {}
impl TestWidget {}
}

glib::wrapper! {
pub struct TestWidget(ObjectSubclass<imp6::TestWidget>)
@extends gtk::Widget;
}
55 changes: 54 additions & 1 deletion gtk4/src/subclass/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,12 +1211,13 @@ pub unsafe trait WidgetClassExt: ClassStruct {
unsafe impl<T: ClassStruct> WidgetClassExt for T where T::Type: WidgetImpl {}

#[derive(Debug, PartialEq, Eq)]
#[repr(transparent)]
#[repr(C)]
pub struct TemplateChild<T>
where
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
{
ptr: *mut <T as ObjectType>::GlibType,
should_drop: bool,
}

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

Self {
ptr: std::ptr::null_mut(),
should_drop: false,
}
}
}
Expand All @@ -1245,6 +1247,44 @@ where
}
}

impl<T> ToValue for TemplateChild<T>
where
T: ToValue + ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
{
#[inline]
fn to_value(&self) -> glib::Value {
T::to_value(&self.get())
}

#[inline]
fn value_type(&self) -> glib::Type {
T::static_type()
}
}

impl<T> glib::value::ValueType for TemplateChild<T>
where
T: glib::value::ValueType + ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
{
type Type = <T as glib::value::ValueType>::Type;
}

unsafe impl<'a, T> glib::value::FromValue<'a> for TemplateChild<T>
where
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
{
type Checker = glib::value::GenericValueTypeChecker<T>;

#[inline]
unsafe fn from_value(value: &'a glib::Value) -> Self {
skip_assert_initialized!();
TemplateChild {
ptr: T::from_value(value).into_glib_ptr(),
should_drop: true,
}
}
}

impl<T> std::ops::Deref for TemplateChild<T>
where
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
Expand Down Expand Up @@ -1274,6 +1314,19 @@ where
}
}

impl<T> Drop for TemplateChild<T>
where
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
{
fn drop(&mut self) {
if self.should_drop {
unsafe {
crate::glib::gobject_ffi::g_object_unref(self.ptr as *mut _);
}
}
}
}

impl<T> TemplateChild<T>
where
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
Expand Down

0 comments on commit c9927cf

Please sign in to comment.