Skip to main content

i_slint_compiler/passes/
check_drag_area.rs

1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4//! This pass warns about a `DragArea` that never starts a drag, either because
5//! it permits no drag action (none of `allow-copy`, `allow-move`, or
6//! `allow-link` is set) or because its `data` payload is left unset.
7
8use crate::diagnostics::BuildDiagnostics;
9use crate::object_tree::{Component, ElementRc};
10
11const ALLOW_PROPERTIES: [&str; 3] = ["allow-copy", "allow-move", "allow-link"];
12
13pub fn check_drag_area(component: &Component, diag: &mut BuildDiagnostics) {
14    crate::object_tree::recurse_elem_including_sub_components_no_borrow(
15        component,
16        &(),
17        &mut |elem, _| {
18            if !is_drag_area(elem) {
19                return;
20            }
21            if !ALLOW_PROPERTIES.iter().any(|prop| elem.borrow().is_property_set(prop)) {
22                diag.push_warning(
23                    "This 'DragArea' permits no drag action and will never start a drag; \
24                     set 'allow-copy', 'allow-move', or 'allow-link' to true"
25                        .into(),
26                    &*elem.borrow(),
27                );
28            }
29            if !elem.borrow().is_property_set("data") {
30                diag.push_warning(
31                    "This 'DragArea' has no 'data' set and will never start a drag; \
32                     add a binding for the 'data' property"
33                        .into(),
34                    &*elem.borrow(),
35                );
36            }
37        },
38    );
39}
40
41fn is_drag_area(e: &ElementRc) -> bool {
42    e.borrow().builtin_type().is_some_and(|bt| bt.name.as_str() == "DragArea")
43}