//! A band of cells a pixel shell draws over the grid at its own pitch: a //! pane's, a column's or the workspace's tag rows, a notice chip, or a file //! body whose compact tree-sitter context rows outnumber its grid rows. The //! grid under it holds a copy of what fits there (draw.zig joins them), so a //! terminal client, which has only the grid, loses nothing but the pitch. //! //! Its viewport is in grid cells, so compact text never moves a hit target. //! A tag's rows are each one grid row, their glyphs at the tagline's size; a //! body's first `context_rows` rows are stacked at the tagline's height, the //! rest at the body's. `rows == 0` is no layer. const std = @import("std"); const pardes = @import("pardes.zig"); const config = @import("config.zig"); const limits = @import("memory.zig").limits; const layout = @import("layout.zig"); const Layer = @This(); /// `notice` is a pane's transient line -- a builtin's name, a pending leader /// chord -- carried as a tagline-shaped band so it gets the tagline's height, /// small-font metrics, band offset and border instead of being a body-grid row /// wearing a tagline font. pub const Kind = enum(u8) { workspace, column, pane, notice, body }; pub const Metrics = struct { body_w: u16 = 1, body_h: u16 = 1, tagline_w: u16 = 1, tagline_h: u16 = 1, }; /// What a hit is resolved against: the layer it landed on, as the frame /// the frontend hit-tested had it. A hit carries it, so the step resolves /// the hit (again, or clamped back onto it) from the input, never from a /// frame: the frame is the process's own (Pardes.replica's `surface`), and /// a session never draws a replica's view. pub const Place = struct { viewport: pardes.Rect = .{}, cols: u16 = 0, rows: u16 = 0, context_rows: u16 = 0, }; /// A point on a tag, notice or header layer: which row of it and which /// column at the tagline's pitch. pub const TagHit = struct { kind: Kind, id: u16, serial: u32, col: u16, /// Which row of a taller tag. line: u16 = 0, pixel_x: f32, pixel_y: f32, metrics: Metrics, place: Place = .{}, }; kind: Kind = .body, /// The pane slot, or the column, it belongs to. id: u16 = 0, serial: u32 = 0, viewport: pardes.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 }, cols: u16 = 0, rows: u16 = 0, /// A body's compact rows, from its top. context_rows: u16 = 0, context_separators: std.bit_set.StaticBitSet(limits.wrap_rows) = .empty, /// `rows * cols`, row after row. cells: []pardes.Cell = &.{}, cursor: ?struct { x: u16, y: u16, bar: bool = false } = null, /// A band's fill, where its cells run out. bg: [3]u8 = .{ 0, 0, 0 }, /// Tag columns a viewport this wide holds at the tagline's pitch. pub fn capacity(layer: *const Layer, metrics: Metrics) u16 { return @intCast(@min(std.math.maxInt(u16), @max(1, @as(u32, layer.viewport.w) * @max(1, metrics.body_w) / @max(1, metrics.tagline_w)))); } pub fn setContextLines(layer: *Layer, lines: []const i32) void { layer.context_separators = .empty; const count = @min(lines.len, layer.context_rows); var index: usize = 0; while (index + 1 < count and index < limits.wrap_rows) : (index += 1) { if (lines[index + 1] != lines[index] +| 1) layer.context_separators.set(index); } } pub fn hasContextBorderAfter(layer: *const Layer, index: usize) bool { if (index >= layer.context_rows or index >= limits.wrap_rows) return false; return index + 1 == layer.context_rows or layer.context_separators.isSet(index); } /// Pixel top of row `index`: a body's compact rows stacked at the tagline's /// height, every other row a grid row. pub fn rowTop(layer: *const Layer, index: u16, body_h: f32, tagline_h: f32) f32 { const body = @max(1, body_h); const tag = std.math.clamp(tagline_h, 1, body); return @as(f32, @floatFromInt(layer.viewport.y)) * body + @as(f32, @floatFromInt(@min(index, layer.context_rows))) * tag + @as(f32, @floatFromInt(index -| layer.context_rows)) * body; } pub fn rowHeight(layer: *const Layer, index: u16, body_h: f32, tagline_h: f32) f32 { return if (index < layer.context_rows) std.math.clamp(tagline_h, 1, @max(1, body_h)) else @max(1, body_h); } const Point = struct { index: u16, col: u16, metrics: Metrics }; /// The row and column a window pixel lands on, each row at its own pitch: /// a tag's at the tagline's width, a body's compact rows too. pub fn place(layer: *const Layer) Place { return .{ .viewport = layer.viewport, .cols = layer.cols, .rows = layer.rows, .context_rows = layer.context_rows }; } fn pointIn(kind: Kind, layer: Place, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?Point { if (layer.rows == 0 or !std.math.isFinite(x) or !std.math.isFinite(y)) return null; // A band's hit is refused outright on metrics no host could have; a // body's has always been floored to a pixel instead, and both refuse // what is no number or past a u16 (a host's floats). for ([_]f32{ body_w, body_h, tagline_w, tagline_h }) |metric| { if (!std.math.isFinite(metric) or metric > std.math.maxInt(u16)) return null; if (kind != .body and metric < 1) return null; } const bw = @max(1, body_w); const bh = @max(1, body_h); const tw = std.math.clamp(tagline_w, 1, bw); const th = std.math.clamp(tagline_h, 1, bh); const dx = x - @as(f32, @floatFromInt(layer.viewport.x)) * bw; const dy = y - @as(f32, @floatFromInt(layer.viewport.y)) * bh; if (dx < 0 or dy < 0 or dx >= @as(f32, @floatFromInt(layer.viewport.w)) * bw or dy >= @as(f32, @floatFromInt(layer.viewport.h)) * bh) return null; const headers = @as(f32, @floatFromInt(layer.context_rows)) * th; const index: u16 = @intFromFloat(if (dy < headers) @floor(dy / th) else @as(f32, @floatFromInt(layer.context_rows)) + @floor((dy - headers) / bh)); if (index >= layer.rows) return null; const pitch = if (kind != .body or index < layer.context_rows) tw else bw; return .{ .index = index, .col = @intFromFloat(@min(@as(f32, @floatFromInt(layer.cols -| 1)), @floor(dx / pitch))), .metrics = .{ .body_w = @intFromFloat(bw), .body_h = @intFromFloat(bh), .tagline_w = @intFromFloat(tw), .tagline_h = @intFromFloat(th) }, }; } /// A body layer's pixel as the body cell under it (grid columns, from the /// pane's text edge). pub fn bodyHit(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?pardes.Mouse.BodyHit { if (layer.kind != .body) return null; return bodyHitIn(layer.place(), layer.serial, x, y, .{ .body_w = body_w, .body_h = body_h, .tagline_w = tagline_w, .tagline_h = tagline_h }); } /// A band's pixel as the tag cell under it. pub fn tagHit(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?TagHit { if (layer.kind == .body) return null; return tagHitIn(layer.kind, layer.id, layer.serial, layer.place(), x, y, .{ .body_w = body_w, .body_h = body_h, .tagline_w = tagline_w, .tagline_h = tagline_h }); } const Pixels = struct { body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32 }; fn pixelsOf(m: Metrics) Pixels { return .{ .body_w = @floatFromInt(m.body_w), .body_h = @floatFromInt(m.body_h), .tagline_w = @floatFromInt(m.tagline_w), .tagline_h = @floatFromInt(m.tagline_h) }; } fn bodyHitIn(at: Place, serial: u32, x: f32, y: f32, m: Pixels) ?pardes.Mouse.BodyHit { const point = pointIn(.body, at, x, y, m.body_w, m.body_h, m.tagline_w, m.tagline_h) orelse return null; return .{ .serial = serial, .row = point.index, .col = point.col + config.GUTTER, .pixel_x = x, .pixel_y = y, .metrics = point.metrics, .place = at }; } fn tagHitIn(kind: Kind, id: u16, serial: u32, at: Place, x: f32, y: f32, m: Pixels) ?TagHit { const point = pointIn(kind, at, x, y, m.body_w, m.body_h, m.tagline_w, m.tagline_h) orelse return null; return .{ .kind = kind, .id = id, .serial = serial, .line = point.index, .col = point.col, .pixel_x = x, .pixel_y = y, .metrics = point.metrics, .place = at }; } /// A body hit at its own pixel, on the place it carries. pub fn bodyAgain(hit: pardes.Mouse.BodyHit) ?pardes.Mouse.BodyHit { return bodyHitIn(hit.place, hit.serial, hit.pixel_x, hit.pixel_y, pixelsOf(hit.metrics)); } /// A tag hit at its own pixel, on the place it carries. pub fn tagAgain(hit: TagHit) ?TagHit { return tagHitIn(hit.kind, hit.id, hit.serial, hit.place, hit.pixel_x, hit.pixel_y, pixelsOf(hit.metrics)); } test "compact body layer rows fill reclaimed height and invert exact boundaries" { const layer: Layer = .{ .serial = 7, .viewport = .{ .x = 4, .y = 3, .w = 40, .h = 10 }, .cols = 40, .rows = 15, .context_rows = 5 }; try std.testing.expectEqual(@as(f32, 120), layer.rowTop(5, 20, 12)); try std.testing.expectEqual(@as(f32, 240), layer.rowTop(11, 20, 12)); try std.testing.expectEqual(@as(f32, 12), layer.rowHeight(4, 20, 12)); try std.testing.expectEqual(@as(f32, 20), layer.rowHeight(5, 20, 12)); for (0..12) |index| { const r: u16 = @intCast(index); const y = layer.rowTop(r, 20, 12) + layer.rowHeight(r, 20, 12) / 2; const width: f32 = if (r < 5) 6 else 10; const hit = layer.bodyHit(40 + 3.5 * width, y, 10, 20, 6, 12).?; try std.testing.expectEqual(r, hit.row); try std.testing.expectEqual(@as(u16, 3 + config.GUTTER), hit.col); } try std.testing.expectEqual(@as(u16, 4), layer.bodyHit(45, 119.99, 10, 20, 6, 12).?.row); try std.testing.expectEqual(@as(u16, 5), layer.bodyHit(45, 120, 10, 20, 6, 12).?.row); try std.testing.expectEqual(@as(u16, 11), layer.bodyHit(45, 259.99, 10, 20, 6, 12).?.row); try std.testing.expect(layer.bodyHit(45, 260, 10, 20, 6, 12) == null); try std.testing.expect(layer.bodyHit(39.99, 80, 10, 20, 6, 12) == null); try std.testing.expect(layer.bodyHit(std.math.nan(f32), 80, 10, 20, 6, 12) == null); } test "compact context borders separate source gaps and preserve multiline headers" { var layer: Layer = .{ .context_rows = 6 }; layer.setContextLines(&.{ 3, 4, 8, 9, 10, 17 }); const expected = [_]bool{ false, true, false, false, true, true, false }; for (expected, 0..) |border, index| try std.testing.expectEqual(border, layer.hasContextBorderAfter(index)); layer.context_rows = 2; layer.setContextLines(&.{ 9, 10 }); try std.testing.expect(!layer.hasContextBorderAfter(0)); try std.testing.expect(layer.hasContextBorderAfter(1)); try std.testing.expect(!layer.hasContextBorderAfter(4)); try std.testing.expect(!layer.context_separators.isSet(4)); } test "tag text capacity uses its own pitch and excludes the physical grip" { const layer: Layer = .{ .kind = .pane, .id = 2, .serial = 7, .viewport = .{ .x = 12, .y = 4, .w = 38, .h = 1 }, .cols = 90, .rows = 1 }; try std.testing.expectEqual(@as(u16, 63), layer.capacity(.{ .body_w = 10, .tagline_w = 6 })); try std.testing.expect(layer.tagHit(119.99, 85, 10, 20, 6, 12) == null); try std.testing.expectEqual(@as(u16, 0), layer.tagHit(120, 85, 10, 20, 6, 12).?.col); try std.testing.expectEqual(@as(u16, 50), layer.tagHit(423, 85, 10, 20, 6, 12).?.col); try std.testing.expect(layer.tagHit(500, 85, 10, 20, 6, 12) == null); try std.testing.expect(layer.tagHit(120, 85, std.math.nan(f32), 20, 6, 12) == null); try std.testing.expect(layer.tagHit(120, 85, 10, 20, 0, 12) == null); } test "a taller tag's rows are grid rows, each hit on its own line" { const layer: Layer = .{ .kind = .pane, .id = 2, .serial = 7, .viewport = .{ .x = 12, .y = 4, .w = 38, .h = 3 }, .cols = 90, .rows = 3 }; try std.testing.expectEqual(@as(u16, 0), layer.tagHit(120, 80, 10, 20, 6, 12).?.line); try std.testing.expectEqual(@as(u16, 1), layer.tagHit(120, 100, 10, 20, 6, 12).?.line); try std.testing.expectEqual(@as(u16, 2), layer.tagHit(120, 139.99, 10, 20, 6, 12).?.line); try std.testing.expect(layer.tagHit(120, 140, 10, 20, 6, 12) == null); try std.testing.expect(layer.bodyHit(120, 100, 10, 20, 6, 12) == null); } // Pointer hits on tag bands: comparing two, and bringing one that left // its band back onto it. /// Whether two pointer hits name the same cell of the same tag line. pub fn sameCell(a: ?TagHit, b: ?TagHit) bool { const first = a orelse return b == null; const second = b orelse return false; return first.kind == second.kind and first.id == second.id and first.serial == second.serial and first.line == second.line and first.col == second.col; } /// The tag column a host's pointer is at. Clamped, a point off the tag is /// brought back onto line `line` of it, for a drag that leaves it. pub fn columnAt(p: *const pardes.Pardes, kind: Kind, id: usize, supplied: ?TagHit, clamp: bool, line: u16) ?u16 { var point = supplied orelse return null; if (point.kind != kind or point.id != id) return null; if (clamp and line < point.place.rows) { const v = point.place.viewport; const bw: f32 = @floatFromInt(point.metrics.body_w); const bh: f32 = @floatFromInt(point.metrics.body_h); const left = @as(f32, @floatFromInt(v.x)) * bw; const right = @as(f32, @floatFromInt(v.x + v.w)) * bw; point.pixel_x = std.math.clamp(point.pixel_x, left, @max(left, right - 0.001)); point.pixel_y = (@as(f32, @floatFromInt(v.y + line)) + 0.5) * bh; } return if (layout.reprojectTagHit(p, point)) |mapped| mapped.col else null; } /// A tag caret `w` wide at glyph `x`, its left and right edges in the unit /// `cell` (a grid cell's width) is in, glyphs `pitch` apart: past a full /// row's end it sits at the tag's right edge, where the grid's is clamped. pub fn caretSpan(layer: *const Layer, x: u16, cell: f32, pitch: f32, w: f32) [2]f32 { const left = @as(f32, @floatFromInt(layer.viewport.x)) * cell; const right = left + @as(f32, @floatFromInt(layer.viewport.w)) * cell; const x0 = @min(left + @as(f32, @floatFromInt(x)) * pitch, right - w); return .{ x0, @min(right, x0 + w) }; } pub fn taglineBandOffset(row: u16, canvas_h: f32, cell_h: u32, tagline_h: u32) u32 { const spare = cell_h -| tagline_h; const rows: u32 = @max(1, @as(u32, @intFromFloat(@floor(canvas_h / @as(f32, @floatFromInt(cell_h)))))); // Every band centers in its cell, so text and anchors sit at the same // height in the workspace, column and pane tags alike. Only the last row // faces the window edge (Tagbottom). if (@as(u32, row) + 1 >= rows) return spare; return spare / 2; } pub fn topbarPaneBorderPixels(cell_h: u32, tagline_h: u32) u32 { const spare = cell_h -| tagline_h; return @min(@as(u32, config.gui_topbar_pane_border_px), spare * 2); } test "tagline bands center in their cells and Tagbottom faces the window edge" { const cell_h: u32 = 20; const tagline_h: u32 = 16; const canvas_h: f32 = 200; // Workspace, column and pane rows alike: text lines up across bands. for ([_]u16{ 0, 1, 2, 5, 8 }) |row| try std.testing.expectEqual(@as(u32, 2), taglineBandOffset(row, canvas_h, cell_h, tagline_h)); try std.testing.expectEqual(@as(u32, 4), taglineBandOffset(9, canvas_h, cell_h, tagline_h)); }