1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::Transform;
use glib::translate::*;

impl Transform {
    #[doc(alias = "gsk_transform_parse")]
    pub fn parse(string: &str) -> Result<Self, glib::BoolError> {
        assert_initialized_main_thread!();
        unsafe {
            let mut out_transform = std::ptr::null_mut();
            let ret = from_glib(ffi::gsk_transform_parse(
                string.to_glib_none().0,
                &mut out_transform,
            ));
            if ret {
                Ok(from_glib_full(out_transform))
            } else {
                Err(glib::bool_error!("Can't parse Transform"))
            }
        }
    }

    #[doc(alias = "gsk_transform_invert")]
    pub fn invert(&self) -> Result<Self, glib::BoolError> {
        unsafe {
            let matrix = self.to_matrix();
            if matrix == graphene::Matrix::new_identity() {
                return Ok(self.clone());
            }

            let res: Option<Self> = from_glib_full(ffi::gsk_transform_invert(self.to_glib_full()));
            res.ok_or_else(|| glib::bool_error!("Failed to invert the transform"))
        }
    }

    #[doc(alias = "gsk_transform_rotate")]
    #[must_use]
    pub fn rotate(&self, angle: f32) -> Self {
        unsafe {
            let res: Option<Self> =
                from_glib_full(ffi::gsk_transform_rotate(self.to_glib_full(), angle));
            res.unwrap_or_else(Self::new)
        }
    }

    #[doc(alias = "gsk_transform_rotate_3d")]
    #[must_use]
    pub fn rotate_3d(&self, angle: f32, axis: &graphene::Vec3) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_rotate_3d(
                self.to_glib_full(),
                angle,
                axis.to_glib_none().0,
            ));
            res.unwrap_or_else(Self::new)
        }
    }

    #[doc(alias = "gsk_transform_scale")]
    #[must_use]
    pub fn scale(&self, factor_x: f32, factor_y: f32) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_scale(
                self.to_glib_full(),
                factor_x,
                factor_y,
            ));
            res.unwrap_or_else(Self::new)
        }
    }

    #[doc(alias = "gsk_transform_scale_3d")]
    #[must_use]
    pub fn scale_3d(&self, factor_x: f32, factor_y: f32, factor_z: f32) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_scale_3d(
                self.to_glib_full(),
                factor_x,
                factor_y,
                factor_z,
            ));
            res.unwrap_or_else(Self::new)
        }
    }

    #[cfg(any(feature = "v4_6", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v4_6")))]
    #[doc(alias = "gsk_transform_skew")]
    #[must_use]
    pub fn skew(&self, skew_x: f32, skew_y: f32) -> Self {
        unsafe {
            let res: Option<Self> =
                from_glib_full(ffi::gsk_transform_skew(self.to_glib_full(), skew_x, skew_y));
            res.unwrap_or_else(Self::new)
        }
    }

    #[doc(alias = "gsk_transform_transform")]
    #[must_use]
    pub fn transform(&self, other: Option<&Self>) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_transform(
                self.to_glib_full(),
                other.to_glib_none().0,
            ));
            res.unwrap_or_else(Self::new)
        }
    }

    #[doc(alias = "gsk_transform_translate")]
    #[must_use]
    pub fn translate(&self, point: &graphene::Point) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_translate(
                self.to_glib_full(),
                point.to_glib_none().0,
            ));
            res.unwrap_or_else(Self::new)
        }
    }

    #[doc(alias = "gsk_transform_translate_3d")]
    #[must_use]
    pub fn translate_3d(&self, point: &graphene::Point3D) -> Self {
        unsafe {
            let res: Option<Self> = from_glib_full(ffi::gsk_transform_translate_3d(
                self.to_glib_full(),
                point.to_glib_none().0,
            ));
            res.unwrap_or_else(Self::new)
        }
    }
}

impl std::str::FromStr for Transform {
    type Err = glib::BoolError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        skip_assert_initialized!();
        Self::parse(s)
    }
}

#[test]
fn invert_identity_is_identity() {
    let transform = Transform::new();
    let output = transform.invert();
    assert_eq!(output.unwrap(), transform);
}