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 18, 2024
1 parent b16f52b commit 66717ef
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
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]
test_widget: gtk::TemplateChild<gtk::Widget>,

Check failure on line 325 in gtk4-macros/tests/templates.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC

Template child with id `test_widget` not found in template XML
}

#[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();

Check failure on line 335 in gtk4-macros/tests/templates.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC

the method `bind_template` exists for mutable reference `&mut ClassStruct<TestWidget>`, but its trait bounds were not satisfied
}

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

Check failure on line 339 in gtk4-macros/tests/templates.rs

View workflow job for this annotation

GitHub Actions / Windows MSVC

the method `init_template` exists for reference `&InitializingObject<TestWidget>`, but its trait bounds were not satisfied
}
}

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

glib::wrapper! {
pub struct TestWidget(ObjectSubclass<imp6::TestWidget>)
@extends gtk::Widget;
}
37 changes: 37 additions & 0 deletions gtk4/src/subclass/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,43 @@ 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).as_ptr(),
}
}
}

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

0 comments on commit 66717ef

Please sign in to comment.