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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use std::cmp;

use serde::{Deserialize, Serialize};

use crate::geometry::{Flip, Reserve, Rotation, Size, Split};

use super::defaults::{
    center_main, center_main_balanced, center_main_fluid, dwindle, even_horizontal, even_vertical,
    fibonacci, grid, main_and_deck, main_and_horizontal_stack, main_and_vert_stack, monocle,
    right_main_and_vert_stack,
};

const DEFAULT_MAIN_SIZE_CHANGE_PIXEL: i32 = 50;
const DEFAULT_MAIN_SIZE_CHANGE_PERCENTAGE: i32 = 5;

/// A helper struct that represents a set of layouts and provides
/// convenience methods
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Layouts {
    pub layouts: Vec<Layout>,
}

impl Eq for Layouts {}

impl Layouts {
    pub fn get(&self, name: &str) -> Option<&Layout> {
        self.layouts.iter().find(|&l| l.name.as_str() == name)
    }

    pub fn get_mut<'a>(&'a mut self, name: &str) -> Option<&'a mut Layout> {
        self.layouts.iter_mut().find(|l| l.name.as_str() == name)
    }

    pub fn names(&self) -> Vec<String> {
        self.layouts.iter().map(|x| x.name.clone()).collect()
    }

    pub fn len(&self) -> usize {
        self.layouts.len()
    }

    pub fn is_empty(&self) -> bool {
        self.layouts.is_empty()
    }

    pub fn get_index(&self, name: &str) -> Option<usize> {
        self.layouts.iter().position(|l| l.name.as_str() == name)
    }
}

impl Default for Layouts {
    fn default() -> Self {
        Self {
            layouts: vec![
                even_horizontal(),
                even_vertical(),
                monocle(),
                grid(),
                main_and_vert_stack(),
                main_and_horizontal_stack(),
                right_main_and_vert_stack(),
                fibonacci(),
                dwindle(),
                main_and_deck(),
                center_main(),
                center_main_balanced(),
                center_main_fluid(),
            ],
        }
    }
}

type LayoutName = String;

/// Describes a layout or pattern in which tiles (windows) will be arranged.
/// The [`Layout`] allows to describe various types of "fixed" layouts used by a dynamic tiling manager.
/// Those include layouts like `MainAndStack`, `Fibonacci`, `Dwindle`, `CenterMain`, etc.
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(default)]
pub struct Layout {
    /// Name and identifier of the layout.
    /// This is user chosen and no two layouts can have the same name.
    pub name: LayoutName,

    /// Flips the entire result of tiles as a whole if specified to be anything other than [`Flip::None`]
    pub flip: Flip,

    /// Rotate the entire result of tiles as a whole, if specified to be anything other than [`Rotation::North`]
    pub rotate: Rotation,

    /// Defines the layouts behavior if certain "columns" (eg. main, stack, or second-stack) are empty.
    /// See [`Reserve`] for more information.
    pub reserve: Reserve,

    /// Configuration concerning the [`Main`], [`Stack`], and [`SecondStack`] columns.
    /// See [`Columns`] for more information.
    pub columns: Columns,
}

impl Layout {
    /// Returns `true` if the layout must be considered a `Monocle` layout.
    ///
    /// The `Monocle` layout is a special layout that always consists
    /// of 0 or 1 windows. If there is a window, it is shown full screen.
    pub fn is_monocle(&self) -> bool {
        self.columns.main.is_none()
            && self.columns.second_stack.is_none()
            && self.columns.stack.split.is_none()
    }

    /// Returns `true` if the layout must be considered a `MainAndDeck` layout.
    ///
    /// The `MainAndDeck` layout is a special layout that always consists
    /// of 0, 1 or 2 windows.
    pub fn is_main_and_deck(&self) -> bool {
        match &self.columns.main {
            Some(main) => {
                self.columns.second_stack.is_none()
                    && main.split.is_none()
                    && self.columns.stack.split.is_none()
            }
            None => false,
        }
    }

