gtk4/auto/constraint_layout.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, Buildable, Constraint, ConstraintGuide, LayoutManager};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9 /// Uses constraints to describe relations between widgets.
10 ///
11 /// [`ConstraintLayout`][crate::ConstraintLayout] is a layout manager that uses relations between
12 /// widget attributes, expressed via [`Constraint`][crate::Constraint] instances, to
13 /// measure and allocate widgets.
14 ///
15 /// ### How do constraints work
16 ///
17 /// Constraints are objects defining the relationship between attributes
18 /// of a widget; you can read the description of the [`Constraint`][crate::Constraint]
19 /// class to have a more in depth definition.
20 ///
21 /// By taking multiple constraints and applying them to the children of
22 /// a widget using [`ConstraintLayout`][crate::ConstraintLayout], it's possible to describe
23 /// complex layout policies; each constraint applied to a child or to the parent
24 /// widgets contributes to the full description of the layout, in terms of
25 /// parameters for resolving the value of each attribute.
26 ///
27 /// It is important to note that a layout is defined by the totality of
28 /// constraints; removing a child, or a constraint, from an existing layout
29 /// without changing the remaining constraints may result in an unstable
30 /// or unsolvable layout.
31 ///
32 /// Constraints have an implicit "reading order"; you should start describing
33 /// each edge of each child, as well as their relationship with the parent
34 /// container, from the top left (or top right, in RTL languages), horizontally
35 /// first, and then vertically.
36 ///
37 /// A constraint-based layout with too few constraints can become "unstable",
38 /// that is: have more than one solution. The behavior of an unstable layout
39 /// is undefined.
40 ///
41 /// A constraint-based layout with conflicting constraints may be unsolvable,
42 /// and lead to an unstable layout. You can use the [`strength`][struct@crate::Constraint#strength]
43 /// property of [`Constraint`][crate::Constraint] to "nudge" the layout towards a solution.
44 ///
45 /// ### GtkConstraintLayout as GtkBuildable
46 ///
47 /// [`ConstraintLayout`][crate::ConstraintLayout] implements the [`Buildable`][crate::Buildable] interface and
48 /// has a custom "constraints" element which allows describing constraints in
49 /// a [`Builder`][crate::Builder] UI file.
50 ///
51 /// An example of a UI definition fragment specifying a constraint:
52 ///
53 /// ```xml
54 /// <object class="GtkConstraintLayout">
55 /// <constraints>
56 /// <constraint target="button" target-attribute="start"
57 /// relation="eq"
58 /// source="super" source-attribute="start"
59 /// constant="12"
60 /// strength="required" />
61 /// <constraint target="button" target-attribute="width"
62 /// relation="ge"
63 /// constant="250"
64 /// strength="strong" />
65 /// </constraints>
66 /// </object>
67 /// ```
68 ///
69 /// The definition above will add two constraints to the GtkConstraintLayout:
70 ///
71 /// - a required constraint between the leading edge of "button" and
72 /// the leading edge of the widget using the constraint layout, plus
73 /// 12 pixels
74 /// - a strong, constant constraint making the width of "button" greater
75 /// than, or equal to 250 pixels
76 ///
77 /// The "target" and "target-attribute" attributes are required.
78 ///
79 /// The "source" and "source-attribute" attributes of the "constraint"
80 /// element are optional; if they are not specified, the constraint is
81 /// assumed to be a constant.
82 ///
83 /// The "relation" attribute is optional; if not specified, the constraint
84 /// is assumed to be an equality.
85 ///
86 /// The "strength" attribute is optional; if not specified, the constraint
87 /// is assumed to be required.
88 ///
89 /// The "source" and "target" attributes can be set to "super" to indicate
90 /// that the constraint target is the widget using the GtkConstraintLayout.
91 ///
92 /// There can be "constant" and "multiplier" attributes.
93 ///
94 /// Additionally, the "constraints" element can also contain a description
95 /// of the `GtkConstraintGuides` used by the layout:
96 ///
97 /// ```xml
98 /// <constraints>
99 /// <guide min-width="100" max-width="500" name="hspace"/>
100 /// <guide min-height="64" nat-height="128" name="vspace" strength="strong"/>
101 /// </constraints>
102 /// ```
103 ///
104 /// The "guide" element has the following optional attributes:
105 ///
106 /// - "min-width", "nat-width", and "max-width", describe the minimum,
107 /// natural, and maximum width of the guide, respectively
108 /// - "min-height", "nat-height", and "max-height", describe the minimum,
109 /// natural, and maximum height of the guide, respectively
110 /// - "strength" describes the strength of the constraint on the natural
111 /// size of the guide; if not specified, the constraint is assumed to
112 /// have a medium strength
113 /// - "name" describes a name for the guide, useful when debugging
114 ///
115 /// ### Using the Visual Format Language
116 ///
117 /// Complex constraints can be described using a compact syntax called VFL,
118 /// or *Visual Format Language*.
119 ///
120 /// The Visual Format Language describes all the constraints on a row or
121 /// column, typically starting from the leading edge towards the trailing
122 /// one. Each element of the layout is composed by "views", which identify
123 /// a [`ConstraintTarget`][crate::ConstraintTarget].
124 ///
125 /// For instance:
126 ///
127 /// ```text
128 /// [button]-[textField]
129 /// ```
130 ///
131 /// Describes a constraint that binds the trailing edge of "button" to the
132 /// leading edge of "textField", leaving a default space between the two.
133 ///
134 /// Using VFL is also possible to specify predicates that describe constraints
135 /// on attributes like width and height:
136 ///
137 /// ```text
138 /// // Width must be greater than, or equal to 50
139 /// [button(>=50)]
140 ///
141 /// // Width of button1 must be equal to width of button2
142 /// [button1(==button2)]
143 /// ```
144 ///
145 /// The default orientation for a VFL description is horizontal, unless
146 /// otherwise specified:
147 ///
148 /// ```text
149 /// // horizontal orientation, default attribute: width
150 /// H:[button(>=150)]
151 ///
152 /// // vertical orientation, default attribute: height
153 /// V:[button1(==button2)]
154 /// ```
155 ///
156 /// It's also possible to specify multiple predicates, as well as their
157 /// strength:
158 ///
159 /// ```text
160 /// // minimum width of button must be 150
161 /// // natural width of button can be 250
162 /// [button(>=150@required, ==250@medium)]
163 /// ```
164 ///
165 /// Finally, it's also possible to use simple arithmetic operators:
166 ///
167 /// ```text
168 /// // width of button1 must be equal to width of button2
169 /// // divided by 2 plus 12
170 /// [button1(button2 / 2 + 12)]
171 /// ```
172 ///
173 /// # Implements
174 ///
175 /// [`LayoutManagerExt`][trait@crate::prelude::LayoutManagerExt], [`trait@glib::ObjectExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt]
176 #[doc(alias = "GtkConstraintLayout")]
177 pub struct ConstraintLayout(Object<ffi::GtkConstraintLayout, ffi::GtkConstraintLayoutClass>) @extends LayoutManager, @implements Buildable;
178
179 match fn {
180 type_ => || ffi::gtk_constraint_layout_get_type(),
181 }
182}
183
184impl ConstraintLayout {
185 /// Creates a new [`ConstraintLayout`][crate::ConstraintLayout] layout manager.
186 ///
187 /// # Returns
188 ///
189 /// the newly created [`ConstraintLayout`][crate::ConstraintLayout]
190 #[doc(alias = "gtk_constraint_layout_new")]
191 pub fn new() -> ConstraintLayout {
192 assert_initialized_main_thread!();
193 unsafe { LayoutManager::from_glib_full(ffi::gtk_constraint_layout_new()).unsafe_cast() }
194 }
195
196 /// Adds a constraint to the layout manager.
197 ///
198 /// The [`source`][struct@crate::Constraint#source] and [`target`][struct@crate::Constraint#target]
199 /// properties of `constraint` can be:
200 ///
201 /// - set to `NULL` to indicate that the constraint refers to the
202 /// widget using `layout`
203 /// - set to the [`Widget`][crate::Widget] using `layout`
204 /// - set to a child of the [`Widget`][crate::Widget] using `layout`
205 /// - set to a [`ConstraintGuide`][crate::ConstraintGuide] that is part of `layout`
206 ///
207 /// The @self acquires the ownership of @constraint after calling
208 /// this function.
209 /// ## `constraint`
210 /// a [`Constraint`][crate::Constraint]
211 #[doc(alias = "gtk_constraint_layout_add_constraint")]
212 pub fn add_constraint(&self, constraint: Constraint) {
213 unsafe {
214 ffi::gtk_constraint_layout_add_constraint(
215 self.to_glib_none().0,
216 constraint.into_glib_ptr(),
217 );
218 }
219 }
220
221 /// Adds a guide to `layout`.
222 ///
223 /// A guide can be used as the source or target of constraints,
224 /// like a widget, but it is not visible.
225 ///
226 /// The `layout` acquires the ownership of `guide` after calling
227 /// this function.
228 /// ## `guide`
229 /// a [`ConstraintGuide`][crate::ConstraintGuide] object
230 #[doc(alias = "gtk_constraint_layout_add_guide")]
231 pub fn add_guide(&self, guide: ConstraintGuide) {
232 unsafe {
233 ffi::gtk_constraint_layout_add_guide(self.to_glib_none().0, guide.into_glib_ptr());
234 }
235 }
236
237 /// Returns a `GListModel` to track the constraints that are
238 /// part of the layout.
239 ///
240 /// Calling this function will enable extra internal bookkeeping
241 /// to track constraints and emit signals on the returned listmodel.
242 /// It may slow down operations a lot.
243 ///
244 /// Applications should try hard to avoid calling this function
245 /// because of the slowdowns.
246 ///
247 /// # Returns
248 ///
249 /// a
250 /// `GListModel` tracking the layout's constraints
251 #[doc(alias = "gtk_constraint_layout_observe_constraints")]
252 pub fn observe_constraints(&self) -> gio::ListModel {
253 unsafe {
254 from_glib_full(ffi::gtk_constraint_layout_observe_constraints(
255 self.to_glib_none().0,
256 ))
257 }
258 }
259
260 /// Returns a `GListModel` to track the guides that are
261 /// part of the layout.
262 ///
263 /// Calling this function will enable extra internal bookkeeping
264 /// to track guides and emit signals on the returned listmodel.
265 /// It may slow down operations a lot.
266 ///
267 /// Applications should try hard to avoid calling this function
268 /// because of the slowdowns.
269 ///
270 /// # Returns
271 ///
272 /// a
273 /// `GListModel` tracking the layout's guides
274 #[doc(alias = "gtk_constraint_layout_observe_guides")]
275 pub fn observe_guides(&self) -> gio::ListModel {
276 unsafe {
277 from_glib_full(ffi::gtk_constraint_layout_observe_guides(
278 self.to_glib_none().0,
279 ))
280 }
281 }
282
283 /// Removes all constraints from the layout manager.
284 #[doc(alias = "gtk_constraint_layout_remove_all_constraints")]
285 pub fn remove_all_constraints(&self) {
286 unsafe {
287 ffi::gtk_constraint_layout_remove_all_constraints(self.to_glib_none().0);
288 }
289 }
290
291 /// Removes `constraint` from the layout manager,
292 /// so that it no longer influences the layout.
293 /// ## `constraint`
294 /// a [`Constraint`][crate::Constraint]
295 #[doc(alias = "gtk_constraint_layout_remove_constraint")]
296 pub fn remove_constraint(&self, constraint: &Constraint) {
297 unsafe {
298 ffi::gtk_constraint_layout_remove_constraint(
299 self.to_glib_none().0,
300 constraint.to_glib_none().0,
301 );
302 }
303 }
304
305 /// Removes `guide` from the layout manager,
306 /// so that it no longer influences the layout.
307 /// ## `guide`
308 /// a [`ConstraintGuide`][crate::ConstraintGuide] object
309 #[doc(alias = "gtk_constraint_layout_remove_guide")]
310 pub fn remove_guide(&self, guide: &ConstraintGuide) {
311 unsafe {
312 ffi::gtk_constraint_layout_remove_guide(self.to_glib_none().0, guide.to_glib_none().0);
313 }
314 }
315}
316
317impl Default for ConstraintLayout {
318 fn default() -> Self {
319 Self::new()
320 }
321}