Skip to content

Commit 9285c1b

Browse files
felinirabilelmoussaoui
authored andcommitted
template_child: Implement PropertyGet for TemplateChild
1 parent 453237c commit 9285c1b

File tree

1 file changed

+20
-62
lines changed

1 file changed

+20
-62
lines changed

gtk4/src/subclass/widget.rs

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
66
use std::{boxed::Box as Box_, collections::HashMap, fmt, future::Future};
77

8-
use glib::{clone::Downgrade, subclass::SignalId, translate::*, GString, Variant};
8+
use glib::{
9+
clone::Downgrade,
10+
property::{Property, PropertyGet},
11+
subclass::SignalId,
12+
translate::*,
13+
GString, Variant,
14+
};
915

1016
use crate::{
1117
ffi, prelude::*, subclass::prelude::*, Accessible, AccessibleRole, Buildable, BuilderRustScope,
@@ -1214,13 +1220,19 @@ pub unsafe trait WidgetClassExt: ClassStruct {
12141220
unsafe impl<T: ClassStruct> WidgetClassExt for T where T::Type: WidgetImpl {}
12151221

12161222
#[derive(Debug, PartialEq, Eq)]
1217-
#[repr(C)]
1223+
#[repr(transparent)]
12181224
pub struct TemplateChild<T>
12191225
where
12201226
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
12211227
{
12221228
ptr: *mut <T as ObjectType>::GlibType,
1223-
should_drop: bool,
1229+
}
1230+
1231+
impl<T: Property> Property for TemplateChild<T>
1232+
where
1233+
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1234+
{
1235+
type Value = T::Value;
12241236
}
12251237

12261238
impl<T> Default for TemplateChild<T>
@@ -1232,59 +1244,18 @@ where
12321244

12331245
Self {
12341246
ptr: std::ptr::null_mut(),
1235-
should_drop: false,
12361247
}
12371248
}
12381249
}
12391250

1240-
impl<T> glib::HasParamSpec for TemplateChild<T>
1241-
where
1242-
T: ObjectType + IsA<glib::Object> + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1243-
{
1244-
type ParamSpec = glib::ParamSpecObject;
1245-
type SetValue = T;
1246-
type BuilderFn = fn(&str) -> glib::ParamSpecObjectBuilder<T>;
1247-
1248-
fn param_spec_builder() -> Self::BuilderFn {
1249-
Self::ParamSpec::builder
1250-
}
1251-
}
1252-
1253-
impl<T> ToValue for TemplateChild<T>
1254-
where
1255-
T: ToValue + ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1256-
{
1257-
#[inline]
1258-
fn to_value(&self) -> glib::Value {
1259-
T::to_value(&self.get())
1260-
}
1261-
1262-
#[inline]
1263-
fn value_type(&self) -> glib::Type {
1264-
T::static_type()
1265-
}
1266-
}
1267-
1268-
impl<T> glib::value::ValueType for TemplateChild<T>
1251+
impl<T> PropertyGet for TemplateChild<T>
12691252
where
1270-
T: glib::value::ValueType + ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1253+
T: Property + ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
12711254
{
1272-
type Type = <T as glib::value::ValueType>::Type;
1273-
}
1255+
type Value = T;
12741256

1275-
unsafe impl<'a, T> glib::value::FromValue<'a> for TemplateChild<T>
1276-
where
1277-
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1278-
{
1279-
type Checker = glib::value::GenericValueTypeChecker<T>;
1280-
1281-
#[inline]
1282-
unsafe fn from_value(value: &'a glib::Value) -> Self {
1283-
skip_assert_initialized!();
1284-
TemplateChild {
1285-
ptr: T::from_value(value).into_glib_ptr(),
1286-
should_drop: true,
1287-
}
1257+
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
1258+
f(&self.get())
12881259
}
12891260
}
12901261

@@ -1317,19 +1288,6 @@ where
13171288
}
13181289
}
13191290

1320-
impl<T> Drop for TemplateChild<T>
1321-
where
1322-
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
1323-
{
1324-
fn drop(&mut self) {
1325-
if self.should_drop {
1326-
unsafe {
1327-
crate::glib::gobject_ffi::g_object_unref(self.ptr as *mut _);
1328-
}
1329-
}
1330-
}
1331-
}
1332-
13331291
impl<T> TemplateChild<T>
13341292
where
13351293
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,

0 commit comments

Comments
 (0)