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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::Buildable;
use crate::Constraint;
use crate::ConstraintGuide;
use crate::LayoutManager;
use glib::object::Cast;
use glib::translate::*;
use std::fmt;

glib::wrapper! {
    /// A layout manager using constraints to describe relations between widgets.
    ///
    /// [`ConstraintLayout`][crate::ConstraintLayout] is a layout manager that uses relations between
    /// widget attributes, expressed via [`Constraint`][crate::Constraint] instances, to
    /// measure and allocate widgets.
    ///
    /// ### How do constraints work
    ///
    /// Constraints are objects defining the relationship between attributes
    /// of a widget; you can read the description of the [`Constraint`][crate::Constraint]
    /// class to have a more in depth definition.
    ///
    /// By taking multiple constraints and applying them to the children of
    /// a widget using [`ConstraintLayout`][crate::ConstraintLayout], it's possible to describe
    /// complex layout policies; each constraint applied to a child or to the parent
    /// widgets contributes to the full description of the layout, in terms of
    /// parameters for resolving the value of each attribute.
    ///
    /// It is important to note that a layout is defined by the totality of
    /// constraints; removing a child, or a constraint, from an existing layout
    /// without changing the remaining constraints may result in an unstable
    /// or unsolvable layout.
    ///
    /// Constraints have an implicit "reading order"; you should start describing
    /// each edge of each child, as well as their relationship with the parent
    /// container, from the top left (or top right, in RTL languages), horizontally
    /// first, and then vertically.
    ///
    /// A constraint-based layout with too few constraints can become "unstable",
    /// that is: have more than one solution. The behavior of an unstable layout
    /// is undefined.
    ///
    /// A constraint-based layout with conflicting constraints may be unsolvable,
    /// and lead to an unstable layout. You can use the `property::Constraint::strength`
    /// property of [`Constraint`][crate::Constraint] to "nudge" the layout towards a solution.
    ///
    /// ### GtkConstraintLayout as GtkBuildable
    ///
    /// [`ConstraintLayout`][crate::ConstraintLayout] implements the [`Buildable`][crate::Buildable] interface and
    /// has a custom "constraints" element which allows describing constraints in
    /// a [`Builder`][crate::Builder] UI file.
    ///
    /// An example of a UI definition fragment specifying a constraint:
    ///
    /// ```xml
    ///   <object class="GtkConstraintLayout">
    ///     <constraints>
    ///       <constraint target="button" target-attribute="start"
    ///                   relation="eq"
    ///                   source="super" source-attribute="start"
    ///                   constant="12"
    ///                   strength="required" />
    ///       <constraint target="button" target-attribute="width"
    ///                   relation="ge"
    ///                   constant="250"
    ///                   strength="strong" />
    ///     </constraints>
    ///   </object>
    /// ```
    ///
    /// The definition above will add two constraints to the GtkConstraintLayout:
    ///
    ///  - a required constraint between the leading edge of "button" and
    ///    the leading edge of the widget using the constraint layout, plus
    ///    12 pixels
    ///  - a strong, constant constraint making the width of "button" greater
    ///    than, or equal to 250 pixels
    ///
    /// The "target" and "target-attribute" attributes are required.
    ///
    /// The "source" and "source-attribute" attributes of the "constraint"
    /// element are optional; if they are not specified, the constraint is
    /// assumed to be a constant.
    ///
    /// The "relation" attribute is optional; if not specified, the constraint
    /// is assumed to be an equality.
    ///
    /// The "strength" attribute is optional; if not specified, the constraint
    /// is assumed to be required.
    ///
    /// The "source" and "target" attributes can be set to "super" to indicate
    /// that the constraint target is the widget using the GtkConstraintLayout.
    ///
    /// There can be "constant" and "multiplier" attributes.
    ///
    /// Additionally, the "constraints" element can also contain a description
    /// of the `GtkConstraintGuides` used by the layout:
    ///
    /// ```xml
    ///   <constraints>
    ///     <guide min-width="100" max-width="500" name="hspace"/>
    ///     <guide min-height="64" nat-height="128" name="vspace" strength="strong"/>
    ///   </constraints>
    /// ```
    ///
    /// The "guide" element has the following optional attributes:
    ///
    ///   - "min-width", "nat-width", and "max-width", describe the minimum,
    ///     natural, and maximum width of the guide, respectively
    ///   - "min-height", "nat-height", and "max-height", describe the minimum,
    ///     natural, and maximum height of the guide, respectively
    ///   - "strength" describes the strength of the constraint on the natural
    ///     size of the guide; if not specified, the constraint is assumed to
    ///     have a medium strength
    ///   - "name" describes a name for the guide, useful when debugging
    ///
    /// ### Using the Visual Format Language
    ///
    /// Complex constraints can be described using a compact syntax called VFL,
    /// or *Visual Format Language*.
    ///
    /// The Visual Format Language describes all the constraints on a row or
    /// column, typically starting from the leading edge towards the trailing
    /// one. Each element of the layout is composed by "views", which identify
    /// a [`ConstraintTarget`][crate::ConstraintTarget].
    ///
    /// For instance:
    ///
    /// ```text
    ///   [button]-[textField]
    /// ```
    ///
    /// Describes a constraint that binds the trailing edge of "button" to the
    /// leading edge of "textField", leaving a default space between the two.
    ///
    /// Using VFL is also possible to specify predicates that describe constraints
    /// on attributes like width and height:
    ///
    /// ```text
    ///   // Width must be greater than, or equal to 50
    ///   [button(>=50)]
    ///
    ///   // Width of button1 must be equal to width of button2
    ///   [button1(==button2)]
    /// ```
    ///
    /// The default orientation for a VFL description is horizontal, unless
    /// otherwise specified:
    ///
    /// ```text
    ///   // horizontal orientation, default attribute: width
    ///   H:[button(>=150)]
    ///
    ///   // vertical orientation, default attribute: height
    ///   V:[button1(==button2)]
    /// ```
    ///
    /// It's also possible to specify multiple predicates, as well as their
    /// strength:
    ///
    /// ```text
    ///   // minimum width of button must be 150
    ///   // natural width of button can be 250
    ///   [button(>=150@required, ==250@medium)]
    /// ```
    ///
    /// Finally, it's also possible to use simple arithmetic operators:
    ///
    /// ```text
    ///   // width of button1 must be equal to width of button2
    ///   // divided by 2 plus 12
    ///   [button1(button2 / 2 + 12)]
    /// ```
    ///
    /// # Implements
    ///
    /// [`LayoutManagerExt`][trait@crate::prelude::LayoutManagerExt], [`trait@glib::ObjectExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt]
    #[doc(alias = "GtkConstraintLayout")]
    pub struct ConstraintLayout(Object<ffi::GtkConstraintLayout, ffi::GtkConstraintLayoutClass>) @extends LayoutManager, @implements Buildable;

    match fn {
        type_ => || ffi::gtk_constraint_layout_get_type(),
    }
}

