diff options
Diffstat (limited to 'src/Layer.zig')
| -rw-r--r-- | src/Layer.zig | 104 |
1 files changed, 52 insertions, 52 deletions
diff --git a/src/Layer.zig b/src/Layer.zig index aa346413..78dfa069 100644 --- a/src/Layer.zig +++ b/src/Layer.zig @@ -179,6 +179,58 @@ 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)); } +// 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. + 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 "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)); @@ -234,58 +286,6 @@ test "a taller tag's rows are grid rows, each hit on its own line" { 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. - 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 the last row faces the window edge" { const cell_h: u32 = 20; const tagline_h: u32 = 16; |
