Skip to content

Commit

Permalink
gtk: add Scrollable subclassing support
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Nov 25, 2020
1 parent 20a5f70 commit 5f65745
Show file tree
Hide file tree
Showing 2 changed files with 39 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 scrollable;
pub mod sorter;
pub mod style_context;
pub mod toggle_button;
Expand All @@ -37,6 +38,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::scrollable::ScrollableImpl;
pub use super::sorter::SorterImpl;
pub use super::style_context::StyleContextImpl;
pub use super::toggle_button::ToggleButtonImpl;
Expand Down
37 changes: 37 additions & 0 deletions gtk4/src/subclass/scrollable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{Border, Scrollable};
use glib::subclass::prelude::*;
use glib::translate::*;
use glib::Cast;

pub trait ScrollableImpl: ObjectImpl {
fn get_border(&self, scrollable: &Self::Type) -> Option<Border>;
}

unsafe impl<T: ScrollableImpl> IsImplementable<T> for Scrollable {
unsafe extern "C" fn interface_init(
iface: glib::ffi::gpointer,
_iface_data: glib::ffi::gpointer,
) {
let scrollable_iface = &mut *(iface as *mut ffi::GtkScrollableInterface);

scrollable_iface.get_border = Some(scrollable_get_border::<T>);
}
}

unsafe extern "C" fn scrollable_get_border<T: ScrollableImpl>(
scrollable: *mut ffi::GtkScrollable,
borderptr: *mut ffi::GtkBorder,
) -> glib::ffi::gboolean {
let instance = &*(scrollable as *mut T::Instance);
let imp = instance.get_impl();

if let Some(border) =
imp.get_border(from_glib_borrow::<_, Scrollable>(scrollable).unsafe_cast_ref())
{
*borderptr = *border.to_glib_full();
true.to_glib()
} else {
*borderptr = *std::ptr::null();
false.to_glib()
}
}

0 comments on commit 5f65745

Please sign in to comment.