impl ConstraintLayout {
    /// Creates a new [`ConstraintLayout`][crate::ConstraintLayout] layout manager.
    ///
    /// # Returns
    ///
    /// the newly created [`ConstraintLayout`][crate::ConstraintLayout]
    #[doc(alias = "gtk_constraint_layout_new")]
    pub fn new() -> ConstraintLayout {
        assert_initialized_main_thread!();
        unsafe { LayoutManager::from_glib_full(ffi::gtk_constraint_layout_new()).unsafe_cast() }
    }

    /// Adds a constraint to the layout manager.
    ///
    /// The `property::Constraint::source` and `property::Constraint::target`
    /// properties of `constraint` can be:
    ///
    ///  - set to `NULL` to indicate that the constraint refers to the
    ///    widget using `layout`
    ///  - set to the [`Widget`][crate::Widget] using `layout`
    ///  - set to a child of the [`Widget`][crate::Widget] using `layout`
    ///  - set to a [`ConstraintGuide`][crate::ConstraintGuide] that is part of `layout`
    ///
    /// The @self acquires the ownership of @constraint after calling
    /// this function.
    /// ## `constraint`
    /// a [`Constraint`][crate::Constraint]
    #[doc(alias = "gtk_constraint_layout_add_constraint")]
    pub fn add_constraint(&self, constraint: &Constraint) {
        unsafe {
            ffi::gtk_constraint_layout_add_constraint(
                self.to_glib_none().0,
                constraint.to_glib_full(),
            );
        }
    }

