summaryrefslogtreecommitdiff
path: root/examples/live/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'examples/live/src/main.zig')
-rw-r--r--examples/live/src/main.zig144
1 files changed, 78 insertions, 66 deletions
diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig
index d7a514f..14058de 100644
--- a/examples/live/src/main.zig
+++ b/examples/live/src/main.zig
@@ -132,19 +132,21 @@ pub fn update(s: *State, in: *const sk.Input) void {
continue;
},
.mouse_down => {
- // the number token under the pointer, by the grammar's parse
- if (e.code != 1 or e.x < margin or e.y < margin) continue;
+ // the byte under the pointer, laid out as `code` draws it,
+ // and the number token there, by the grammar's parse
+ if (e.code != 1) continue;
var text: [Editor.capacity.text]u8 = undefined;
const t = s.ed.read(&text);
- const row: usize = @intFromFloat((e.y - margin) / cell[1]);
- const col: usize = @intFromFloat((e.x - margin) / cell[0]);
- var start: usize = 0;
- for (0..row) |_| start = (std.mem.indexOfScalarPos(u8, t, start, '\n') orelse t.len) + 1;
- if (start > t.len) continue;
- const end = std.mem.indexOfScalarPos(u8, t, start, '\n') orelse t.len;
- if (start + col >= end) continue;
+ const at = fba.allocator().alloc(sk.text.Caret, t.len + 1) catch continue;
+ _ = sk.text.places(t, paragraph(fba.allocator(), &s.ed, t), at);
+ var hit: ?usize = null;
+ for (at[0..t.len], 0..) |c, i| {
+ if (e.y - margin >= c.y and e.y - margin < c.y + c.h and e.x - margin >= c.x) hit = i;
+ }
+ const b = hit orelse continue;
+ if (t[b] == '\n') continue;
const tree = syn.parse(fba.allocator(), &s.ed, language()) orelse continue;
- const n = tree.rootNode().descendantForByteRange(@intCast(start + col), @intCast(start + col + 1)) orelse continue;
+ const n = tree.rootNode().descendantForByteRange(@intCast(b), @intCast(b + 1)) orelse continue;
if (!std.mem.eql(u8, n.kind(), "number")) continue;
const literal = t[n.startByte()..n.endByte()];
// ponytail: a number with an exponent is not dragged
@@ -369,11 +371,6 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void {
}
const scale = 1.25;
-/// A character of the code face at `scale`, wide and high, in pixels:
-/// measured (`f.host.textSizeIn(.code, 1.25, "M")` natively, and ten of
-/// them are 100), so `update` finds the byte under the pointer as `code`
-/// lays it out.
-const cell: [2]f32 = .{ 10, 22 };
const margin = 16;
const ink = 0xe8e8e8;
const red = 0xff5a50;
@@ -382,66 +379,81 @@ const also_selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 90 };
/// pardes's own theme: its text's ink and a class's.
const theme = &pe.syntax.themes[0];
+/// The code as a paragraph in the code face: a span in its class's ink
+/// where the grammar's class changes, in `a`. `code` draws it and `update`
+/// lays out the same to find the byte under the pointer.
+fn paragraph(a: std.mem.Allocator, ed: *const Editor, text: []const u8) sk.text.Paragraph {
+ const base = pe.syntax.ink(theme);
+ var p: sk.text.Paragraph = .{ .face = .code, .scale = scale, .color = .{ .r = base[0], .g = base[1], .b = base[2] } };
+ // a class a byte, from the grammar's highlights (none if it fails)
+ const classes = pe.Syntax.colors(a, ed, language(), 0, text.len) catch return p;
+ var runs: usize = 0;
+ for (classes, 0..) |class, i| runs += @intFromBool(i == 0 or classes[i - 1] != class);
+ const spans = a.alloc(sk.text.Span, runs) catch return p;
+ var n: usize = 0;
+ for (classes, 0..) |class, i| {
+ if (i > 0 and classes[i - 1] == class) continue;
+ const c = pe.syntax.synInk(theme, class) orelse base;
+ spans[n] = .{ .start = @intCast(i), .color = .{ .r = c[0], .g = c[1], .b = c[2] } };
+ n += 1;
+ }
+ p.spans = spans;
+ return p;
+}
+
+/// Byte `i`'s box, laid out at `margin`: from its place to the next byte's
+/// on its line; a `\n` and the end are `space` wide.
+fn box(at: []const sk.text.Caret, i: usize, space: f32) sk.Rect {
+ const right = if (i + 1 < at.len and at[i + 1].y == at[i].y) at[i + 1].x else at[i].x + space;
+ return .{ .x = margin + at[i].x, .y = margin + at[i].y, .w = right - at[i].x, .h = at[i].h };
+}
+
/// The editor's text over the picture on a dark band: its lines coloured
-/// by the grammar in pardes's theme, every
-/// range (the primary stronger), a caret at each range's head that says
-/// the mode (a block in normal and select, a bar in insert), the mode's
-/// name in the corner; the problem as a red mark under its byte and its
-/// words under the text.
-// ponytail: one face at one size, a column a fixed advance and a byte of
-// this ASCII language a column; sketchbook's paragraph call (spans, any
-// byte's place) takes the place of the measuring and the textIn here.
+/// by the grammar in pardes's theme, every range (the primary stronger), a
+/// caret at each range's head that says the mode (a block in normal and
+/// select, a bar in insert), the mode's name in the corner; the problem as
+/// a red mark under its byte and its words under the text. Every place is
+/// measured: one layout gives each byte's, another draws.
fn code(s: *const State, f: *sk.Frame) void {
const text = s.ed.bytes(f.scratch) catch return;
const ranges = f.scratch.create([pe.max_selections]pe.Selection) catch return;
const got = s.ed.ranges(f.scratch, ranges) catch return;
- // a class a byte, from the grammar's highlights (none if it fails)
- const colors = pe.Syntax.colors(f.scratch, &s.ed, language(), 0, text.len) catch &.{};
+ const p = paragraph(f.scratch, &s.ed, text);
+ const at = f.scratch.alloc(sk.text.Caret, text.len + 1) catch return;
+ const laid = sk.text.places(text, p, at);
+ const space = sk.text.size(.code, scale, " ")[0];
const problem = s.problem.len > 0;
- var lines: usize = 1;
- var widest: usize = 24;
- var it = std.mem.splitScalar(u8, text, '\n');
- while (it.next()) |line| : (lines += 1) widest = @max(widest, line.len + 1);
- if (problem) widest = @max(widest, s.problem.len);
- f.fill(.{ .x = 0, .y = 0, .w = @as(f32, @floatFromInt(widest)) * cell[0] + 2 * margin, .h = @as(f32, @floatFromInt(lines - 1 + @intFromBool(problem))) * cell[1] + 2 * margin }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 });
+ const words: [2]f32 = if (problem) sk.text.size(.code, scale, s.problem.text()) else .{ 0, 0 };
+ f.fill(.{ .x = 0, .y = 0, .w = @max(24 * space, laid.w + space, words[0]) + 2 * margin, .h = laid.h + words[1] + 2 * margin }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 });
- var y: f32 = margin;
- var start: usize = 0;
- it = std.mem.splitScalar(u8, text, '\n');
- while (it.next()) |line| : (y += cell[1]) {
- // a line's bytes and its newline, start..end
- const end = start + line.len + 1;
- for (ranges[0..got.n], 0..) |r, i| {
- const from = @max(@min(r.anchor, r.head), start);
- const to = @min(@max(r.anchor, r.head), end);
- if (from < to) f.fill(.{ .x = margin + @as(f32, @floatFromInt(from - start)) * cell[0], .y = y, .w = @as(f32, @floatFromInt(to - from)) * cell[0], .h = cell[1] }, if (i == got.primary) selected else also_selected);
- }
- // a run of bytes of one class at a time, in the theme's inks
- var a: usize = 0;
- while (a < line.len) {
- const class = if (start + a < colors.len) colors[start + a] else .none;
- var b = a + 1;
- while (b < line.len and (if (start + b < colors.len) colors[start + b] else .none) == class) b += 1;
- const c = pe.syntax.synInk(theme, class) orelse pe.syntax.ink(theme);
- f.textIn(.code, margin + @as(f32, @floatFromInt(a)) * cell[0], y, scale, line[a..b], sk.Color{ .r = c[0], .g = c[1], .b = c[2] });
- a = b;
+ // a range a run of boxes on each line it covers
+ for (ranges[0..got.n], 0..) |r, n| {
+ var i: usize = @min(r.anchor, r.head);
+ const to = @max(r.anchor, r.head);
+ while (i < to) {
+ var j = i + 1;
+ while (j < to and at[j].y == at[i].y) j += 1;
+ const first = box(at, i, space);
+ const last = box(at, j - 1, space);
+ f.fill(.{ .x = first.x, .y = first.y, .w = last.x + last.w - first.x, .h = first.h }, if (n == got.primary) selected else also_selected);
+ i = j;
}
- for (ranges[0..got.n], 0..) |r, i| {
- // the head's character: the one before it in a range that
- // grows forward
- const at = if (r.head > r.anchor) r.head - 1 else r.head;
- if (at < start or at >= end) continue;
- const x = margin + @as(f32, @floatFromInt(at - start)) * cell[0];
- const caret: sk.Color = .{ .r = 0xe8, .g = 0xe8, .b = 0xe8, .a = if (i == got.primary) 200 else 110 };
- if (s.ed.mode() == .insert) {
- f.fill(.{ .x = x - 1, .y = y, .w = 2, .h = cell[1] }, caret);
- } else f.fill(.{ .x = x, .y = y, .w = cell[0], .h = cell[1] }, caret.alpha(caret.a / 2));
- }
- if (problem and s.problem.at >= start and s.problem.at < end)
- f.fill(.{ .x = margin + @as(f32, @floatFromInt(s.problem.at - start)) * cell[0], .y = y + cell[1] - 2, .w = cell[0], .h = 2 }, red);
- start = end;
}
- if (problem) f.textIn(.code, margin, y, scale, s.problem.text(), red);
+ _ = f.paragraph(margin, margin, text, p);
+ for (ranges[0..got.n], 0..) |r, n| {
+ // the head's character: the one before it in a range that grows
+ // forward
+ const b = box(at, if (r.head > r.anchor) r.head - 1 else r.head, space);
+ const caret: sk.Color = .{ .r = 0xe8, .g = 0xe8, .b = 0xe8, .a = if (n == got.primary) 200 else 110 };
+ if (s.ed.mode() == .insert) {
+ f.fill(.{ .x = b.x - 1, .y = b.y, .w = 2, .h = b.h }, caret);
+ } else f.fill(b, caret.alpha(caret.a / 2));
+ }
+ if (problem) {
+ const b = box(at, @min(s.problem.at, text.len), space);
+ f.fill(.{ .x = b.x, .y = b.y + b.h - 2, .w = b.w, .h = 2 }, red);
+ f.textIn(.code, margin, margin + laid.h, scale, s.problem.text(), red);
+ }
const mode = switch (f.in.mode) {
.live => "live",