1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
//! 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.
const std = @import("std");
const objc = @import("objc.zig");
const msg = objc.msg;
const id = objc.id;
const SEL = objc.SEL;
const NSRect = objc.NSRect;
const NSPoint = objc.NSPoint;
const NSSize = objc.NSSize;
extern const NSViewFrameDidChangeNotification: 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)
const gap = 6; // review-card buttons
const card_h = 80;
const card_max_w = 500;
const card_bottom = strip_h + 12;
var content: id = null;
var ctl: id = null;
var small: id = null;
var ink: id = null;
var me: id = null; // the target of the Lab's own actions
var accent: id = null;
var bright: id = null;
var bar: [4]id = .{null} ** 4; // Save, Open, Overflow, Lab
var tracking: [4]id = .{null} ** 4;
var hovering: id = null;
var saved_dirty = false;
var overflow_open = false;
var card: id = null;
var note_field: id = null;
var hint_field: id = null;
var card_btn: [4]Btn = undefined; // Discard, Next, Keep, Apply
var tools = true;
var review = false;
var has_note = false;
const Btn = struct { v: id, w: f64 = 0, h: f64 = 0 };
// Drop the Next button first on a narrow review card.
const card_drop = [_]u8{ 1, 0, 3, 2 };
const passes = [_]struct { title: []const u8, tag: isize }{
.{ .title = "Fix punctuation and typos", .tag = 0 },
.{ .title = "Mark the weakest sentences", .tag = 1 },
.{ .title = "Mark sentences that run long", .tag = 2 },
.{ .title = "Mark convoluted sentences", .tag = 3 },
.{ .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 guide =
\\Each idea asks the AI about your draft and only marks or fades words. It never rewrites.
\\
\\Fix punctuation and typos — marks slips and shows the fix.
\\Mark the weakest sentences — the ones the piece needs least.
\\Mark sentences that run long — where a reader runs out of breath.
\\Mark convoluted sentences — hard to follow on a first read.
\\Mark words that don’t fit the tone — wording that jars with the rest.
\\Mark hedges and filler — “just”, “sort of”, “in order to”.
\\
\\Trims ghost proposed cuts: slight (~10%), tighter (~20%), sharper (~30%), half (~50%). Saving leaves the ghosts out of rendered Markdown. Keep revives a passage; Original revives all remaining proposals. Apply rest deletes them from the draft.
\\
\\Your draft is sent to the model only when you choose a pass.
;
// --- Layout ---------------------------------------------------------------------------------
/// Which of `widths` fit in `avail` (with `gap` between), dropping in `drop` order. Returns
/// the width the shown ones take.
fn plan(widths: []const f64, drop: []const u8, avail: f64, shown: []bool) f64 {
var total: f64 = 0;
var n: usize = 0;
for (widths, shown) |w, *s| {
s.* = true;
total += w;
n += 1;
}
total += gap * @as(f64, @floatFromInt(n -| 1));
for (drop) |i| {
if (total <= avail) break;
shown[i] = false;
total -= widths[i] + (if (n > 1) @as(f64, gap) else 0);
n -= 1;
}
return if (total <= avail and n > 0) total else 0;
}
/// Lay a row of buttons against `right`, vertically centred in `height`; returns its left edge.
fn row(btns: []Btn, drop: []const u8, visible: bool, right: f64, avail: f64, height: f64) f64 {
var widths: [4]f64 = undefined;
var shown: [4]bool = undefined;
for (btns, 0..) |b, i| widths[i] = b.w;
const total = if (visible) plan(widths[0..btns.len], drop, avail, shown[0..btns.len]) else 0;
var x = right;
var i = btns.len;
while (i > 0) {
i -= 1;
const b = btns[i];
const on = visible and total > 0 and shown[i];
msg(void, b.v, "setHidden:", .{!on});
if (!on) continue;
x -= b.w;
msg(void, b.v, "setFrameOrigin:", .{NSPoint{ .x = x, .y = @round((height - b.h) / 2) }});
x -= gap;
}
return if (total > 0) x + gap else right;
}
fn relayout() void {
if (content == null) return;
const w = msg(NSRect, content, "bounds", .{}).size.width;
// Centers measured from the clip. Ink is small; each control has a larger hit area.
const centers = [_]f64{ 31, 65.5, w - 35, w / 2 };
for (bar, centers, 0..) |b, x, i| {
const width: f64 = if (i == 3) 56 else 34;
msg(void, b, "setFrame:", .{NSRect{ .origin = .{ .x = @round(x - width / 2), .y = 15 }, .size = .{ .width = width, .height = 34 } }});
msg(void, b, "setHidden:", .{!tools});
}
const cw = @min(card_max_w, w - 24);
const show = tools and has_note and cw >= 160;
msg(void, card, "setHidden:", .{!show});
if (!show) return;
msg(void, card, "setFrame:", .{NSRect{ .origin = .{ .x = @round((w - cw) / 2), .y = card_bottom }, .size = .{ .width = cw, .height = card_h } }});
msg(void, note_field, "setFrame:", .{NSRect{ .origin = .{ .x = 14, .y = 54 }, .size = .{ .width = cw - 28, .height = 16 } }});
msg(void, hint_field, "setFrame:", .{NSRect{ .origin = .{ .x = 14, .y = 37 }, .size = .{ .width = cw - 28, .height = 14 } }});
if (review) {
_ = row(&card_btn, &card_drop, true, cw - 12, cw - 24, 36);
} else {
for (card_btn[1..]) |b| msg(void, b.v, "setHidden:", .{true});
_ = row(card_btn[0..1], &.{0}, true, cw - 12, cw - 24, 36);
}
}
/// State tint never changes the frame or the click target.
pub fn state(dirty: bool, overflow: bool) void {
saved_dirty = dirty;
overflow_open = overflow;
tint();
}
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);
msg(void, b, "setContentTintColor:", .{if (hovering != null and hovering == area) bright else if (active) accent else ink});
}
}
fn entered(_: id, _: SEL, event: id) callconv(.c) void {
hovering = msg(id, event, "trackingArea", .{});
tint();
}
fn exited(_: id, _: SEL, event: id) callconv(.c) void {
if (hovering == msg(id, event, "trackingArea", .{})) hovering = null;
tint();
}
// --- State ----------------------------------------------------------------------------------
/// `tools` is main's tool mode (off hides every control but the count). While `review` is on
/// the card shows `note`; `can_apply` says whether anything is left for Apply to do.
pub fn update(tools_on: bool, note: []const u8, review_on: bool, can_apply: bool) void {
if (content == null) return;
tools = tools_on;
review = review_on;
if (!tools) hovering = null;
has_note = note.len != 0;
tint();
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)});
msg(void, card_btn[3].v, "setEnabled:", .{can_apply});
relayout();
}
// --- The Lab menu ---------------------------------------------------------------------------
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", .{});
}
fn rule(menu: id) void {
msg(void, menu, "addItem:", .{msg(id, objc.class("NSMenuItem"), "separatorItem", .{})});
}
/// 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});
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);
}
rule(menu);
item(menu, "Your draft is sent only when you pick a pass.", "", 0, false, null);
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 });
}
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 relaid(_: id, _: SEL, _: id) callconv(.c) void {
relayout();
}
// --- Setup ----------------------------------------------------------------------------------
fn button(parent: id, title: []const u8, action: [*:0]const u8, target: id, tip: []const u8) Btn {
const b = msg(id, objc.class("NSButton"), "buttonWithTitle:target:action:", .{ objc.str(title), target, objc.sel(action) });
msg(void, b, "setControlSize:", .{@as(isize, 1)}); // small
msg(void, b, "setFont:", .{msg(id, objc.class("NSFont"), "systemFontOfSize:", .{@as(f64, 11)})});
if (tip.len > 0) msg(void, b, "setToolTip:", .{objc.str(tip)});
msg(void, b, "sizeToFit", .{});
msg(void, parent, "addSubview:", .{b});
const f = msg(NSRect, b, "frame", .{});
return .{ .v = b, .w = f.size.width, .h = f.size.height };
}
fn label(parent: id, font: id, color: id) id {
const f = msg(id, objc.class("NSTextField"), "labelWithString:", .{objc.str("")});
msg(void, f, "setFont:", .{font});
msg(void, f, "setTextColor:", .{color});
msg(void, msg(id, f, "cell", .{}), "setLineBreakMode:", .{@as(isize, 4)}); // truncate the tail
msg(void, parent, "addSubview:", .{f});
return f;
}
/// 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 {
accent = accent_ink;
bright = hot_ink;
content = content_view;
ctl = controller;
small = small_font;
ink = dim_ink;
const cls = objc.subclass("NSObject", "ROLabUI", &.{
.{ .name = "labGuide:", .imp = @ptrCast(&labGuide), .types = "v@:@" },
.{ .name = "mouseEntered:", .imp = @ptrCast(&entered), .types = "v@:@" },
.{ .name = "mouseExited:", .imp = @ptrCast(&exited), .types = "v@:@" },
.{ .name = "relaid:", .imp = @ptrCast(&relaid), .types = "v@:@" },
});
me = msg(id, msg(id, cls, "alloc", .{}), "init", .{});
// Monochrome vector templates; paths for the tiny lettering keep their spacing
// independent of the installed fonts. Native buttons retain focus, press and AX.
const glyphs = [_][]const u8{
"<svg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 18 18'><g fill='none' stroke='black' stroke-width='1.65' stroke-linejoin='round'><path d='M1.4 1.4h11l4.2 4.2v11H1.4z M5 1.4v5.5h7.8V1.9 M4.5 16.3V10h9v6.3'/></g></svg>",
"<svg xmlns='http://www.w3.org/2000/svg' width='18' height='18'><path d='M1.4 2.1h5.7l2.1 2.3h7.4v11.7H1.4z' fill='none' stroke='black' stroke-width='1.65' stroke-linejoin='round'/></svg>",
"<svg xmlns='http://www.w3.org/2000/svg' width='25' height='18'><g fill='none' stroke='black'><rect x='.9' y='.9' width='23.2' height='16.2' rx='3.1' stroke-width='1.65'/><path d='m4.7 5.8 4.1 6.5m0-6.5-4.1 6.5m5.8-6.5 2.1 3.3 2.1-3.3m-2.1 3.3v3.2m3.5-6.2h4.1l-4.1 6h4.2' stroke-width='1.35' stroke-linejoin='round'/></g></svg>",
"<svg xmlns='http://www.w3.org/2000/svg' width='46' height='26'><g fill='none' stroke='black'><rect x='.6' y='.6' width='44.8' height='24.8' rx='4.6' stroke-dasharray='3 2'/><path d='M13.1 9.4v7.2h4.5m2.5 0 2.6-7.2h.6l2.6 7.2m-4.8-2.4h3.9m4-4.8v7.2h2.9c3.2 0 3.1-3.8.2-3.8H29m0-3.4h2.8c2.8 0 2.8 3.4.2 3.4' stroke-width='1.25' stroke-linejoin='round'/></g></svg>",
};
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 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) });
msg(void, b.*, "setBordered:", .{false});
if (image != null) {
msg(void, image, "setTemplate:", .{true});
msg(void, b.*, "setImage:", .{image});
msg(void, image, "release", .{});
msg(void, b.*, "setImagePosition:", .{@as(isize, 1)}); // NSImageOnly
}
msg(void, b.*, "setAccessibilityLabel:", .{objc.str(title)});
msg(void, b.*, "setToolTip:", .{objc.str(tip)});
msg(void, content, "addSubview:", .{b.*});
area.* = msg(id, msg(id, objc.class("NSTrackingArea"), "alloc", .{}), "initWithRect:options:owner:userInfo:", .{ NSRect{}, @as(usize, 1 | 0x20 | 0x200), me, @as(id, null) }); // entered/exited, in key window, in visible rect
msg(void, b.*, "addTrackingArea:", .{area.*});
msg(void, area.*, "release", .{});
}
tint();
// The review card: a popover-material card, so it follows the system's look.
card = msg(id, msg(id, objc.class("NSVisualEffectView"), "alloc", .{}), "initWithFrame:", .{NSRect{ .size = .{ .width = card_max_w, .height = card_h } }});
msg(void, card, "setMaterial:", .{@as(isize, 6)}); // popover
msg(void, card, "setBlendingMode:", .{@as(isize, 1)}); // within window
msg(void, card, "setState:", .{@as(isize, 1)}); // always active
msg(void, card, "setWantsLayer:", .{true});
const layer = msg(id, card, "layer", .{});
msg(void, layer, "setCornerRadius:", .{@as(f64, 12)});
msg(void, layer, "setMasksToBounds:", .{true});
msg(void, layer, "setBorderWidth:", .{@as(f64, 1)});
msg(void, layer, "setBorderColor:", .{msg(id, msg(id, objc.class("NSColor"), "separatorColor", .{}), "CGColor", .{})});
msg(void, card, "setHidden:", .{true});
note_field = label(card, small, msg(id, objc.class("NSColor"), "labelColor", .{}));
hint_field = label(card, small, ink);
msg(void, hint_field, "setStringValue:", .{objc.str("Keep skips this one · Apply rest takes the others")});
msg(void, card, "setToolTip:", .{objc.str("Sent to the model only when you chose this pass")});
card_btn[0] = button(card, "Discard", "labOriginal:", ctl, "Put it all away; the text stays as it was");
card_btn[1] = button(card, "Next", "labNext:", ctl, "Move on, leaving this one undecided");
card_btn[2] = button(card, "Keep", "labKeep:", ctl, "Keep this one as it is and skip it");
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
msg(void, content, "setPostsFrameChangedNotifications:", .{true});
msg(void, msg(id, objc.class("NSNotificationCenter"), "defaultCenter", .{}), "addObserver:selector:name:object:", .{ me, objc.sel("relaid:"), NSViewFrameDidChangeNotification, content });
relayout();
}
test "plan drops in order until the rest fit" {
const w = [_]f64{ 50, 50, 50, 50 };
var shown: [4]bool = undefined;
// all four: 200 + 3 gaps
try std.testing.expectEqual(@as(f64, 200 + 3 * gap), plan(&w, &card_drop, 1000, &shown));
try std.testing.expect(shown[0] and shown[1] and shown[2] and shown[3]);
// room for two: Next (1) goes, then Discard (0)
try std.testing.expectEqual(@as(f64, 100 + gap), plan(&w, &card_drop, 110, &shown));
try std.testing.expect(!shown[0] and !shown[1] and shown[2] and shown[3]);
// no room at all: nothing shown
try std.testing.expectEqual(@as(f64, 0), plan(&w, &card_drop, 10, &shown));
try std.testing.expect(!shown[0] and !shown[1] and !shown[2] and !shown[3]);
}
|