    // Get the size of the [`Main`] column,
    // may return [`None`] if there is no [`Main`] column.
    pub fn main_size(&self) -> Option<Size> {
        self.columns.main.as_ref().map(|m| m.size)
    }

    // Get the amount of window spaces of the [`Main`] column,
    // may return [`None`] if there is no [`Main`] column.
    pub fn main_window_count(&self) -> Option<usize> {
        self.columns.main.as_ref().map(|m| m.count)
    }

    /// Set the [`Size`] of the [`Main`] column to a specific value
    pub fn set_main_size(&mut self, size: Size) {
        if let Some(main) = self.columns.main.as_mut() {
            main.size = size;
        }
    }

    /// Increase the [`Size`] of the [`Main`] column, but to no
    /// larger value than what is set in `upper_bound`.
    ///
    /// The column is increased by a default amount,
    /// either [`DEFAULT_MAIN_SIZE_CHANGE_PIXEL`] or
    /// [`DEFAULT_MAIN_SIZE_CHANGE_PERCENTAGE`] depending
    /// on whether the current [`Size`] is a [`Size::Pixel`] or [`Size::Ratio`].
    ///
    /// If the current layout has no [`Main`] column, nothing happens
    pub fn increase_main_size(&mut self, upper_bound: i32) {
        if let Some(main) = self.columns.main.as_mut() {
            match main.size {
                Size::Pixel(_) => {
                    self.change_main_size(DEFAULT_MAIN_SIZE_CHANGE_PIXEL, upper_bound);
                }
                Size::Ratio(_) => {
                    self.change_main_size(DEFAULT_MAIN_SIZE_CHANGE_PERCENTAGE, upper_bound);
                }
            };
        };
    }

    /// Decrease the [`Size`] of the [`Main`] column, but to no
    /// smaller value than zero.
    ///
    /// The column is decreased by a default amount,
    /// either [`DEFAULT_MAIN_SIZE_CHANGE_PIXEL`] or
    /// [`DEFAULT_MAIN_SIZE_CHANGE_PERCENTAGE`] depending
    /// on whether the current [`Size`] is a [`Size::Pixel`] or [`Size::Ratio`].
    ///
    /// If the current layout has no [`Main`] column, nothing happens
    pub fn decrease_main_size(&mut self) {
        if let Some(main) = self.columns.main.as_mut() {
            // note: upper bound doesn't matter when we're decreasing,
            // so just set it to i32::MAX
            match main.size {
                Size::Pixel(_) => self.change_main_size(-DEFAULT_MAIN_SIZE_CHANGE_PIXEL, i32::MAX),
                Size::Ratio(_) => {
                    self.change_main_size(-DEFAULT_MAIN_SIZE_CHANGE_PERCENTAGE, i32::MAX);
                }
            };
        };
    }

    /// Change the [`Size`] of the [`Main`] column by a `delta` value.
    ///
    /// The `delta` value can be positive or negative and is interpreted
    /// as either [`Size::Pixel`] or [`Size::Ratio`] based on the current
    /// [`Size`] of the [`Main`] column.
    ///
    /// When the current [`Size`] is a [`Size::Pixel`], the delta is
    /// interpreted as a pixel value.
    ///
    /// ```
    /// use leftwm_layouts::Layout;
    /// use leftwm_layouts::geometry::Size;
    ///
    /// let mut layout = Layout::default();
    /// layout.set_main_size(Size::Pixel(200));
    /// layout.change_main_size(100, 500);
    /// assert_eq!(Size::Pixel(300), layout.columns.main.unwrap().size);
    /// ```
    ///
    /// When the current [`Size`] is a [`Size::Ratio`], the delta is
    /// interpreted as a percentage value and converted into a ratio
    /// (i.e. `5` (percent) => `Size::Ratio(0.05)`).
    ///
    /// ```
    /// use leftwm_layouts::Layout;
    /// use leftwm_layouts::geometry::Size;
    ///
    /// let mut layout = Layout::default();
    /// layout.set_main_size(Size::Ratio(0.5));
    /// layout.change_main_size(5, 500);
    /// assert_eq!(Size::Ratio(0.55), layout.columns.main.unwrap().size);
    /// ```
    pub fn change_main_size(&mut self, delta: i32, upper_bound: i32) {
        if let Some(main) = self.columns.main.as_mut() {
            main.size = match main.size {
                Size::Pixel(px) => Size::Pixel(cmp::max(0, cmp::min(upper_bound, px + delta))),
                Size::Ratio(ratio) => {
                    Size::Ratio(f32::max(0.0, f32::min(1.0, ratio + (delta as f32 * 0.01))))
                }
            }
        }
    }

