Skip to content

Commit

Permalink
gtk: Relax Class methods trait requirements
Browse files Browse the repository at this point in the history
So they can be called outside of class_init unless required
in which case we add an explicit Self: ClassStruct requirement
  • Loading branch information
bilelmoussaoui committed Jul 10, 2024
1 parent 4a94837 commit 0040bb0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions gtk4/src/subclass/cell_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ unsafe extern "C" fn cell_area_foreach_alloc<T: CellAreaImpl>(
}

#[allow(clippy::missing_safety_doc)]
pub unsafe trait CellAreaClassExt: ClassStruct {
pub unsafe trait CellAreaClassExt {
#[doc(alias = "gtk_cell_area_class_find_cell_property")]
fn find_cell_property(&self, property_name: &str) -> Option<ParamSpec> {
unsafe {
Expand All @@ -956,4 +956,4 @@ pub unsafe trait CellAreaClassExt: ClassStruct {
}
}

unsafe impl<T: ClassStruct> CellAreaClassExt for T where T::Type: CellAreaImpl {}
unsafe impl<T: IsA<CellArea> + glib::object::IsClass> CellAreaClassExt for glib::object::Class<T> {}
43 changes: 30 additions & 13 deletions gtk4/src/subclass/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ unsafe extern "C" fn widget_unroot<T: WidgetImpl>(ptr: *mut ffi::GtkWidget) {
}

#[allow(clippy::missing_safety_doc)]
pub unsafe trait WidgetClassExt: ClassStruct {
pub unsafe trait WidgetClassExt {
#[doc(alias = "gtk_widget_class_set_template")]
fn set_template_bytes(&mut self, template: &glib::Bytes) {
unsafe {
Expand Down Expand Up @@ -905,6 +905,7 @@ pub unsafe trait WidgetClassExt: ClassStruct {
+ 'static
+ Clone,
Fut: Future<Output = ()>,
Self: ClassStruct,
{
self.install_action(
action_name,
Expand Down Expand Up @@ -939,6 +940,7 @@ pub unsafe trait WidgetClassExt: ClassStruct {
) where
F: Fn(&<<Self as ClassStruct>::Type as ObjectSubclass>::Type, &str, Option<&Variant>)
+ 'static,
Self: ClassStruct,
{
unsafe {
// We store the activate callbacks in a HashMap<action_name, activate>
Expand Down Expand Up @@ -1024,7 +1026,9 @@ pub unsafe trait WidgetClassExt: ClassStruct {
keyval: gdk::Key,
mods: gdk::ModifierType,
callback: F,
) {
) where
Self: ClassStruct,
{
let shortcut = crate::Shortcut::new(
Some(crate::KeyvalTrigger::new(keyval, mods)),
Some(crate::CallbackAction::new(
Expand All @@ -1046,8 +1050,11 @@ pub unsafe trait WidgetClassExt: ClassStruct {
}

#[doc(alias = "gtk_widget_class_add_binding_signal")]
fn add_binding_signal(&mut self, keyval: gdk::Key, mods: gdk::ModifierType, signal_name: &str) {
let type_ = <Self::Type as ObjectSubclassType>::type_();
fn add_binding_signal(&mut self, keyval: gdk::Key, mods: gdk::ModifierType, signal_name: &str)
where
Self: ClassStruct,
{
let type_ = <<Self as ClassStruct>::Type as ObjectSubclassType>::type_();
assert!(
SignalId::lookup(signal_name, type_).is_some(),
"Signal '{signal_name}' doesn't exists for type '{type_}'",
Expand Down Expand Up @@ -1103,8 +1110,11 @@ pub unsafe trait WidgetClassExt: ClassStruct {
}

#[doc(alias = "gtk_widget_class_set_activate_signal_from_name")]
fn set_activate_signal_from_name(&mut self, signal_name: &str) {
let type_ = <Self::Type as ObjectSubclassType>::type_();
fn set_activate_signal_from_name(&mut self, signal_name: &str)
where
Self: ClassStruct,
{
let type_ = <<Self as ClassStruct>::Type as ObjectSubclassType>::type_();
assert!(
SignalId::lookup(signal_name, type_).is_some(),
"Signal '{signal_name}' doesn't exists for type '{type_}'",
Expand Down Expand Up @@ -1179,12 +1189,13 @@ pub unsafe trait WidgetClassExt: ClassStruct {
&mut self,
name: &str,
internal: bool,
offset: field_offset::FieldOffset<Self::Type, TemplateChild<T>>,
offset: field_offset::FieldOffset<<Self as ClassStruct>::Type, TemplateChild<T>>,
) where
T: ObjectType + FromGlibPtrNone<*mut <T as ObjectType>::GlibType>,
Self: ClassStruct,
{
let widget_class = self as *mut _ as *mut ffi::GtkWidgetClass;
let private_offset = <Self::Type as ObjectSubclassType>::type_data()
let private_offset = <<Self as ClassStruct>::Type as ObjectSubclassType>::type_data()
.as_ref()
.impl_offset();
ffi::gtk_widget_class_bind_template_child_full(
Expand All @@ -1195,10 +1206,13 @@ pub unsafe trait WidgetClassExt: ClassStruct {
)
}

fn rust_template_scope(&mut self) -> BuilderRustScope {
fn rust_template_scope(&mut self) -> BuilderRustScope
where
Self: ClassStruct,
{
assert_initialized_main_thread!();
unsafe {
let mut data = <Self::Type as ObjectSubclassType>::type_data();
let mut data = <<Self as ClassStruct>::Type as ObjectSubclassType>::type_data();
let internal = data
.as_mut()
.class_data_mut::<Internal>(<Self::Type as ObjectSubclassType>::type_())
Expand All @@ -1213,7 +1227,7 @@ pub unsafe trait WidgetClassExt: ClassStruct {
}
}

unsafe impl<T: ClassStruct> WidgetClassExt for T where T::Type: WidgetImpl {}
unsafe impl<T: IsA<Widget> + glib::object::IsClass> WidgetClassExt for glib::object::Class<T> {}

#[derive(Debug, PartialEq, Eq)]
#[repr(transparent)]
Expand Down Expand Up @@ -1354,13 +1368,16 @@ pub trait CompositeTemplateCallbacks {
// rustdoc-stripper-ignore-next
/// Binds the template callbacks from this type into the default template
/// scope for `klass`.
fn bind_template_callbacks<T: WidgetClassExt>(klass: &mut T) {
fn bind_template_callbacks<T: WidgetClassExt + ClassStruct>(klass: &mut T) {
Self::add_callbacks_to_scope(&klass.rust_template_scope());
}
// rustdoc-stripper-ignore-next
/// Binds the template callbacks from this type into the default template
/// scope for `klass`, prepending `prefix` to each callback name.
fn bind_template_callbacks_prefixed<T: WidgetClassExt>(klass: &mut T, prefix: &str) {
fn bind_template_callbacks_prefixed<T: WidgetClassExt + ClassStruct>(
klass: &mut T,
prefix: &str,
) {
Self::add_callbacks_to_scope_prefixed(&klass.rust_template_scope(), prefix);
}
// rustdoc-stripper-ignore-next
Expand Down

0 comments on commit 0040bb0

Please sign in to comment.