Skip to content

Commit

Permalink
gtk: add Sorter subclassing support
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Nov 25, 2020
1 parent 2338657 commit 20a5f70
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gtk4/src/subclass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod layout_manager;
pub mod list_box_row;
pub mod native_dialog;
pub mod popover;
pub mod sorter;
pub mod style_context;
pub mod toggle_button;
pub mod widget;
Expand All @@ -36,6 +37,7 @@ pub mod prelude {
pub use super::list_box_row::ListBoxRowImpl;
pub use super::native_dialog::NativeDialogImpl;
pub use super::popover::PopoverImpl;
pub use super::sorter::SorterImpl;
pub use super::style_context::StyleContextImpl;
pub use super::toggle_button::ToggleButtonImpl;
pub use super::widget::WidgetImpl;
Expand Down
84 changes: 84 additions & 0 deletions gtk4/src/subclass/sorter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use crate::{Ordering, Sorter, SorterOrder};
use glib::subclass::object::ObjectImpl;
use glib::subclass::prelude::*;
use glib::translate::*;
use glib::{Cast, Object};

pub trait SorterImpl: SorterImplExt + ObjectImpl {
fn compare(&self, sorter: &Self::Type, item1: &Object, item2: &Object) -> Ordering {
self.parent_compare(sorter, item1, item2)
}
fn get_order(&self, sorter: &Self::Type) -> SorterOrder {
self.parent_get_order(sorter)
}
}

pub trait SorterImplExt: ObjectSubclass {
fn parent_compare(&self, sorter: &Self::Type, item1: &Object, item2: &Object) -> Ordering;
fn parent_get_order(&self, sorter: &Self::Type) -> SorterOrder;
}

impl<T: SorterImpl> SorterImplExt for T {
fn parent_compare(&self, sorter: &Self::Type, item1: &Object, item2: &Object) -> Ordering {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GtkSorterClass;
let f = (*parent_class)
.compare
.expect("No parent class impl for \"compare\"");
from_glib(f(
sorter.unsafe_cast_ref::<Sorter>().to_glib_none().0,
item1.to_glib_none().0,
item2.to_glib_none().0,
))
}
}

fn parent_get_order(&self, sorter: &Self::Type) -> SorterOrder {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().get_parent_class() as *mut ffi::GtkSorterClass;
let f = (*parent_class)
.get_order
.expect("No parent class impl for \"get_order\"");
from_glib(f(sorter.unsafe_cast_ref::<Sorter>().to_glib_none().0))
}
}
}

unsafe impl<T: SorterImpl> IsSubclassable<T> for Sorter {
fn override_vfuncs(class: &mut glib::Class<Self>) {
<Object as IsSubclassable<T>>::override_vfuncs(class);

let klass = class.as_mut();
klass.compare = Some(sorter_compare::<T>);
klass.get_order = Some(sorter_get_order::<T>);
}
}

unsafe extern "C" fn sorter_compare<T: SorterImpl>(
ptr: *mut ffi::GtkSorter,
item1ptr: *mut glib::gobject_ffi::GObject,
item2ptr: *mut glib::gobject_ffi::GObject,
) -> ffi::GtkOrdering {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Borrowed<Sorter> = from_glib_borrow(ptr);

imp.compare(
wrap.unsafe_cast_ref(),
&from_glib_borrow(item1ptr),
&from_glib_borrow(item2ptr),
)
.to_glib()
}

unsafe extern "C" fn sorter_get_order<T: SorterImpl>(
ptr: *mut ffi::GtkSorter,
) -> ffi::GtkSorterOrder {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.get_impl();
let wrap: Borrowed<Sorter> = from_glib_borrow(ptr);

imp.get_order(wrap.unsafe_cast_ref()).to_glib()
}

0 comments on commit 20a5f70

Please sign in to comment.