RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
chrome: move colours, control painting and tests out of chrome.zig
Claude38f7d2568378
diff --git a/src/chrome.zig b/src/chrome.zigindex dfa3fc2..89598d5 100644--- a/src/chrome.zig+++ b/src/chrome.zig@@ -207,7 +207,7 @@ pub fn controlSlot(kind: ControlKind) f32 {};}-inline fn mix(a: f32, b: f32, t: f32) f32 {+pub inline fn mix(a: f32, b: f32, t: f32) f32 {return a + (b - a) * t;}@@ -527,470 +527,17 @@ pub const ShadowMemo = struct {}};-test "titlebar glass holds only while the titlebar is at least 98% opaque" {- const saved = theme.global.window_bg;- defer theme.global.window_bg = saved;- theme.global.window_bg[3] = 0.98;- try std.testing.expect(titlebarGlassMayHold(1));- try std.testing.expect(!titlebarGlassMayHold(0.9));- theme.global.window_bg[3] = 250.0 / 255.0; // #rrggbbfa- try std.testing.expect(titlebarGlassMayHold(1));- theme.global.window_bg[3] = 0.97;- try std.testing.expect(!titlebarGlassMayHold(1));+test {+ _ = @import("chrome_tests.zig");}-fn borderFill(hover: f32) Color {- const t = clamp01(hover);- if (t <= 0) return Color.border();- if (t >= 1) return Color.border_hover();- return Color.lerp(Color.border(), Color.border_hover(), t);-}--/// Straight border edges, matching the rasterized corner arcs.-pub fn borderColor(hover: f32) col.Premul {- return borderFill(hover).straight().premultiply();-}--fn windowBorderFill(hover: f32, workspace_focus: bool) Color {- return if (workspace_focus) Color.fromRgba(theme.global.workspace_active_border) else borderFill(hover);-}--pub fn windowBorderColor(hover: f32, workspace_focus: bool) col.Premul {- return windowBorderFill(hover, workspace_focus).straight().premultiply();-}--/// Chrome backing beneath the border, shared by the header and client sides.-pub fn fillColor() col.Premul {- return Color.chrome().straight().premultiply();-}--const Color = struct {- r: f32,- g: f32,- b: f32,- a: f32,-- // Decoration-specific colors leave the rest of the shell independent.- fn chrome() Color {- return fromRgba(theme.global.window_bg);- }- fn border() Color {- return fromRgba(theme.global.window_border);- }- fn divider() Color {- return fromRgba(theme.global.window_divider);- }- fn text_dim() Color {- return fromRgba(theme.global.window_dim);- }-- // .window:hover border, .win-btn:hover, .win-btn.close:hover and --text.- fn border_hover() Color {- return fromRgba(theme.global.window_border_hover);- }- fn control_hover() Color {- return fromRgba(theme.global.surface_hover);- }- fn close_hover() Color {- var c = fromRgba(theme.global.window_close_hover);- c.a *= theme.global.window_close_hover_alpha;- return c;- }- fn text() Color {- return fromRgba(theme.global.window_fg);- }- fn textOnClose() Color {- return Color.fromRgba(theme.global.window_close_fg);- }-- pub fn fromRgba(rgba: [4]f32) Color {- return .{ .r = rgba[0], .g = rgba[1], .b = rgba[2], .a = rgba[3] };- }-- // Unpacks a premultiplied 0xAARRGGBB sample (icon_cache.Entry.pixels'- // format) back to this struct's straight alpha. Mirrors- // Taskbar.Color.fromPremultiplied; the two Color types are private to- // each file, so this is a small, deliberate duplicate rather than a- // shared one.- pub fn fromPremultiplied(sample: u32) Color {- const a: f32 = @as(f32, @floatFromInt((sample >> 24) & 0xff)) / 255.0;- if (a == 0) return .{ .r = 0, .g = 0, .b = 0, .a = 0 };- const r: f32 = @as(f32, @floatFromInt((sample >> 16) & 0xff)) / 255.0;- const g: f32 = @as(f32, @floatFromInt((sample >> 8) & 0xff)) / 255.0;- const b: f32 = @as(f32, @floatFromInt(sample & 0xff)) / 255.0;- return .{ .r = r / a, .g = g / a, .b = b / a, .a = a };- }-- pub fn over(source: Color, destination: Color) Color {- const alpha = source.a + destination.a * (1 - source.a);- if (alpha == 0) return .{ .r = 0, .g = 0, .b = 0, .a = 0 };- return .{- .r = (source.r * source.a + destination.r * destination.a * (1 - source.a)) / alpha,- .g = (source.g * source.a + destination.g * destination.a * (1 - source.a)) / alpha,- .b = (source.b * source.a + destination.b * destination.a * (1 - source.a)) / alpha,- .a = alpha,- };- }-- pub fn lerp(from: Color, to: Color, t: f32) Color {- return .{- .r = from.r + (to.r - from.r) * t,- .g = from.g + (to.g - from.g) * t,- .b = from.b + (to.b - from.b) * t,- .a = from.a + (to.a - from.a) * t,- };- }-- pub fn scaled(color: Color, alpha: f32) Color {- return .{ .r = color.r, .g = color.g, .b = color.b, .a = color.a * alpha };- }-- fn straight(color: Color) col.Straight {- return .{ .r = color.r, .g = color.g, .b = color.b, .a = color.a };- }-- pub fn argb(color: Color) u32 {- return color.straight().argb();- }-};--fn paintControls(- base: Color,- px: f32,- py: f32,- min_btn: Rect,- max_btn: Rect,- close_btn: Rect,- chip_pos: f32,- chip_alpha: f32,- diag: f32,- scale: f32,- density: f32,- maximized: bool,-) Color {- var color = base;-- // Window-control hover chip, drawn under the glyph. One chip- // slides between the buttons; its rect and red-ness are interpolated- // from the slot coordinate.- if (chip_alpha > 0) {- const pos = std.math.clamp(chip_pos, 0, 2);- const from_max = pos >= 1;- const a_btn = if (from_max) max_btn else min_btn;- const b_btn = if (from_max) close_btn else max_btn;- const t = if (from_max) pos - 1 else pos;- const chip_width = mix(@floatFromInt(a_btn.w), @floatFromInt(b_btn.w), t);- const chip_height = mix(@floatFromInt(a_btn.h), @floatFromInt(b_btn.h), t);- const fill = Color.lerp(Color.control_hover(), Color.close_hover(), clamp01(pos - 1));- const chip = edgeCoverage(sdRoundedBox(- px,- py,- mix(@floatFromInt(a_btn.x), @floatFromInt(b_btn.x), t),- mix(@floatFromInt(a_btn.y), @floatFromInt(b_btn.y), t),- chip_width,- chip_height,- window_chrome.cornerRadius(theme.global.chrome_round_buttons, chip_width, chip_height, btn_radius * density),- ), scale);- if (chip > 0) color = fill.scaled(chip * clamp01(chip_alpha)).over(color);- }-- // Enlarge glyphs within the existing hover chips and hit targets.- const glyph_scale = density * button_glyph_scale;- const min_cov = minimizeIconCoverage(min_btn.centerX() + (px - min_btn.centerX()) / glyph_scale, min_btn.centerY() + (py - min_btn.centerY()) / glyph_scale, min_btn, scale * glyph_scale);- if (min_cov > 0) color = iconColor(chip_pos, chip_alpha, .minimize).scaled(min_cov).over(color);- const max_cov = maximizeIconCoverage(max_btn.centerX() + (px - max_btn.centerX()) / glyph_scale, max_btn.centerY() + (py - max_btn.centerY()) / glyph_scale, max_btn, scale * glyph_scale, maximized);- if (max_cov > 0) color = iconColor(chip_pos, chip_alpha, .maximize).scaled(max_cov).over(color);- const close_cov = closeIconCoverage(close_btn.centerX() + (px - close_btn.centerX()) / glyph_scale, close_btn.centerY() + (py - close_btn.centerY()) / glyph_scale, close_btn, diag, scale * glyph_scale);- if (close_cov > 0) color = iconColor(chip_pos, chip_alpha, .close).scaled(close_cov).over(color);- return color;-}--// Resting glyphs use neutral dim ink, --text on hover, #fff over close red.-fn iconColor(chip_pos: f32, chip_alpha: f32, kind: ControlKind) Color {- // Keep the theme's dim brightness without its blue tint on the controls.- const dim = Color.text_dim();- const luminance = dim.r * 0.2126 + dim.g * 0.7152 + dim.b * 0.0722;- const resting = Color{ .r = luminance, .g = luminance, .b = luminance, .a = dim.a };- const weight = clamp01(1 - @abs(chip_pos - controlSlot(kind))) * clamp01(chip_alpha);- return Color.lerp(resting, if (kind == .close) Color.textOnClose() else Color.text(), weight);-}--fn minimizeIconCoverage(px: f32, py: f32, btn: Rect, scale: f32) f32 {- // A 12px dash at the lower edge of the glyph area.- const cx = btn.centerX();- const cy = btn.centerY() + 5;- return edgeCoverage(sdRoundedBox(px, py, cx - 6, cy - 0.85, 12, 1.7, 0.85), scale);-}--fn maximizeIconCoverage(px: f32, py: f32, btn: Rect, scale: f32, maximized: bool) f32 {- // A 10px outlined square, balanced against the dash and close glyph.- const cx = btn.centerX();- const cy = btn.centerY();- if (!maximized) return outlineSquare(px, py, cx, cy, scale);- // The lower-left window shows only its left and bottom edges, leaving- // a clear gap around the complete upper-right window.- const lower = edgeCoverage(@min(- sdRoundedBox(px, py, cx - 7, cy - 3, 1.5, 10, 0.5),- sdRoundedBox(px, py, cx - 7, cy + 5.5, 10, 1.5, 0.5),- ), scale);- return @max(lower, outlineSquare(px - 2, py + 2, cx, cy, scale));-}--fn outlineSquare(px: f32, py: f32, cx: f32, cy: f32, scale: f32) f32 {- const outer = edgeCoverage(sdRoundedBox(px, py, cx - 5, cy - 5, 10, 10, 1), scale);- const inner = edgeCoverage(sdRoundedBox(px, py, cx - 3.5, cy - 3.5, 7, 7, 0.5), scale);- return clamp01(outer - inner);-}--const closeIconCoverage = window_chrome.closeIconCoverage;--// Shared with ui/paint.zig, which must not depend on the compositor.-pub const sdQuad = sdf.sdQuad;-pub const sdRoundedBox = sdf.sdRoundedBox;-pub const coverage = sdf.coverage;--// Same ramp, but for a distance measured in logical pixels while sampling on a-// denser grid: the edge stays one device pixel wide however far it is scaled.-inline fn edgeCoverage(distance: f32, scale: f32) f32 {- return clamp01(0.5 - distance * scale);-}--pub fn devicePixels(logical: i32, scale: f32) i32 {- return geometry.devicePixels(logical, scale);-}--inline fn clamp01(value: f32) f32 {- return @max(0, @min(1, value));-}--fn sampleTitlebarMemo() TitlebarMemo {- return .{- .width = 200,- .height = 100,- .title_ptr = null,- .scale = 1,- .hover = 0,- .chip_pos = 0,- .chip_alpha = 0,- .maximized = false,- .icon = null,- .glass = .{ 0, 0, 0, 1 },- .border = .{ 1, 1, 1, 0.09 },- .border_soft = .{ 1, 1, 1, 0.06 },- .fg = .{ 1, 1, 1, 1 },- .dim = .{ 0.5, 0.5, 0.5, 1 },- .border_hover = .{ 1, 1, 1, 0.22 },- .surface_hover = .{ 1, 1, 1, 0.08 },- .danger = .{ 1, 0, 0, 1 },- .title_size = 13.5,- };-}--test "TitlebarMemo.eql is true for identical inputs" {- try std.testing.expect(sampleTitlebarMemo().eql(sampleTitlebarMemo()));- const inactive = sampleTitlebarMemo();- var active = inactive;- active.active = true;- try std.testing.expect(!inactive.eql(active));- try std.testing.expect(inactive.reusableFor(active));-}--test "TitlebarMemo.eql compares the title pointer, not the string contents" {- var a_buf = [_:0]u8{ 'h', 'e', 'l', 'l', 'o' };- var b_buf = [_:0]u8{ 'h', 'e', 'l', 'l', 'o' };- var left = sampleTitlebarMemo();- var right = sampleTitlebarMemo();- left.title_ptr = &a_buf;- right.title_ptr = &b_buf;- try std.testing.expect(!left.eql(right));- right.title_ptr = &a_buf;- try std.testing.expect(left.eql(right));-}--test "TitlebarMemo.eql compares icon identity by id, not pixel pointer" {- var pixels_a = [_]u32{0xFF000000};- var pixels_b = [_]u32{0xFF000000};- var left = sampleTitlebarMemo();- var right = sampleTitlebarMemo();-- // Same id, different backing pixel buffers: still the same icon.- left.icon = .{ .id = 1, .pixels = &pixels_a, .size = 1 };- right.icon = .{ .id = 1, .pixels = &pixels_b, .size = 1 };- try std.testing.expect(left.eql(right));-- // Different id, even sharing a pixel buffer: a different icon. A raw- // pointer comparison would get this wrong once eviction can free and- // reuse a buffer's address for a different icon.- right.icon = .{ .id = 2, .pixels = &pixels_a, .size = 1 };- try std.testing.expect(!left.eql(right));-}--test "TitlebarMemo.eql detects font, title size and close control appearance" {- var left = sampleTitlebarMemo();- var right = sampleTitlebarMemo();- right.font_generation +%= 1;- try std.testing.expect(!left.eql(right));- try std.testing.expect(!left.reusableFor(right));- right = left;- right.title_size = 14;- try std.testing.expect(!left.eql(right));- right = left;- right.close_fg = .{ 0, 1, 0, 1 };- try std.testing.expect(!left.eql(right));- right = left;- right.close_hover_alpha = 0.4;- try std.testing.expect(!left.eql(right));-}--test "TitlebarMemo.capture reads title_size from theme.global" {- const saved = theme.global;- defer theme.global = saved;- theme.global.title_size = 19.25;- const memo = TitlebarMemo.capture(10, 10, null, .{ .scale = 1 });- try std.testing.expectEqual(@as(f32, 19.25), memo.title_size);-}--test "chrome geometry changes invalidate retained titlebar pixels" {- const saved = theme.global;- defer theme.global = saved;- const before = TitlebarMemo.capture(400, 320, null, .{});- theme.global.chrome_control_gap += 1;- const spaced = TitlebarMemo.capture(400, 320, null, .{});- try std.testing.expect(!before.eql(spaced));- try std.testing.expect(!before.reusableFor(spaced));- theme.global.chrome_control_gap = saved.chrome_control_gap;- theme.global.chrome_height += 1;- const taller = TitlebarMemo.capture(400, 320, null, .{});- try std.testing.expect(!before.reusableFor(taller));- theme.global.chrome_height = saved.chrome_height;- theme.global.chrome_round_buttons = !saved.chrome_round_buttons;- const round = TitlebarMemo.capture(400, 320, null, .{});- try std.testing.expect(!before.eql(round));- try std.testing.expect(!before.reusableFor(round));-}--test "chrome controls fit and stay centered at every supported height and density" {- const saved = theme.global;- defer theme.global = saved;- for ([_]f32{ 28, 55, 84 }) |height| {- theme.global.chrome_height = height;- for ([_]f32{ 1, 0.75 }) |density| {- const m: Metrics = .{ .density = density };- for ([_]f32{ 0, 4, 24 }) |gap| {- theme.global.chrome_control_gap = gap;- const min = controlRectScaled(400, .minimize, density);- const max = controlRectScaled(400, .maximize, density);- const close = controlRectScaled(400, .close, density);- try std.testing.expectEqual(m.controlGap(), max.x - (min.x + min.w));- try std.testing.expectEqual(m.controlGap(), close.x - (max.x + max.w));- try std.testing.expect(min.y >= 0 and min.y + min.h < m.titlebarHeight());- try std.testing.expect(@abs(2 * min.y + min.h - m.titlebarHeight()) <= 1);- try std.testing.expect(m.iconSize() < min.h);- try std.testing.expect(min.x > m.length(title_inset) + m.iconSize() + m.length(title_icon_gap));- }- }- }-}--test "ShadowMemo.eql ignores geometry while the shadow is disabled" {- const a = ShadowMemo{- .width = 10,- .height = 10,- .radius = 0,- .color = .{ 0, 0, 0, 0 },- .softness = 0,- .offset_y = 0,- .scale = 1,- .disabled = true,- };- var b = a;- b.width = 999;- try std.testing.expect(a.eql(b));- b.disabled = false;- try std.testing.expect(!a.eql(b));-}--test "ShadowMemo.eql is false when softness changes" {- const a = ShadowMemo{- .width = 200,- .height = 100,- .radius = 10,- .color = .{ 0, 0, 0, 0.35 },- .softness = 24,- .offset_y = 8,- .scale = 1,- .disabled = false,- };- var b = a;- try std.testing.expect(a.eql(b));- b.softness = 32;- try std.testing.expect(!a.eql(b));-}--test "cached chrome updates exactly match complete rasters across hover controls and scales" {- for ([_]i32{ 20, 200, 640 }) |width| {- for ([_]f32{ 1, 1.25, 1.5, 2 }) |scale| {- for ([_]f32{ 1, 0.75, 1.0 / 1.7 }) |density| {- var state: ChromeState = .{ .density = density, .scale = scale, .title = "Cached window title" };- var previous = try ChromeBuffer.create(width, state);- defer previous.base.drop();- // Titles change alone and together with hover and- // controls: growing, shrinking, ellipsized, fallback- // glyphs (a terminal spinner) and empty.- const titles = [_][]const u8{- "Cached window title",- "\u{280b} Working on a task that is long enough to be ellipsized before the controls",- "Short",- "Short",- "",- "\u{2819} Working \u{2014} claude",- };- var step: usize = 0;- for ([_]f32{ 0.35, 1, 0 }) |hover| {- for ([_]?ControlKind{ .minimize, .maximize, .close, null }) |control| {- const previous_title = state.title;- state.hover = hover;- state.chip_pos = if (control) |kind| controlSlot(kind) else 0.5;- state.chip_alpha = if (control == null) 0.35 else 1;- state.title = titles[step % titles.len];- state.active = step % 2 == 0;- state.workspace_focus = step % 3 == 0;- step += 1;- const expected = try ChromeBuffer.create(width, state);- defer expected.base.drop();- const updated = try ChromeBuffer.createUpdated(previous, state, !std.mem.eql(u8, previous_title, state.title));- errdefer updated.base.drop();- try std.testing.expectEqualSlices(u32, expected.pixels, updated.pixels);- previous.base.drop();- previous = updated;- }- }- }- }- }-}--test "tab title updates preserve neighbouring pixels and match a full raster" {- const Tab = @import("chrome_tabs.zig").Tab;- for ([_]f32{ 1, 1.5, 2 }) |scale| {- const before = [_]Tab{ .{ .id = 1, .title = "A longer title", .active = true, .attention = false }, .{ .id = 2, .title = "Untouched", .active = false, .attention = false } };- var state: ChromeState = .{ .scale = scale, .tabs = .{ .tabs = &before } };- const old = try ChromeBuffer.create(600, state);- defer old.base.drop();- var after = before;- after[0].title = "Short";- state.tabs.tabs = &after;- const updated = try ChromeBuffer.createUpdated(old, state, true);- defer updated.base.drop();- const full = try ChromeBuffer.create(600, state);- defer full.base.drop();- try std.testing.expectEqualSlices(u32, full.pixels, updated.pixels);- const geom = @import("chrome_tabs.zig").geometry(600, 1, state.tabs);- const untouched = geom.tab(1);- for (0..@intCast(old.height)) |y| {- const start: usize = y * @as(usize, @intCast(old.width)) + @as(usize, @intFromFloat(@ceil(untouched.x * scale)));- const end: usize = y * @as(usize, @intCast(old.width)) + @as(usize, @intFromFloat(@floor((untouched.x + untouched.w) * scale)));- try std.testing.expectEqualSlices(u32, old.pixels[start..end], updated.pixels[start..end]);- }- }-}+const chrome_paint = @import("chrome_paint.zig");+pub const borderColor = chrome_paint.borderColor;+pub const windowBorderColor = chrome_paint.windowBorderColor;+pub const fillColor = chrome_paint.fillColor;+const Color = chrome_paint.Color;+const paintControls = chrome_paint.paintControls;+pub const sdQuad = chrome_paint.sdQuad;+pub const sdRoundedBox = chrome_paint.sdRoundedBox;+pub const coverage = chrome_paint.coverage;+pub const devicePixels = chrome_paint.devicePixels;diff --git a/src/chrome_paint.zig b/src/chrome_paint.zignew file mode 100644index 0000000..779b56b--- /dev/null+++ b/src/chrome_paint.zig@@ -0,0 +1,242 @@+//! Chrome colours (borders and fill) and painting the window controls with SDF coverage.++const std = @import("std");+const sdf = @import("ui").sdf;+const col = @import("color.zig");+const geometry = @import("geometry.zig");+const theme = @import("ui").theme;+const text = @import("ui").text;+const window_chrome = @import("ui").window_chrome;+const btn_radius = window_chrome.control_radius;+const button_glyph_scale = window_chrome.button_glyph_scale;+const ControlKind = window_chrome.ControlKind;+const Rect = window_chrome.Rect;+const chrome_mod = @import("chrome.zig");+const controlSlot = chrome_mod.controlSlot;+const mix = chrome_mod.mix;++pub fn borderFill(hover: f32) Color {+ const t = clamp01(hover);+ if (t <= 0) return Color.border();+ if (t >= 1) return Color.border_hover();+ return Color.lerp(Color.border(), Color.border_hover(), t);+}++/// Straight border edges, matching the rasterized corner arcs.+pub fn borderColor(hover: f32) col.Premul {+ return borderFill(hover).straight().premultiply();+}++pub fn windowBorderFill(hover: f32, workspace_focus: bool) Color {+ return if (workspace_focus) Color.fromRgba(theme.global.workspace_active_border) else borderFill(hover);+}++pub fn windowBorderColor(hover: f32, workspace_focus: bool) col.Premul {+ return windowBorderFill(hover, workspace_focus).straight().premultiply();+}++/// Chrome backing beneath the border, shared by the header and client sides.+pub fn fillColor() col.Premul {+ return Color.chrome().straight().premultiply();+}++pub const Color = struct {+ r: f32,+ g: f32,+ b: f32,+ a: f32,++ // Decoration-specific colors leave the rest of the shell independent.+ pub fn chrome() Color {+ return fromRgba(theme.global.window_bg);+ }+ fn border() Color {+ return fromRgba(theme.global.window_border);+ }+ pub fn divider() Color {+ return fromRgba(theme.global.window_divider);+ }+ fn text_dim() Color {+ return fromRgba(theme.global.window_dim);+ }++ // .window:hover border, .win-btn:hover, .win-btn.close:hover and --text.+ fn border_hover() Color {+ return fromRgba(theme.global.window_border_hover);+ }+ fn control_hover() Color {+ return fromRgba(theme.global.surface_hover);+ }+ fn close_hover() Color {+ var c = fromRgba(theme.global.window_close_hover);+ c.a *= theme.global.window_close_hover_alpha;+ return c;+ }+ pub fn text() Color {+ return fromRgba(theme.global.window_fg);+ }+ fn textOnClose() Color {+ return Color.fromRgba(theme.global.window_close_fg);+ }++ pub fn fromRgba(rgba: [4]f32) Color {+ return .{ .r = rgba[0], .g = rgba[1], .b = rgba[2], .a = rgba[3] };+ }++ // Unpacks a premultiplied 0xAARRGGBB sample (icon_cache.Entry.pixels'+ // format) back to this struct's straight alpha. Mirrors+ // Taskbar.Color.fromPremultiplied; the two Color types are private to+ // each file, so this is a small, deliberate duplicate rather than a+ // shared one.+ pub fn fromPremultiplied(sample: u32) Color {+ const a: f32 = @as(f32, @floatFromInt((sample >> 24) & 0xff)) / 255.0;+ if (a == 0) return .{ .r = 0, .g = 0, .b = 0, .a = 0 };+ const r: f32 = @as(f32, @floatFromInt((sample >> 16) & 0xff)) / 255.0;+ const g: f32 = @as(f32, @floatFromInt((sample >> 8) & 0xff)) / 255.0;+ const b: f32 = @as(f32, @floatFromInt(sample & 0xff)) / 255.0;+ return .{ .r = r / a, .g = g / a, .b = b / a, .a = a };+ }++ pub fn over(source: Color, destination: Color) Color {+ const alpha = source.a + destination.a * (1 - source.a);+ if (alpha == 0) return .{ .r = 0, .g = 0, .b = 0, .a = 0 };+ return .{+ .r = (source.r * source.a + destination.r * destination.a * (1 - source.a)) / alpha,+ .g = (source.g * source.a + destination.g * destination.a * (1 - source.a)) / alpha,+ .b = (source.b * source.a + destination.b * destination.a * (1 - source.a)) / alpha,+ .a = alpha,+ };+ }++ pub fn lerp(from: Color, to: Color, t: f32) Color {+ return .{+ .r = from.r + (to.r - from.r) * t,+ .g = from.g + (to.g - from.g) * t,+ .b = from.b + (to.b - from.b) * t,+ .a = from.a + (to.a - from.a) * t,+ };+ }++ pub fn scaled(color: Color, alpha: f32) Color {+ return .{ .r = color.r, .g = color.g, .b = color.b, .a = color.a * alpha };+ }++ fn straight(color: Color) col.Straight {+ return .{ .r = color.r, .g = color.g, .b = color.b, .a = color.a };+ }++ pub fn argb(color: Color) u32 {+ return color.straight().argb();+ }+};++pub fn paintControls(+ base: Color,+ px: f32,+ py: f32,+ min_btn: Rect,+ max_btn: Rect,+ close_btn: Rect,+ chip_pos: f32,+ chip_alpha: f32,+ diag: f32,+ scale: f32,+ density: f32,+ maximized: bool,+) Color {+ var color = base;++ // Window-control hover chip, drawn under the glyph. One chip+ // slides between the buttons; its rect and red-ness are interpolated+ // from the slot coordinate.+ if (chip_alpha > 0) {+ const pos = std.math.clamp(chip_pos, 0, 2);+ const from_max = pos >= 1;+ const a_btn = if (from_max) max_btn else min_btn;+ const b_btn = if (from_max) close_btn else max_btn;+ const t = if (from_max) pos - 1 else pos;+ const chip_width = mix(@floatFromInt(a_btn.w), @floatFromInt(b_btn.w), t);+ const chip_height = mix(@floatFromInt(a_btn.h), @floatFromInt(b_btn.h), t);+ const fill = Color.lerp(Color.control_hover(), Color.close_hover(), clamp01(pos - 1));+ const chip = edgeCoverage(sdRoundedBox(+ px,+ py,+ mix(@floatFromInt(a_btn.x), @floatFromInt(b_btn.x), t),+ mix(@floatFromInt(a_btn.y), @floatFromInt(b_btn.y), t),+ chip_width,+ chip_height,+ window_chrome.cornerRadius(theme.global.chrome_round_buttons, chip_width, chip_height, btn_radius * density),+ ), scale);+ if (chip > 0) color = fill.scaled(chip * clamp01(chip_alpha)).over(color);+ }++ // Enlarge glyphs within the existing hover chips and hit targets.+ const glyph_scale = density * button_glyph_scale;+ const min_cov = minimizeIconCoverage(min_btn.centerX() + (px - min_btn.centerX()) / glyph_scale, min_btn.centerY() + (py - min_btn.centerY()) / glyph_scale, min_btn, scale * glyph_scale);+ if (min_cov > 0) color = iconColor(chip_pos, chip_alpha, .minimize).scaled(min_cov).over(color);+ const max_cov = maximizeIconCoverage(max_btn.centerX() + (px - max_btn.centerX()) / glyph_scale, max_btn.centerY() + (py - max_btn.centerY()) / glyph_scale, max_btn, scale * glyph_scale, maximized);+ if (max_cov > 0) color = iconColor(chip_pos, chip_alpha, .maximize).scaled(max_cov).over(color);+ const close_cov = closeIconCoverage(close_btn.centerX() + (px - close_btn.centerX()) / glyph_scale, close_btn.centerY() + (py - close_btn.centerY()) / glyph_scale, close_btn, diag, scale * glyph_scale);+ if (close_cov > 0) color = iconColor(chip_pos, chip_alpha, .close).scaled(close_cov).over(color);+ return color;+}++// Resting glyphs use neutral dim ink, --text on hover, #fff over close red.+pub fn iconColor(chip_pos: f32, chip_alpha: f32, kind: ControlKind) Color {+ // Keep the theme's dim brightness without its blue tint on the controls.+ const dim = Color.text_dim();+ const luminance = dim.r * 0.2126 + dim.g * 0.7152 + dim.b * 0.0722;+ const resting = Color{ .r = luminance, .g = luminance, .b = luminance, .a = dim.a };+ const weight = clamp01(1 - @abs(chip_pos - controlSlot(kind))) * clamp01(chip_alpha);+ return Color.lerp(resting, if (kind == .close) Color.textOnClose() else Color.text(), weight);+}++pub fn minimizeIconCoverage(px: f32, py: f32, btn: Rect, scale: f32) f32 {+ // A 12px dash at the lower edge of the glyph area.+ const cx = btn.centerX();+ const cy = btn.centerY() + 5;+ return edgeCoverage(sdRoundedBox(px, py, cx - 6, cy - 0.85, 12, 1.7, 0.85), scale);+}++pub fn maximizeIconCoverage(px: f32, py: f32, btn: Rect, scale: f32, maximized: bool) f32 {+ // A 10px outlined square, balanced against the dash and close glyph.+ const cx = btn.centerX();+ const cy = btn.centerY();+ if (!maximized) return outlineSquare(px, py, cx, cy, scale);+ // The lower-left window shows only its left and bottom edges, leaving+ // a clear gap around the complete upper-right window.+ const lower = edgeCoverage(@min(+ sdRoundedBox(px, py, cx - 7, cy - 3, 1.5, 10, 0.5),+ sdRoundedBox(px, py, cx - 7, cy + 5.5, 10, 1.5, 0.5),+ ), scale);+ return @max(lower, outlineSquare(px - 2, py + 2, cx, cy, scale));+}++pub fn outlineSquare(px: f32, py: f32, cx: f32, cy: f32, scale: f32) f32 {+ const outer = edgeCoverage(sdRoundedBox(px, py, cx - 5, cy - 5, 10, 10, 1), scale);+ const inner = edgeCoverage(sdRoundedBox(px, py, cx - 3.5, cy - 3.5, 7, 7, 0.5), scale);+ return clamp01(outer - inner);+}++pub const closeIconCoverage = window_chrome.closeIconCoverage;++// Shared with ui/paint.zig, which must not depend on the compositor.+pub const sdQuad = sdf.sdQuad;++pub const sdRoundedBox = sdf.sdRoundedBox;++pub const coverage = sdf.coverage;++// Same ramp, but for a distance measured in logical pixels while sampling on a+// denser grid: the edge stays one device pixel wide however far it is scaled.+pub inline fn edgeCoverage(distance: f32, scale: f32) f32 {+ return clamp01(0.5 - distance * scale);+}++pub fn devicePixels(logical: i32, scale: f32) i32 {+ return geometry.devicePixels(logical, scale);+}++pub inline fn clamp01(value: f32) f32 {+ return @max(0, @min(1, value));+}diff --git a/src/chrome_tests.zig b/src/chrome_tests.zignew file mode 100644index 0000000..ce8f90f--- /dev/null+++ b/src/chrome_tests.zig@@ -0,0 +1,259 @@+const std = @import("std");+const theme = @import("ui").theme;+const window_chrome = @import("ui").window_chrome;+const Metrics = window_chrome.Metrics;+const title_inset = window_chrome.title_inset;+const title_icon_gap = window_chrome.title_icon_gap;+const ControlKind = window_chrome.ControlKind;+const controlRectScaled = window_chrome.controlRectScaled;+const chrome_mod = @import("chrome.zig");+const titlebarGlassMayHold = chrome_mod.titlebarGlassMayHold;+const ChromeState = chrome_mod.ChromeState;+const TitlebarMemo = chrome_mod.TitlebarMemo;+const controlSlot = chrome_mod.controlSlot;+const ChromeBuffer = chrome_mod.ChromeBuffer;+const ShadowMemo = chrome_mod.ShadowMemo;++test "titlebar glass holds only while the titlebar is at least 98% opaque" {+ const saved = theme.global.window_bg;+ defer theme.global.window_bg = saved;+ theme.global.window_bg[3] = 0.98;+ try std.testing.expect(titlebarGlassMayHold(1));+ try std.testing.expect(!titlebarGlassMayHold(0.9));+ theme.global.window_bg[3] = 250.0 / 255.0; // #rrggbbfa+ try std.testing.expect(titlebarGlassMayHold(1));+ theme.global.window_bg[3] = 0.97;+ try std.testing.expect(!titlebarGlassMayHold(1));+}++pub fn sampleTitlebarMemo() TitlebarMemo {+ return .{+ .width = 200,+ .height = 100,+ .title_ptr = null,+ .scale = 1,+ .hover = 0,+ .chip_pos = 0,+ .chip_alpha = 0,+ .maximized = false,+ .icon = null,+ .glass = .{ 0, 0, 0, 1 },+ .border = .{ 1, 1, 1, 0.09 },+ .border_soft = .{ 1, 1, 1, 0.06 },+ .fg = .{ 1, 1, 1, 1 },+ .dim = .{ 0.5, 0.5, 0.5, 1 },+ .border_hover = .{ 1, 1, 1, 0.22 },+ .surface_hover = .{ 1, 1, 1, 0.08 },+ .danger = .{ 1, 0, 0, 1 },+ .title_size = 13.5,+ };+}++test "TitlebarMemo.eql is true for identical inputs" {+ try std.testing.expect(sampleTitlebarMemo().eql(sampleTitlebarMemo()));+ const inactive = sampleTitlebarMemo();+ var active = inactive;+ active.active = true;+ try std.testing.expect(!inactive.eql(active));+ try std.testing.expect(inactive.reusableFor(active));+}++test "TitlebarMemo.eql compares the title pointer, not the string contents" {+ var a_buf = [_:0]u8{ 'h', 'e', 'l', 'l', 'o' };+ var b_buf = [_:0]u8{ 'h', 'e', 'l', 'l', 'o' };+ var left = sampleTitlebarMemo();+ var right = sampleTitlebarMemo();+ left.title_ptr = &a_buf;+ right.title_ptr = &b_buf;+ try std.testing.expect(!left.eql(right));+ right.title_ptr = &a_buf;+ try std.testing.expect(left.eql(right));+}++test "TitlebarMemo.eql compares icon identity by id, not pixel pointer" {+ var pixels_a = [_]u32{0xFF000000};+ var pixels_b = [_]u32{0xFF000000};+ var left = sampleTitlebarMemo();+ var right = sampleTitlebarMemo();++ // Same id, different backing pixel buffers: still the same icon.+ left.icon = .{ .id = 1, .pixels = &pixels_a, .size = 1 };+ right.icon = .{ .id = 1, .pixels = &pixels_b, .size = 1 };+ try std.testing.expect(left.eql(right));++ // Different id, even sharing a pixel buffer: a different icon. A raw+ // pointer comparison would get this wrong once eviction can free and+ // reuse a buffer's address for a different icon.+ right.icon = .{ .id = 2, .pixels = &pixels_a, .size = 1 };+ try std.testing.expect(!left.eql(right));+}++test "TitlebarMemo.eql detects font, title size and close control appearance" {+ var left = sampleTitlebarMemo();+ var right = sampleTitlebarMemo();+ right.font_generation +%= 1;+ try std.testing.expect(!left.eql(right));+ try std.testing.expect(!left.reusableFor(right));+ right = left;+ right.title_size = 14;+ try std.testing.expect(!left.eql(right));+ right = left;+ right.close_fg = .{ 0, 1, 0, 1 };+ try std.testing.expect(!left.eql(right));+ right = left;+ right.close_hover_alpha = 0.4;+ try std.testing.expect(!left.eql(right));+}++test "TitlebarMemo.capture reads title_size from theme.global" {+ const saved = theme.global;+ defer theme.global = saved;+ theme.global.title_size = 19.25;+ const memo = TitlebarMemo.capture(10, 10, null, .{ .scale = 1 });+ try std.testing.expectEqual(@as(f32, 19.25), memo.title_size);+}++test "chrome geometry changes invalidate retained titlebar pixels" {+ const saved = theme.global;+ defer theme.global = saved;+ const before = TitlebarMemo.capture(400, 320, null, .{});+ theme.global.chrome_control_gap += 1;+ const spaced = TitlebarMemo.capture(400, 320, null, .{});+ try std.testing.expect(!before.eql(spaced));+ try std.testing.expect(!before.reusableFor(spaced));+ theme.global.chrome_control_gap = saved.chrome_control_gap;+ theme.global.chrome_height += 1;+ const taller = TitlebarMemo.capture(400, 320, null, .{});+ try std.testing.expect(!before.reusableFor(taller));+ theme.global.chrome_height = saved.chrome_height;+ theme.global.chrome_round_buttons = !saved.chrome_round_buttons;+ const round = TitlebarMemo.capture(400, 320, null, .{});+ try std.testing.expect(!before.eql(round));+ try std.testing.expect(!before.reusableFor(round));+}++test "chrome controls fit and stay centered at every supported height and density" {+ const saved = theme.global;+ defer theme.global = saved;+ for ([_]f32{ 28, 55, 84 }) |height| {+ theme.global.chrome_height = height;+ for ([_]f32{ 1, 0.75 }) |density| {+ const m: Metrics = .{ .density = density };+ for ([_]f32{ 0, 4, 24 }) |gap| {+ theme.global.chrome_control_gap = gap;+ const min = controlRectScaled(400, .minimize, density);+ const max = controlRectScaled(400, .maximize, density);+ const close = controlRectScaled(400, .close, density);+ try std.testing.expectEqual(m.controlGap(), max.x - (min.x + min.w));+ try std.testing.expectEqual(m.controlGap(), close.x - (max.x + max.w));+ try std.testing.expect(min.y >= 0 and min.y + min.h < m.titlebarHeight());+ try std.testing.expect(@abs(2 * min.y + min.h - m.titlebarHeight()) <= 1);+ try std.testing.expect(m.iconSize() < min.h);+ try std.testing.expect(min.x > m.length(title_inset) + m.iconSize() + m.length(title_icon_gap));+ }+ }+ }+}++test "ShadowMemo.eql ignores geometry while the shadow is disabled" {+ const a = ShadowMemo{+ .width = 10,+ .height = 10,+ .radius = 0,+ .color = .{ 0, 0, 0, 0 },+ .softness = 0,+ .offset_y = 0,+ .scale = 1,+ .disabled = true,+ };+ var b = a;+ b.width = 999;+ try std.testing.expect(a.eql(b));+ b.disabled = false;+ try std.testing.expect(!a.eql(b));+}++test "ShadowMemo.eql is false when softness changes" {+ const a = ShadowMemo{+ .width = 200,+ .height = 100,+ .radius = 10,+ .color = .{ 0, 0, 0, 0.35 },+ .softness = 24,+ .offset_y = 8,+ .scale = 1,+ .disabled = false,+ };+ var b = a;+ try std.testing.expect(a.eql(b));+ b.softness = 32;+ try std.testing.expect(!a.eql(b));+}++test "cached chrome updates exactly match complete rasters across hover controls and scales" {+ for ([_]i32{ 20, 200, 640 }) |width| {+ for ([_]f32{ 1, 1.25, 1.5, 2 }) |scale| {+ for ([_]f32{ 1, 0.75, 1.0 / 1.7 }) |density| {+ var state: ChromeState = .{ .density = density, .scale = scale, .title = "Cached window title" };+ var previous = try ChromeBuffer.create(width, state);+ defer previous.base.drop();+ // Titles change alone and together with hover and+ // controls: growing, shrinking, ellipsized, fallback+ // glyphs (a terminal spinner) and empty.+ const titles = [_][]const u8{+ "Cached window title",+ "\u{280b} Working on a task that is long enough to be ellipsized before the controls",+ "Short",+ "Short",+ "",+ "\u{2819} Working \u{2014} claude",+ };+ var step: usize = 0;+ for ([_]f32{ 0.35, 1, 0 }) |hover| {+ for ([_]?ControlKind{ .minimize, .maximize, .close, null }) |control| {+ const previous_title = state.title;+ state.hover = hover;+ state.chip_pos = if (control) |kind| controlSlot(kind) else 0.5;+ state.chip_alpha = if (control == null) 0.35 else 1;+ state.title = titles[step % titles.len];+ state.active = step % 2 == 0;+ state.workspace_focus = step % 3 == 0;+ step += 1;+ const expected = try ChromeBuffer.create(width, state);+ defer expected.base.drop();+ const updated = try ChromeBuffer.createUpdated(previous, state, !std.mem.eql(u8, previous_title, state.title));+ errdefer updated.base.drop();+ try std.testing.expectEqualSlices(u32, expected.pixels, updated.pixels);+ previous.base.drop();+ previous = updated;+ }+ }+ }+ }+ }+}++test "tab title updates preserve neighbouring pixels and match a full raster" {+ const Tab = @import("chrome_tabs.zig").Tab;+ for ([_]f32{ 1, 1.5, 2 }) |scale| {+ const before = [_]Tab{ .{ .id = 1, .title = "A longer title", .active = true, .attention = false }, .{ .id = 2, .title = "Untouched", .active = false, .attention = false } };+ var state: ChromeState = .{ .scale = scale, .tabs = .{ .tabs = &before } };+ const old = try ChromeBuffer.create(600, state);+ defer old.base.drop();+ var after = before;+ after[0].title = "Short";+ state.tabs.tabs = &after;+ const updated = try ChromeBuffer.createUpdated(old, state, true);+ defer updated.base.drop();+ const full = try ChromeBuffer.create(600, state);+ defer full.base.drop();+ try std.testing.expectEqualSlices(u32, full.pixels, updated.pixels);+ const geom = @import("chrome_tabs.zig").geometry(600, 1, state.tabs);+ const untouched = geom.tab(1);+ for (0..@intCast(old.height)) |y| {+ const start: usize = y * @as(usize, @intCast(old.width)) + @as(usize, @intFromFloat(@ceil(untouched.x * scale)));+ const end: usize = y * @as(usize, @intCast(old.width)) + @as(usize, @intFromFloat(@floor((untouched.x + untouched.w) * scale)));+ try std.testing.expectEqualSlices(u32, old.pixels[start..end], updated.pixels[start..end]);+ }+ }+}