leftwm-layouts 0.9.1

Provides customizable layouts for list-based dynamic tiling window managers
Documentation
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use super::Rect;

use serde::{Deserialize, Serialize};
use std::str::FromStr;

/// Represents the four different direction where we can search for a neighbor
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub enum Direction {
    #[default]
    /// Search for neighbor starting from the top left of the current rect
    /// This is the default value.
    ///
    /// ```txt
    ///    North
    ///
    /// +---------+
    /// | ^       |
    /// |         |
    /// |         |
    /// +---------+
    /// ```
    North,

    /// Search for neighbor starting from the right top of the current rect
    ///
    /// ```txt
    ///    East
    ///
    /// +---------+
    /// |       > |
    /// |         |
    /// |         |
    /// +---------+
    /// ```
    East,

    /// Search for neighbor starting from the bottom left of the current rect
    ///
    /// ```txt
    ///    South
    /// +---------+
    /// |         |
    /// |         |
    /// | V       |
    /// +---------+
    ///
    /// ```
    South,

    /// Search for neighbor starting from the left top of the current rect
    ///
    /// ```txt
    ///     West
    ///
    /// +---------+
    /// | <       |
    /// |         |
    /// |         |
    /// +---------+
    /// ```
    West,
}

impl FromStr for Direction {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "North" => Ok(Direction::North),
            "South" => Ok(Direction::South),
            "East" => Ok(Direction::East),
            "West" => Ok(Direction::West),
            _ => Err(()),
        }
    }
}

// Find the north neighbor starting from a given `Rect` with index `current` in an array of
// [`Rect`].
fn find_north(rects: &[Rect], current: usize) -> Option<usize> {
    let current_rect = rects.get(current).or(None)?;

    // We are all the way up, no neighbor available
    if current_rect.top_edge() <= 0 {
        return None;
    }

    let mut nearest_rect: Option<usize> = None;
    let mut min_x: Option<i32> = None;
    let mut min_y: Option<i32> = None;

    for (i, r) in rects.iter().enumerate() {
        if r == current_rect || // skip current rect
        r.right_edge() - 1 < current_rect.left_edge() || // skip too right
        r.left_edge() + 1 > current_rect.right_edge() || // skip too left
        r.top_edge() + 1 > current_rect.bottom_edge()
        // skip too low
        {
            continue;
        }

        let x_distance = current_rect.left_edge() - r.right_edge();
        let y_distance = current_rect.top_edge() - r.bottom_edge();

        find_nearest_rect(
            &mut min_x,
            &mut min_y,
            &mut nearest_rect,
            x_distance,
            y_distance,
            i,
            true,
        );
    }

    nearest_rect
}

// Find the east neighbor starting from a given `Rect` with index `current` in an array of
// [`Rect`].
fn find_east(rects: &[Rect], current: usize, display_width: u32) -> Option<usize> {
    let current_rect = rects.get(current).or(None)?;

    // We are all the way right, no neighbor available
    if current_rect.right_edge() >= display_width as i32 {
        return None;
    }

    let mut nearest_rect: Option<usize> = None;
    let mut min_x: Option<i32> = None;
    let mut min_y: Option<i32> = None;

    for (i, r) in rects.iter().enumerate() {
        if r == current_rect || // skip current rect
        r.right_edge() - 1 < current_rect.right_edge() || // skip too left
        r.bottom_edge() - 1 < current_rect.top_edge() || // skip too high
        r.top_edge() + 1 > current_rect.bottom_edge()
        // skip too low
        {
            continue;
        }

        let x_distance = r.left_edge() - current_rect.right_edge();
        let y_distance = r.top_edge() - current_rect.bottom_edge();

        find_nearest_rect(
            &mut min_x,
            &mut min_y,
            &mut nearest_rect,
            x_distance,
            y_distance,
            i,
            false,
        );
    }

    nearest_rect
}

