Skip to content

Commit

Permalink
gdk4-macos: manually implement native_window method
Browse files Browse the repository at this point in the history
For now we need to depend on the cocoa crate in order to implement
this method since it is the only decent crate implementing
the cocoa api.
  • Loading branch information
nacho committed Dec 9, 2024
1 parent 3a99628 commit 7a38527
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 44 deletions.
116 changes: 116 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gdk4-macos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ version.workspace = true

[features]
v4_8 = ["gdk4-macos-sys/v4_8"]
cocoa = ["dep:cocoa"]

[dependencies]
gdk4-macos-sys.workspace = true
gdk.workspace = true
gio.workspace = true
glib.workspace = true
libc.workspace = true
cocoa = { version = "0.26", optional = true }

[dev-dependencies]
gir-format-check.workspace = true
Expand Down
13 changes: 11 additions & 2 deletions gdk4-macos/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ generate = [
"GdkMacos.MacosGLContext",
"GdkMacos.MacosKeymap",
"GdkMacos.MacosSeat",
"GdkMacos.MacosSurface",
]

manual = [
Expand All @@ -36,4 +35,14 @@ name = "GdkMacos.MacosMonitor"
status = "generate"
[[object.function]]
name = "get_geometry"
ignore = true # The function does not exists
ignore = true # The function does not exists

[[object]]
name = "GdkMacos.MacosSurface"
status = "generate"
[[object.function]]
name = "get_native_window"
manual = true
[[object.property]]
name = "native"
ignore = true # Same as native_window
43 changes: 1 addition & 42 deletions gdk4-macos/src/auto/macos_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
// DO NOT EDIT

use crate::ffi;
use glib::{
prelude::*,
signal::{connect_raw, SignalHandlerId},
translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
#[doc(alias = "GdkMacosSurface")]
Expand All @@ -19,39 +13,4 @@ glib::wrapper! {
}
}

impl MacosSurface {
//#[cfg(feature = "v4_8")]
//#[cfg_attr(docsrs, doc(cfg(feature = "v4_8")))]
//#[doc(alias = "gdk_macos_surface_get_native_window")]
//#[doc(alias = "get_native_window")]
//pub fn native_window(&self) -> /*Unimplemented*/Option<Basic: Pointer> {
// unsafe { TODO: call ffi:gdk_macos_surface_get_native_window() }
//}

//pub fn native(&self) -> /*Unimplemented*/Basic: Pointer {
// ObjectExt::property(self, "native")
//}

#[doc(alias = "native")]
pub fn connect_native_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_native_trampoline<F: Fn(&MacosSurface) + 'static>(
this: *mut ffi::GdkMacosSurface,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
let f: &F = &*(f as *const F);
f(&from_glib_borrow(this))
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
b"notify::native\0".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_native_trampoline::<F> as *const (),
)),
Box_::into_raw(f),
)
}
}
}
impl MacosSurface {}
3 changes: 3 additions & 0 deletions gdk4-macos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ mod auto;
pub mod prelude;

pub use auto::*;

#[cfg(feature = "cocoa")]
mod macos_surface;
26 changes: 26 additions & 0 deletions gdk4-macos/src/macos_surface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(feature = "v4_8")]
use crate::ffi;
use crate::MacosSurface;
#[cfg(feature = "v4_8")]
use cocoa::base::id;
#[cfg(feature = "v4_8")]
use glib::translate::*;

impl MacosSurface {
#[cfg(feature = "v4_8")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_8")))]
#[doc(alias = "gdk_macos_surface_get_native_window")]
#[doc(alias = "get_native_window")]
pub fn native_window(&self) -> Option<id> {
unsafe {
let native_window_ptr = ffi::gdk_macos_surface_get_native_window(self.to_glib_none().0);
if native_window_ptr.is_null() {
None
} else {
Some(native_window_ptr as id)
}
}
}
}

0 comments on commit 7a38527

Please sign in to comment.