1extern crate self as escriba_ui;
4
5pub mod chrome;
9
10pub mod splash;
13
14pub mod syntax;
18
19pub mod shikiri;
22
23pub mod picker;
27
28pub mod gutter;
32
33use escriba_core::{BufferId, Position, WindowId};
34use schemars::JsonSchema;
35use serde::{Deserialize, Serialize};
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
38pub struct Viewport {
39 pub top_line: u32,
40 pub left_column: u32,
41 pub visible_lines: u32,
42 pub visible_columns: u32,
43}
44
45impl Viewport {
46 #[must_use]
56 pub fn scroll_to_contain(mut self, p: Position, margin: u32) -> Self {
57 let bot = p.line.saturating_add(margin);
59 if p.line < self.top_line {
60 self.top_line = p.line.saturating_sub(margin);
61 }
62 if bot >= self.top_line.saturating_add(self.visible_lines) {
63 self.top_line = bot.saturating_sub(self.visible_lines.saturating_sub(1));
64 }
65
66 let right = p.column.saturating_add(margin);
68 if p.column < self.left_column {
69 self.left_column = p.column.saturating_sub(margin);
70 }
71 if right >= self.left_column.saturating_add(self.visible_columns) {
72 self.left_column = right.saturating_sub(self.visible_columns.saturating_sub(1));
73 }
74 self
75 }
76}
77
78pub use shikiri::Window;
81
82#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
83pub struct Layout {
84 tree: shikiri::Shikiri,
87 active: WindowId,
89 frame: shikiri::Rect,
92 next_id: u64,
93 pub statusline: bool,
94 pub tabbar: bool,
95}
96
97impl Layout {
98 #[must_use]
99 pub fn single(window: Window) -> Self {
100 Self {
101 active: window.id,
102 next_id: window.id.0 + 1,
103 tree: shikiri::Shikiri::Pane(window),
104 frame: shikiri::Rect::default(),
105 statusline: true,
106 tabbar: true,
107 }
108 }
109
110 #[must_use]
111 pub const fn active(&self) -> WindowId {
112 self.active
113 }
114
115 pub fn focus(&mut self, id: WindowId) -> bool {
117 if self.windows().any(|w| w.id == id) {
118 self.active = id;
119 true
120 } else {
121 false
122 }
123 }
124
125 pub fn set_frame(&mut self, frame: shikiri::Rect) {
128 self.frame = frame;
129 }
130
131 #[must_use]
132 pub const fn frame(&self) -> shikiri::Rect {
133 self.frame
134 }
135
136 #[must_use]
138 pub fn solved(&self) -> shikiri::Solved {
139 shikiri::solve(&self.tree, self.frame)
140 }
141
142 pub fn windows(&self) -> impl Iterator<Item = &Window> {
144 fn walk<'a>(n: &'a shikiri::Shikiri, out: &mut Vec<&'a Window>) {
145 match n {
146 shikiri::Shikiri::Pane(w) => out.push(w),
147 shikiri::Shikiri::Split(s) => s.children().for_each(|c| walk(c, out)),
148 }
149 }
150 let mut v = Vec::new();
151 walk(&self.tree, &mut v);
152 v.into_iter()
153 }
154
155 pub fn windows_mut(&mut self) -> Vec<&mut Window> {
157 fn walk<'a>(n: &'a mut shikiri::Shikiri, out: &mut Vec<&'a mut Window>) {
158 match n {
159 shikiri::Shikiri::Pane(w) => out.push(w),
160 shikiri::Shikiri::Split(s) => s.children_mut().for_each(|c| walk(c, out)),
161 }
162 }
163 let mut v = Vec::new();
164 walk(&mut self.tree, &mut v);
165 v
166 }
167
168 #[must_use]
169 pub fn active_window(&self) -> Option<&Window> {
170 let id = self.active;
171 self.windows().find(|w| w.id == id)
172 }
173
174 pub fn active_window_mut(&mut self) -> Option<&mut Window> {
175 let id = self.active;
176 self.windows_mut().into_iter().find(|w| w.id == id)
177 }
178
179 #[must_use]
180 pub fn count(&self) -> usize {
181 self.windows().count()
182 }
183
184 pub fn split_active(&mut self, axis: shikiri::Axis) -> WindowId {
192 let id = WindowId(self.next_id);
193 self.next_id += 1;
194 let active = self.active;
195 let fresh = self
196 .active_window()
197 .map(|w| Window {
198 id,
199 buffer_id: w.buffer_id,
200 viewport: w.viewport,
201 })
202 .unwrap_or(Window {
203 id,
204 buffer_id: BufferId(0),
205 viewport: Viewport::default(),
206 });
207 split_at(&mut self.tree, active, axis, fresh);
208 self.active = id;
209 id
210 }
211
212 pub fn close(&mut self, id: WindowId) -> bool {
215 if self.count() <= 1 {
216 return false;
217 }
218 if self.active == id {
220 let next = self
221 .windows()
222 .map(|w| w.id)
223 .find(|w| *w != id)
224 .unwrap_or(id);
225 self.active = next;
226 }
227 remove(&mut self.tree, id)
228 }
229
230 #[must_use]
236 pub fn neighbour(&self, dir: Dir) -> Option<WindowId> {
237 let solved = self.solved();
238 let here = solved.rect_of(self.active)?;
239 solved
240 .panes
241 .iter()
242 .filter(|(id, _)| *id != self.active)
243 .filter(|(_, r)| match dir {
244 Dir::Left => r.x + r.w <= here.x,
245 Dir::Right => r.x >= here.x + here.w,
246 Dir::Up => r.y + r.h <= here.y,
247 Dir::Down => r.y >= here.y + here.h,
248 })
249 .min_by_key(|(_, r)| match dir {
253 Dir::Left => (here.x.saturating_sub(r.x + r.w), here.y.abs_diff(r.y)),
254 Dir::Right => (r.x.saturating_sub(here.x + here.w), here.y.abs_diff(r.y)),
255 Dir::Up => (here.y.saturating_sub(r.y + r.h), here.x.abs_diff(r.x)),
256 Dir::Down => (r.y.saturating_sub(here.y + here.h), here.x.abs_diff(r.x)),
257 })
258 .map(|(id, _)| *id)
259 }
260}
261
262#[derive(Debug, Clone, Copy, PartialEq, Eq)]
264pub enum Dir {
265 Left,
266 Right,
267 Up,
268 Down,
269}
270
271fn split_at(
273 node: &mut shikiri::Shikiri,
274 target: WindowId,
275 axis: shikiri::Axis,
276 fresh: Window,
277) -> bool {
278 match node {
279 shikiri::Shikiri::Pane(w) if w.id == target => {
280 let existing = std::mem::replace(node, shikiri::Shikiri::Pane(fresh.clone()));
281 *node = shikiri::Shikiri::Split(shikiri::Split::new(
282 axis,
283 shikiri::Shikiri::Pane(fresh),
284 existing,
285 ));
286 true
287 }
288 shikiri::Shikiri::Pane(_) => false,
289 shikiri::Shikiri::Split(s) => s
290 .children_mut()
291 .any(|c| split_at(c, target, axis, fresh.clone())),
292 }
293}
294
295fn remove(node: &mut shikiri::Shikiri, id: WindowId) -> bool {
297 let shikiri::Shikiri::Split(s) = node else {
298 return false;
299 };
300 if let Some(rest) = s.without(id) {
301 *node = rest;
302 return true;
303 }
304 let shikiri::Shikiri::Split(s) = node else {
305 return false;
306 };
307 s.children_mut().any(|c| remove(c, id))
308}
309
310#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
311pub struct StatusLine {
312 pub mode: String,
313 pub path: Option<String>,
314 pub cursor: Position,
315 pub modified: bool,
316 pub line_count: u32,
317}
318
319#[cfg(test)]
320mod tests {
321 use super::*;
322
323 fn small() -> Viewport {
324 Viewport {
327 top_line: 0,
328 left_column: 0,
329 visible_lines: 5,
330 visible_columns: 10,
331 }
332 }
333
334 #[test]
335 fn viewport_scrolls_down() {
336 let v = Viewport {
337 top_line: 0,
338 left_column: 0,
339 visible_lines: 20,
340 visible_columns: 80,
341 };
342 let v2 = v.scroll_to_contain(Position::new(30, 0), 2);
343 assert!(v2.top_line > 0);
344 }
345
346 #[test]
347 fn scroll_noop_when_already_visible() {
348 let v = small();
349 let v2 = v.scroll_to_contain(Position::new(2, 4), 2);
351 assert_eq!(v2, v, "an in-window position must not move the viewport");
352 }
353
354 #[test]
355 fn scroll_down_keeps_cursor_visible() {
356 let v = small();
357 let v2 = v.scroll_to_contain(Position::new(30, 0), 2);
358 assert!(
359 v2.top_line <= 30 && 30 < v2.top_line + v2.visible_lines,
360 "cursor line must be within [top_line, top_line+visible_lines): {v2:?}"
361 );
362 }
363
364 #[test]
365 fn scroll_right_keeps_cursor_visible() {
366 let v = small();
367 let v2 = v.scroll_to_contain(Position::new(0, 50), 2);
369 assert!(
370 v2.left_column <= 50 && 50 < v2.left_column + v2.visible_columns,
371 "cursor column must be within [left_column, left_column+visible_columns): {v2:?}"
372 );
373 }
374
375 #[test]
376 fn scroll_up_left_returns_toward_origin() {
377 let v = Viewport {
380 top_line: 20,
381 left_column: 30,
382 visible_lines: 5,
383 visible_columns: 10,
384 };
385 let v2 = v.scroll_to_contain(Position::new(2, 3), 2);
386 assert!(v2.top_line <= 2, "must scroll up to reveal line 2: {v2:?}");
387 assert!(
388 v2.left_column <= 3,
389 "must scroll left to reveal col 3: {v2:?}"
390 );
391 }
392
393 #[test]
394 fn scroll_saturates_at_origin() {
395 let v = small();
397 let v2 = v.scroll_to_contain(Position::ZERO, 2);
398 assert_eq!(v2.top_line, 0);
399 assert_eq!(v2.left_column, 0);
400 }
401
402 #[test]
403 fn scroll_respects_margin_on_both_axes() {
404 let v = small(); let v2 = v.scroll_to_contain(Position::new(4, 9), 2);
409 assert_eq!(v2.top_line, 2, "{v2:?}");
411 assert_eq!(v2.left_column, 2, "{v2:?}");
413 assert!(v2.top_line <= 4 && 4 < v2.top_line + v2.visible_lines);
415 assert!(v2.left_column <= 9 && 9 < v2.left_column + v2.visible_columns);
416 }
417
418 #[test]
419 fn layout_active_resolves() {
420 let w = Window {
421 id: WindowId(1),
422 buffer_id: BufferId(1),
423 viewport: Viewport::default(),
424 };
425 let layout = Layout::single(w);
426 assert_eq!(layout.active_window().unwrap().id, WindowId(1));
427 }
428}