    //pub fn change_main_size_enum(&mut self, amount: Size, upper_bound: i32) {
    //    if let Some(main) = self.columns.main.as_mut() {
    //        match (main.size, amount) {
    //            (Size::Pixel(_), Size::Pixel(px)) => self.change_main_size(px, upper_bound),
    //            (Size::Pixel(_), Size::Ratio(_)) => todo!(), // ?
    //            (Size::Ratio(_), Size::Pixel(_)) => todo!(), // ?
    //            (Size::Ratio(_), Size::Ratio(ratio)) => {
    //                self.change_main_size((ratio * 100.0).round() as i32, upper_bound)
    //            }
    //        }
    //    };
    //    amount.into_absolute(upper_bound.unsigned_abs());
    //}

    // Set the amount of main windows to a specific amount
    pub fn set_main_window_count(&mut self, count: usize) {
        if let Some(main) = self.columns.main.as_mut() {
            main.count = cmp::max(0, count);
        }
    }

    // Increase the amount of main windows by 1
    pub fn increase_main_window_count(&mut self) {
        if let Some(main) = self.columns.main.as_mut() {
            main.count = main.count.saturating_add(1);
        }
    }

    // Decrease the amount of main windows by 1
    pub fn decrease_main_window_count(&mut self) {
        if let Some(main) = self.columns.main.as_mut() {
            main.count = main.count.saturating_sub(1);
        }
    }

    // Rotate the layout as a whole.
    // Rotates clockwise if `true` and counter-clockwise if `false`.
    pub fn rotate(&mut self, clockwise: bool) {
        self.rotate = if clockwise {
            self.rotate.clockwise()
        } else {
            self.rotate.counter_clockwise()
        }
    }

    pub fn check(&self) {
        if self.columns.second_stack.is_some() && self.columns.main.is_none() {
            // warning -> alternate_stack is ignored -> 1-column
        }
    }

    pub fn update_defaults(custom: &Vec<Layout>) -> Vec<Layout> {
        let mut layouts = Layouts::default().layouts;
        for custom_layout in custom {
            layouts.push(custom_layout.clone());
        }
        layouts
    }
}

impl Default for Layout {
    fn default() -> Self {
        Self {
            name: String::from("Default"),
            flip: Flip::None,
            rotate: Rotation::North,
            reserve: Reserve::None,
            columns: Columns::default(),
        }
    }
}

/// Describes the columns of a layout. There are only 3 columns which are a fixed part of
/// `leftwm_layouts`, those are `main`, `stack`, and `second_stack`.
///
/// ```txt
/// +------+------+------+
/// |      |      |      |
/// |      |      |      |
/// |      |      |      |
/// +------+------+------+
///  stack   main  second
///                stack
/// ```
///
/// ## Modifiers
/// Modifiers like [`Flip`] and [`Rotation`] are applied only to the columns themselves and not their contents.
///
/// For example, if you wish for the `Stack` to be on the left side instead of the right side
/// in a `MainAndStack` layout configuration, the [`Flip`] property could be set to [`Flip::Vertical`],
/// which results in the columns being flipped, **but not their contents**.
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(default)]
pub struct Columns {
    /// How the columns should be flipped, does not apply to their contents
    pub flip: Flip,

    /// How the columns should be rotated, does not apply to their contents
    pub rotate: Rotation,

