From 3d9f581b96c2fb1673c34773777218dbd91e52da Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Wed, 28 Oct 2026 17:36:20 -0700 Subject: [PATCH] feat(animation): draw the border of a window into its ghost A border is a window of its own, which cannot be cut off at the edge of the monitor as a ghost is, so the borders of the windows that slide in and out were left out of it: they were gone before the windows left and did not come back until they were shown. The picture a ghost shows is now given the border its window has, drawn in room that is made for it around the window and over its edge, in the colour of the kind of border the window has (single, stack, monocle, unfocused...). The ghost is bigger than the window by that room, and is clipped to the monitor as it was, so the window and its border slide and are cut off as one. The pictures that are kept of the windows that leave are of the windows alone, as the kind of border a window has when it comes back is not the one it had when it left. --- komorebi/src/animation/ghost.rs | 11 ++ komorebi/src/animation/ghost_dispatcher.rs | 25 +++- komorebi/src/animation/snapshot.rs | 147 +++++++++++++++++++++ komorebi/src/border_manager/mod.rs | 51 +++++++ komorebi/src/window_manager.rs | 12 +- komorebi/src/workspace.rs | 41 ++++++ 6 files changed, 284 insertions(+), 3 deletions(-) diff --git a/komorebi/src/animation/ghost.rs b/komorebi/src/animation/ghost.rs index ebd88383..192d6f96 100644 --- a/komorebi/src/animation/ghost.rs +++ b/komorebi/src/animation/ghost.rs @@ -524,6 +524,9 @@ pub struct GhostWindow { origin: (i32, i32), /// How big the picture is, in pixels source_size: (i32, i32), + /// How far the picture reaches out of the window on every side: the room + /// for its border + margin: i32, /// What the ghost was last made to show shown: Mutex>, disposed: bool, @@ -561,6 +564,11 @@ impl GhostWindow { ) -> eyre::Result { let captured = start.elapsed(); let source_size = (snapshot.width, snapshot.height); + let margin = snapshot.margin; + + // the picture is the window and its border, which is bigger than it + let mut start_rect = start_rect; + start_rect.add_margin(margin); let (reply_tx, reply_rx) = bounded::>(1); ghost_owner() @@ -584,6 +592,7 @@ impl GhostWindow { hthumb, origin, source_size, + margin, // it is registered where the window is, with all of its picture that // is to be seen, and opaque shown: Mutex::new(Some(Shown { @@ -608,6 +617,8 @@ impl GhostWindow { /// part of it that is outside is cut off, and it is not shown at all when /// all of it is. pub fn update_clipped(&self, rect: Rect, opacity: u8, clip: Option) -> eyre::Result<()> { + let mut rect = rect; + rect.add_margin(self.margin); let drawn = drawn_part(rect, self.source_size, clip); if !needs_update(&mut self.shown.lock(), Shown { drawn, opacity }) { diff --git a/komorebi/src/animation/ghost_dispatcher.rs b/komorebi/src/animation/ghost_dispatcher.rs index 71e4204d..5e5c035a 100644 --- a/komorebi/src/animation/ghost_dispatcher.rs +++ b/komorebi/src/animation/ghost_dispatcher.rs @@ -230,7 +230,13 @@ impl GhostDispatcher { // picture of itself then, as it is hidden snapshot::remember(self.hwnd, &picture); - GhostWindow::create_from(picture, self.hwnd, self.start_rect, Some(clip), start) + GhostWindow::create_from( + self.with_border(picture), + self.hwnd, + self.start_rect, + Some(clip), + start, + ) }); match ghost { @@ -253,7 +259,13 @@ impl GhostDispatcher { { let start = std::time::Instant::now(); - match GhostWindow::create_from(picture, self.hwnd, self.start_rect, Some(clip), start) { + match GhostWindow::create_from( + self.with_border(picture), + self.hwnd, + self.start_rect, + Some(clip), + start, + ) { Ok(ghost) => { *self.ghost.lock() = Some(ghost); self.lay_out_entering_window(); @@ -270,6 +282,15 @@ impl GhostDispatcher { self.show_entered_at_once(); } + /// The picture with the border the window has drawn around it, as the + /// border window cannot slide with it: it would be seen on the monitors + /// beside this one. + fn with_border(&self, picture: Snapshot) -> Snapshot { + border_manager::border_look(self.hwnd) + .and_then(|look| picture.with_border(&look).ok()) + .unwrap_or(picture) + } + /// What the ghost of a window that is coming in shows. The window is hidden, so /// it is not asked to render itself if there is a picture of it from when /// it left: asking a hidden Chromium or Electron window to has been seen to diff --git a/komorebi/src/animation/snapshot.rs b/komorebi/src/animation/snapshot.rs index 69b6575f..2be05264 100644 --- a/komorebi/src/animation/snapshot.rs +++ b/komorebi/src/animation/snapshot.rs @@ -59,6 +59,7 @@ use windows::core::PCWSTR; use windows::core::w; use crate::WindowsApi; +use crate::border_manager::BorderLook; /// Ask for what the window looks like to the compositor, which is the only /// way to capture a window that is drawn with DirectComposition @@ -76,6 +77,9 @@ pub struct Snapshot { pixels: usize, pub width: i32, pub height: i32, + /// How much of the picture, on every side, is room for a border around the + /// window, which is not the window: a ghost is bigger than the window by this + pub margin: i32, } // A GDI bitmap is not tied to the thread that made it @@ -187,6 +191,64 @@ fn round_corners(pixels: &mut [u32], width: i32, height: i32, radius: i32) { } } +/// Draw a border into a picture that has room for it: the outline of a +/// rectangle with rounded corners, or square ones, as wide as `look.width` and +/// where the border window would draw it, which is in from the picture's edge by +/// half of its width less the offset. Over what is there, with the edges of the +/// line smoothed. +fn draw_border(pixels: &mut [u32], width: i32, height: i32, look: &BorderLook, margin: i32) { + let stroke = look.width as f32; + let inset = (look.width / 2 - look.offset) as f32; + let half_width = width as f32 / 2.0 - inset; + let half_height = height as f32 / 2.0 - inset; + let radius = if look.rounded { + 8.0 + stroke / 2.0 + } else { + 0.0 + } + .min(half_width) + .min(half_height) + .max(0.0); + + let red = (look.colour & 0xFF) as f32; + let green = ((look.colour >> 8) & 0xFF) as f32; + let blue = ((look.colour >> 16) & 0xFF) as f32; + + // nothing of the line is further in than this + let band = (margin + (stroke as i32) + 2).min(width).min(height); + + for y in 0..height { + for x in 0..width { + if x >= band && x < width - band && y >= band && y < height - band { + continue; + } + + // the distance from the line along the middle of the border, which + // is negative inside of the rounded rectangle it makes + let px = (x as f32 + 0.5 - width as f32 / 2.0).abs() - (half_width - radius); + let py = (y as f32 + 0.5 - height as f32 / 2.0).abs() - (half_height - radius); + let distance = px.max(0.0).hypot(py.max(0.0)) + px.max(py).min(0.0) - radius; + + let coverage = (stroke / 2.0 + 0.5 - distance.abs()).clamp(0.0, 1.0); + if coverage <= 0.0 { + continue; + } + + // premultiplied, over what is there + let below = pixels[(y * width + x) as usize]; + let keep = 1.0 - coverage; + let channel = |shift: u32, colour: f32| { + let below = ((below >> shift) & 0xFF) as f32; + ((colour * coverage + below * keep).round() as u32).min(255) << shift + }; + let alpha = ((255.0 * coverage + (below >> 24) as f32 * keep).round() as u32).min(255); + + pixels[(y * width + x) as usize] = + (alpha << 24) | channel(16, red) | channel(8, green) | channel(0, blue); + } + } +} + /// What a window rendered of itself, and where in that its visible frame is struct Rendered { bitmap: HBITMAP, @@ -319,6 +381,7 @@ impl Snapshot { pixels: pixels.as_ptr() as usize, width: rendered.width, height: rendered.height, + margin: 0, } }); @@ -350,6 +413,7 @@ impl Snapshot { pixels: pixels.as_ptr() as usize, width, height, + margin: 0, } } } @@ -372,6 +436,50 @@ impl Snapshot { pixels: pixels.as_ptr() as usize, width: self.width, height: self.height, + margin: self.margin, + } + }); + + let _ = DeleteDC(dc); + ReleaseDC(None, screen); + + result + } + } + + /// The same picture with a border drawn around the window, as its own border + /// would be: in a margin that the picture is given, and over the edge of the + /// window as far as the border reaches in. + pub fn with_border(&self, look: &BorderLook) -> eyre::Result { + let margin = (look.width + look.offset).max(0); + let width = self.width + margin * 2; + let height = self.height + margin * 2; + + unsafe { + let screen = GetDC(None); + let dc = CreateCompatibleDC(Some(screen)); + + let result = create_dib(dc, width, height).map(|(bitmap, pixels)| { + let source = std::slice::from_raw_parts( + self.pixels as *const u32, + (self.width * self.height) as usize, + ); + + for row in 0..self.height { + let from = (row * self.width) as usize; + let to = ((row + margin) * width + margin) as usize; + pixels[to..to + self.width as usize] + .copy_from_slice(&source[from..from + self.width as usize]); + } + + draw_border(pixels, width, height, look, margin); + + Self { + bitmap, + pixels: pixels.as_ptr() as usize, + width, + height, + margin, } }); @@ -832,4 +940,43 @@ mod tests { assert_eq!(pixels(&original), pixels(©)); assert_ne!(original.pixels, copy.pixels); } + + #[test] + fn a_border_is_drawn_in_the_room_around_a_window_and_over_its_edge() { + let look = BorderLook { + width: 8, + offset: -1, + rounded: false, + colour: 0x0000_00FF, + }; + let margin = look.width + look.offset; + let (width, height) = (100 + margin * 2, 80 + margin * 2); + let mut pixels = vec![0u32; (width * height) as usize]; + let at = |pixels: &[u32], x: i32, y: i32| pixels[(y * width + x) as usize]; + + draw_border(&mut pixels, width, height, &look, margin); + + // red along the middle of the line, which is 5 in from the edge + assert_eq!(at(&pixels, width / 2, 5), 0xFFFF_0000); + assert_eq!(at(&pixels, 5, height / 2), 0xFFFF_0000); + // the line reaches in over the window, but not to its middle, and out + // as far as the margin and no further + assert_eq!(at(&pixels, width / 2, margin + 1) >> 24, 0xFF); + assert_eq!(at(&pixels, width / 2, height / 2), 0); + assert_eq!(at(&pixels, width / 2, 0), 0); + } + + #[test] + fn a_picture_given_a_border_has_room_for_it() { + let look = BorderLook { + width: 8, + offset: -1, + rounded: true, + colour: 0x00FF_0000, + }; + let bordered = picture(100, 80).with_border(&look).unwrap(); + + assert_eq!(bordered.margin, 7); + assert_eq!((bordered.width, bordered.height), (114, 94)); + } } diff --git a/komorebi/src/border_manager/mod.rs b/komorebi/src/border_manager/mod.rs index 394b5f63..30c4abbb 100644 --- a/komorebi/src/border_manager/mod.rs +++ b/komorebi/src/border_manager/mod.rs @@ -166,6 +166,57 @@ pub(crate) fn leaves_border_alone(hwnd: isize) -> bool { is_ghosted(hwnd) || is_held(hwnd) } +lazy_static! { + /// What kind of border the windows of the workspaces that are being slid + /// in and out are to be given in their ghosts: the borders themselves + /// do not slide, so the ghosts carry theirs + static ref GHOST_KINDS: Mutex> = Mutex::new(HashMap::new()); +} + +/// How a border is drawn, for a picture of the window it is around +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct BorderLook { + pub width: i32, + pub offset: i32, + pub rounded: bool, + /// As a `COLORREF`: 0x00BBGGRR + pub colour: u32, +} + +/// Say what kind of border each of these windows has, or would have if it were +/// shown, for `border_look` +pub fn set_ghost_kinds(kinds: impl IntoIterator) { + let mut ghost_kinds = GHOST_KINDS.lock(); + // only what was asked about most lately is of any use + if ghost_kinds.len() > 256 { + ghost_kinds.clear(); + } + ghost_kinds.extend(kinds); +} + +/// The border to draw into the picture of a window that is sliding, if it has +/// one +pub fn border_look(hwnd: isize) -> Option { + if !BORDER_ENABLED.load(Ordering::Relaxed) { + return None; + } + + let kind = GHOST_KINDS.lock().get(&hwnd).copied()?; + + let rounded = match STYLE.load() { + BorderStyle::System => *crate::WINDOWS_11, + BorderStyle::Rounded => true, + BorderStyle::Square => false, + }; + + Some(BorderLook { + width: BORDER_WIDTH.load(Ordering::Relaxed), + offset: BORDER_OFFSET.load(Ordering::Relaxed), + rounded, + colour: window_kind_colour(kind), + }) +} + pub fn window_border(hwnd: isize) -> Option { let id = WINDOWS_BORDERS.lock().get(&hwnd)?.clone(); BORDER_STATE.lock().get(&id).map(|b| BorderInfo { diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 273791df..5e09998b 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -4067,7 +4067,17 @@ fn workspace_transition_plan( .map(|window| window.hwnd) .collect(); - transition::plan(direction, monitor.size, &outgoing, &incoming) + let plan = transition::plan(direction, monitor.size, &outgoing, &incoming)?; + + // the borders of the windows that slide are drawn into their ghosts + border_manager::set_ghost_kinds( + leaving + .border_kinds_on_show() + .into_iter() + .chain(entering.border_kinds_on_show()), + ); + + Some(plan) } #[cfg(test)] diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index c2ea0a27..ca671fb4 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -35,6 +35,7 @@ use crate::core::OperationDirection; use crate::core::PlacementMatchingRules; use crate::core::PlacementTarget; use crate::core::Rect; +use crate::core::WindowKind; use crate::core::WindowPlacement; use crate::lockable_sequence::LockableSequence; use crate::ring::Ring; @@ -2033,6 +2034,46 @@ impl Workspace { .collect() } + /// What kind of border each of the windows that are on show has + pub fn border_kinds_on_show(&self) -> Vec<(isize, WindowKind)> { + if let Some(monocle) = &self.monocle_container { + return monocle + .focused_window() + .map(|window| (window.hwnd, WindowKind::Monocle)) + .into_iter() + .collect(); + } + + let mut kinds: Vec<(isize, WindowKind)> = self + .containers() + .iter() + .enumerate() + .filter_map(|(idx, container)| { + let kind = if idx != self.focused_container_idx() { + if container.locked { + WindowKind::UnfocusedLocked + } else { + WindowKind::Unfocused + } + } else if container.windows().len() > 1 { + WindowKind::Stack + } else { + WindowKind::Single + }; + + container.focused_window().map(|window| (window.hwnd, kind)) + }) + .collect(); + + kinds.extend( + self.floating_windows() + .iter() + .map(|window| (window.hwnd, WindowKind::Unfocused)), + ); + + kinds + } + pub fn visible_window_details(&self) -> Vec { let mut vec: Vec = vec![];