diff options
| author | t <t@tjp.lol> | 2026-11-24 22:30:27 -0700 |
|---|---|---|
| committer | t <t@tjp.lol> | 2026-11-24 22:45:56 -0700 |
| commit | 17e3a3589d8482f545bb5956a4f7ea01c646698f (patch) | |
| tree | a828c6dd2138e7cb9e4b1bfa810e89f50ab5eed9 /src/lab_ui.zig | |
| parent | 3594d230f5368d4200e590654aad040432bee82e (diff) | |
Expand writing app interface and add Markdown support
Diffstat (limited to 'src/lab_ui.zig')
| -rw-r--r-- | src/lab_ui.zig | 514 |
1 files changed, 462 insertions, 52 deletions
diff --git a/src/lab_ui.zig b/src/lab_ui.zig index a66513a..c92c2c2 100644 --- a/src/lab_ui.zig +++ b/src/lab_ui.zig @@ -1,6 +1,5 @@ -//! The Lab's native controls: the strip along the bottom (Save, Open, Overflow, Lab), the -//! menu the Lab button opens, and the card shown while a trim or pass is being reviewed. -//! Nothing here knows what a pass does: every control targets the controller's selectors. +//! The Lab's bottom strip, in-window chooser and guide, and review card. +//! Nothing here knows what a pass does: the controls invoke the controller's selectors. const std = @import("std"); const objc = @import("objc.zig"); const msg = objc.msg; @@ -11,9 +10,11 @@ const NSPoint = objc.NSPoint; const NSSize = objc.NSSize; extern const NSViewFrameDidChangeNotification: id; +extern const NSWindowDidResignKeyNotification: id; +extern const NSFontAttributeName: id; +extern const NSForegroundColorAttributeName: id; /// The trim level on show, as its `labPass:` tag (6…9), or null when nothing is proposed. -/// The menu ticks it; set it whenever a proposal starts or ends. pub var pending: ?u8 = null; const strip_h = 40; // the bar the word count lives in (main's status_h) @@ -56,12 +57,9 @@ const passes = [_]struct { title: []const u8, tag: isize }{ .{ .title = "Mark words that don’t fit the tone", .tag = 4 }, .{ .title = "Mark hedges and filler", .tag = 5 }, }; -const trims = [_]struct { title: []const u8, tag: isize }{ - .{ .title = "Slight trim ~10%", .tag = 6 }, - .{ .title = "Tighten more ~20%", .tag = 7 }, - .{ .title = "Even sharper ~30%", .tag = 8 }, - .{ .title = "Cut in half ~50%", .tag = 9 }, -}; +const trims = [_][]const u8{ "Original", "Slight trim", "Tighten more", "Even sharper", "Cut in half" }; +const percents = [_][]const u8{ "", "~10%", "~20%", "~30%", "~50%" }; +const privacy = "Your draft is sent only when you pick a pass."; const guide = \\Each idea asks the AI about your draft and only marks or fades words. It never rewrites. @@ -158,7 +156,7 @@ pub fn state(dirty: bool, overflow: bool) void { fn tint() void { for (bar, tracking, 0..) |b, area, i| { if (b == null) continue; - const active = (i == 0 and saved_dirty) or (i == 2 and overflow_open) or (i == 3 and has_note); + const active = (i == 0 and saved_dirty) or (i == 2 and overflow_open) or (i == 3 and (has_note or (chooser_open and writing_at == null))); msg(void, b, "setContentTintColor:", .{if (hovering != null and hovering == area) bright else if (active) accent else ink}); } } @@ -180,9 +178,13 @@ pub fn update(tools_on: bool, note: []const u8, review_on: bool, can_apply: bool if (content == null) return; tools = tools_on; review = review_on; - if (!tools) hovering = null; + if (!tools) { + closePanel(); + hovering = null; + } has_note = note.len != 0; tint(); + tintPanel(); msg(void, note_field, "setToolTip:", .{objc.str(note)}); msg(void, hint_field, "setStringValue:", .{objc.str(if (!review) "" else if (can_apply) "Keep skips this change · Apply rest edits the draft" else "Next visits a mark · Keep clears it")}); msg(void, note_field, "setStringValue:", .{objc.str(note)}); @@ -190,56 +192,458 @@ pub fn update(tools_on: bool, note: []const u8, review_on: bool, can_apply: bool relayout(); } -// --- The Lab menu --------------------------------------------------------------------------- +// --- The Lab chooser ------------------------------------------------------------------------ + +const panel_w = 620; +const panel_margin = 10; +const panel_bottom = 50; // just above the trigger, so LAB can also close it +const row_h = 32.5; +const trim_h = 44; +const trim_gap = 4; +const first_trim = 1 + passes.len; +const done_index = first_trim + trims.len; -fn item(menu: id, title: []const u8, action: [*:0]const u8, tag: isize, on: bool, target: id) void { - const it = msg(id, msg(id, objc.class("NSMenuItem"), "alloc", .{}), "initWithTitle:action:keyEquivalent:", .{ objc.str(title), if (target != null) objc.sel(action) else null, objc.str("") }); - msg(void, it, "setTarget:", .{target}); - msg(void, it, "setTag:", .{tag}); - msg(void, it, "setEnabled:", .{target != null}); // no target: a note, not a command - msg(void, it, "setState:", .{@as(isize, if (on) 1 else 0)}); - msg(void, menu, "addItem:", .{it}); - msg(void, it, "release", .{}); +var chooser_open = false; +var guide_open = false; +var shield: id = null; +var panel: id = null; +var panel_scroll: id = null; +var paper: id = null; +var panel_btn: [done_index + 1]id = .{null} ** (done_index + 1); +var rules: [2]id = .{ null, null }; +var privacy_note: id = null; +var guide_text: id = null; +var panel_font: id = null; +var compact_font: id = null; +var surface: id = null; +var edge: id = null; +var highlight: id = null; +var draft_view: id = null; +var return_focus: id = null; +var click_focus: id = null; +var hover_row: id = null; +var trim_cols: usize = trims.len; + +const writing_actions = [_][*:0]const u8{ "alternative:", "aiAlternative:", "ghost:", "stash:" }; +const writing_keys = [_][]const u8{ "a", "g", "/", "x" }; +const writing_shortcuts = [_][]const u8{ "Ctrl+Shift+A", "Ctrl+Shift+G", "Ctrl+/", "Ctrl+Shift+X" }; +var writing_btn: [4]id = .{null} ** 4; +var writing_at: ?NSPoint = null; + +fn rect(x: f64, y: f64, w: f64, h: f64) NSRect { + return .{ .origin = .{ .x = x, .y = y }, .size = .{ .width = w, .height = h } }; } -fn rule(menu: id) void { - msg(void, menu, "addItem:", .{msg(id, objc.class("NSMenuItem"), "separatorItem", .{})}); +fn number(i: usize) f64 { + return @floatFromInt(i); } -/// The Lab's menu, popped up at `sender` (the Lab button if null). -pub fn openMenu(sender: id) void { - if (content == null) return; - const menu = msg(id, msg(id, objc.class("NSMenu"), "alloc", .{}), "initWithTitle:", .{objc.str("Lab")}); - defer msg(void, menu, "release", .{}); - msg(void, menu, "setAutoenablesItems:", .{false}); +fn panelWindow() id { + return msg(id, content, "window", .{}); +} + +fn triggerMouseDown(self: id, _: SEL, event: id) callconv(.c) void { + click_focus = msg(id, panelWindow(), "firstResponder", .{}); + defer click_focus = null; + objc.msgSuper(void, self, "NSButton", "mouseDown:", .{event}); +} + +pub fn togglePanel() void { + if (chooser_open) { + if (writing_at == null) return closePanel(); + closePanel(); + } + if (content == null or !tools) return; + guide_open = false; + openPanel(click_focus); + tint(); +} + +pub fn canOpenWritingMenu() bool { + return content != null and tools and !chooser_open; +} + +pub fn openWritingMenu(point: NSPoint, titles: [4][]const u8) void { + writing_at = msg(NSPoint, content, "convertPoint:fromView:", .{ point, @as(id, null) }); + for (writing_btn, titles) |b, title| msg(void, b, "setTitle:", .{objc.str(title)}); + openPanel(null); +} + +fn openPanel(prior: id) void { + const win = panelWindow(); + return_focus = prior orelse msg(id, win, "firstResponder", .{}); + if (return_focus == bar[3] or return_focus == win) return_focus = draft_view; + chooser_open = true; + msg(void, panel_scroll, "setHidden:", .{writing_at != null}); + for (writing_btn) |b| msg(void, b, "setHidden:", .{writing_at == null}); + msg(void, msg(id, panel, "layer", .{}), "setCornerRadius:", .{@as(f64, if (writing_at != null) 12 else 8)}); + msg(void, shield, "setHidden:", .{false}); + layoutPanel(); + focusRow(0); +} + +fn closePanel() void { + if (!chooser_open) return; + const win = panelWindow(); + const first = msg(id, win, "firstResponder", .{}); + const owns_focus = msg(bool, first, "isKindOfClass:", .{objc.class("NSView")}) and msg(bool, first, "isDescendantOf:", .{shield}); + const was_writing = writing_at != null; + chooser_open = false; + writing_at = null; + hover_row = null; + msg(void, shield, "setHidden:", .{true}); + if (owns_focus) { + const sel = if (was_writing) msg(objc.NSRange, draft_view, "selectedRange", .{}) else objc.NSRange{}; + const available = msg(id, return_focus, "window", .{}) == win and !msg(bool, return_focus, "isHiddenOrHasHiddenAncestor", .{}); + if (!available or !msg(bool, win, "makeFirstResponder:", .{return_focus})) { + _ = msg(bool, win, "makeFirstResponder:", .{draft_view}); + } + if (was_writing and !std.meta.eql(sel, msg(objc.NSRange, draft_view, "selectedRange", .{}))) + msg(void, draft_view, "setSelectedRange:", .{sel}); + } + return_focus = null; + tint(); +} + +fn dismiss(_: id, _: SEL, _: id) callconv(.c) void { + closePanel(); +} + +fn panelIndex(b: id) usize { + return std.mem.indexOfScalar(id, &panel_btn, b).?; +} + +fn choose(_: id, _: SEL, sender: id) callconv(.c) void { + if (!chooser_open) return; + if (writing_at != null) { + const i = std.mem.indexOfScalar(id, &writing_btn, sender) orelse return; + closePanel(); + return msg(void, ctl, writing_actions[i], .{sender}); + } + const i = panelIndex(sender); + if (i == 0) { + guide_open = true; + hover_row = null; + layoutPanel(); + focusRow(done_index); + return; + } + closePanel(); + if (i == done_index) return; + if (i == first_trim) return msg(void, ctl, "labOriginal:", .{sender}); + msg(void, ctl, "labPass:", .{sender}); +} + +fn focusRow(i: usize) void { + const b = if (writing_at != null) writing_btn[i] else panel_btn[i]; + _ = msg(bool, panelWindow(), "makeFirstResponder:", .{b}); + _ = msg(bool, b, "scrollRectToVisible:", .{msg(NSRect, b, "bounds", .{})}); + tintPanel(); +} + +fn acceptsFocus(_: id, _: SEL) callconv(.c) bool { + return true; +} + +fn panelKeys(self: id, _: SEL, event: id) callconv(.c) void { + const mods = msg(usize, event, "modifierFlags", .{}); + const code = msg(u16, event, "keyCode", .{}); + if (mods & (0x40000 | 0x80000 | 0x100000) == 0) { // Control, Option, Command + hover_row = null; + switch (code) { + 53 => return closePanel(), // Escape + 36, 49, 76 => return msg(void, self, "performClick:", .{self}), // Return, Space, keypad Enter + else => {}, + } + if (writing_at != null) { + const i = std.mem.indexOfScalar(id, &writing_btn, self).?; + const n = writing_btn.len; + switch (code) { + 123, 126 => return focusRow((i + n - 1) % n), + 124, 125 => return focusRow((i + 1) % n), + 48 => return focusRow((i + if (mods & 0x20000 != 0) n - 1 else @as(usize, 1)) % n), + else => {}, + } + } else switch (code) { + 48, 123...126 => { + if (guide_open) return focusRow(done_index); + const i = panelIndex(self); + const next = switch (code) { + 48 => if (mods & 0x20000 != 0) (i + done_index - 1) % done_index else (i + 1) % done_index, // Tab + 123 => if (i == 0) done_index - 1 else i - 1, + 124 => (i + 1) % done_index, + 125 => if (i < first_trim) i + 1 else if (i + trim_cols < done_index) i + trim_cols else 0, + 126 => if (i == 0) done_index - 1 else if (i < first_trim) i - 1 else if (i >= first_trim + trim_cols) i - trim_cols else first_trim - 1, + else => unreachable, + }; + return focusRow(next); + }, + else => {}, + } + } + objc.msgSuper(void, self, "NSButton", "keyDown:", .{event}); +} + +fn cg(color: id) id { + return msg(id, color, "CGColor", .{}); +} + +fn activeTrim(i: usize) bool { + if (i < first_trim or i >= done_index) return false; + return if (pending) |p| i == @as(usize, p) + 2 else i == first_trim; +} + +fn tintPanel() void { + if (!chooser_open) return; + if (writing_at != null) { + for (writing_btn) |b| msg(void, b, "setNeedsDisplay:", .{true}); + return; + } + for (panel_btn, 0..) |b, i| { + msg(void, b, "setNeedsDisplay:", .{true}); + if (i >= first_trim and i < done_index) msg(void, b, "setAccessibilityValue:", .{objc.str(if (activeTrim(i)) "Selected" else "Not selected")}); + } +} + +fn panelEnter(self: id, _: SEL, _: id) callconv(.c) void { + if (writing_at != null) return focusRow(std.mem.indexOfScalar(id, &writing_btn, self) orelse return); + hover_row = self; + tintPanel(); +} + +fn panelExit(self: id, _: SEL, _: id) callconv(.c) void { + if (hover_row != self) return; + hover_row = null; + tintPanel(); +} + +const Align = enum { left, center, right }; +fn drawString(title: id, font: id, color: id, box: NSRect, alignment: Align) void { + const attrs = msg(id, objc.class("NSDictionary"), "dictionaryWithObjects:forKeys:count:", .{ + &[_]id{ font, color }, &[_]id{ NSFontAttributeName, NSForegroundColorAttributeName }, @as(usize, 2), + }); + const size = msg(NSSize, title, "sizeWithAttributes:", .{attrs}); + const w = @min(size.width, box.size.width); + var frame = box; + frame.size.width = w + 0.5; // leave room for the final glyph's fractional advance + frame.origin.x += switch (alignment) { + .left => 0, + .center => (box.size.width - w) / 2, + .right => box.size.width - w, + }; + msg(void, title, "drawInRect:withAttributes:", .{ frame, attrs }); +} + +fn drawPanelButton(self: id, _: SEL, _: NSRect) callconv(.c) void { + const writing = std.mem.indexOfScalar(id, &writing_btn, self); + const i = writing orelse panelIndex(self); + const trim = writing == null and i >= first_trim and i < done_index; + const active = trim and activeTrim(i); + const focused = msg(id, panelWindow(), "firstResponder", .{}) == self; + const hot = self == hover_row or msg(bool, self, "isHighlighted", .{}); + const bounds = msg(NSRect, self, "bounds", .{}); + const w = bounds.size.width; + const h = bounds.size.height; + if (w <= 1 or h <= 1) return; + const radius: f64 = if (writing != null) 8 else 5; + const path = msg(id, objc.class("NSBezierPath"), "bezierPathWithRoundedRect:xRadius:yRadius:", .{ rect(0.5, 0.5, w - 1, h - 1), radius, radius }); + if (hot or focused) { + msg(void, highlight, "setFill", .{}); + msg(void, path, "fill", .{}); + } + if (trim or (focused and hover_row != null and !hot)) { + msg(void, if (active or focused) accent else edge, "setStroke", .{}); + msg(void, path, "stroke", .{}); + } + const title = msg(id, self, "title", .{}); + if (writing) |j| { + const y = (h - 20) / 2; + drawString(title, compact_font, bright, rect(10, y, @max(1, w - 130), 20), .left); + drawString(objc.str(writing_shortcuts[j]), compact_font, ink, rect(@max(10, w - 119), y, @min(109, @max(1, w - 20)), 20), .right); + } else if (trim) { + const percent = percents[i - first_trim]; + // NSButton drawing coordinates are flipped: smaller y comes first. + drawString(title, panel_font, if (active) accent else bright, rect(3, 6, w - 6, 21), .center); + if (percent.len != 0) drawString(objc.str(percent), small, ink, rect(3, 26, w - 6, 15), .center); + } else if (i == done_index) { + drawString(objc.str("The Lab"), panel_font, bright, rect(10, (h - 20) / 2, w - 20, 20), .left); + drawString(title, panel_font, accent, rect(10, (h - 20) / 2, w - 20, 20), .right); + } else { + const compact = w < 305; + const text_h: f64 = if (w < 288) h else 20; + drawString(title, if (compact) compact_font else panel_font, bright, rect(10, (h - text_h) / 2, w - 20, text_h), .left); + if (i == 0 and w >= 340) drawString(objc.str("what each idea does"), small, ink, rect(10, (h - 15) / 2, w - 20, 15), .right); + } +} + +fn fitLabel(f: id, w: f64) f64 { + msg(void, f, "setPreferredMaxLayoutWidth:", .{w}); + return @ceil(msg(NSSize, msg(id, f, "cell", .{}), "cellSizeForBounds:", .{rect(0, 0, w, 10000)}).height) + 2; +} - item(menu, "The Lab guide…", "labGuide:", 0, false, me); - rule(menu); - for (passes) |p| item(menu, p.title, "labPass:", p.tag, false, ctl); - rule(menu); - item(menu, "Original", "labOriginal:", 0, pending == null, ctl); - for (trims) |t| { - item(menu, t.title, "labPass:", t.tag, pending != null and pending.? == t.tag, ctl); +fn layoutPanel() void { + const bounds = msg(NSRect, content, "bounds", .{}); + if (writing_at) |p| { + const w = @min(376, bounds.size.width); + const h = @min(136, bounds.size.height); + const rh = @max(1, (h - 12) / 4); + msg(void, shield, "setFrame:", .{bounds}); + msg(void, panel, "setFrame:", .{rect(std.math.clamp(p.x, 0, bounds.size.width - w), std.math.clamp(p.y - h, 0, bounds.size.height - h), w, h)}); + for (writing_btn, 0..) |b, i| msg(void, b, "setFrame:", .{rect(5, h - 6 - rh * number(i + 1), @max(1, w - 10), rh)}); + return tintPanel(); } - rule(menu); - item(menu, "Your draft is sent only when you pick a pass.", "", 0, false, null); + const w = @max(1, @min(panel_w, bounds.size.width - 2 * panel_margin)); + const trim_w = @max(1, w - 26); + // Keep all trim settings reachable rather than dropping buttons on small windows. + trim_cols = if (trim_w >= 546) 5 else if (trim_w >= 326) 3 else if (trim_w >= 216) 2 else 1; + const trim_rows = (trims.len + trim_cols - 1) / trim_cols; + const note_h = fitLabel(privacy_note, @max(1, w - 32)); + const guide_h = fitLabel(guide_text, @max(1, w - 32)); + const menu_h = 10 + row_h + 13 + number(passes.len) * row_h + 14 + number(trim_rows) * (trim_h + trim_gap) + note_h + 12; + const doc_h = if (guide_open) 10 + row_h + 12 + guide_h + 12 else menu_h; + const bottom = @min(panel_bottom, @max(panel_margin, bounds.size.height - 100)); + const h = @max(1, @min(doc_h, bounds.size.height - bottom - panel_margin)); + msg(void, shield, "setFrame:", .{bounds}); + msg(void, panel, "setFrame:", .{rect(@round((bounds.size.width - w) / 2), bottom, w, h)}); + msg(void, panel_scroll, "setFrame:", .{rect(1, 1, @max(1, w - 2), @max(1, h - 2))}); + const paper_w = @max(1, w - 2); + msg(void, paper, "setFrame:", .{rect(0, 0, paper_w, doc_h - 2)}); + for (panel_btn, 0..) |b, i| msg(void, b, "setHidden:", .{guide_open != (i == done_index)}); + for (rules) |line| msg(void, line, "setHidden:", .{guide_open}); + msg(void, privacy_note, "setHidden:", .{guide_open}); + msg(void, guide_text, "setHidden:", .{!guide_open}); - const view = if (sender != null) sender else bar[3]; - const h = msg(NSRect, view, "bounds", .{}).size.height; - _ = msg(bool, menu, "popUpMenuPositioningItem:atLocation:inView:", .{ @as(id, null), NSPoint{ .x = 0, .y = h }, view }); + var y = doc_h - 12 - row_h; + if (guide_open) { + msg(void, panel_btn[done_index], "setFrame:", .{rect(6, y, @max(1, paper_w - 12), row_h)}); + msg(void, guide_text, "setFrame:", .{rect(15, y - 9 - guide_h, @max(1, paper_w - 30), guide_h)}); + } else { + msg(void, panel_btn[0], "setFrame:", .{rect(6, y, @max(1, paper_w - 12), row_h)}); + y -= 7; + msg(void, rules[0], "setFrame:", .{rect(16, y, @max(1, paper_w - 32), 1)}); + y -= 6; + for (panel_btn[1..first_trim]) |b| { + y -= row_h; + msg(void, b, "setFrame:", .{rect(6, y, @max(1, paper_w - 12), row_h)}); + } + y -= 6; + msg(void, rules[1], "setFrame:", .{rect(16, y, @max(1, paper_w - 32), 1)}); + y -= 8 + trim_h; + const cell_w = @max(1, @floor((trim_w - number(trim_cols - 1) * trim_gap) / number(trim_cols))); + for (panel_btn[first_trim..done_index], 0..) |b, i| { + msg(void, b, "setFrame:", .{rect(12 + number(i % trim_cols) * (cell_w + trim_gap), y - number(i / trim_cols) * (trim_h + trim_gap), cell_w, trim_h)}); + } + y -= number(trim_rows - 1) * (trim_h + trim_gap); + msg(void, privacy_note, "setFrame:", .{rect(15, y - 6 - note_h, @max(1, paper_w - 30), note_h)}); + } + const first = msg(id, panelWindow(), "firstResponder", .{}); + for (panel_btn) |b| if (first == b and !msg(bool, b, "isHidden", .{})) { + _ = msg(bool, b, "scrollRectToVisible:", .{msg(NSRect, b, "bounds", .{})}); + break; + }; + tintPanel(); } -fn labGuide(_: id, _: SEL, _: id) callconv(.c) void { - const a = objc.new("NSAlert"); - defer msg(void, a, "release", .{}); - msg(void, a, "setMessageText:", .{objc.str("The Lab")}); - msg(void, a, "setInformativeText:", .{objc.str(guide)}); - _ = msg(id, a, "addButtonWithTitle:", .{objc.str("Done")}); - _ = msg(isize, a, "runModal", .{}); +fn colorRGB(rgb: u24) id { + return msg(id, objc.class("NSColor"), "colorWithSRGBRed:green:blue:alpha:", .{ + number((rgb >> 16) & 255) / 255, number((rgb >> 8) & 255) / 255, number(rgb & 255) / 255, @as(f64, 1), + }); +} + +fn view(cls: id, parent: id) id { + const v = msg(id, msg(id, cls, "alloc", .{}), "initWithFrame:", .{NSRect{}}); + msg(void, parent, "addSubview:", .{v}); + msg(void, v, "release", .{}); + return v; +} + +fn fillView(v: id, color: id) id { + msg(void, v, "setWantsLayer:", .{true}); + const layer = msg(id, v, "layer", .{}); + msg(void, layer, "setBackgroundColor:", .{cg(color)}); + return layer; +} + +fn wrapLabel(parent: id, text: []const u8, font: id, color: id) id { + const f = msg(id, objc.class("NSTextField"), "wrappingLabelWithString:", .{objc.str(text)}); + msg(void, f, "setFont:", .{font}); + msg(void, f, "setTextColor:", .{color}); + msg(void, parent, "addSubview:", .{f}); + return f; +} + +fn setupPanel() void { + // Write_On frame at 5:00: ink on #121522, two hairlines, one accent outline. + surface = colorRGB(0x121522); + edge = colorRGB(0x272a38); + highlight = colorRGB(0x252532); + panel_font = msg(id, objc.class("NSFont"), "monospacedSystemFontOfSize:weight:", .{ @as(f64, 14), @as(f64, 0) }); + compact_font = msg(id, objc.class("NSFont"), "monospacedSystemFontOfSize:weight:", .{ @as(f64, 13), @as(f64, 0) }); + const shield_class = objc.subclass("NSView", "ROLabShield", &.{ + .{ .name = "mouseDown:", .imp = @ptrCast(&dismiss), .types = "v@:@" }, + .{ .name = "rightMouseDown:", .imp = @ptrCast(&dismiss), .types = "v@:@" }, + .{ .name = "otherMouseDown:", .imp = @ptrCast(&dismiss), .types = "v@:@" }, + }); + const btn_class = objc.subclass("NSButton", "ROLabChoice", &.{ + .{ .name = "acceptsFirstResponder", .imp = @ptrCast(&acceptsFocus), .types = "B@:" }, + .{ .name = "keyDown:", .imp = @ptrCast(&panelKeys), .types = "v@:@" }, + .{ .name = "drawRect:", .imp = @ptrCast(&drawPanelButton), .types = "v@:{CGRect={CGPoint=dd}{CGSize=dd}}" }, + .{ .name = "mouseEntered:", .imp = @ptrCast(&panelEnter), .types = "v@:@" }, + .{ .name = "mouseExited:", .imp = @ptrCast(&panelExit), .types = "v@:@" }, + }); + const nsview = objc.class("NSView"); + shield = view(shield_class, content); + msg(void, shield, "setHidden:", .{true}); + panel = view(nsview, shield); + const layer = fillView(panel, surface); + msg(void, layer, "setCornerRadius:", .{@as(f64, 8)}); + msg(void, layer, "setBorderWidth:", .{@as(f64, 1)}); + msg(void, layer, "setBorderColor:", .{cg(edge)}); + msg(void, layer, "setMasksToBounds:", .{true}); + panel_scroll = view(objc.class("NSScrollView"), panel); + msg(void, panel_scroll, "setDrawsBackground:", .{false}); + msg(void, panel_scroll, "setHasVerticalScroller:", .{true}); + msg(void, panel_scroll, "setScrollerStyle:", .{@as(isize, 1)}); // overlay, no reserved width + msg(void, panel_scroll, "setAutohidesScrollers:", .{true}); + paper = msg(id, msg(id, nsview, "alloc", .{}), "initWithFrame:", .{NSRect{}}); + msg(void, panel_scroll, "setDocumentView:", .{paper}); + msg(void, paper, "release", .{}); + for (&rules) |*line| { + line.* = view(nsview, paper); + _ = fillView(line.*, edge); + } + for (&panel_btn, 0..) |*b, i| { + const title = if (i == 0) "The Lab guide…" else if (i < first_trim) passes[i - 1].title else if (i < done_index) trims[i - first_trim] else "Done"; + b.* = msg(id, btn_class, "buttonWithTitle:target:action:", .{ objc.str(title), me, objc.sel("choose:") }); + const tag: isize = if (i == 0 or i == first_trim or i == done_index) -1 else if (i < first_trim) passes[i - 1].tag else @intCast(i - 2); + msg(void, b.*, "setTag:", .{tag}); + if (i > first_trim and i < done_index) msg(void, b.*, "setAccessibilityHelp:", .{objc.str(percents[i - first_trim])}); + installRow(paper, b.*); + } + for (&writing_btn, writing_keys, writing_shortcuts, 0..) |*b, key, shortcut, i| { + b.* = msg(id, btn_class, "buttonWithTitle:target:action:", .{ objc.str(""), me, objc.sel("choose:") }); + msg(void, b.*, "setKeyEquivalent:", .{objc.str(key)}); + msg(void, b.*, "setKeyEquivalentModifierMask:", .{@as(usize, if (i == 2) 0x40000 else 0x60000)}); + msg(void, b.*, "setAccessibilityHelp:", .{objc.str(shortcut)}); + installRow(panel, b.*); + } + privacy_note = wrapLabel(paper, privacy, small, ink); + guide_text = wrapLabel(paper, guide, panel_font, bright); + msg(void, msg(id, objc.class("NSNotificationCenter"), "defaultCenter", .{}), "addObserver:selector:name:object:", .{ me, objc.sel("dismiss:"), NSWindowDidResignKeyNotification, panelWindow() }); +} + +fn installRow(parent: id, b: id) void { + msg(void, b, "setBordered:", .{false}); + msg(void, b, "setFocusRingType:", .{@as(usize, 1)}); // theme draws focus + msg(void, parent, "addSubview:", .{b}); + const area = msg(id, msg(id, objc.class("NSTrackingArea"), "alloc", .{}), "initWithRect:options:owner:userInfo:", .{ NSRect{}, @as(usize, 1 | 0x20 | 0x200), b, @as(id, null) }); + msg(void, b, "addTrackingArea:", .{area}); + msg(void, area, "release", .{}); } fn relaid(_: id, _: SEL, _: id) callconv(.c) void { relayout(); + if (chooser_open) layoutPanel(); } // --- Setup ---------------------------------------------------------------------------------- @@ -266,7 +670,8 @@ fn label(parent: id, font: id, color: id) id { /// Call once, after the page, the status bar and the count are in `content_view`. `small` is /// the 11 pt face and `dim_ink` the quiet color the app already uses. -pub fn setup(content_view: id, controller: id, small_font: id, dim_ink: id, accent_ink: id, hot_ink: id) void { +pub fn setup(content_view: id, controller: id, editor: id, small_font: id, dim_ink: id, accent_ink: id, hot_ink: id) void { + draft_view = editor; accent = accent_ink; bright = hot_ink; content = content_view; @@ -274,7 +679,8 @@ pub fn setup(content_view: id, controller: id, small_font: id, dim_ink: id, acce small = small_font; ink = dim_ink; const cls = objc.subclass("NSObject", "ROLabUI", &.{ - .{ .name = "labGuide:", .imp = @ptrCast(&labGuide), .types = "v@:@" }, + .{ .name = "choose:", .imp = @ptrCast(&choose), .types = "v@:@" }, + .{ .name = "dismiss:", .imp = @ptrCast(&dismiss), .types = "v@:@" }, .{ .name = "mouseEntered:", .imp = @ptrCast(&entered), .types = "v@:@" }, .{ .name = "mouseExited:", .imp = @ptrCast(&exited), .types = "v@:@" }, .{ .name = "relaid:", .imp = @ptrCast(&relaid), .types = "v@:@" }, @@ -292,10 +698,13 @@ pub fn setup(content_view: id, controller: id, small_font: id, dim_ink: id, acce const titles = [_][]const u8{ "Save", "Open", "Overflow", "Lab" }; const tips = [_][]const u8{ "Save (⌘S)", "Open (⌘O)", "Overflow (⌃⇧D)", "Lab — mark or trim the draft" }; const actions = [_][*:0]const u8{ "saveDoc:", "openDoc:", "toggleOverflow:", "labMenu:" }; - for (&bar, &tracking, titles, tips, actions, glyphs) |*b, *area, title, tip, action, svg| { + const trigger_class = objc.subclass("NSButton", "ROLabTrigger", &.{ + .{ .name = "mouseDown:", .imp = @ptrCast(&triggerMouseDown), .types = "v@:@" }, + }); + for (&bar, &tracking, titles, tips, actions, glyphs, 0..) |*b, *area, title, tip, action, svg, i| { const data = msg(id, objc.class("NSData"), "dataWithBytes:length:", .{ svg.ptr, svg.len }); const image = msg(id, msg(id, objc.class("NSImage"), "alloc", .{}), "initWithData:", .{data}); - b.* = msg(id, objc.class("NSButton"), "buttonWithTitle:target:action:", .{ objc.str(title), ctl, objc.sel(action) }); + b.* = msg(id, if (i == 3) trigger_class else objc.class("NSButton"), "buttonWithTitle:target:action:", .{ objc.str(title), ctl, objc.sel(action) }); msg(void, b.*, "setBordered:", .{false}); if (image != null) { msg(void, image, "setTemplate:", .{true}); @@ -336,6 +745,7 @@ pub fn setup(content_view: id, controller: id, small_font: id, dim_ink: id, acce card_btn[3] = button(card, "Apply rest", "labApply:", ctl, "Apply every remaining finding"); msg(void, content, "addSubview:", .{card}); // last, so it is above the page + setupPanel(); // last in the stack, above both the editor and review card msg(void, content, "setPostsFrameChangedNotifications:", .{true}); msg(void, msg(id, objc.class("NSNotificationCenter"), "defaultCenter", .{}), "addObserver:selector:name:object:", .{ me, objc.sel("relaid:"), NSViewFrameDidChangeNotification, content }); relayout(); |
