summaryrefslogtreecommitdiff
path: root/src/Layer.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
committerGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
commita479792a1ab2b593642d7f648194bfa23ce01dba (patch)
tree96ddee7d879a9084daa07db43b0b7910b072a785 /src/Layer.zig
parente179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff)
parent4c761893e5b3fdae422622cfa02104ab36633045 (diff)
downloadpardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz
pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'src/Layer.zig')
-rw-r--r--src/Layer.zig70
1 files changed, 57 insertions, 13 deletions
diff --git a/src/Layer.zig b/src/Layer.zig
index ba45bc88..609ddbc4 100644
--- a/src/Layer.zig
+++ b/src/Layer.zig
@@ -28,6 +28,18 @@ pub const Metrics = struct {
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 {
@@ -40,6 +52,7 @@ pub const TagHit = struct {
pixel_x: f32,
pixel_y: f32,
metrics: Metrics,
+ place: Place = .{},
};
kind: Kind = .body,
@@ -103,14 +116,22 @@ 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 pointAt(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?Point {
+ return pointIn(layer.kind, layer.place(), x, y, body_w, body_h, tagline_w, tagline_h);
+}
+
+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: web's exports).
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 (layer.kind != .body and metric < 1) return null;
+ if (kind != .body and metric < 1) return null;
}
const bw = @max(1, body_w);
const bh = @max(1, body_h);
@@ -122,7 +143,7 @@ fn pointAt(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, taglin
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 (layer.kind != .body or index < layer.context_rows) tw else bw;
+ 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))),
@@ -134,15 +155,39 @@ fn pointAt(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, taglin
/// 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;
- const point = layer.pointAt(x, y, body_w, body_h, tagline_w, tagline_h) orelse return null;
- return .{ .serial = layer.serial, .row = point.index, .col = point.col + config.GUTTER, .pixel_x = x, .pixel_y = y, .metrics = point.metrics };
+ 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;
- const point = layer.pointAt(x, y, body_w, body_h, tagline_w, tagline_h) orelse return null;
- return .{ .kind = layer.kind, .id = layer.id, .serial = layer.serial, .line = point.index, .col = point.col, .pixel_x = x, .pixel_y = y, .metrics = point.metrics };
+ 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" {
@@ -215,15 +260,14 @@ pub fn sameCell(a: ?TagHit, b: ?TagHit) bool {
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) for (p.surface.tagLayers()) |*layer| {
- if (layer.rows == 0 or layer.kind != kind or layer.id != id or layer.serial != point.serial or line >= layer.rows) continue;
+ 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(layer.viewport.x)) * bw;
- const right = @as(f32, @floatFromInt(layer.viewport.x + layer.viewport.w)) * bw;
+ 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(layer.viewport.y + line)) + 0.5) * bh;
- break;
- };
+ point.pixel_y = (@as(f32, @floatFromInt(v.y + line)) + 0.5) * bh;
+ }
return if (p.reprojectTagHit(point)) |mapped| mapped.col else null;
}