// Find the south neighbor starting from a given `Rect` with index `current` in an array of
// [`Rect`].
fn find_south(rects: &[Rect], current: usize, display_height: u32) -> Option<usize> {
    let current_rect = rects.get(current).or(None)?;

    // We are at the bottom, no neighbor available
    if current_rect.y + current_rect.h as i32 >= display_height as i32 {
        return None;
    }

    let mut nearest_rect: Option<usize> = None;
    let mut min_x: Option<i32> = None;
    let mut min_y: Option<i32> = None;

    for (i, r) in rects.iter().enumerate() {
        if r == current_rect || // skip current rect
        r.right_edge() - 1 < current_rect.left_edge() || // skip too left
        r.left_edge() + 1 > current_rect.right_edge() || // skip too right
        r.bottom_edge() - 1 < current_rect.top_edge()
        // skip too high
        {
            // skip current rect
            continue;
        }

        let x_distance = current_rect.left_edge() - r.right_edge();
        let y_distance = r.top_edge() - current_rect.bottom_edge();

        find_nearest_rect(
            &mut min_x,
            &mut min_y,
            &mut nearest_rect,
            x_distance,
            y_distance,
            i,
            true,
        );
    }

    nearest_rect
}

// Find the west neighbor starting from a given `Rect` with index `current` in an array of
// [`Rect`].
fn find_west(rects: &[Rect], current: usize) -> Option<usize> {
    let current_rect = rects.get(current).or(None)?;

    // We are all the way left; no neighbor available
    if current_rect.left_edge() <= 0 {
        return None;
    }

    let mut nearest_rect: Option<usize> = None;
    let mut min_x: Option<i32> = None;
    let mut min_y: Option<i32> = None;

    for (i, r) in rects.iter().enumerate() {
        if r == current_rect || // skip current rect
         r.left_edge() + 1 > current_rect.right_edge() || // skip too right
         r.bottom_edge() - 1 < current_rect.top_edge() || // skip too high
         r.top_edge() + 1 > current_rect.bottom_edge()
        // skip too low
        {
            // skip current rect
            continue;
        }

        let x_distance = current_rect.left_edge() - r.right_edge();
        let y_distance = r.top_edge() - current_rect.bottom_edge();

        find_nearest_rect(
            &mut min_x,
            &mut min_y,
            &mut nearest_rect,
            x_distance,
            y_distance,
            i,
            false,
        );
    }

    nearest_rect
}

// Find the nearest `Rect`. If updown is true, evaluate y_distance and then x_distance. If updown
// is false, evaluate x_distance and then y_distance.
fn find_nearest_rect(
    min_x: &mut Option<i32>,
    min_y: &mut Option<i32>,
    nearest_rect: &mut Option<usize>,
    x_distance: i32,
    y_distance: i32,
    index: usize,
    updown: bool,
) {
    if min_x.is_none() {
        *min_x = Some(x_distance);
        *nearest_rect = Some(index);
    }

    if min_y.is_none() {
        *min_y = Some(y_distance);
        *nearest_rect = Some(index);
    }

    if updown {
        if y_distance < min_y.unwrap() {
            // take the nearest up/down
            *min_y = Some(y_distance);
            *nearest_rect = Some(index);
        } else if y_distance == min_y.unwrap() && x_distance < min_x.unwrap() {
            // take the left most
            *min_x = Some(x_distance);
            *nearest_rect = Some(index);
        }
    } else if x_distance < min_x.unwrap() {
        // take the nearest left/right
        *min_x = Some(x_distance);
        *nearest_rect = Some(index);
    } else if x_distance == min_x.unwrap() && y_distance < min_y.unwrap() {
        // take the higher
        *min_y = Some(y_distance);
        *nearest_rect = Some(index);
    }
}

