i_slint_core/window/
popup.rs

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
333
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

//! Pupup window handling helpers

use crate::lengths::{LogicalPoint, LogicalRect, LogicalSize};

/// A collection of data that might influence the palcement of a `Popup`.
pub enum Placement {
    /// Request a fixed position
    Fixed(LogicalRect),
}

/// Find a placement for the `Popup`, using the provided `Placement`.
/// When a `clip_region` is provided, then the `Popup` will stay within those bounds.
/// The `clip_region` typically is the window or the screen the window is on.
pub fn place_popup(placement: Placement, clip_region: &Option<LogicalRect>) -> LogicalRect {
    match placement {
        Placement::Fixed(rect) => {
            let clip = clip_region.unwrap_or_else(|| rect.clone());
            if clip.contains_rect(&rect) {
                rect
            } else {
                let size = LogicalSize::new(
                    crate::Coord::min(rect.size.width, clip.size.width),
                    crate::Coord::min(rect.size.height, clip.size.height),
                );
                let origin = LogicalPoint::new(
                    rect.origin
                        .x
                        .clamp(clip.origin.x, clip.origin.x + clip.size.width - size.width),
                    rect.origin
                        .y
                        .clamp(clip.origin.y, clip.origin.y + clip.size.height - size.height),
                );
                LogicalRect::new(origin, size)
            }
        }
    }
}

#[cfg(test)]
fn r(x: i32, y: i32, w: i32, h: i32) -> LogicalRect {
    LogicalRect::new(LogicalPoint::new(x as f32, y as f32), LogicalSize::new(w as f32, h as f32))
}

#[cfg(test)]
#[track_caller]
fn fixed_placement(input: LogicalRect, expected: LogicalRect, clip: Option<LogicalRect>) {
    eprintln!("fixed: {input:?}, clip({clip:?}) => {expected:?}");
    let result = place_popup(Placement::Fixed(input.clone()), &clip);
    if let Some(clip) = clip {
        clip.contains_rect(&result);
    }
    assert_eq!(result, expected);
}

#[test]
fn test_place_popup_fixed_unclipped() {
    let data = r(5, 5, 100, 100);
    fixed_placement(data.clone(), data, None);

    let data = r(5, -20, 100, 100);
    fixed_placement(data.clone(), data, None);
    let data = r(2000, -20, 100, 100);
    fixed_placement(data.clone(), data, None);
    let data = r(2000, 5, 100, 100);
    fixed_placement(data.clone(), data, None);
    let data = r(2000, 2000, 100, 100);
    fixed_placement(data.clone(), data, None);
    let data = r(5, 2000, 100, 100);
    fixed_placement(data.clone(), data, None);
    let data = r(-20, 2000, 100, 100);
    fixed_placement(data.clone(), data, None);
    let data = r(-20, 5, 100, 100);
    fixed_placement(data.clone(), data, None);
    let data = r(-20, -20, 100, 100);
    fixed_placement(data.clone(), data, None);

    let data = r(-20, -20, 2000, 2000);
    fixed_placement(data.clone(), data, None);
}

#[test]
fn test_place_popup_fixed_clipped() {
    for (clip_offset_x, clip_offset_y) in [
        (-200, -200),
        (-200, 0),
        (-200, 200),
        (0, -200),
        (0, 0),
        (0, 200),
        (200, -200),
        (200, 0),
        (200, 200),
    ] {
        for (clip_width, clip_height) in [(110, 110), (800, 600)] {
            let clip = r(clip_offset_x, clip_offset_y, clip_width, clip_height);

            let x_w = clip_offset_x - 10;
            let x_c = clip_offset_x + 5;
            let x_e = clip_offset_x + clip_width - 80;

            let y_n = clip_offset_y - 10;
            let y_c = clip_offset_y + 5;
            let y_s = clip_offset_y + clip_height - 80;

            let x_min = clip_offset_x;
            let x_max = clip_offset_x + clip_width;
            let y_min = clip_offset_y;
            let y_max = clip_offset_y + clip_height;

            assert!(clip_width > 105 && clip_width < 1000);
            assert!(clip_height > 105 && clip_height < 1000);

            // smaller, inside
            fixed_placement(r(x_c, y_c, 100, 100), r(x_c, y_c, 100, 100), Some(clip.clone()));

            // smaller, partial outside
            fixed_placement(r(x_c, y_n, 100, 100), r(x_c, y_min, 100, 100), Some(clip.clone()));
            fixed_placement(
                r(x_e, y_n, 100, 100),
                r(x_max - 100, y_min, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_e, y_c, 100, 100),
                r(x_max - 100, y_c, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_e, y_s, 100, 100),
                r(x_max - 100, y_max - 100, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_c, y_s, 100, 100),
                r(x_c, y_max - 100, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_c, y_s, 100, 100),
                r(x_c, y_max - 100, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_w, y_s, 100, 100),
                r(x_min, y_max - 100, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(r(x_w, y_c, 100, 100), r(x_min, y_c, 100, 100), Some(clip.clone()));
            fixed_placement(r(x_w, y_n, 100, 100), r(x_min, y_min, 100, 100), Some(clip.clone()));

            // smaller, totally outside
            fixed_placement(r(x_c, -2000, 100, 100), r(x_c, y_min, 100, 100), Some(clip.clone()));
            fixed_placement(
                r(2000, -2000, 100, 100),
                r(x_max - 100, y_min, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(
                r(2000, y_c, 100, 100),
                r(x_max - 100, y_c, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(
                r(2000, 2000, 100, 100),
                r(x_max - 100, y_max - 100, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_c, 2000, 100, 100),
                r(x_c, y_max - 100, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(
                r(-2000, 2000, 100, 100),
                r(x_min, y_max - 100, 100, 100),
                Some(clip.clone()),
            );
            fixed_placement(r(-2000, y_c, 100, 100), r(x_min, y_c, 100, 100), Some(clip.clone()));
            fixed_placement(
                r(-2000, -2000, 100, 100),
                r(x_min, y_min, 100, 100),
                Some(clip.clone()),
            );

            // matching size, covering
            fixed_placement(
                r(x_min, y_min, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );

            // matching size, overlapping
            fixed_placement(
                r(x_c, y_c, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_c, y_n, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );

            fixed_placement(
                r(x_e, y_n, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_e, y_c, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_e, y_s, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_c, y_s, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_w, y_s, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_w, y_c, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_w, y_n, clip_width, clip_height),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );

            // too big, overlapping
            fixed_placement(
                r(x_c, y_c, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_c, y_n, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_e, y_n, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_e, y_c, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_e, y_s, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_c, y_s, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_w, y_s, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_w, y_c, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_w, y_n, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );

            // too big, outside
            fixed_placement(
                r(x_c, -3000, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(3000, -3000, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(3000, y_c, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(3000, 3000, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(x_c, 3000, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(-3000, 3000, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(-3000, y_c, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
            fixed_placement(
                r(-3000, -3000, clip_width + 5, clip_height + 5),
                r(x_min, y_min, clip_width, clip_height),
                Some(clip.clone()),
            );
        }
    }
}