diff options
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/gui.zig | 136 |
1 files changed, 135 insertions, 1 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index c2a4cb4e..01a58eae 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1310,7 +1310,9 @@ const Cover = packed struct(u16) { pane_grip: bool = false, /// In a grip's first row, where its button is. grip_top: bool = false, - _: u4 = 0, + /// In an unfocused pane's tag or body rows while InactiveDim fades them. + dim: bool = false, + _: u3 = 0, }; /// Marks the grid cells a body or tag layer covers, which the layers paint @@ -1341,6 +1343,10 @@ fn coverFrame(cover: []Cover, cols: u16, rows: u16, body_layers: []const pardes. for (regions) |region| { if (region.kind == .column_grip) bar = region.rect; } + // InactiveDim: an unfocused pane's tag and body rows (Dim.begin). + if (dim.amount > 0) for (regions) |region| { + if ((region.kind == .tag or region.kind == .body) and dim.of(region.owner)) mark.rect(cover, cols, rows, region.rect, "dim", true); + }; for (regions) |region| switch (region.kind) { .grip, .column_grip => { mark.rect(cover, cols, rows, region.rect, "grip", true); @@ -5440,6 +5446,7 @@ fn renderFrame( const cells: u32 = @as(u32, surface.cols) * surface.rows; try g.cover.resize(gpa, cells); const covered = g.cover.items[0..cells]; + dim.begin(core.settings.inactive_dim, surface.regionList()); coverFrame(covered, surface.cols, surface.rows, surface.bodyLayers(), surface.tagLayers(), surface.regionList()); try buildDecor(g, gpa, surface, &groups, layout, win_w, win_h, cursors_shown); @@ -5828,6 +5835,7 @@ fn emitTagLayer(g: *Gui, instances: [*]CellInstance, next: *u32, layer: *const p // A window is rarely a whole number of cells wide: a band that ends at // the last column runs on through the leftover pixels to the edge. const band_right = if (win_w - right < bw) @max(right, win_w) else right; + const faded = (layer.kind == .pane or layer.kind == .notice) and dim.of(layer.id); // A row to each line, each a grid row with the tagline's glyphs. for (0..layer.rows) |index| { const y = layer.viewport.y + @as(u16, @intCast(index)); @@ -5841,6 +5849,7 @@ fn emitTagLayer(g: *Gui, instances: [*]CellInstance, next: *u32, layer: *const p const cursor = if (layer.cursor) |cu| show_cursor and !cu.bar and cu.x == col and cu.y == index else false; emitInstance(g, instances, next.*, @intCast(col), y, .{ .w = tw, .h = bh, .x_off = left, .y_off = slide }, win_w, win_h, .tagline, cells, col, line_cursor, cursor, page); clipInstance(&instances[next.*], .{ -1e9, if (slide != 0) top else -1e9, right, 1e9 }, win_w, win_h); + if (faded and !cursor) dim.fade(&instances[next.*]); next.* += 1; } } @@ -5862,6 +5871,7 @@ fn emitBodyLayer(g: *Gui, instances: [*]CellInstance, next: *u32, layer: *const const left = @as(f32, @floatFromInt(layer.viewport.x)) * bw; const right = left + @as(f32, @floatFromInt(layer.viewport.w)) * bw; const bottom = @as(f32, @floatFromInt(layer.viewport.y + layer.viewport.h)) * bh; + const faded = dim.of(layer.id); for (0..layer.rows) |index| { const row: u16 = @intCast(index); const top = layer.rowTop(row, bh, th); @@ -5885,6 +5895,7 @@ fn emitBodyLayer(g: *Gui, instances: [*]CellInstance, next: *u32, layer: *const const glyph_top = top - (if (header) @as(f32, @floatFromInt(taglineBandOffset(0, win_h, g.cell_h, g.tagline_height))) else 0); emitInstance(g, instances, next.*, 0, 0, .{ .w = width, .h = height, .x_off = left + @as(f32, @floatFromInt(col)) * width, .y_off = glyph_top }, win_w, win_h, role, line, col, line_cursor, cursor, page); clipInstance(&instances[next.*], .{ -1e9, -1e9, right, bottom }, win_w, win_h); + if (faded and !cursor) dim.fade(&instances[next.*]); next.* += 1; } if (layer.hasContextBorderAfter(row)) { @@ -6077,10 +6088,12 @@ fn emitSurfaceCell( instances[next.*].x0 += @as(f32, @floatFromInt(inset)) / win_w * 2; instances[next.*].u0 += @as(f32, @floatFromInt(inset)) / atlas_w; } + if (here.dim and !is_cursor) dim.fade(&instances[next.*]); next.* += 1; return; } emitInstance(g, instances, next.*, col, row, body_layout, win_w, win_h, role, line, at, cursor, is_cursor, page); + if (line_cover[at].dim and !is_cursor) dim.fade(&instances[next.*]); next.* += 1; } @@ -7642,6 +7655,127 @@ fn updateDisplayScale(window: *c.SDL_Window) void { display_scale = std.math.clamp(display_scale, 1, 4); } +/// InactiveDim: the unfocused panes' text faded toward its ground by the +/// setting's percent, never below the pair's own contrast or 4.5, whichever +/// is lower. The window's alone, from the regions' focus: renderFrame +/// begins it each frame. ponytail: one window per process, as display_scale. +var dim: Dim = .{}; + +const Dim = struct { + amount: f32 = 0, + /// The pane with the keyboard, from its regions. + focused: u16 = pardes.Region.none, + /// The few ink and paper pairs a frame has, each faded once. + keys: [32]?[6]u8 = @splat(null), + values: [32][3]u8 = undefined, + + fn begin(d: *Dim, percent: u8, regions: []const pardes.Region) void { + d.* = .{ .amount = @as(f32, @floatFromInt(@min(percent, 100))) / 100 }; + for (regions) |region| if (region.active and (region.kind == .grip or region.kind == .tag or region.kind == .body)) { + d.focused = region.owner; + break; + }; + if (d.focused == pardes.Region.none) d.amount = 0; + } + + /// Whether pane `owner`'s text fades this frame. + fn of(d: *const Dim, owner: u16) bool { + return d.amount > 0 and owner != d.focused; + } + + /// A cell instance's ink faded toward its ground. + fn fade(d: *Dim, instance: *CellInstance) void { + const ink = d.faded(byteRgb(.{ instance.fr, instance.fg, instance.fb }), byteRgb(.{ instance.br, instance.bg, instance.bb })); + instance.fr = @as(f32, @floatFromInt(ink[0])) / 255.0; + instance.fg = @as(f32, @floatFromInt(ink[1])) / 255.0; + instance.fb = @as(f32, @floatFromInt(ink[2])) / 255.0; + } + + fn faded(d: *Dim, ink: [3]u8, paper: [3]u8) [3]u8 { + const key = ink ++ paper; + const slot = (@as(usize, ink[0]) *% 31 +% @as(usize, ink[1]) *% 7 +% ink[2] +% @as(usize, paper[0]) *% 13 +% @as(usize, paper[1]) *% 3 +% paper[2]) % d.keys.len; + if (d.keys[slot]) |held| if (std.mem.eql(u8, &held, &key)) return d.values[slot]; + // Contrast falls as the ink nears its paper: the most of `amount` + // that keeps the pair at its own contrast or 4.5, whichever is lower. + const floor = @min(pardes.colors.contrast(ink, paper), 4.5); + var t = d.amount; + if (pardes.colors.contrast(blendLinear(ink, paper, t), paper) < floor) { + var lo: f32 = 0; + var hi: f32 = t; + for (0..8) |_| { + const mid = (lo + hi) / 2; + if (pardes.colors.contrast(blendLinear(ink, paper, mid), paper) >= floor) lo = mid else hi = mid; + } + t = lo; + } + d.keys[slot] = key; + d.values[slot] = blendLinear(ink, paper, t); + return d.values[slot]; + } + + fn byteRgb(rgb: [3]f32) [3]u8 { + var out: [3]u8 = undefined; + for (&out, rgb) |*o, v| o.* = @intFromFloat(@round(std.math.clamp(v, 0, 1) * 255)); + return out; + } +}; + +/// `t` of the way from one colour to the other in linear light, so a +/// coloured ink does not muddy on its way to the paper. +fn blendLinear(from: [3]u8, to: [3]u8, t: f32) [3]u8 { + var out: [3]u8 = undefined; + for (&out, from, to) |*o, a, b| { + const l = pardes.colors.to_linear[a] * (1 - t) + pardes.colors.to_linear[b] * t; + o.* = encoded[@intFromFloat(@round(std.math.clamp(l, 0, 1) * (encoded.len - 1)))]; + } + return out; +} + +/// Linear light back to an sRGB channel, in 4096 steps. +const encoded: [4096]u8 = blk: { + @setEvalBranchQuota(1_000_000); + var table: [4096]u8 = undefined; + for (&table, 0..) |*out, i| { + const l = @as(f64, @floatFromInt(i)) / 4095; + const v = if (l <= 0.0031308) l * 12.92 else 1.055 * std.math.pow(f64, l, 1.0 / 2.4) - 0.055; + out.* = @intFromFloat(@round(v * 255)); + } + break :blk table; +}; + +test "InactiveDim fades the unfocused panes' rows alone, never below the text's floor" { + const gpa = std.testing.allocator; + const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 24 }); + defer core.deinit(); + _ = try core.newShell(1, ""); + try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); + core.sync(); + const s = try core.render(core.scratch.allocator()); + defer dim = .{}; + dim.begin(60, s.regionList()); + try std.testing.expect(dim.focused != pardes.Region.none); + var cover: [80 * 24]Cover = undefined; + coverFrame(&cover, 80, 24, &.{}, &.{}, s.regionList()); + var seen: [2]bool = .{ false, false }; + for (s.regionList()) |region| if (region.kind == .body or region.kind == .tag) { + const unfocused = region.owner != dim.focused; + try std.testing.expectEqual(unfocused, cover[@as(usize, region.rect.y) * 80 + region.rect.x].dim); + seen[@intFromBool(unfocused)] = true; + } else if (region.kind == .grip) try std.testing.expect(!cover[@as(usize, region.rect.y) * 80 + region.rect.x].dim); + try std.testing.expect(seen[0] and seen[1]); + // Toward the ground, a pair already near its floor no further. + var instance: CellInstance = std.mem.zeroes(CellInstance); + instance.fr = 0.9; + instance.fg = 0.9; + instance.fb = 0.9; + dim.fade(&instance); + try std.testing.expect(instance.fr < 0.9 and instance.fr > 0); + try std.testing.expect(pardes.colors.contrast(Dim.byteRgb(.{ instance.fr, instance.fg, instance.fb }), .{ 0, 0, 0 }) >= 4.5 - 1e-3); + // Off, nothing fades. + dim.begin(0, s.regionList()); + try std.testing.expect(!dim.of(0) and !dim.of(1)); +} + /// A logical pixel size in the display's pixels: rounded, at least one. fn dp(logical: f32) f32 { return @max(1, @round(logical * display_scale)); |