    /// Adds a guide to `layout`.
    ///
    /// A guide can be used as the source or target of constraints,
    /// like a widget, but it is not visible.
    ///
    /// The `layout` acquires the ownership of `guide` after calling
    /// this function.
    /// ## `guide`
    /// a [`ConstraintGuide`][crate::ConstraintGuide] object
    #[doc(alias = "gtk_constraint_layout_add_guide")]
    pub fn add_guide(&self, guide: &ConstraintGuide) {
        unsafe {
            ffi::gtk_constraint_layout_add_guide(self.to_glib_none().0, guide.to_glib_full());
        }
    }

    /// Returns a `GListModel` to track the constraints that are
    /// part of the layout.
    ///
    /// Calling this function will enable extra internal bookkeeping
    /// to track constraints and emit signals on the returned listmodel.
    /// It may slow down operations a lot.
    ///
    /// Applications should try hard to avoid calling this function
    /// because of the slowdowns.
    ///
    /// # Returns
    ///
    /// a
    ///   `GListModel` tracking the layout's constraints
    #[doc(alias = "gtk_constraint_layout_observe_constraints")]
    pub fn observe_constraints(&self) -> gio::ListModel {
        unsafe {
            from_glib_full(ffi::gtk_constraint_layout_observe_constraints(
                self.to_glib_none().0,
            ))
        }
    }

    /// Returns a `GListModel` to track the guides that are
    /// part of the layout.
    ///
    /// Calling this function will enable extra internal bookkeeping
    /// to track guides and emit signals on the returned listmodel.
    /// It may slow down operations a lot.
    ///
    /// Applications should try hard to avoid calling this function
    /// because of the slowdowns.
    ///
    /// # Returns
    ///
    /// a
    ///   `GListModel` tracking the layout's guides
    #[doc(alias = "gtk_constraint_layout_observe_guides")]
    pub fn observe_guides(&self) -> gio::ListModel {
        unsafe {
            from_glib_full(ffi::gtk_constraint_layout_observe_guides(
                self.to_glib_none().0,
            ))
        }
    }

    /// Removes all constraints from the layout manager.
    #[doc(alias = "gtk_constraint_layout_remove_all_constraints")]
    pub fn remove_all_constraints(&self) {
        unsafe {
            ffi::gtk_constraint_layout_remove_all_constraints(self.to_glib_none().0);
        }
    }

    /// Removes `constraint` from the layout manager,
    /// so that it no longer influences the layout.
    /// ## `constraint`
    /// a [`Constraint`][crate::Constraint]
    #[doc(alias = "gtk_constraint_layout_remove_constraint")]
    pub fn remove_constraint(&self, constraint: &Constraint) {
        unsafe {
            ffi::gtk_constraint_layout_remove_constraint(
                self.to_glib_none().0,
                constraint.to_glib_none().0,
            );
        }
    }

    /// Removes `guide` from the layout manager,
    /// so that it no longer influences the layout.
    /// ## `guide`
    /// a [`ConstraintGuide`][crate::ConstraintGuide] object
    #[doc(alias = "gtk_constraint_layout_remove_guide")]
    pub fn remove_guide(&self, guide: &ConstraintGuide) {
        unsafe {
            ffi::gtk_constraint_layout_remove_guide(self.to_glib_none().0, guide.to_glib_none().0);
        }
    }
}

impl Default for ConstraintLayout {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for ConstraintLayout {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("ConstraintLayout")
    }
}