Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template_child: Implement PropertyGet for TemplateChild #1886

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 20 additions & 62 deletions gtk4/src/subclass/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*, AccessibleRole, BuilderRustScope, BuilderScope,
Expand Down Expand Up @@ -1211,13 +1217,19 @@ pub unsafe trait WidgetClassExt: ClassStruct {
unsafe impl<T: ClassStruct> WidgetClassExt for T where T::Type: WidgetImpl {}

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

impl<T: Property> Property for TemplateChild<T>
where
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
{
type Value = T::Value;
}

impl<T> Default for TemplateChild<T>
Expand All @@ -1229,59 +1241,18 @@ where

Self {
ptr: std::ptr::null_mut(),
should_drop: false,
}
}
}

impl<T> glib::HasParamSpec for TemplateChild<T>
where
T: ObjectType + IsA<glib::Object> + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
{
type ParamSpec = glib::ParamSpecObject;
type SetValue = T;
type BuilderFn = fn(&str) -> glib::ParamSpecObjectBuilder<T>;

fn param_spec_builder() -> Self::BuilderFn {
Self::ParamSpec::builder
}
}

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>
impl<T> PropertyGet for TemplateChild<T>
where
T: glib::value::ValueType + ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
T: Property + ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
{
type Type = <T as glib::value::ValueType>::Type;
}
type Value = T;

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,
}
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
f(&self.get())
}
}

Expand Down Expand Up @@ -1314,19 +1285,6 @@ 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
Loading