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.
This commit is contained in:
LGUG2Z committed 2026-10-30 12:13:42 -07:00
1 parent e5512ca5df
commit 75ebd1ae2c
6 files changed
+284 -3

No files matched your search

+11
View File
@@ -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<Option<Shown>>,
disposed: bool,
@@ -561,6 +564,11 @@ impl GhostWindow {
) -> eyre::Result<Self> {
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::<eyre::Result<(isize, (i32, i32))>>(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<Rect>) -> 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 }) {
+23 -2
View File
@@ -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
+147
View File
@@ -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<Self> {
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(&copy));
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));
}
}
+51
View File
@@ -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<HashMap<isize, WindowKind>> = 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<Item = (isize, WindowKind)>) {
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<BorderLook> {
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<BorderInfo> {
let id = WINDOWS_BORDERS.lock().get(&hwnd)?.clone();
BORDER_STATE.lock().get(&id).map(|b| BorderInfo {
+11 -1
View File
@@ -4066,7 +4066,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)]
+41
View File
@@ -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<WindowDetails> {
let mut vec: Vec<WindowDetails> = vec![];