1use ratatui::buffer::Buffer;
5use ratatui::layout::Rect;
6use std::slice::Iter;
7
8use crate::{Caps, Role, Theme};
9
10mod arrange;
11mod drag;
12mod fit;
13mod place;
14
15pub use drag::{Change, changed};
16pub use place::{Place, copy_visible};
17
18pub(crate) const MIN_SIZE: (u16, u16) = (16, 6);
20
21const BACKGROUND: &str = "\u{2591}";
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub struct Window<Id> {
29 pub id: Id,
30 pub place: Place,
31 pub zoomed: bool,
32 pub caps: Caps,
33}
34
35impl<Id> Window<Id> {
36 fn set_place(&mut self, to: Place) {
38 self.place = match self.caps.grow {
39 true => to,
40 false => Place {
41 x: to.x,
42 y: to.y,
43 ..self.place
44 },
45 };
46 }
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct Desktop<Id> {
52 windows: Vec<Window<Id>>,
53}
54
55impl<Id> Default for Desktop<Id> {
56 fn default() -> Self {
57 Self {
58 windows: Vec::new(),
59 }
60 }
61}
62
63impl<Id: Copy + Eq> Desktop<Id> {
64 pub fn open(&mut self, id: Id, rect: Rect, caps: Caps) {
66 self.windows.push(Window {
67 id,
68 place: Place::from(rect),
69 zoomed: false,
70 caps,
71 });
72 }
73
74 pub fn raise(&mut self, id: Id) -> bool {
76 let Some(index) = self.position(id) else {
77 return false;
78 };
79 let window = self.windows.remove(index);
80 self.windows.push(window);
81 true
82 }
83
84 pub fn lower(&mut self, id: Id) -> bool {
86 let Some(index) = self.position(id) else {
87 return false;
88 };
89 let window = self.windows.remove(index);
90 self.windows.insert(0, window);
91 true
92 }
93
94 pub fn close(&mut self, id: Id) -> bool {
96 let Some(index) = self.position(id) else {
97 return false;
98 };
99 self.windows.remove(index);
100 true
101 }
102
103 pub fn front(&self) -> Option<Id> {
104 self.windows.last().map(|window| window.id)
105 }
106
107 pub fn rearmost(&self) -> Option<Id> {
108 self.windows.first().map(|window| window.id)
109 }
110
111 pub fn toggle_zoom(&mut self, id: Id) -> bool {
113 let Some(index) = self.position(id) else {
114 return false;
115 };
116 let window = &mut self.windows[index];
117 window.zoomed ^= window.caps.zoom;
118 window.caps.zoom
119 }
120
121 pub fn tile(&mut self, area: Rect) -> bool {
124 match arrange::tile(self.tileable(), area) {
125 Some(rects) => self.place(&rects),
126 None => false,
127 }
128 }
129
130 pub fn cascade(&mut self, area: Rect) -> bool {
133 match arrange::cascade(self.tileable(), area) {
134 Some(rects) => self.place(&rects),
135 None => false,
136 }
137 }
138
139 fn tileable(&self) -> usize {
140 self.windows
141 .iter()
142 .filter(|window| window.caps.tile)
143 .count()
144 }
145
146 fn place(&mut self, rects: &[Rect]) -> bool {
148 let tileable = self.windows.iter_mut().filter(|window| window.caps.tile);
149 for (window, rect) in tileable.zip(rects) {
150 window.place = Place::from(*rect);
151 window.zoomed = false;
152 }
153 true
154 }
155
156 pub fn resize(&mut self, old: Rect, new: Rect) {
159 for window in &mut self.windows {
160 window.set_place(window.place.scaled(old, new));
161 }
162 }
163
164 pub fn place_of(&self, id: Id, area: Rect) -> Option<Place> {
168 let window = self.windows.get(self.position(id)?)?;
169 match window.zoomed {
170 true => Some(Place::from(area)),
171 false => self.restored_place(id, area),
172 }
173 }
174
175 pub fn restored_place(&self, id: Id, area: Rect) -> Option<Place> {
178 let window = self.windows.get(self.position(id)?)?;
179 Some(window.place.limited(area))
180 }
181
182 pub fn occluded(&self, id: Id, area: Rect) -> bool {
184 let Some(index) = self.position(id) else {
185 return false;
186 };
187 let Some(place) = self.place_of(id, area) else {
188 return false;
189 };
190 let in_front: Vec<Rect> = self.windows[index + 1..]
191 .iter()
192 .filter_map(|window| self.place_of(window.id, area))
193 .map(|front| front.visible_in(area))
194 .collect();
195 place
196 .visible_in(area)
197 .positions()
198 .all(|position| in_front.iter().any(|front| front.contains(position)))
199 }
200
201 pub fn window(&self, id: Id) -> Option<Window<Id>> {
203 self.windows.get(self.position(id)?).copied()
204 }
205
206 pub fn restore(&mut self, saved: Window<Id>) -> bool {
209 let Some(index) = self.position(saved.id) else {
210 return false;
211 };
212 self.windows[index] = saved;
213 true
214 }
215
216 pub fn iter_back_to_front(&self) -> Iter<'_, Window<Id>> {
218 self.windows.iter()
219 }
220
221 fn window_mut(&mut self, id: Id) -> Option<&mut Window<Id>> {
222 let index = self.position(id)?;
223 self.windows.get_mut(index)
224 }
225
226 fn position(&self, id: Id) -> Option<usize> {
227 self.windows.iter().position(|window| window.id == id)
228 }
229}
230
231pub fn render_background(area: Rect, buf: &mut Buffer, theme: &Theme) {
233 let style = theme.style(Role::Desktop);
234 for position in area.intersection(buf.area).positions() {
235 if let Some(cell) = buf.cell_mut(position) {
236 cell.set_symbol(BACKGROUND).set_style(style);
237 }
238 }
239}
240
241#[cfg(test)]
242mod caps_tests;
243#[cfg(test)]
244mod fit_tests;
245#[cfg(test)]
246mod tests;