Skip to content

Commit

Permalink
gsk: Path API improvments & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Aug 12, 2023
1 parent fbb4812 commit d352922
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 74 deletions.
41 changes: 38 additions & 3 deletions gsk4/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ generate = [
"Gsk.MaskNode",
"Gsk.OpacityNode",
"Gsk.OutsetShadowNode",
"Gsk.Path",
"Gsk.PathBuilder",
"Gsk.PathDirection",
"Gsk.PathForeachFlags",
"Gsk.PathOperation",
"Gsk.PathPoint",
"Gsk.RadialGradientNode",
"Gsk.RepeatingLinearGradientNode",
"Gsk.RepeatingRadialGradientNode",
Expand Down Expand Up @@ -156,6 +153,44 @@ status = "generate"
name = "get_child"
manual = true # assert that idx < n_children

[[object]]
name = "Gsk.Path"
status = "generate"
[[object.function]]
name = "to_cairo"
[[object.function.parameter]]
name = "cr"
const = true
[[object.function]]
name = "parse"
[object.function.return]
nullable_return_is_error = "Can't parse Path"
[[object.function]]
name = "print"
ignore = true # to_str exists
[[object.function]]
name = "foreach"
manual = true # tries to call from_glib_borrow on an enum
[[object.function]]
name = "point_get_curvature"
manual = true # handle nullable point return value

[[object]]
name = "Gsk.PathBuilder"
status = "generate"
[[object.function]]
name = "free_to_path"
ignore = true # not useful for bindings
[[object.function]]
name = "add_cairo_path"
manual = true # use as_ptr for cairo::Path

[[object]]
name = "Gsk.PathPoint"
status = "generate"
boxed_inline = true


[[object]]
name = "Gsk.Renderer"
status = "generate"
Expand Down
65 changes: 8 additions & 57 deletions gsk4/src/auto/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{FillRule, PathDirection, PathForeachFlags, PathOperation, PathPoint, Stroke};
use crate::{FillRule, PathDirection, PathPoint, Stroke};
use glib::translate::*;
use std::fmt;

Expand All @@ -18,38 +18,6 @@ glib::wrapper! {
}

impl Path {
#[doc(alias = "gsk_path_foreach")]
pub fn foreach<P: FnMut(&PathOperation, &graphene::Point, usize) -> bool>(
&self,
flags: PathForeachFlags,
func: P,
) -> bool {
let func_data: P = func;
unsafe extern "C" fn func_func<
P: FnMut(&PathOperation, &graphene::Point, usize) -> bool,
>(
op: ffi::GskPathOperation,
pts: *const graphene::ffi::graphene_point_t,
n_pts: libc::size_t,
user_data: glib::ffi::gpointer,
) -> glib::ffi::gboolean {
let op = from_glib_borrow(op);
let pts = from_glib_borrow(pts);
let callback: *mut P = user_data as *const _ as usize as *mut P;
(*callback)(&op, &pts, n_pts).into_glib()
}
let func = Some(func_func::<P> as _);
let super_callback0: &P = &func_data;
unsafe {
from_glib(ffi::gsk_path_foreach(
self.to_glib_none().0,
flags.into_glib(),
func,
super_callback0 as *const _ as usize as *mut _,
))
}
}

#[doc(alias = "gsk_path_get_bounds")]
#[doc(alias = "get_bounds")]
pub fn bounds(&self) -> Option<graphene::Rect> {
Expand Down Expand Up @@ -159,19 +127,6 @@ impl Path {
unsafe { from_glib(ffi::gsk_path_is_empty(self.to_glib_none().0)) }
}

#[doc(alias = "gsk_path_point_get_curvature")]
pub fn point_get_curvature(&self, point: &PathPoint) -> (f32, Option<graphene::Point>) {
unsafe {
let mut center = graphene::Point::uninitialized();
let ret = ffi::gsk_path_point_get_curvature(
self.to_glib_none().0,
point.to_glib_none().0,
center.to_glib_none_mut().0,
);
(ret, center)
}
}

#[doc(alias = "gsk_path_point_get_position")]
pub fn point_get_position(&self, point: &PathPoint) -> graphene::Point {
unsafe {
Expand Down Expand Up @@ -199,17 +154,10 @@ impl Path {
}
}

#[doc(alias = "gsk_path_print")]
pub fn print(&self, string: &mut glib::String) {
unsafe {
ffi::gsk_path_print(self.to_glib_none().0, string.to_glib_none_mut().0);
}
}

#[doc(alias = "gsk_path_to_cairo")]
pub fn to_cairo(&self, cr: &mut cairo::Context) {
pub fn to_cairo(&self, cr: &cairo::Context) {
unsafe {
ffi::gsk_path_to_cairo(self.to_glib_none().0, cr.to_glib_none_mut().0);
ffi::gsk_path_to_cairo(self.to_glib_none().0, mut_override(cr.to_glib_none().0));
}
}

Expand All @@ -220,9 +168,12 @@ impl Path {
}

#[doc(alias = "gsk_path_parse")]
pub fn parse(string: &str) -> Option<Path> {
pub fn parse(string: &str) -> Result<Path, glib::BoolError> {
assert_initialized_main_thread!();
unsafe { from_glib_full(ffi::gsk_path_parse(string.to_glib_none().0)) }
unsafe {
Option::<_>::from_glib_full(ffi::gsk_path_parse(string.to_glib_none().0))
.ok_or_else(|| glib::bool_error!("Can't parse Path"))
}
}
}

Expand Down
12 changes: 0 additions & 12 deletions gsk4/src/auto/path_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ impl PathBuilder {
unsafe { from_glib_full(ffi::gsk_path_builder_new()) }
}

#[doc(alias = "gsk_path_builder_add_cairo_path")]
pub fn add_cairo_path(&self, path: &cairo::Path) {
unsafe {
ffi::gsk_path_builder_add_cairo_path(self.to_glib_none().0, path.to_glib_none().0);
}
}

#[doc(alias = "gsk_path_builder_add_circle")]
pub fn add_circle(&self, center: &graphene::Point, radius: f32) {
unsafe {
Expand Down Expand Up @@ -95,11 +88,6 @@ impl PathBuilder {
}
}

#[doc(alias = "gsk_path_builder_free_to_path")]
pub fn free_to_path(&self) -> Path {
unsafe { from_glib_full(ffi::gsk_path_builder_free_to_path(self.to_glib_none().0)) }
}

#[doc(alias = "gsk_path_builder_get_current_point")]
#[doc(alias = "get_current_point")]
pub fn current_point(&self) -> graphene::Point {
Expand Down
3 changes: 1 addition & 2 deletions gsk4/src/auto/path_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use glib::translate::*;
use std::cmp;

glib::wrapper! {
#[derive(Debug, Hash)]
pub struct PathPoint(Boxed<ffi::GskPathPoint>);
pub struct PathPoint(BoxedInline<ffi::GskPathPoint>);

match fn {
copy => |ptr| ffi::gsk_path_point_copy(mut_override(ptr)),
Expand Down
6 changes: 6 additions & 0 deletions gsk4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ mod ngl_renderer;
mod opacity_node;
mod outset_shadow_node;
mod parse_location;
#[cfg(feature = "v4_14")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
mod path;
#[cfg(feature = "v4_14")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
mod path_builder;
mod radial_gradient_node;
mod repeat_node;
mod repeating_linear_gradient_node;
Expand Down
63 changes: 63 additions & 0 deletions gsk4/src/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{Path, PathForeachFlags, PathOperation, PathPoint};
use glib::translate::*;

impl Path {
#[doc(alias = "gsk_path_foreach")]
pub fn foreach<P: FnMut(&PathOperation, &graphene::Point, usize) -> bool>(
&self,
flags: PathForeachFlags,
func: P,
) -> bool {
let func_data: P = func;
unsafe extern "C" fn func_func<
P: FnMut(&PathOperation, &graphene::Point, usize) -> bool,
>(
op: ffi::GskPathOperation,
pts: *const graphene::ffi::graphene_point_t,
n_pts: libc::size_t,
user_data: glib::ffi::gpointer,
) -> glib::ffi::gboolean {
let op = from_glib(op);
let pts = from_glib_borrow(pts);
let callback: *mut P = user_data as *const _ as usize as *mut P;
(*callback)(&op, &pts, n_pts).into_glib()
}
let func = Some(func_func::<P> as _);
let super_callback0: &P = &func_data;
unsafe {
from_glib(ffi::gsk_path_foreach(
self.to_glib_none().0,
flags.into_glib(),
func,
super_callback0 as *const _ as usize as *mut _,
))
}
}

#[doc(alias = "gsk_path_point_get_curvature")]
pub fn point_get_curvature(&self, point: &PathPoint) -> (f32, Option<graphene::Point>) {
unsafe {
let mut center = graphene::Point::uninitialized();
let ret = ffi::gsk_path_point_get_curvature(
self.to_glib_none().0,
point.to_glib_none().0,
center.to_glib_none_mut().0,
);
if ret == 0.0 {
(ret, None)
} else {
(ret, Some(center))
}
}
}
}

impl std::str::FromStr for Path {
type Err = glib::BoolError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
assert_initialized_main_thread!();
Path::parse(s)
}
}
13 changes: 13 additions & 0 deletions gsk4/src/path_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::PathBuilder;
use glib::translate::*;

impl PathBuilder {
#[doc(alias = "gsk_path_builder_add_cairo_path")]
pub fn add_cairo_path(&self, path: &cairo::Path) {
unsafe {
ffi::gsk_path_builder_add_cairo_path(self.to_glib_none().0, path.as_ptr());
}
}
}

0 comments on commit d352922

Please sign in to comment.