    /// Configurations concerning the `main` column.
    /// This can be set to [`None`], in which case the layout
    /// will not have a main column. For example, in single-column
    /// layouts like `EvenVertical`, `Monocle`, etc.
    /// See [`Main`] for more information.
    pub main: Option<Main>,

    /// Configurations concerning the `stack` column.
    /// Other than `main` and `second_stack`, this column is always present.
    /// See [`Stack`] for more information.
    pub stack: Stack,

    /// Configurations concerning the `second_stack` column.
    /// This can be set to [`None`], in which case the layout
    /// is going to be a two-column layout like `MainAndStack`, `Fibonacci`, etc.
    ///
    /// *Note: If this is present but `main` is absent, it is condiered an invalid
    /// layout configuration. The `second_stack` configuration may be ignored if
    /// `main` is [`None`]*
    /// See [`SecondStack`] for more information.
    pub second_stack: Option<SecondStack>,
}

impl Default for Columns {
    fn default() -> Self {
        Self {
            flip: Flip::default(),
            rotate: Rotation::default(),
            main: Some(Main::default()),
            stack: Stack::default(),
            second_stack: None,
        }
    }
}

/// Configurations concerning the `main` column
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(default)]
pub struct Main {
    /// The default amount of windows to occupy the `main` column (default: `1`)
    pub count: usize,

    /// The default size of the `main` column (default: `50%`)
    pub size: Size,

    /// Flip modifier to apply only to the `main` columns' contents
    pub flip: Flip,

    /// Rotation modifier to apply only to the `main` columns' contents
    pub rotate: Rotation,

    /// How tiles (windows) inside the `main` column should be split up,
    /// when there is more than one.
    ///
    /// *Note: This can be set to [`None`], in which case the `main` column can't
    /// contain more than one window (eg. `MainAndDeck`)*
    pub split: Option<Split>,
}

impl Default for Main {
    fn default() -> Self {
        Self {
            count: 1,
            size: Size::Ratio(0.5),
            flip: Flip::default(),
            rotate: Rotation::default(),
            split: Some(Split::Vertical),
        }
    }
}

/// Configurations concerning the `stack` column
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(default)]
pub struct Stack {
    /// Flip modifier to apply only to the `stack` columns' contents
    pub flip: Flip,

    /// Rotation modifier to apply only to the `stack` columns' contents
    pub rotate: Rotation,

    /// How tiles (windows) inside the `stack` column should be split up,
    /// when there is more than one.
    ///
    /// *Note: This can be set to [`None`], in which case the `stack` column can't
    /// contain more than one window (eg. `Monocle`, `MainAndDeck`)*
    pub split: Option<Split>,
}

impl Default for Stack {
    fn default() -> Self {
        Self {
            flip: Flip::default(),
            rotate: Rotation::default(),
            split: Some(Split::Horizontal),
        }
    }
}

/// Configurations concerning the `second_stack` column
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(default)]
pub struct SecondStack {
    /// Flip modifier to apply only to the `second_stack` columns' contents
    pub flip: Flip,

    /// Rotation modifier to apply only to the `second_stack` columns' contents
    pub rotate: Rotation,

    /// How tiles (windows) inside the `second_stack` column should be split up,
    /// when there is more than one.
    pub split: Split,
}

