From 9285c1be66ca65e6c51568b35493c603f315daeb Mon Sep 17 00:00:00 2001 From: Fina Wilke Date: Thu, 24 Oct 2024 22:11:34 +0200 Subject: [PATCH] template_child: Implement PropertyGet for TemplateChild --- gtk4/src/subclass/widget.rs | 82 +++++++++---------------------------- 1 file changed, 20 insertions(+), 62 deletions(-) diff --git a/gtk4/src/subclass/widget.rs b/gtk4/src/subclass/widget.rs index 9d451cceb22e..acdb0242ead7 100644 --- a/gtk4/src/subclass/widget.rs +++ b/gtk4/src/subclass/widget.rs @@ -5,7 +5,13 @@ use std::{boxed::Box as Box_, collections::HashMap, fmt, future::Future}; -use glib::{clone::Downgrade, subclass::SignalId, translate::*, GString, Variant}; +use glib::{ + clone::Downgrade, + property::{Property, PropertyGet}, + subclass::SignalId, + translate::*, + GString, Variant, +}; use crate::{ ffi, prelude::*, subclass::prelude::*, Accessible, AccessibleRole, Buildable, BuilderRustScope, @@ -1214,13 +1220,19 @@ pub unsafe trait WidgetClassExt: ClassStruct { unsafe impl WidgetClassExt for T where T::Type: WidgetImpl {} #[derive(Debug, PartialEq, Eq)] -#[repr(C)] +#[repr(transparent)] pub struct TemplateChild where T: ObjectType + FromGlibPtrNone<*mut ::GlibType>, { ptr: *mut ::GlibType, - should_drop: bool, +} + +impl Property for TemplateChild +where + T: ObjectType + FromGlibPtrNone<*mut ::GlibType>, +{ + type Value = T::Value; } impl Default for TemplateChild @@ -1232,59 +1244,18 @@ where Self { ptr: std::ptr::null_mut(), - should_drop: false, } } } -impl glib::HasParamSpec for TemplateChild -where - T: ObjectType + IsA + FromGlibPtrNone<*mut ::GlibType>, -{ - type ParamSpec = glib::ParamSpecObject; - type SetValue = T; - type BuilderFn = fn(&str) -> glib::ParamSpecObjectBuilder; - - fn param_spec_builder() -> Self::BuilderFn { - Self::ParamSpec::builder - } -} - -impl ToValue for TemplateChild -where - T: ToValue + ObjectType + FromGlibPtrNone<*mut ::GlibType>, -{ - #[inline] - fn to_value(&self) -> glib::Value { - T::to_value(&self.get()) - } - - #[inline] - fn value_type(&self) -> glib::Type { - T::static_type() - } -} - -impl glib::value::ValueType for TemplateChild +impl PropertyGet for TemplateChild where - T: glib::value::ValueType + ObjectType + FromGlibPtrNone<*mut ::GlibType>, + T: Property + ObjectType + FromGlibPtrNone<*mut ::GlibType>, { - type Type = ::Type; -} + type Value = T; -unsafe impl<'a, T> glib::value::FromValue<'a> for TemplateChild -where - T: ObjectType + FromGlibPtrNone<*mut ::GlibType>, -{ - type Checker = glib::value::GenericValueTypeChecker; - - #[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, - } + fn get R>(&self, f: F) -> R { + f(&self.get()) } } @@ -1317,19 +1288,6 @@ where } } -impl Drop for TemplateChild -where - T: ObjectType + FromGlibPtrNone<*mut ::GlibType>, -{ - fn drop(&mut self) { - if self.should_drop { - unsafe { - crate::glib::gobject_ffi::g_object_unref(self.ptr as *mut _); - } - } - } -} - impl TemplateChild where T: ObjectType + FromGlibPtrNone<*mut ::GlibType>,