Alt+Tab switcher: show a preview of each window
CI / test (push) Failing after 19s

The switcher is now a grid of cards: a scaled-down picture of each window's
last frame over its app icon and title, the selected card lit. Cards wrap
into rows and shrink to fit small screens.

- Previews keep the buffer's aspect ratio and are never enlarged.
- Minimized windows show the last frame they drew, dimmed, marked
  "minimized". A window with no buffer yet shows its app icon (or its
  initial) on a plain tile.
- Cheap: nothing is scaled while the switcher is closed. A preview is made
  when the switcher opens and again only when that window commits a new
  buffer (or its shown size changes), into the same image handle; handles
  of closed windows are reused. The native scaler samples at most 3x3
  points per preview pixel, so big windows cost about the same as small.
- Both hosts: native scales the wl_shm snapshot in the software renderer
  (renderer.shm_thumbnail + set_image); the wasm compositor asks the host
  with the new chandra:host/raster thumbnail(surface, w, h, reuse), which
  scales the captured surface natively, so no pixels cross the boundary.
- Frames the screenshot overlay held back mark previews stale too.

Tests: switcher_test (preview pixels, aspect, no-buffer, stale and
remade into the same handle, minimized), renderer shm_thumbnail/set_image,
host-wasm thumbnail scaling and handle reuse. Pre-existing failures on
main are unchanged (6 panel-module host tests, the screenshot panel
module test, host-wasm translucent_surfaces_blend).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjDvAsSQUdTTUGA3n5
This commit is contained in:
Claude committed 2026-11-25 19:14:30 +00:00
1 parent 8f88d93d9f
commit 51b914f6cf
16 files changed
+528 -50

No files matched your search

