Skip to content

Commit 215211b

Browse files
gsk: Path API improvments & fixes
1 parent 7d55b28 commit 215211b

File tree

7 files changed

+129
-74
lines changed

7 files changed

+129
-74
lines changed

gsk4/Gir.toml

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ generate = [
3636
"Gsk.MaskNode",
3737
"Gsk.OpacityNode",
3838
"Gsk.OutsetShadowNode",
39-
"Gsk.Path",
40-
"Gsk.PathBuilder",
4139
"Gsk.PathDirection",
4240
"Gsk.PathForeachFlags",
4341
"Gsk.PathOperation",
44-
"Gsk.PathPoint",
4542
"Gsk.RadialGradientNode",
4643
"Gsk.RepeatingLinearGradientNode",
4744
"Gsk.RepeatingRadialGradientNode",
@@ -156,6 +153,44 @@ status = "generate"
156153
name = "get_child"
157154
manual = true # assert that idx < n_children
158155

156+
[[object]]
157+
name = "Gsk.Path"
158+
status = "generate"
159+
[[object.function]]
160+
name = "to_cairo"
161+
[[object.function.parameter]]
162+
name = "cr"
163+
const = true
164+
[[object.function]]
165+
name = "parse"
166+
[object.function.return]
167+
nullable_return_is_error = "Can't parse Path"
168+
[[object.function]]
169+
name = "print"
170+
ignore = true # to_str exists
171+
[[object.function]]
172+
name = "foreach"
173+
manual = true # tries to call from_glib_borrow on an enum
174+
[[object.function]]
175+
name = "point_get_curvature"
176+
manual = true # handle nullable point return value
177+
178+
[[object]]
179+
name = "Gsk.PathBuilder"
180+
status = "generate"
181+
[[object.function]]
182+
name = "free_to_path"
183+
ignore = true # not useful for bindings
184+
[[object.function]]
185+
name = "add_cairo_path"
186+
manual = true # use as_ptr for cairo::Path
187+
188+
[[object]]
189+
name = "Gsk.PathPoint"
190+
status = "generate"
191+
boxed_inline = true
192+
193+
159194
[[object]]
160195
name = "Gsk.Renderer"
161196
status = "generate"

gsk4/src/auto/path.rs

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// from gir-files (https://github.com/gtk-rs/gir-files)
33
// DO NOT EDIT
44

5-
use crate::{FillRule, PathDirection, PathForeachFlags, PathOperation, PathPoint, Stroke};
5+
use crate::{FillRule, PathDirection, PathPoint, Stroke};
66
use glib::translate::*;
77
use std::fmt;
88

@@ -18,38 +18,6 @@ glib::wrapper! {
1818
}
1919

2020
impl Path {
21-
#[doc(alias = "gsk_path_foreach")]
22-
pub fn foreach<P: FnMut(&PathOperation, &graphene::Point, usize) -> bool>(
23-
&self,
24-
flags: PathForeachFlags,
25-
func: P,
26-
) -> bool {
27-
let func_data: P = func;
28-
unsafe extern "C" fn func_func<
29-
P: FnMut(&PathOperation, &graphene::Point, usize) -> bool,
30-
>(
31-
op: ffi::GskPathOperation,
32-
pts: *const graphene::ffi::graphene_point_t,
33-
n_pts: libc::size_t,
34-
user_data: glib::ffi::gpointer,
35-
) -> glib::ffi::gboolean {
36-
let op = from_glib_borrow(op);
37-
let pts = from_glib_borrow(pts);
38-
let callback: *mut P = user_data as *const _ as usize as *mut P;
39-
(*callback)(&op, &pts, n_pts).into_glib()
40-
}
41-
let func = Some(func_func::<P> as _);
42-
let super_callback0: &P = &func_data;
43-
unsafe {
44-
from_glib(ffi::gsk_path_foreach(
45-
self.to_glib_none().0,
46-
flags.into_glib(),
47-
func,
48-
super_callback0 as *const _ as usize as *mut _,
49-
))
50-
}
51-
}
52-
5321
#[doc(alias = "gsk_path_get_bounds")]
5422
#[doc(alias = "get_bounds")]
5523
pub fn bounds(&self) -> Option<graphene::Rect> {
@@ -159,19 +127,6 @@ impl Path {
159127
unsafe { from_glib(ffi::gsk_path_is_empty(self.to_glib_none().0)) }
160128
}
161129

162-
#[doc(alias = "gsk_path_point_get_curvature")]
163-
pub fn point_get_curvature(&self, point: &PathPoint) -> (f32, Option<graphene::Point>) {
164-
unsafe {
165-
let mut center = graphene::Point::uninitialized();
166-
let ret = ffi::gsk_path_point_get_curvature(
167-
self.to_glib_none().0,
168-
point.to_glib_none().0,
169-
center.to_glib_none_mut().0,
170-
);
171-
(ret, center)
172-
}
173-
}
174-
175130
#[doc(alias = "gsk_path_point_get_position")]
176131
pub fn point_get_position(&self, point: &PathPoint) -> graphene::Point {
177132
unsafe {
@@ -199,17 +154,10 @@ impl Path {
199154
}
200155
}
201156

202-
#[doc(alias = "gsk_path_print")]
203-
pub fn print(&self, string: &mut glib::String) {
204-
unsafe {
205-
ffi::gsk_path_print(self.to_glib_none().0, string.to_glib_none_mut().0);
206-
}
207-
}
208-
209157
#[doc(alias = "gsk_path_to_cairo")]
210-
pub fn to_cairo(&self, cr: &mut cairo::Context) {
158+
pub fn to_cairo(&self, cr: &cairo::Context) {
211159
unsafe {
212-
ffi::gsk_path_to_cairo(self.to_glib_none().0, cr.to_glib_none_mut().0);
160+
ffi::gsk_path_to_cairo(self.to_glib_none().0, mut_override(cr.to_glib_none().0));
213161
}
214162
}
215163

@@ -220,9 +168,12 @@ impl Path {
220168
}
221169

222170
#[doc(alias = "gsk_path_parse")]
223-
pub fn parse(string: &str) -> Option<Path> {
171+
pub fn parse(string: &str) -> Result<Path, glib::BoolError> {
224172
assert_initialized_main_thread!();
225-
unsafe { from_glib_full(ffi::gsk_path_parse(string.to_glib_none().0)) }
173+
unsafe {
174+
Option::<_>::from_glib_full(ffi::gsk_path_parse(string.to_glib_none().0))
175+
.ok_or_else(|| glib::bool_error!("Can't parse Path"))
176+
}
226177
}
227178
}
228179

gsk4/src/auto/path_builder.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ impl PathBuilder {
2323
unsafe { from_glib_full(ffi::gsk_path_builder_new()) }
2424
}
2525

26-
#[doc(alias = "gsk_path_builder_add_cairo_path")]
27-
pub fn add_cairo_path(&self, path: &cairo::Path) {
28-
unsafe {
29-
ffi::gsk_path_builder_add_cairo_path(self.to_glib_none().0, path.to_glib_none().0);
30-
}
31-
}
32-
3326
#[doc(alias = "gsk_path_builder_add_circle")]
3427
pub fn add_circle(&self, center: &graphene::Point, radius: f32) {
3528
unsafe {
@@ -95,11 +88,6 @@ impl PathBuilder {
9588
}
9689
}
9790

98-
#[doc(alias = "gsk_path_builder_free_to_path")]
99-
pub fn free_to_path(&self) -> Path {
100-
unsafe { from_glib_full(ffi::gsk_path_builder_free_to_path(self.to_glib_none().0)) }
101-
}
102-
10391
#[doc(alias = "gsk_path_builder_get_current_point")]
10492
#[doc(alias = "get_current_point")]
10593
pub fn current_point(&self) -> graphene::Point {

gsk4/src/auto/path_point.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use glib::translate::*;
66
use std::cmp;
77

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

1211
match fn {
1312
copy => |ptr| ffi::gsk_path_point_copy(mut_override(ptr)),

gsk4/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ mod ngl_renderer;
6161
mod opacity_node;
6262
mod outset_shadow_node;
6363
mod parse_location;
64+
#[cfg(feature = "v4_14")]
65+
#[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
66+
mod path;
67+
#[cfg(feature = "v4_14")]
68+
#[cfg_attr(docsrs, doc(cfg(feature = "v4_14")))]
69+
mod path_builder;
6470
mod radial_gradient_node;
6571
mod repeat_node;
6672
mod repeating_linear_gradient_node;

gsk4/src/path.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use crate::{Path, PathForeachFlags, PathOperation, PathPoint};
4+
use glib::translate::*;
5+
6+
impl Path {
7+
#[doc(alias = "gsk_path_foreach")]
8+
pub fn foreach<P: FnMut(&PathOperation, &graphene::Point, usize) -> bool>(
9+
&self,
10+
flags: PathForeachFlags,
11+
func: P,
12+
) -> bool {
13+
let func_data: P = func;
14+
unsafe extern "C" fn func_func<
15+
P: FnMut(&PathOperation, &graphene::Point, usize) -> bool,
16+
>(
17+
op: ffi::GskPathOperation,
18+
pts: *const graphene::ffi::graphene_point_t,
19+
n_pts: libc::size_t,
20+
user_data: glib::ffi::gpointer,
21+
) -> glib::ffi::gboolean {
22+
let op = from_glib(op);
23+
let pts = from_glib_borrow(pts);
24+
let callback: *mut P = user_data as *const _ as usize as *mut P;
25+
(*callback)(&op, &pts, n_pts).into_glib()
26+
}
27+
let func = Some(func_func::<P> as _);
28+
let super_callback0: &P = &func_data;
29+
unsafe {
30+
from_glib(ffi::gsk_path_foreach(
31+
self.to_glib_none().0,
32+
flags.into_glib(),
33+
func,
34+
super_callback0 as *const _ as usize as *mut _,
35+
))
36+
}
37+
}
38+
39+
#[doc(alias = "gsk_path_point_get_curvature")]
40+
pub fn point_get_curvature(&self, point: &PathPoint) -> (f32, Option<graphene::Point>) {
41+
unsafe {
42+
let mut center = graphene::Point::uninitialized();
43+
let ret = ffi::gsk_path_point_get_curvature(
44+
self.to_glib_none().0,
45+
point.to_glib_none().0,
46+
center.to_glib_none_mut().0,
47+
);
48+
if ret == 0.0 {
49+
(ret, None)
50+
} else {
51+
(ret, Some(center))
52+
}
53+
}
54+
}
55+
}
56+
57+
impl std::str::FromStr for Path {
58+
type Err = glib::BoolError;
59+
fn from_str(s: &str) -> Result<Self, Self::Err> {
60+
assert_initialized_main_thread!();
61+
Path::parse(s)
62+
}
63+
}

gsk4/src/path_builder.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use crate::PathBuilder;
4+
use glib::translate::*;
5+
6+
impl PathBuilder {
7+
#[doc(alias = "gsk_path_builder_add_cairo_path")]
8+
pub fn add_cairo_path(&self, path: &cairo::Path) {
9+
unsafe {
10+
ffi::gsk_path_builder_add_cairo_path(self.to_glib_none().0, path.as_ptr());
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)