impl Direction {
    /// Find the neighbor in a given direction (`North`, `East`, `South`, `West`), starting from a
    /// given `Rect` identified by the index `current` in an array of [`Rect`]
    pub fn find_neighbor(
        rects: &[Rect],
        current: usize,
        direction: Direction,
        container: &Rect,
    ) -> Option<usize> {
        if current >= rects.len() {
            return None;
        }

        match direction {
            Direction::North => find_north(rects, current),
            Direction::East => find_east(rects, current, container.w),
            Direction::South => find_south(rects, current, container.h),
            Direction::West => find_west(rects, current),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::geometry::{Direction, Rect};

    const CONTAINER: Rect = Rect {
        x: 0,
        y: 0,
        w: 600,
        h: 600,
    };

    // Test layout
    // +-----------------+
    // |+---+ +---+ +---+|
    // || 0 | | 3 | | 4 ||
    // |+---+ +---+ +---+|
    // |+---+ +---+ +---+|
    // || 1 | | 6 | |   ||
    // |+---+ +---+ |   ||
    // |+---+       | 5 ||
    // || 2 |       |   ||
    // |+---+       +---+|
    // +-----------------+
    const ARRAY: [Rect; 7] = [
        Rect {
            x: 0,
            y: 0,
            w: 200,
            h: 200,
        },
        Rect {
            x: 0,
            y: 200,
            w: 200,
            h: 200,
        },
        Rect {
            x: 0,
            y: 400,
            w: 200,
            h: 200,
        },
        Rect {
            x: 200,
            y: 0,
            w: 200,
            h: 200,
        },
        Rect {
            x: 400,
            y: 0,
            w: 200,
            h: 200,
        },
        Rect {
            x: 400,
            y: 200,
            w: 200,
            h: 400,
        },
        Rect {
            x: 200,
            y: 200,
            w: 200,
            h: 400,
        },
    ];

    #[test]
    fn north_neighbor() {
        let res = Direction::find_neighbor(&ARRAY, 0, Direction::North, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 1, Direction::North, &CONTAINER);
        assert_eq!(res, Some(0));
        let res = Direction::find_neighbor(&ARRAY, 2, Direction::North, &CONTAINER);
        assert_eq!(res, Some(1));
        let res = Direction::find_neighbor(&ARRAY, 3, Direction::North, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 4, Direction::North, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 5, Direction::North, &CONTAINER);
        assert_eq!(res, Some(4));
        let res = Direction::find_neighbor(&ARRAY, 6, Direction::North, &CONTAINER);
        assert_eq!(res, Some(3));
    }

    #[test]
    fn east_neighbor() {
        let res = Direction::find_neighbor(&ARRAY, 0, Direction::East, &CONTAINER);
        assert_eq!(res, Some(3));
        let res = Direction::find_neighbor(&ARRAY, 1, Direction::East, &CONTAINER);
        assert_eq!(res, Some(6));
        let res = Direction::find_neighbor(&ARRAY, 2, Direction::East, &CONTAINER);
        assert_eq!(res, Some(6));
        let res = Direction::find_neighbor(&ARRAY, 3, Direction::East, &CONTAINER);
        assert_eq!(res, Some(4));
        let res = Direction::find_neighbor(&ARRAY, 4, Direction::East, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 5, Direction::East, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 6, Direction::East, &CONTAINER);
        assert_eq!(res, Some(5));
    }

    #[test]
    fn south_neighbor() {
        let res = Direction::find_neighbor(&ARRAY, 0, Direction::South, &CONTAINER);
        assert_eq!(res, Some(1));
        let res = Direction::find_neighbor(&ARRAY, 1, Direction::South, &CONTAINER);
        assert_eq!(res, Some(2));
        let res = Direction::find_neighbor(&ARRAY, 2, Direction::South, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 3, Direction::South, &CONTAINER);
        assert_eq!(res, Some(6));
        let res = Direction::find_neighbor(&ARRAY, 4, Direction::South, &CONTAINER);
        assert_eq!(res, Some(5));
        let res = Direction::find_neighbor(&ARRAY, 5, Direction::South, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 6, Direction::South, &CONTAINER);
        assert_eq!(res, None);
    }

    #[test]
    fn west_neighbor() {
        let res = Direction::find_neighbor(&ARRAY, 0, Direction::West, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 1, Direction::West, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 2, Direction::West, &CONTAINER);
        assert_eq!(res, None);
        let res = Direction::find_neighbor(&ARRAY, 3, Direction::West, &CONTAINER);
        assert_eq!(res, Some(0));
        let res = Direction::find_neighbor(&ARRAY, 4, Direction::West, &CONTAINER);
        assert_eq!(res, Some(3));
        let res = Direction::find_neighbor(&ARRAY, 5, Direction::West, &CONTAINER);
        assert_eq!(res, Some(6));
        let res = Direction::find_neighbor(&ARRAY, 6, Direction::West, &CONTAINER);
        assert_eq!(res, Some(1));
    }
}