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

manually add typed and with_type methods #1386

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions gdk4/src/content_provider.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{prelude::*, ContentProvider};
use glib::translate::*;
use glib::{translate::*, value::FromValue};

// rustdoc-stripper-ignore-next
/// Trait containing manually implemented methods of [`ContentProvider`](crate::ContentProvider).
pub trait ContentProviderExtManual {
#[doc(alias = "gdk_content_provider_get_value")]
fn value(&self, type_: glib::Type) -> Result<glib::Value, glib::Error>;
fn value<T: for<'a> FromValue<'a> + StaticType>(&self) -> Result<T, glib::Error> {
self.value_with_type(T::static_type())
.map(|v| v.get::<T>().unwrap())
}
#[doc(alias = "gdk_content_provider_get_value")]
fn value_with_type(&self, type_: glib::Type) -> Result<glib::Value, glib::Error>;
}

impl<O: IsA<ContentProvider>> ContentProviderExtManual for O {
fn value(&self, type_: glib::Type) -> Result<glib::Value, glib::Error> {
fn value_with_type(&self, type_: glib::Type) -> Result<glib::Value, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let mut value = glib::Value::from_type(type_);
Expand Down
10 changes: 5 additions & 5 deletions gdk4/src/subclass/content_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub trait ContentProviderImpl: ContentProviderImplExt + ObjectImpl {
self.parent_write_mime_type_future(mime_type, stream, io_priority)
}

fn value(&self, type_: glib::Type) -> Result<Value, glib::Error> {
self.parent_value(type_)
fn value_with_type(&self, type_: glib::Type) -> Result<glib::Value, glib::Error> {
self.parent_value_with_type(type_)
}
}

Expand Down Expand Up @@ -72,7 +72,7 @@ pub trait ContentProviderImplExt: ObjectSubclass {
io_priority: glib::Priority,
) -> Pin<Box<dyn Future<Output = Result<(), glib::Error>> + 'static>>;

fn parent_value(&self, type_: glib::Type) -> Result<Value, glib::Error>;
fn parent_value_with_type(&self, type_: glib::Type) -> Result<Value, glib::Error>;
}

impl<T: ContentProviderImpl> ContentProviderImplExt for T {
Expand Down Expand Up @@ -258,7 +258,7 @@ impl<T: ContentProviderImpl> ContentProviderImplExt for T {
))
}

fn parent_value(&self, type_: glib::Type) -> Result<Value, glib::Error> {
fn parent_value_with_type(&self, type_: glib::Type) -> Result<Value, glib::Error> {
unsafe {
let data = T::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GdkContentProviderClass;
Expand Down Expand Up @@ -421,7 +421,7 @@ unsafe extern "C" fn content_provider_get_value<T: ContentProviderImpl>(
let imp = instance.imp();
let value: Value = from_glib_none(value_ptr);

let ret = imp.value(value.type_());
let ret = imp.value_with_type(value.type_());
match ret {
Ok(v) => {
glib::gobject_ffi::g_value_copy(v.to_glib_none().0, value_ptr);
Expand Down
3 changes: 3 additions & 0 deletions gtk4/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,9 @@ status = "generate"
name = "Gtk.Widget"
status = "generate"
manual_traits = ["WidgetExtManual"]
[[object.function]]
name = "ancestor"
manual = true
[[object.function]]
name = "add_tick_callback"
manual = true
Expand Down
21 changes: 21 additions & 0 deletions gtk4/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,30 @@ pub trait WidgetExtManual: 'static {
&self,
callback: P,
) -> TickCallbackId;

#[doc(alias = "gtk_widget_get_ancestor")]
#[doc(alias = "get_ancestor")]
#[must_use]
fn ancestor<T: IsA<Widget>>(&self) -> Option<T> {
self.ancestor_with_type(T::static_type())
.and_then(|w| w.downcast().ok())
}

#[doc(alias = "gtk_widget_get_ancestor")]
#[doc(alias = "get_ancestor")]
#[must_use]
fn ancestor_with_type(&self, widget_type: glib::types::Type) -> Option<Widget>;
}

impl<O: IsA<Widget>> WidgetExtManual for O {
fn ancestor_with_type(&self, widget_type: glib::types::Type) -> Option<Widget> {
unsafe {
from_glib_none(ffi::gtk_widget_get_ancestor(
self.as_ref().to_glib_none().0,
widget_type.into_glib(),
))
}
}
fn add_tick_callback<P: Fn(&Self, &gdk::FrameClock) -> Continue + 'static>(
&self,
callback: P,
Expand Down