+183 -13
View File
@@ -123,7 +123,138 @@ fn ChandraCompositor::switcher_key(self : ChandraCompositor, code : UInt, presse
false
}
///| The switcher's draw ops: a centred list, the selected row lit. Empty when closed.
///| A window's preview in the switcher: an image handle (renderer or host image) of its last
/// buffer scaled to `w`x`h`, made again only when the window committed a new buffer (`stale`) or
/// the size it is shown at changed, and only while the switcher is up.
pub(all) struct SwitcherThumb {
mut handle : UInt
mut w : Int
mut h : Int
mut stale : Bool
}
///| The window committed a new buffer: its preview is made again the next time it is shown.
fn ChandraCompositor::thumb_stale(self : ChandraCompositor, id : UInt64) -> Unit {
match self.thumbs.get(id) {
Some(t) => t.stale = true
None => ()
}
}
///| The window is gone: keep its preview's image handle for the next window's.
fn ChandraCompositor::thumb_forget(self : ChandraCompositor, id : UInt64) -> Unit {
match self.thumbs.get(id) {
Some(t) => {
if t.handle != 0U {
self.thumb_free.push(t.handle)
}
ignore(self.thumbs.remove(id))
}
None => ()
}
}
///| `bw`x`bh` fitted inside `box_w`x`box_h` with its aspect ratio kept, never enlarged.
pub fn thumb_fit(bw : Int, bh : Int, box_w : Int, box_h : Int) -> (Int, Int) {
if bw <= 0 || bh <= 0 || box_w <= 0 || box_h <= 0 {
return (0, 0)
}
if bw <= box_w && bh <= box_h {
return (bw, bh)
}
if bw * box_h >= bh * box_w {
let h = bh * box_w / bw
(box_w, if h < 1 { 1 } else { h })
} else {
let w = bw * box_h / bh
(if w < 1 { 1 } else { w }, box_h)
}
}
///| The preview of window `id` fitted in `box_w`x`box_h`: (image handle, w, h), handle 0 when the
/// window has no buffer yet. A minimized window keeps the last buffer it showed.
fn ChandraCompositor::switcher_thumb(self : ChandraCompositor, id : UInt64, box_w : Int, box_h : Int) -> (UInt, Int, Int) {
let snap = match self.windows.get(id) {
Some(win) => win.surface
None => None
}
let old = self.thumbs.get(id)
match snap {
None =>
match old {
Some(t) => (t.handle, t.w, t.h)
None => (0U, 0, 0)
}
Some(buf) => {
let (tw, th) = thumb_fit(buf.width, buf.height, box_w, box_h)
match old {
Some(t) if !t.stale && t.w == tw && t.h == th => return (t.handle, tw, th)
_ => ()
}
let reuse = match old {
Some(t) => t.handle
None => self.thumb_free.pop().unwrap_or(0U)
}
let h = if tw <= 0 {
0U
} else if buf.host_id != 0U {
@sys.surface_thumbnail(buf.host_id, tw, th, reuse)
} else {
match @renderer.shm_thumbnail(buf, tw, th) {
Some(px) => @renderer.set_image(reuse, px)
None => 0U
}
}
if h == 0U {
// Couldn't make one: show the previous preview if there is one, and try again next time.
match old {
Some(t) => (t.handle, t.w, t.h)
None => {
if reuse != 0U {
self.thumb_free.push(reuse)
}
(0U, 0, 0)
}
}
} else {
match old {
Some(t) => {
t.handle = h
t.w = tw
t.h = th
t.stale = false
}
None => self.thumbs.set(id, { handle: h, w: tw, h: th, stale: false })
}
(h, tw, th)
}
}
}
}
///| Where the switcher's cards go: (columns, preview box width, preview box height). Cards wrap
/// into rows, and shrink when the screen is too small for them.
fn switcher_grid(n : Int, out_w : Int, out_h : Int) -> (Int, Int, Int) {
let mut bw = 220
let mut bh = 138
while true {
let card_w = bw + 16
let card_h = bh + 46
let fit = (out_w - 60 + 8) / (card_w + 8)
let cols = if fit < 1 { 1 } else if fit > n { n } else { fit }
let rows = (n + cols - 1) / cols
if (rows * (card_h + 8) + 40 <= out_h && cols * (card_w + 8) + 52 <= out_w) || bw <= 64 {
return (cols, bw, bh)
}
bw = bw * 4 / 5
bh = bh * 4 / 5
}
(1, bw, bh)
}
///| The switcher's draw ops: a centred grid of cards, each a preview of the window (its last
/// frame, also for minimized windows) over its app icon and title; the selected card lit.
/// Empty when closed.
fn ChandraCompositor::switcher_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] {
let ops : Array[@scene.DrawOp] = []
if !self.switcher_on {
@@ -131,21 +262,28 @@ fn ChandraCompositor::switcher_ops(self : ChandraCompositor) -> Array[@scene.Dra
}
let p = self.theme.palette
let n = self.switcher.length()
let row_h = 38
let pad = 10
let w = if self.output_w < 560 { self.output_w - 20 } else { 520 }
let h = n * row_h + pad * 2
let (cols, bw, bh) = switcher_grid(n, self.output_w, self.output_h)
let rows = (n + cols - 1) / cols
let cp = 8 // card padding
let gap = 8
let pad = 12
let label_h = 30
let card_w = bw + cp * 2
let card_h = cp + bh + label_h + cp
let w = cols * card_w + (cols - 1) * gap + pad * 2
let h = rows * card_h + (rows - 1) * gap + pad * 2
let x = (self.output_w - w) / 2
let y = (self.output_h - h) / 2
let r = self.theme.radius + 4
let fs = self.theme.font_size + 1
// Dim everything behind it a little, then the card.
let fs = self.theme.font_size
// Dim everything behind it a little, then the panel.
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, self.output_w, self.output_h), @scene.Rgba::new(0, 0, 0, 90), 0))
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(x, y, w, h), p.surface_popup, r))
ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(x, y, w, h), p.border, 1, r))
for i = 0; i < n; i = i + 1 {
let id = self.switcher[i]
let ry = y + pad + i * row_h
let cx = x + pad + i % cols * (card_w + gap)
let cy = y + pad + i / cols * (card_h + gap)
let sel = i == self.switcher_sel
let (title, app, minimized) = match self.windows.get(id) {
Some(win) => {
@@ -158,15 +296,47 @@ fn ChandraCompositor::switcher_ops(self : ChandraCompositor) -> Array[@scene.Dra
None => ("", "", false)
}
if sel {
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(x + pad, ry, w - pad * 2, row_h - 4), p.accent, self.theme.radius))
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(cx, cy, card_w, card_h), p.accent, self.theme.radius + 2))
}
let icon = self.shell.window_icon(app)
// The preview, centred in its box; without a buffer yet, the app icon on a plain tile.
let (thumb, tw, th) = self.switcher_thumb(id, bw, bh)
let bx = cx + cp
let by = cy + cp
if thumb != 0U {
let tr = @scene.Rect::new(bx + (bw - tw) / 2, by + (bh - th) / 2, tw, th)
ops.push(@scene.DrawOp::Image(tr, thumb))
ops.push(@scene.DrawOp::StrokeRect(tr, p.border, 1, 0))
if minimized {
ops.push(@scene.DrawOp::FillRect(tr, @scene.Rgba::new(0, 0, 0, 120), 0))
}
} else {
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(bx, by, bw, bh), p.surface, self.theme.radius))
ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(bx, by, bw, bh), p.border, 1, self.theme.radius))
let big = if bh / 2 < 48 { bh / 2 } else { 48 }
if icon != 0U {
ops.push(@scene.DrawOp::Image(@scene.Rect::new(bx + (bw - big) / 2, by + (bh - big) / 2, big, big), icon))
} else {
let letter = if app.length() > 0 { app[:1].to_string().to_upper() } else if title.length() > 0 { title[:1].to_string().to_upper() } else { "?" }
ops.push(@scene.DrawOp::Text(bx + bw / 2, by + bh / 2, letter, big * 2 / 3, p.foreground_muted, @scene.TextAlign::Center, true))
}
}
if minimized {
ops.push(@scene.DrawOp::Text(bx + bw / 2, by + bh / 2, "minimized", fs - 1, @scene.Rgba::new(255, 255, 255, 230), @scene.TextAlign::Center, true))
}
// The label: app icon, then the title cut to fit.
let ly = by + bh + label_h / 2
let isz = 18
let mut tx = bx + 2
if icon != 0U {
ops.push(@scene.DrawOp::Image(@scene.Rect::new(tx, ly - isz / 2, isz, isz), icon))
tx = tx + isz + 6
}
let fg = if sel { p.accent_foreground } else if minimized { p.foreground_muted } else { p.foreground }
let cw = (fs * 11 + 19) / 20
let max_chars = (w - pad * 2 - 150) / cw
let max_chars = (bx + bw - tx) / cw
let shown = if title.length() > max_chars && max_chars > 1 { title[:max_chars - 1].to_string() + "…" } else { title }
ops.push(@scene.DrawOp::Text(x + pad + 12, ry + (row_h - 4) / 2, shown, fs, fg, @scene.TextAlign::Left, sel))
let tag = if minimized { app + " (minimized)" } else { app }
ops.push(@scene.DrawOp::Text(x + w - pad - 12, ry + (row_h - 4) / 2, tag, fs - 2, if sel { p.accent_foreground } else { p.foreground_muted }, @scene.TextAlign::Right, false))
ops.push(@scene.DrawOp::Text(tx, ly, shown, fs, fg, @scene.TextAlign::Left, sel))
}
ops
}