impl Default for SecondStack {
    fn default() -> Self {
        Self {
            flip: Flip::default(),
            rotate: Rotation::default(),
            split: Split::Horizontal,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        geometry::Size,
        layouts::{
            layout::{DEFAULT_MAIN_SIZE_CHANGE_PERCENTAGE, DEFAULT_MAIN_SIZE_CHANGE_PIXEL},
            Layouts,
        },
        Layout,
    };

    #[test]
    fn monocle_layout_is_monocle() {
        let layouts = Layouts::default();
        let layout = layouts.get("Monocle").unwrap();
        assert!(layout.is_monocle());
    }

    #[test]
    fn main_and_deck_layout_is_main_and_deck() {
        let layouts = Layouts::default();
        let layout = layouts.get("MainAndDeck").unwrap();
        assert!(layout.is_main_and_deck());
    }

    #[test]
    fn set_main_size_works() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Ratio(0.5));
        assert_eq!(Some(Size::Ratio(0.5)), layout.main_size());
    }

    #[test]
    fn increase_main_size_percentage_works() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Ratio(0.5));
        layout.increase_main_size(500);
        assert_eq!(
            Some(Size::Ratio(
                0.5 + (DEFAULT_MAIN_SIZE_CHANGE_PERCENTAGE as f32 * 0.01)
            )),
            layout.main_size()
        );
    }

    #[test]
    fn decrease_main_size_percentage_works() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Ratio(0.5));
        layout.decrease_main_size();
        assert_eq!(
            Some(Size::Ratio(
                0.5 - (DEFAULT_MAIN_SIZE_CHANGE_PERCENTAGE as f32 * 0.01)
            )),
            layout.main_size()
        );
    }

    #[test]
    fn increase_main_size_pixel_works() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Pixel(200));
        layout.increase_main_size(500);
        assert_eq!(
            Some(Size::Pixel(200 + DEFAULT_MAIN_SIZE_CHANGE_PIXEL)),
            layout.main_size()
        );
    }

    #[test]
    fn decrease_main_size_pixel_works() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Pixel(200));
        layout.decrease_main_size();
        assert_eq!(
            Some(Size::Pixel(200 - DEFAULT_MAIN_SIZE_CHANGE_PIXEL)),
            layout.main_size()
        );
    }

    #[test]
    fn change_main_size_percentage_negative_works() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Ratio(0.5));
        layout.change_main_size(-5, 500);
        assert_eq!(Some(Size::Ratio(0.45)), layout.main_size());
    }

    #[test]
    fn change_main_size_percentage_positive_works() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Ratio(0.5));
        layout.change_main_size(5, 500);
        assert_eq!(Some(Size::Ratio(0.55)), layout.main_size());
    }

    #[test]
    fn change_main_size_pixel_negative_works() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Pixel(200));
        layout.change_main_size(-5, 500);
        assert_eq!(Some(Size::Pixel(195)), layout.main_size());
    }

    #[test]
    fn change_main_size_pixel_positive_works() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Pixel(200));
        layout.change_main_size(5, 500);
        assert_eq!(Some(Size::Pixel(205)), layout.main_size());
    }

    #[test]
    fn decrease_main_size_does_not_go_below_zero() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Pixel(200));
        layout.change_main_size(-200, 500);
        assert_eq!(Some(Size::Pixel(0)), layout.main_size());
        layout.change_main_size(-200, 500);
        assert_eq!(Some(Size::Pixel(0)), layout.main_size());
    }

    #[test]
    fn decrease_main_size_does_not_go_above_upper_bound() {
        let mut layout = Layout::default();
        layout.set_main_size(Size::Pixel(200));
        layout.change_main_size(200, 500);
        assert_eq!(Some(Size::Pixel(400)), layout.main_size());
        layout.change_main_size(200, 500);
        assert_eq!(Some(Size::Pixel(500)), layout.main_size());
    }

    #[test]
    fn set_main_window_count_works() {
        let mut layout = Layout::default();
        layout.set_main_window_count(5);
        assert_eq!(Some(5), layout.main_window_count());
    }

    #[test]
    fn increase_main_window_count_works() {
        let mut layout = Layout::default();
        layout.set_main_window_count(5);
        layout.increase_main_window_count();
        assert_eq!(Some(6), layout.main_window_count());
    }

    #[test]
    fn decrease_main_window_count_works() {
        let mut layout = Layout::default();
        layout.set_main_window_count(5);
        layout.decrease_main_window_count();
        assert_eq!(Some(4), layout.main_window_count());
    }

    #[test]
    fn main_window_count_does_not_go_below_zero() {
        let mut layout = Layout::default();
        layout.set_main_window_count(1);
        layout.decrease_main_window_count();
        assert_eq!(Some(0), layout.main_window_count());
        layout.decrease_main_window_count();
        assert_eq!(Some(0), layout.main_window_count());
    }
}