b001ce7817ed | atagen | 2026-10-19 09:23 UTC | 1 | use crate::{Layout, Scratchpad, WindowId, centre_in, centred, translate}; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 2 | |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 3 | impl Layout { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 4 | /// Adds a window to a named scratchpad, creating it. Scratchpad windows |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 5 | /// float and are shown only by summoning. |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 6 | pub fn scratchpad_add(&mut self, w: WindowId, name: &str) { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 7 | let Some(win) = self.windows.get(w) else { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 8 | return; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 9 | }; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 10 | if let Some(s) = win.scratchpad { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 11 | self.leave_scratchpad(s, w); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 12 | } |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 13 | self.unplace(w); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 14 | let s = match self.scratchpads.iter().position(|sp| sp.name == name) { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 15 | Some(s) => s, |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 16 | None => { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 17 | self.scratchpads.push(Scratchpad { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 18 | name: name.into(), |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 19 | windows: Vec::new(), |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 20 | shown: None, |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 21 | output: None, |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 22 | }); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 23 | self.scratchpads.len() - 1 |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 24 | } |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 25 | }; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 26 | self.scratchpads[s].windows.push(w); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 27 | let area = self.windows[w].output.map(|o| self.outputs[o].area); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 28 | let win = &mut self.windows[w]; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 29 | win.scratchpad = Some(s); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 30 | win.floating = true; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 31 | if win.float_rect.is_none() { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 32 | win.float_rect = area.map(|a| centred(a, 0.6)); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 33 | } |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 34 | } |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 35 | |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 36 | /// Cycles a scratchpad through off, each window, and off again, on the |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 37 | /// active output. |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 38 | pub fn summon(&mut self, name: &str) -> Option<WindowId> { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 39 | let s = self.scratchpads.iter().position(|sp| sp.name == name)?; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 40 | let o = self.active?; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 41 | let sp = &mut self.scratchpads[s]; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 42 | let next = match sp.shown { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 43 | None => 0, |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 44 | Some(i) => i + 1, |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 45 | }; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 46 | if next >= sp.windows.len() { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 47 | sp.shown = None; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 48 | return None; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 49 | } |
4b73e40af1e3 | atagen | 2026-10-19 07:44 UTC | 50 | let from = sp.output.map(|f| self.outputs[f].usable()); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 51 | sp.shown = Some(next); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 52 | sp.output = Some(o); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 53 | let w = sp.windows[next]; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 54 | self.last_scratchpad = Some(s); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 55 | let area = self.outputs[o].area; |
4b73e40af1e3 | atagen | 2026-10-19 07:44 UTC | 56 | let usable = self.outputs[o].usable(); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 57 | let win = &mut self.windows[w]; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 58 | if win.output != Some(o) { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 59 | win.float_rect = match (from, win.float_rect) { |
4b73e40af1e3 | atagen | 2026-10-19 07:44 UTC | 60 | (Some(from), Some(r)) => Some(translate(r, from, usable)), |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 61 | _ => Some(centred(area, 0.6)), |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 62 | }; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 63 | win.output = Some(o); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 64 | } |
b001ce7817ed | atagen | 2026-10-19 09:23 UTC | 65 | win.float_rect = win.float_rect.map(|r| centre_in(usable, r.w, r.h)); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 66 | self.focus(w); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 67 | Some(w) |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 68 | } |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 69 | |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 70 | /// Hides a scratchpad, by default the last one summoned. |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 71 | pub fn dismiss(&mut self, name: Option<&str>) { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 72 | let s = match name { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 73 | Some(name) => self.scratchpads.iter().position(|sp| sp.name == name), |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 74 | None => self.last_scratchpad, |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 75 | }; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 76 | if let Some(sp) = s.and_then(|s| self.scratchpads.get_mut(s)) { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 77 | sp.shown = None; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 78 | } |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 79 | } |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 80 | |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 81 | pub(crate) fn leave_scratchpad(&mut self, s: usize, w: WindowId) { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 82 | let sp = &mut self.scratchpads[s]; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 83 | let Some(i) = sp.windows.iter().position(|&x| x == w) else { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 84 | return; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 85 | }; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 86 | sp.windows.remove(i); |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 87 | sp.shown = match sp.shown { |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 88 | Some(j) if j == i => None, |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 89 | Some(j) if j > i => Some(j - 1), |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 90 | other => other, |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 91 | }; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 92 | self.windows[w].scratchpad = None; |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 93 | } |
4cbf881c67bd | atagen | 2026-10-17 04:56 UTC | 94 | } |