[go: up one dir, main page]

gtk4/
constraint_layout.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5use crate::{ffi, prelude::*, Constraint, ConstraintLayout, Widget};
6
7impl ConstraintLayout {
8    #[doc(alias = "gtk_constraint_layout_add_constraints_from_descriptionv")]
9    #[doc(alias = "gtk_constraint_layout_add_constraints_from_description")]
10    #[doc(alias = "add_constraints_from_descriptionv")]
11    pub fn add_constraints_from_description<'a, W: IsA<Widget>>(
12        &self,
13        lines: impl IntoStrV,
14        hspacing: i32,
15        vspacing: i32,
16        views: impl IntoIterator<Item = (&'a str, &'a W)>,
17    ) -> Result<Vec<Constraint>, glib::Error> {
18        unsafe {
19            let mut err = std::ptr::null_mut();
20            let hash_table = glib::ffi::g_hash_table_new_full(
21                Some(glib::ffi::g_str_hash),
22                Some(glib::ffi::g_str_equal),
23                Some(glib::ffi::g_free),
24                None,
25            );
26
27            for (key, widget) in views {
28                let key_ptr: *mut libc::c_char = key.to_glib_full();
29                glib::ffi::g_hash_table_insert(
30                    hash_table,
31                    key_ptr as *mut _,
32                    widget.as_ptr() as *mut _,
33                );
34            }
35
36            lines.run_with_strv(|lines| {
37                let out = ffi::gtk_constraint_layout_add_constraints_from_descriptionv(
38                    self.to_glib_none().0,
39                    lines.as_ptr() as *const _,
40                    lines.len() as _,
41                    hspacing,
42                    vspacing,
43                    hash_table,
44                    &mut err,
45                );
46
47                glib::ffi::g_hash_table_unref(hash_table);
48
49                if !err.is_null() {
50                    Err(from_glib_full(err))
51                } else {
52                    Ok(FromGlibPtrContainer::from_glib_container(out))
53                }
54            })
55        }
56    }
57}