duat_core/ui/layout.rs
1//! Ways to organize opened [`Buffer`]s
2//!
3//! By default, when calling `:e some_buffer<Enter>`, Duat will follow
4//! [`MasterOnLeft`], a type of [`Layout`] for opening `Buffer`s.
5//! That is, the first opened `Buffer` will be on the left of the
6//! screeng, and all subsequent `Buffer`s will be stacked vertically
7//! on the right of the screen.
8//!
9//! You can create your own [`Layout`] fairly trivially, for example,
10//! here's a spiraled layout:
11//!
12//! ```rust
13//! # duat_core::doc_duat!(duat);
14//! use duat::prelude::*;
15//! use ui::{PushSpecs, Side, Window, layout::Layout};
16//!
17//! pub struct Spiraled;
18//!
19//! impl Layout for Spiraled {
20//! fn new_buffer(&mut self, pa: &Pass, windows: &[Window]) -> (Handle, PushSpecs) {
21//! let cur_win = context::current_win_index(pa);
22//! let buffers = windows[cur_win].buffers(pa);
23//! let last = buffers.iter().last().unwrap().clone();
24//!
25//! match buffers.len() % 4 {
26//! 0 => (last, PushSpecs { side: Side::Right, ..Default::default() }),
27//! 1 => (last, PushSpecs { side: Side::Below, ..Default::default() }),
28//! 2 => (last, PushSpecs { side: Side::Left, ..Default::default() }),
29//! 3 => (last, PushSpecs { side: Side::Above, ..Default::default() }),
30//! _ => unreachable!("That's not how math works, man!"),
31//! }
32//! }
33//! }
34//! ```
35//!
36//! Also notice that this function can fail, which means you can set a
37//! limit to how many [`Buffer`]s should can open in a single window.
38//!
39//! [`Buffer`]: crate::buffer::Buffer
40use super::PushSpecs;
41use crate::{
42 context::{self, Handle},
43 data::Pass,
44 ui::{Side, Window},
45};
46
47/// A form of organizing opened [`Buffer`]s
48///
49/// Determines how the n'th `Buffer` should be opened, given the
50/// previously opened `Buffer`s on the same window.
51///
52/// [`Buffer`]: crate::buffer::Buffer
53pub trait Layout: Send + Sync {
54 /// Opens a new [`Buffer`]
55 ///
56 /// The returned `(Handle<Buffer>, PushSpecs)` value
57 /// represents the [`PushSpecs`] to use when pushing this new
58 /// [`Buffer`], and the [`Handle<Buffer>`] representing which
59 /// `Buffer` to push this `Buffer` to.
60 ///
61 /// There will _always_ be at least one `Buffer` open, since the
62 /// first opened `Buffer` doesn't follow layouts.
63 ///
64 /// [`Ok(Handle<Buffer>, PushSpecs)`]: Handle
65 ///
66 /// [`Buffer`]: crate::buffer::Buffer
67 fn new_buffer(&mut self, pa: &Pass, windows: &[Window]) -> (Handle, PushSpecs);
68}
69
70/// [`Layout`]: One [`Buffer`] on the left, others on the right
71///
72/// One `Buffer` will occupy the whole left side of the screen, and
73/// future buffers will be vertically stacked on the right
74///
75/// [`Buffer`]: crate::buffer::Buffer
76#[derive(Clone)]
77pub struct MasterOnLeft;
78
79impl Layout for MasterOnLeft {
80 fn new_buffer(&mut self, pa: &Pass, windows: &[Window]) -> (Handle, PushSpecs) {
81 let cur_win = context::current_win_index(pa);
82 let last = windows[cur_win].buffers(pa).last().unwrap().clone();
83 if windows[cur_win].buffers(pa).len() == 1 {
84 (last, PushSpecs { side: Side::Right, ..Default::default() })
85 } else {
86 (last, PushSpecs { side: Side::Below, ..Default::default() })
87 }
88 }
89}