gtk4/
border.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5use crate::ffi;
6use glib::translate::*;
7
8glib::wrapper! {
9    /// A struct that specifies a border around a rectangular area.
10    ///
11    /// Each side can have different width.
12    #[doc(alias = "GtkBorder")]
13    pub struct Border(BoxedInline<ffi::GtkBorder>);
14
15    match fn {
16        copy => |ptr| ffi::gtk_border_copy(mut_override(ptr)),
17        free => |ptr| ffi::gtk_border_free(ptr),
18        type_ => || ffi::gtk_border_get_type(),
19    }
20}
21
22impl Border {
23    /// Allocates a new [`Border`][crate::Border] struct and initializes its elements to zero.
24    ///
25    /// # Returns
26    ///
27    /// a newly allocated [`Border`][crate::Border] struct.
28    ///  Free with `Gtk::Border::free()`
29    #[doc(alias = "gtk_border_new")]
30    pub fn new() -> Self {
31        assert_initialized_main_thread!();
32        unsafe { Self::uninitialized() }
33    }
34
35    // rustdoc-stripper-ignore-next
36    /// Creates a new builder-style object to construct a [`Border`].
37    ///
38    /// This method returns an instance of [`BorderBuilder`] which can be used
39    /// to create a [`Border`].
40    pub fn builder() -> BorderBuilder {
41        BorderBuilder::default()
42    }
43
44    #[inline]
45    pub fn left(&self) -> i16 {
46        self.inner.left
47    }
48
49    #[inline]
50    pub fn set_left(&mut self, left: i16) {
51        self.inner.left = left;
52    }
53
54    #[inline]
55    pub fn right(&self) -> i16 {
56        self.inner.right
57    }
58
59    #[inline]
60    pub fn set_right(&mut self, right: i16) {
61        self.inner.right = right;
62    }
63
64    #[inline]
65    pub fn top(&self) -> i16 {
66        self.inner.top
67    }
68
69    #[inline]
70    pub fn set_top(&mut self, top: i16) {
71        self.inner.top = top;
72    }
73
74    #[inline]
75    pub fn bottom(&self) -> i16 {
76        self.inner.bottom
77    }
78
79    #[inline]
80    pub fn set_bottom(&mut self, bottom: i16) {
81        self.inner.bottom = bottom;
82    }
83}
84
85impl Default for Border {
86    fn default() -> Self {
87        Self::new()
88    }
89}
90
91impl fmt::Debug for Border {
92    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
93        fmt.debug_struct("Border")
94            .field("left", &self.left())
95            .field("right", &self.right())
96            .field("top", &self.top())
97            .field("bottom", &self.bottom())
98            .finish()
99    }
100}
101
102impl PartialEq for Border {
103    #[inline]
104    fn eq(&self, other: &Self) -> bool {
105        self.left() == other.left()
106            && self.right() == other.right()
107            && self.top() == other.top()
108            && self.bottom() == other.bottom()
109    }
110}
111
112impl Eq for Border {}
113
114#[derive(Clone, Default)]
115// rustdoc-stripper-ignore-next
116/// A [builder-pattern] type to construct [`Border`] objects.
117///
118/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
119#[must_use = "The builder must be built to be used"]
120pub struct BorderBuilder {
121    left: Option<i16>,
122    right: Option<i16>,
123    bottom: Option<i16>,
124    top: Option<i16>,
125}
126
127impl BorderBuilder {
128    // rustdoc-stripper-ignore-next
129    /// Create a new [`BorderBuilder`].
130    pub fn new() -> Self {
131        Self::default()
132    }
133
134    pub fn left(mut self, left: i16) -> Self {
135        self.left = Some(left);
136        self
137    }
138
139    pub fn right(mut self, right: i16) -> Self {
140        self.right = Some(right);
141        self
142    }
143
144    pub fn bottom(mut self, bottom: i16) -> Self {
145        self.bottom = Some(bottom);
146        self
147    }
148
149    pub fn top(mut self, top: i16) -> Self {
150        self.top = Some(top);
151        self
152    }
153
154    // rustdoc-stripper-ignore-next
155    /// Build the [`Border`].
156    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
157    pub fn build(self) -> Border {
158        let mut border = Border::default();
159        if let Some(left) = self.left {
160            border.set_left(left);
161        }
162        if let Some(right) = self.right {
163            border.set_right(right);
164        }
165        if let Some(bottom) = self.bottom {
166            border.set_bottom(bottom);
167        }
168        if let Some(top) = self.top {
169            border.set_top(top);
170        }
171        border
172    }
173}