diff options
Diffstat (limited to 'src/colors.zig')
| -rw-r--r-- | src/colors.zig | 195 |
1 files changed, 7 insertions, 188 deletions
diff --git a/src/colors.zig b/src/colors.zig index aec2917f..f09bf9db 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -1,16 +1,14 @@ //! Colour themes: the Theme type, the curated and generated ring, chrome -//! colours and their animation, theme files, and switching themes at runtime. +//! colours, theme files, and switching themes at runtime. const pardes = @import("pardes.zig"); const tagline = @import("tagline.zig"); const panes = @import("panes.zig"); const std = @import("std"); -const animation = @import("animation.zig"); const Color = @import("surface.zig").Color; const FontRole = @import("surface.zig").FontRole; const config = @import("config.zig"); const pdf_enabled = panes.Pdf.enabled; const platform = pardes.platform; -const theme_animation = @import("pardes_config").theme_animation; const MAX_PANES = pardes.MAX_PANES; const BOX_H = pardes.BOX_H; const Pardes = pardes.Pardes; @@ -601,55 +599,6 @@ pub const ChromeTheme = struct { } }; -/// The chrome as it is drawn: the theme's, fading from the last one over -/// `animation.transition_steps` frames, each colour through OKLab (G10: a -/// fade keeps its lightness even, never going muddy or dark between two -/// hues as a mix of sRGB bytes does). A change eases in and out (§8.1); one -/// set off partway through another eases out, moving at once, so it never -/// stalls (§7.2). A build without theme_animation snaps instead -/// (finishThemeInitialization). -pub const ChromeAnimation = struct { - from: ChromeTheme, - to: ChromeTheme, - displayed: ChromeTheme, - step: u16 = animation.transition_steps, - retargeted: bool = false, - - pub fn init(chrome: ChromeTheme) ChromeAnimation { - return .{ .from = chrome, .to = chrome, .displayed = chrome }; - } - - pub fn isActive(a: *const ChromeAnimation) bool { - return a.step < animation.transition_steps; - } - - pub fn retarget(a: *ChromeAnimation, target: ChromeTheme) void { - a.retargeted = a.isActive(); - a.from = a.displayed; - a.to = target; - a.step = if (std.meta.eql(a.from, target)) animation.transition_steps else 0; - if (a.step == animation.transition_steps) a.displayed = target; - } - - pub fn advance(a: *ChromeAnimation) void { - if (!a.isActive()) return; - a.step += 1; - if (a.step == animation.transition_steps) { - a.displayed = a.to; - return; - } - const t = @as(f32, @floatFromInt(a.step)) / @as(f32, @floatFromInt(animation.transition_steps)); - // In thousandths of the way, as the fade has always stepped. - const k = @round(animation.sample(if (a.retargeted) .out_cubic else .smooth, t) * 1000); - inline for (@typeInfo(ChromeTheme).@"struct".field_names) |name| - @field(a.displayed, name) = if (k == 0) @field(a.from, name) else if (k >= 1000) @field(a.to, name) else mixOklab(@field(a.from, name), @field(a.to, name), k / 1000); - } - - pub fn snap(a: *ChromeAnimation, chrome: ChromeTheme) void { - a.* = init(chrome); - } -}; - pub const initial_chrome = ChromeTheme.fromTheme(&themes[0]); fn nextThemeFileGeneration(p: *Pardes) u32 { @@ -743,7 +692,7 @@ fn revertThemeFile(p: *Pardes) void { p.emit(.{ .theme_file = .{ .generation = generation, .on = loaded.len > 0 } }); } -pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bool) bool { +pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8) bool { // ZON's generated field dispatch grows with the complete theme schema. @setEvalBranchQuota(10000); const request = themeFileRequest(p, generation) orelse return false; @@ -777,11 +726,7 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bo return false; }; - const target_chrome = ChromeTheme.fromTheme(&parsed); - if (animate and p.animate_theme_changes) - p.chrome_animation.retarget(target_chrome) - else - p.chrome_animation.snap(target_chrome); + p.chrome = ChromeTheme.fromTheme(&parsed); const old = p.custom_theme; p.custom_theme = parsed; if (old) |theme_value| p.gpa.free(theme_value.name); @@ -793,8 +738,7 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bo } pub fn finishThemeInitialization(p: *Pardes) void { - p.chrome_animation.snap(ChromeTheme.fromTheme(p.theme())); - p.animate_theme_changes = theme_animation; + p.chrome = ChromeTheme.fromTheme(p.theme()); } fn invalidateThemeDependentRasters(p: *Pardes) void { @@ -885,11 +829,7 @@ pub fn setThemeIndex(p: *Pardes, index: usize) void { if (index >= themes.len or (p.custom_theme == null and p.theme_file_path.get().len == 0 and index == @as(usize, p.settings.theme))) return; - const target_chrome = ChromeTheme.fromTheme(&themes[index]); - if (p.animate_theme_changes) - p.chrome_animation.retarget(target_chrome) - else - p.chrome_animation.snap(target_chrome); + p.chrome = ChromeTheme.fromTheme(&themes[index]); if (p.custom_theme) |theme_value| { p.custom_theme = null; p.gpa.free(theme_value.name); @@ -938,13 +878,13 @@ test "ThemeFile parses ZON atomically and Theme returns to the compiled ring" { var encoded: std.Io.Writer.Allocating = .init(std.testing.allocator); defer encoded.deinit(); try std.zon.stringify.serialize(custom, .{ .whitespace = true }, &encoded.writer); - try std.testing.expect(loadThemeFile(p, request_effect.generation, encoded.written(), false)); + try std.testing.expect(loadThemeFile(p, request_effect.generation, encoded.written())); try std.testing.expectEqualStrings("mine", p.theme().name); try std.testing.expectEqual(@as(?[3]u8, .{ 1, 2, 3 }), p.theme().bg); try std.testing.expectEqual(ChromeTheme.fromTheme(p.theme()), p.chromeTheme().*); // A half-written save is rejected without disturbing the last good value. - try std.testing.expect(!loadThemeFile(p, request_effect.generation, ".{ .name = ", true)); + try std.testing.expect(!loadThemeFile(p, request_effect.generation, ".{ .name = ")); // Said with its line, in words: never a bare `parse zon`. const said = p.panes[0].?.msg[0..p.panes[0].?.msg_len]; try std.testing.expect(std.mem.indexOf(u8, said, ": line 1: ") != null and std.mem.indexOf(u8, said, "parse zon") == null); @@ -961,127 +901,6 @@ test "ThemeFile parses ZON atomically and Theme returns to the compiled ring" { try std.testing.expect(!stop.on); } -test "runtime theme changes animate chrome and retarget without a jump" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - const first = p.chromeTheme().*; - try std.testing.expect(!p.animationActive()); - - const dark_index = for (themes, 0..) |th, i| { - if (std.mem.eql(u8, th.name, "dark")) break i; - } else return error.MissingDarkTheme; - const acme_index = for (themes, 0..) |th, i| { - if (std.mem.eql(u8, th.name, "acme")) break i; - } else return error.MissingAcmeTheme; - - setThemeIndex(p, dark_index); - // Target semantics are immediate, while the displayed chrome begins at - // the exact previous frame. - try std.testing.expectEqualStrings("dark", p.theme().name); - try std.testing.expectEqual(first, p.chromeTheme().*); - try std.testing.expect(p.animationActive()); - p.update(.tick); - p.update(.tick); - p.update(.tick); - const midflight = p.chromeTheme().*; - try std.testing.expect(!std.meta.eql(first, midflight)); - - setThemeIndex(p, acme_index); - try std.testing.expectEqualStrings("acme", p.theme().name); - try std.testing.expectEqual(midflight, p.chromeTheme().*); - try std.testing.expect(p.animationActive()); - for (0..animation.transition_steps) |_| p.update(.tick); - try std.testing.expect(!p.animationActive()); - try std.testing.expectEqual(ChromeTheme.fromTheme(p.theme()), p.chromeTheme().*); - // Extra ticks are inert at the exact endpoint. - p.update(.tick); - try std.testing.expectEqual(ChromeTheme.fromTheme(p.theme()), p.chromeTheme().*); -} - -/// A chrome of one colour everywhere, for the fade's tests. -fn chromeOf(rgb: [3]u8) ChromeTheme { - var out: ChromeTheme = undefined; - inline for (@typeInfo(ChromeTheme).@"struct".field_names) |name| @field(out, name) = rgb; - return out; -} - -test "a chrome fade starts where it is, lands exactly with every channel on its way, and goes no further" { - const from = chromeOf(.{ 240, 240, 240 }); - const to = chromeOf(.{ 5, 5, 5 }); - var fade: ChromeAnimation = .init(from); - fade.retarget(to); - try std.testing.expectEqual(from, fade.displayed); - var previous = fade.displayed.tag_bg; - for (0..animation.transition_steps) |_| { - fade.advance(); - for (fade.displayed.tag_bg, previous) |now, was| try std.testing.expect(now <= was); - previous = fade.displayed.tag_bg; - } - try std.testing.expect(!fade.isActive()); - try std.testing.expectEqual(to, fade.displayed); - fade.advance(); - try std.testing.expectEqual(to, fade.displayed); -} - -test "a chrome fade retargeted partway sets off from what is shown" { - const first = chromeOf(.{ 0, 40, 200 }); - const second = chromeOf(.{ 200, 140, 0 }); - const third = chromeOf(.{ 20, 10, 250 }); - var fade: ChromeAnimation = .init(first); - fade.retarget(second); - for (0..3) |_| fade.advance(); - const on_screen = fade.displayed; - fade.retarget(third); - try std.testing.expectEqual(on_screen, fade.from); - try std.testing.expectEqual(on_screen, fade.displayed); - try std.testing.expect(fade.isActive()); - for (0..animation.transition_steps) |_| fade.advance(); - try std.testing.expectEqual(third, fade.displayed); -} - -test "render uses target content colors while chrome is in flight" { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer p.deinit(); - const source_chrome = p.chromeTheme().*; - const acme_index = for (themes, 0..) |th, i| { - if (std.mem.eql(u8, th.name, "acme")) break i; - } else return error.MissingAcmeTheme; - setThemeIndex(p, acme_index); - - var frame = std.heap.ArenaAllocator.init(std.testing.allocator); - defer frame.deinit(); - const surface = try p.render(frame.allocator()); - // The anchored topbar has not advanced from its source frame. - try std.testing.expectEqual( - Color{ .rgb = source_chrome.tag_bg }, - surface.at(0, 0).style.bg, - ); - const topbar_cell = surface.at(@import("pardes.zig").TAG_TEXT_INSET, 0); - try std.testing.expect(!std.mem.eql(u8, topbar_cell.grapheme(), " ")); - try std.testing.expectEqual(FontRole.tagline, topbar_cell.style.font_role); - const r = p.rects[0]; - const tag_cell = find_tag_cell: { - var x = r.x + config.GUTTER; - while (x < r.x + r.w) : (x += 1) { - const cell = surface.at(x, r.y); - if (!std.mem.eql(u8, cell.grapheme(), " ")) break :find_tag_cell cell; - } - return error.MissingTagText; - }; - try std.testing.expectEqual(FontRole.tagline, tag_cell.style.font_role); - try std.testing.expectEqual(FontRole.tagline, surface.at(r.x, r.y).style.font_role); - try std.testing.expectEqual(FontRole.tagline, surface.at(r.x + r.w - 1, r.y).style.font_role); - // The document/terminal page does not ride the chrome animation: acme's - // target background is already present in the same render. - const body = surface.at(r.x + r.w - 1, r.y + r.h - 1); - try std.testing.expectEqual(FontRole.body, body.style.font_role); - try std.testing.expectEqual( - Color{ .rgb = p.theme().bg.? }, - body.style.bg, - ); - try std.testing.expect(p.animationActive()); -} - test "native themes keep text and UI roles readable on their own surfaces" { const names = [_][]const u8{ "orchard", "dusk", "ink", "paper", "daybreak", "atelier", "forge", "lagoon", "solarium", "spectrum", "harvest", "clay", "forge_black", "forge_soft", "orchard_black" }; try std.testing.expectEqual(names.len, native_theme_count); |
