gtk4/auto/
constraint.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, ConstraintAttribute, ConstraintRelation, ConstraintTarget};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// [`Constraint`][crate::Constraint] describes a constraint between attributes of two widgets,
10    ///  expressed as a linear equation.
11    ///
12    /// The typical equation for a constraint is:
13    ///
14    /// ```text
15    ///   target.target_attr = source.source_attr × multiplier + constant
16    /// ```
17    ///
18    /// Each [`Constraint`][crate::Constraint] is part of a system that will be solved by a
19    /// [`ConstraintLayout`][crate::ConstraintLayout] in order to allocate and position each
20    /// child widget or guide.
21    ///
22    /// The source and target, as well as their attributes, of a [`Constraint`][crate::Constraint]
23    /// instance are immutable after creation.
24    ///
25    /// ## Properties
26    ///
27    ///
28    /// #### `constant`
29    ///  The constant value to be added to the [`source-attribute`][struct@crate::Constraint#source-attribute].
30    ///
31    /// Readable | Writeable | Construct Only
32    ///
33    ///
34    /// #### `multiplier`
35    ///  The multiplication factor to be applied to
36    /// the [`source-attribute`][struct@crate::Constraint#source-attribute].
37    ///
38    /// Readable | Writeable | Construct Only
39    ///
40    ///
41    /// #### `relation`
42    ///  The order relation between the terms of the constraint.
43    ///
44    /// Readable | Writeable | Construct Only
45    ///
46    ///
47    /// #### `source`
48    ///  The source of the constraint.
49    ///
50    /// The constraint will set the [`target-attribute`][struct@crate::Constraint#target-attribute]
51    /// property of the target using the [`source-attribute`][struct@crate::Constraint#source-attribute]
52    /// property of the source.
53    ///
54    /// Readable | Writeable | Construct Only
55    ///
56    ///
57    /// #### `source-attribute`
58    ///  The attribute of the [`source`][struct@crate::Constraint#source] read by the
59    /// constraint.
60    ///
61    /// Readable | Writeable | Construct Only
62    ///
63    ///
64    /// #### `strength`
65    ///  The strength of the constraint.
66    ///
67    /// The strength can be expressed either using one of the symbolic values
68    /// of the [`ConstraintStrength`][crate::ConstraintStrength] enumeration, or any positive integer
69    /// value.
70    ///
71    /// Readable | Writeable | Construct Only
72    ///
73    ///
74    /// #### `target`
75    ///  The target of the constraint.
76    ///
77    /// The constraint will set the [`target-attribute`][struct@crate::Constraint#target-attribute]
78    /// property of the target using the [`source-attribute`][struct@crate::Constraint#source-attribute]
79    /// property of the source widget.
80    ///
81    /// Readable | Writeable | Construct Only
82    ///
83    ///
84    /// #### `target-attribute`
85    ///  The attribute of the [`target`][struct@crate::Constraint#target] set by the constraint.
86    ///
87    /// Readable | Writeable | Construct Only
88    ///
89    /// # Implements
90    ///
91    /// [`trait@glib::ObjectExt`]
92    #[doc(alias = "GtkConstraint")]
93    pub struct Constraint(Object<ffi::GtkConstraint, ffi::GtkConstraintClass>);
94
95    match fn {
96        type_ => || ffi::gtk_constraint_get_type(),
97    }
98}
99
100impl Constraint {
101    /// Creates a new constraint representing a relation between a layout
102    /// attribute on a source and a layout attribute on a target.
103    /// ## `target`
104    /// the target of the constraint
105    /// ## `target_attribute`
106    /// the attribute of `target` to be set
107    /// ## `relation`
108    /// the relation equivalence between `target_attribute` and `source_attribute`
109    /// ## `source`
110    /// the source of the constraint
111    /// ## `source_attribute`
112    /// the attribute of `source` to be read
113    /// ## `multiplier`
114    /// a multiplication factor to be applied to `source_attribute`
115    /// ## `constant`
116    /// a constant factor to be added to `source_attribute`
117    /// ## `strength`
118    /// the strength of the constraint
119    ///
120    /// # Returns
121    ///
122    /// the newly created constraint
123    #[doc(alias = "gtk_constraint_new")]
124    pub fn new(
125        target: Option<&impl IsA<ConstraintTarget>>,
126        target_attribute: ConstraintAttribute,
127        relation: ConstraintRelation,
128        source: Option<&impl IsA<ConstraintTarget>>,
129        source_attribute: ConstraintAttribute,
130        multiplier: f64,
131        constant: f64,
132        strength: i32,
133    ) -> Constraint {
134        assert_initialized_main_thread!();
135        unsafe {
136            from_glib_full(ffi::gtk_constraint_new(
137                target.map(|p| p.as_ref()).to_glib_none().0,
138                target_attribute.into_glib(),
139                relation.into_glib(),
140                source.map(|p| p.as_ref()).to_glib_none().0,
141                source_attribute.into_glib(),
142                multiplier,
143                constant,
144                strength,
145            ))
146        }
147    }
148
149    /// Creates a new constraint representing a relation between a layout
150    /// attribute on a target and a constant value.
151    /// ## `target`
152    /// a the target of the constraint
153    /// ## `target_attribute`
154    /// the attribute of `target` to be set
155    /// ## `relation`
156    /// the relation equivalence between `target_attribute` and `constant`
157    /// ## `constant`
158    /// a constant factor to be set on `target_attribute`
159    /// ## `strength`
160    /// the strength of the constraint
161    ///
162    /// # Returns
163    ///
164    /// the newly created constraint
165    #[doc(alias = "gtk_constraint_new_constant")]
166    pub fn new_constant(
167        target: Option<&impl IsA<ConstraintTarget>>,
168        target_attribute: ConstraintAttribute,
169        relation: ConstraintRelation,
170        constant: f64,
171        strength: i32,
172    ) -> Constraint {
173        assert_initialized_main_thread!();
174        unsafe {
175            from_glib_full(ffi::gtk_constraint_new_constant(
176                target.map(|p| p.as_ref()).to_glib_none().0,
177                target_attribute.into_glib(),
178                relation.into_glib(),
179                constant,
180                strength,
181            ))
182        }
183    }
184
185    // rustdoc-stripper-ignore-next
186    /// Creates a new builder-pattern struct instance to construct [`Constraint`] objects.
187    ///
188    /// This method returns an instance of [`ConstraintBuilder`](crate::builders::ConstraintBuilder) which can be used to create [`Constraint`] objects.
189    pub fn builder() -> ConstraintBuilder {
190        ConstraintBuilder::new()
191    }
192
193    /// Retrieves the constant factor added to the source attributes' value.
194    ///
195    /// # Returns
196    ///
197    /// a constant factor
198    #[doc(alias = "gtk_constraint_get_constant")]
199    #[doc(alias = "get_constant")]
200    pub fn constant(&self) -> f64 {
201        unsafe { ffi::gtk_constraint_get_constant(self.to_glib_none().0) }
202    }
203
204    /// Retrieves the multiplication factor applied to the source
205    /// attribute's value.
206    ///
207    /// # Returns
208    ///
209    /// a multiplication factor
210    #[doc(alias = "gtk_constraint_get_multiplier")]
211    #[doc(alias = "get_multiplier")]
212    pub fn multiplier(&self) -> f64 {
213        unsafe { ffi::gtk_constraint_get_multiplier(self.to_glib_none().0) }
214    }
215
216    /// The order relation between the terms of the constraint.
217    ///
218    /// # Returns
219    ///
220    /// a relation type
221    #[doc(alias = "gtk_constraint_get_relation")]
222    #[doc(alias = "get_relation")]
223    pub fn relation(&self) -> ConstraintRelation {
224        unsafe { from_glib(ffi::gtk_constraint_get_relation(self.to_glib_none().0)) }
225    }
226
227    /// Retrieves the [`ConstraintTarget`][crate::ConstraintTarget] used as the source for the
228    /// constraint.
229    ///
230    /// If the source is set to `NULL` at creation, the constraint will use
231    /// the widget using the [`ConstraintLayout`][crate::ConstraintLayout] as the source.
232    ///
233    /// # Returns
234    ///
235    /// the source of the constraint
236    #[doc(alias = "gtk_constraint_get_source")]
237    #[doc(alias = "get_source")]
238    pub fn source(&self) -> Option<ConstraintTarget> {
239        unsafe { from_glib_none(ffi::gtk_constraint_get_source(self.to_glib_none().0)) }
240    }
241
242    /// Retrieves the attribute of the source to be read by the constraint.
243    ///
244    /// # Returns
245    ///
246    /// the source's attribute
247    #[doc(alias = "gtk_constraint_get_source_attribute")]
248    #[doc(alias = "get_source_attribute")]
249    #[doc(alias = "source-attribute")]
250    pub fn source_attribute(&self) -> ConstraintAttribute {
251        unsafe {
252            from_glib(ffi::gtk_constraint_get_source_attribute(
253                self.to_glib_none().0,
254            ))
255        }
256    }
257
258    /// Retrieves the strength of the constraint.
259    ///
260    /// # Returns
261    ///
262    /// the strength value
263    #[doc(alias = "gtk_constraint_get_strength")]
264    #[doc(alias = "get_strength")]
265    pub fn strength(&self) -> i32 {
266        unsafe { ffi::gtk_constraint_get_strength(self.to_glib_none().0) }
267    }
268
269    /// Retrieves the [`ConstraintTarget`][crate::ConstraintTarget] used as the target for
270    /// the constraint.
271    ///
272    /// If the targe is set to `NULL` at creation, the constraint will use
273    /// the widget using the [`ConstraintLayout`][crate::ConstraintLayout] as the target.
274    ///
275    /// # Returns
276    ///
277    /// a [`ConstraintTarget`][crate::ConstraintTarget]
278    #[doc(alias = "gtk_constraint_get_target")]
279    #[doc(alias = "get_target")]
280    pub fn target(&self) -> Option<ConstraintTarget> {
281        unsafe { from_glib_none(ffi::gtk_constraint_get_target(self.to_glib_none().0)) }
282    }
283
284    /// Retrieves the attribute of the target to be set by the constraint.
285    ///
286    /// # Returns
287    ///
288    /// the target's attribute
289    #[doc(alias = "gtk_constraint_get_target_attribute")]
290    #[doc(alias = "get_target_attribute")]
291    #[doc(alias = "target-attribute")]
292    pub fn target_attribute(&self) -> ConstraintAttribute {
293        unsafe {
294            from_glib(ffi::gtk_constraint_get_target_attribute(
295                self.to_glib_none().0,
296            ))
297        }
298    }
299
300    /// Checks whether the constraint is attached to a [`ConstraintLayout`][crate::ConstraintLayout],
301    /// and it is contributing to the layout.
302    ///
303    /// # Returns
304    ///
305    /// `TRUE` if the constraint is attached
306    #[doc(alias = "gtk_constraint_is_attached")]
307    pub fn is_attached(&self) -> bool {
308        unsafe { from_glib(ffi::gtk_constraint_is_attached(self.to_glib_none().0)) }
309    }
310
311    /// Checks whether the constraint describes a relation between an attribute
312    /// on the [`target`][struct@crate::Constraint#target] and a constant value.
313    ///
314    /// # Returns
315    ///
316    /// `TRUE` if the constraint is a constant relation
317    #[doc(alias = "gtk_constraint_is_constant")]
318    pub fn is_constant(&self) -> bool {
319        unsafe { from_glib(ffi::gtk_constraint_is_constant(self.to_glib_none().0)) }
320    }
321
322    /// Checks whether the constraint is a required relation for solving the
323    /// constraint layout.
324    ///
325    /// # Returns
326    ///
327    /// [`true`] if the constraint is required
328    #[doc(alias = "gtk_constraint_is_required")]
329    pub fn is_required(&self) -> bool {
330        unsafe { from_glib(ffi::gtk_constraint_is_required(self.to_glib_none().0)) }
331    }
332}
333
334impl Default for Constraint {
335    fn default() -> Self {
336        glib::object::Object::new::<Self>()
337    }
338}
339
340// rustdoc-stripper-ignore-next
341/// A [builder-pattern] type to construct [`Constraint`] objects.
342///
343/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
344#[must_use = "The builder must be built to be used"]
345pub struct ConstraintBuilder {
346    builder: glib::object::ObjectBuilder<'static, Constraint>,
347}
348
349impl ConstraintBuilder {
350    fn new() -> Self {
351        Self {
352            builder: glib::object::Object::builder(),
353        }
354    }
355
356    /// The constant value to be added to the [`source-attribute`][struct@crate::Constraint#source-attribute].
357    pub fn constant(self, constant: f64) -> Self {
358        Self {
359            builder: self.builder.property("constant", constant),
360        }
361    }
362
363    /// The multiplication factor to be applied to
364    /// the [`source-attribute`][struct@crate::Constraint#source-attribute].
365    pub fn multiplier(self, multiplier: f64) -> Self {
366        Self {
367            builder: self.builder.property("multiplier", multiplier),
368        }
369    }
370
371    /// The order relation between the terms of the constraint.
372    pub fn relation(self, relation: ConstraintRelation) -> Self {
373        Self {
374            builder: self.builder.property("relation", relation),
375        }
376    }
377
378    /// The source of the constraint.
379    ///
380    /// The constraint will set the [`target-attribute`][struct@crate::Constraint#target-attribute]
381    /// property of the target using the [`source-attribute`][struct@crate::Constraint#source-attribute]
382    /// property of the source.
383    pub fn source(self, source: &impl IsA<ConstraintTarget>) -> Self {
384        Self {
385            builder: self.builder.property("source", source.clone().upcast()),
386        }
387    }
388
389    /// The attribute of the [`source`][struct@crate::Constraint#source] read by the
390    /// constraint.
391    pub fn source_attribute(self, source_attribute: ConstraintAttribute) -> Self {
392        Self {
393            builder: self.builder.property("source-attribute", source_attribute),
394        }
395    }
396
397    /// The strength of the constraint.
398    ///
399    /// The strength can be expressed either using one of the symbolic values
400    /// of the [`ConstraintStrength`][crate::ConstraintStrength] enumeration, or any positive integer
401    /// value.
402    pub fn strength(self, strength: i32) -> Self {
403        Self {
404            builder: self.builder.property("strength", strength),
405        }
406    }
407
408    /// The target of the constraint.
409    ///
410    /// The constraint will set the [`target-attribute`][struct@crate::Constraint#target-attribute]
411    /// property of the target using the [`source-attribute`][struct@crate::Constraint#source-attribute]
412    /// property of the source widget.
413    pub fn target(self, target: &impl IsA<ConstraintTarget>) -> Self {
414        Self {
415            builder: self.builder.property("target", target.clone().upcast()),
416        }
417    }
418
419    /// The attribute of the [`target`][struct@crate::Constraint#target] set by the constraint.
420    pub fn target_attribute(self, target_attribute: ConstraintAttribute) -> Self {
421        Self {
422            builder: self.builder.property("target-attribute", target_attribute),
423        }
424    }
425
426    // rustdoc-stripper-ignore-next
427    /// Build the [`Constraint`].
428    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
429    pub fn build(self) -> Constraint {
430        assert_initialized_main_thread!();
431        self.builder.build()
432    }
433}