rvlib-shipkit 2.21.0 → 2.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/package.json +1 -1
  3. package/src/asks/asks-queue.ts +8 -7
  4. package/src/assets/kits/identity-kit.ts +158 -2
  5. package/src/cli/cmd_Bump.ts +3 -3
  6. package/src/cli/cmd_Rec.ts +349 -0
  7. package/src/cli/cmd_Releases.ts +4 -1
  8. package/src/cli/cmd_Secret.ts +10 -3
  9. package/src/cli/cmd_Sign.ts +24 -0
  10. package/src/cli/cmd_Social.ts +44 -1
  11. package/src/cli/family-pages.ts +1 -0
  12. package/src/cli/registry.ts +4 -0
  13. package/src/core/agent-spawn.ts +4 -17
  14. package/src/core/code-lines.ts +49 -0
  15. package/src/core/discover-checks.ts +27 -3
  16. package/src/core/hard-reset.ts +34 -0
  17. package/src/core/raw-access.ts +3 -13
  18. package/src/core/throwaway-worktree.ts +72 -0
  19. package/src/home/app-sign.ts +98 -0
  20. package/src/home/gui-app.ts +31 -12
  21. package/src/home/home-routes.ts +6 -0
  22. package/src/home/serve-web.ts +1 -1
  23. package/src/identities/identities.ts +6 -0
  24. package/src/media-kinds.ts +5 -3
  25. package/src/media-store/forget.ts +54 -0
  26. package/src/media-store/put.ts +4 -3
  27. package/src/media-store/space.ts +63 -24
  28. package/src/recordings/delete.ts +19 -0
  29. package/src/recordings/edit.ts +231 -0
  30. package/src/recordings/load.ts +37 -0
  31. package/src/recordings/marks-draw.ts +111 -0
  32. package/src/recordings/marks-png.ts +25 -0
  33. package/src/recordings/marks.ts +138 -0
  34. package/src/recordings/media-kind.ts +67 -0
  35. package/src/recordings/model.ts +170 -0
  36. package/src/recordings/open.ts +33 -0
  37. package/src/recordings/place.ts +89 -0
  38. package/src/recordings/presets.ts +99 -0
  39. package/src/recordings/recorder-app/Capture.swift +510 -0
  40. package/src/recordings/recorder-app/Clip.swift +107 -0
  41. package/src/recordings/recorder-app/Editor.swift +39 -0
  42. package/src/recordings/recorder-app/Tray.swift +298 -0
  43. package/src/recordings/recorder-app/main.swift +859 -0
  44. package/src/recordings/recorder-app.ts +46 -0
  45. package/src/recordings/recorder.shipkit.ts +11 -0
  46. package/src/recordings/routes.ts +140 -0
  47. package/src/recordings/run-recorder.ts +40 -0
  48. package/src/recordings/share.ts +107 -0
  49. package/src/recordings/take.ts +146 -0
  50. package/src/recordings/view.ts +40 -0
  51. package/src/release/gate-checkout.ts +74 -0
  52. package/src/release/release-scope.ts +35 -26
  53. package/src/release/run-bump.ts +79 -37
  54. package/src/release/run-release.ts +67 -53
  55. package/src/releases/add.ts +7 -2
  56. package/src/releases/from-card.ts +3 -3
  57. package/src/releases/model.ts +29 -7
  58. package/src/repo-config.ts +7 -3
  59. package/src/review/trailer-desk.ts +1 -0
  60. package/src/review/trailer-model.ts +2 -0
  61. package/src/secrets/manager.ts +18 -1
  62. package/src/serve/hub-app/home-st.ts +15 -5
  63. package/src/serve/hub-app/home.html +1 -0
  64. package/src/serve/hub-app/hub-app.tsx +6 -1
  65. package/src/serve/hub-app/icons.tsx +2 -0
  66. package/src/serve/hub-app/palette-st.ts +1 -0
  67. package/src/serve/hub-app/project-list.tsx +1 -0
  68. package/src/serve/hub-app/projects-svc.ts +1 -1
  69. package/src/serve/hub-app/recordings-st.ts +641 -0
  70. package/src/serve/hub-app/recordings-view.tsx +504 -0
  71. package/src/serve/hub-app/recordings.css +82 -0
  72. package/src/serve/hub-app/repo-st.ts +1 -1
  73. package/src/serve/hub-app/review/trailer-page.tsx +1 -0
  74. package/src/serve/hub-app/review/trailer-st.ts +16 -0
  75. package/src/serve/hub-app/review/trailer-timeline.tsx +3 -0
  76. package/src/serve/hub-app/review.css +2 -0
  77. package/src/serve/hub-app/social/post-card.tsx +31 -3
  78. package/src/serve/hub-app/social/social-model.ts +4 -0
  79. package/src/serve/hub-app/social/social-st.ts +12 -0
  80. package/src/serve/hub-app/social.css +4 -0
  81. package/src/shipkit.ts +2 -2
  82. package/src/shot/box.ts +23 -17
  83. package/src/shot/browser-slot.ts +13 -3
  84. package/src/shot/film-box.ts +193 -117
  85. package/src/shot/shooter.ts +5 -3
  86. package/src/smoke/run-smoke.ts +8 -6
  87. package/src/social/compose.ts +14 -0
  88. package/src/social/drafts.ts +1 -1
  89. package/src/social/load.ts +16 -2
  90. package/src/social/publish/index.ts +3 -1
  91. package/src/social/publish/secret.ts +12 -4
  92. package/src/social/release-card.ts +69 -0
  93. package/src/social/release-stage.ts +49 -10
  94. package/src/social/routes.ts +5 -0
  95. package/src/social/stage.ts +9 -5
  96. package/src/trailer/build.ts +24 -0
  97. package/src/trailer/ffmpeg.ts +8 -0
  98. package/src/trailer/grammar.ts +9 -2
  99. package/src/trailer/look.ts +7 -0
  100. package/src/veille/chrome.ts +6 -3
@@ -0,0 +1,510 @@
1
+ // the capture overlay of the recorder (wiki tools/shipkit/recordings.md, The recorder): Snagit's crosshair. Every screen
2
+ // dims, the window under the pointer lights up: a click takes it, a drag takes an area. For a video the choice then
3
+ // stays on screen to adjust: handles resize the area (or the window itself, through Accessibility), a drag inside
4
+ // moves it, and a bar under it records, switches the mic and the system sound, or cancels. A frozen screenshot shows the
5
+ // crosshair on one screen only, over that screen as it was the instant its hotkey was pressed. While it records, a red
6
+ // frame and a small stop control stay around it; the recorder's own windows are never in the capture
7
+ import AppKit
8
+ import ApplicationServices
9
+ import Carbon.HIToolbox
10
+
11
+ /** what the person chose: a window (its id and app) or an area, in Cocoa global points, and the sound switches */
12
+ struct Picked {
13
+ var window: (id: CGWindowID, pid: pid_t)?
14
+ var frame: NSRect
15
+ var screen: NSScreen
16
+ var mic: Bool
17
+ var sound: Bool
18
+ }
19
+
20
+ struct WinInfo {
21
+ let id: CGWindowID
22
+ let pid: pid_t
23
+ let frame: NSRect
24
+ }
25
+
26
+ /** the level of the recorder's own windows a capture may take (the tray, the editor window); its overlay and its REC
27
+ * pill sit higher and never are */
28
+ let SHOWN_LEVEL = Int(CGWindowLevelForKey(.floatingWindow))
29
+
30
+ /** the windows on screen, front to back, in Cocoa global points: the desktop and the menu bar left out, and of our own
31
+ * only the tray and the editor window */
32
+ func windowsOnScreen() -> [WinInfo] {
33
+ let me = ProcessInfo.processInfo.processIdentifier
34
+ let top = NSScreen.screens.first?.frame.height ?? 0
35
+ guard let list = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] else { return [] }
36
+ return list.compactMap { w in
37
+ let layer = w[kCGWindowLayer as String] as? Int
38
+ guard let pid = w[kCGWindowOwnerPID as String] as? pid_t, layer == 0 || (pid == me && layer == SHOWN_LEVEL),
39
+ let id = w[kCGWindowNumber as String] as? CGWindowID,
40
+ let b = w[kCGWindowBounds as String] as? [String: CGFloat],
41
+ let x = b["X"], let y = b["Y"], let wd = b["Width"], let h = b["Height"], wd > 40, h > 40 else { return nil }
42
+ return WinInfo(id: id, pid: pid, frame: NSRect(x: x, y: top - y - h, width: wd, height: h))
43
+ }
44
+ }
45
+
46
+ /** the Accessibility window of an app whose frame is this one (its title bar included), nil when none matches */
47
+ func axWindow(pid: pid_t, frame: NSRect) -> AXUIElement? {
48
+ let top = NSScreen.screens.first?.frame.height ?? 0
49
+ var list: CFTypeRef?
50
+ AXUIElementCopyAttributeValue(AXUIElementCreateApplication(pid), kAXWindowsAttribute as CFString, &list)
51
+ for w in (list as? [AXUIElement]) ?? [] {
52
+ var pv: CFTypeRef?
53
+ var sv: CFTypeRef?
54
+ AXUIElementCopyAttributeValue(w, kAXPositionAttribute as CFString, &pv)
55
+ AXUIElementCopyAttributeValue(w, kAXSizeAttribute as CFString, &sv)
56
+ var p = CGPoint.zero
57
+ var s = CGSize.zero
58
+ if let pv { AXValueGetValue(pv as! AXValue, .cgPoint, &p) }
59
+ if let sv { AXValueGetValue(sv as! AXValue, .cgSize, &s) }
60
+ if abs(p.x - frame.minX) < 4 && abs(p.y - (top - frame.maxY)) < 4 && abs(s.width - frame.width) < 4 && abs(s.height - frame.height) < 4 { return w }
61
+ }
62
+ return nil
63
+ }
64
+
65
+ /** a window moved and resized to a Cocoa global frame */
66
+ func axSetFrame(_ w: AXUIElement, _ frame: NSRect) {
67
+ let top = NSScreen.screens.first?.frame.height ?? 0
68
+ var p = CGPoint(x: frame.minX, y: top - frame.maxY)
69
+ var s = frame.size
70
+ if let pv = AXValueCreate(.cgPoint, &p) { AXUIElementSetAttributeValue(w, kAXPositionAttribute as CFString, pv) }
71
+ if let sv = AXValueCreate(.cgSize, &s) { AXUIElementSetAttributeValue(w, kAXSizeAttribute as CFString, sv) }
72
+ }
73
+
74
+ // MARK: the overlay
75
+
76
+ enum Grip { case move, n, s, e, w, ne, nw, se, sw }
77
+
78
+ @MainActor
79
+ final class Chooser {
80
+ let video: Bool
81
+ let windowsToo: Bool
82
+ var mic: Bool
83
+ var sound: Bool
84
+ var views: [ChooseView] = []
85
+ var windows: [NSWindow] = []
86
+ let wins: [WinInfo]
87
+ /** the window under the pointer while choosing */
88
+ var hover: WinInfo?
89
+ var dragFrom: NSPoint?
90
+ var dragTo: NSPoint?
91
+ /** the choice being adjusted (a video): its frame and its window, if one */
92
+ var chosen: NSRect?
93
+ var chosenWin: WinInfo?
94
+ var grip: (Grip, NSPoint, NSRect)?
95
+ var bar: NSView?
96
+ var done: ((Picked?) -> Void)?
97
+ /** a frozen screenshot's screen and its image of that instant: the overlay covers that screen alone, over it */
98
+ let frozen: (screen: NSScreen, image: CGImage)?
99
+
100
+ init(video: Bool, windowsToo: Bool, mic: Bool, sound: Bool, frozen: (screen: NSScreen, image: CGImage)? = nil) {
101
+ self.video = video
102
+ self.windowsToo = windowsToo
103
+ self.mic = mic
104
+ self.sound = sound
105
+ self.frozen = frozen
106
+ self.wins = windowsToo ? windowsOnScreen() : []
107
+ }
108
+
109
+ func open() {
110
+ for s in frozen.map({ [$0.screen] }) ?? NSScreen.screens {
111
+ let w = KeyWindow(contentRect: s.frame, styleMask: .borderless, backing: .buffered, defer: false)
112
+ w.level = .screenSaver
113
+ w.isOpaque = false
114
+ w.backgroundColor = .clear
115
+ w.isReleasedWhenClosed = false
116
+ w.ignoresMouseEvents = false
117
+ w.acceptsMouseMovedEvents = true
118
+ w.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
119
+ let v = ChooseView(frame: NSRect(origin: .zero, size: s.frame.size), chooser: self, screen: s)
120
+ w.contentView = v
121
+ w.makeKeyAndOrderFront(nil)
122
+ w.makeFirstResponder(v)
123
+ views.append(v)
124
+ windows.append(w)
125
+ }
126
+ NSApp.activate()
127
+ NSCursor.crosshair.set()
128
+ // a menu bar app is not handed the keyboard: Escape and Return are taken as hotkeys while the overlay is up,
129
+ // so they never reach the app below (a terminal took an Escape meant for the overlay)
130
+ keys = [(UInt32(kVK_Escape), Chooser.ESC), (UInt32(kVK_Return), Chooser.RETURN)].compactMap { code, id in
131
+ var ref: EventHotKeyRef?
132
+ let ok = RegisterEventHotKey(code, 0, EventHotKeyID(signature: OSType(0x534B_4F56), id: id), GetApplicationEventTarget(), 0, &ref)
133
+ return ok == noErr ? ref : nil
134
+ }
135
+ Chooser.live = self
136
+ moved(NSEvent.mouseLocation)
137
+ }
138
+
139
+ static let ESC: UInt32 = 9001
140
+ static let RETURN: UInt32 = 9002
141
+ /** the overlay up now: the hotkey handler hands it Escape and Return */
142
+ static weak var live: Chooser?
143
+ var keys: [EventHotKeyRef] = []
144
+
145
+ /** a hotkey id this overlay owns, acted on; false when it is no overlay key */
146
+ static func key(_ id: UInt32) -> Bool {
147
+ guard let c = live, id == ESC || id == RETURN else { return false }
148
+ if id == ESC { c.cancel() } else { c.go() }
149
+ return true
150
+ }
151
+
152
+ func close() {
153
+ for k in keys { UnregisterEventHotKey(k) }
154
+ keys = []
155
+ if Chooser.live === self { Chooser.live = nil }
156
+ for w in windows { w.orderOut(nil) }
157
+ windows = []
158
+ views = []
159
+ NSCursor.arrow.set()
160
+ }
161
+
162
+ func redraw() { for v in views { v.needsDisplay = true } }
163
+
164
+ var dragRect: NSRect? {
165
+ guard let a = dragFrom, let b = dragTo else { return nil }
166
+ return NSRect(x: min(a.x, b.x), y: min(a.y, b.y), width: abs(a.x - b.x), height: abs(a.y - b.y))
167
+ }
168
+
169
+ func screenOf(_ r: NSRect) -> NSScreen {
170
+ NSScreen.screens.first { $0.frame.contains(NSPoint(x: r.midX, y: r.midY)) } ?? NSScreen.main!
171
+ }
172
+
173
+ func moved(_ p: NSPoint) {
174
+ guard chosen == nil, dragFrom == nil else { return }
175
+ hover = wins.first { $0.frame.contains(p) }
176
+ redraw()
177
+ }
178
+
179
+ func down(_ p: NSPoint) {
180
+ if let c = chosen {
181
+ if let g = gripAt(p, c) { grip = (g, p, c) }
182
+ return
183
+ }
184
+ dragFrom = p
185
+ dragTo = p
186
+ }
187
+
188
+ func dragged(_ p: NSPoint) {
189
+ if let (g, from, base) = grip {
190
+ chosen = resized(base, g, dx: p.x - from.x, dy: p.y - from.y)
191
+ placeBar()
192
+ redraw()
193
+ return
194
+ }
195
+ guard dragFrom != nil else { return }
196
+ dragTo = p
197
+ hover = nil
198
+ redraw()
199
+ }
200
+
201
+ func up(_ p: NSPoint) {
202
+ if grip != nil {
203
+ grip = nil
204
+ // a window's handles move the window itself: the recording takes it as it now is
205
+ if let w = chosenWin, let c = chosen {
206
+ if AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary), let ax = axWindow(pid: w.pid, frame: w.frame) {
207
+ axSetFrame(ax, c)
208
+ chosenWin = WinInfo(id: w.id, pid: w.pid, frame: c)
209
+ } else {
210
+ // no Accessibility: the window stays as it is, the choice becomes the area dragged
211
+ chosenWin = nil
212
+ }
213
+ }
214
+ return
215
+ }
216
+ guard dragFrom != nil else { return }
217
+ let r = dragRect
218
+ dragFrom = nil
219
+ dragTo = nil
220
+ if let r, r.width >= 16, r.height >= 16 {
221
+ choose(r.intersection(screenOf(r).frame), nil)
222
+ } else if let w = wins.first(where: { $0.frame.contains(p) }) {
223
+ choose(w.frame, w)
224
+ }
225
+ }
226
+
227
+ func choose(_ r: NSRect, _ w: WinInfo?) {
228
+ if !video {
229
+ finish(Picked(window: w.map { ($0.id, $0.pid) }, frame: r, screen: screenOf(r), mic: false, sound: false))
230
+ return
231
+ }
232
+ chosen = r
233
+ chosenWin = w
234
+ hover = nil
235
+ placeBar()
236
+ redraw()
237
+ }
238
+
239
+ func finish(_ p: Picked?) {
240
+ let d = done
241
+ done = nil
242
+ close()
243
+ d?(p)
244
+ }
245
+
246
+ func go() {
247
+ guard let c = chosen else { return }
248
+ finish(Picked(window: chosenWin.map { ($0.id, $0.pid) }, frame: c, screen: screenOf(c), mic: mic, sound: sound))
249
+ }
250
+
251
+ func cancel() { finish(nil) }
252
+
253
+ func gripAt(_ p: NSPoint, _ r: NSRect) -> Grip? {
254
+ let k: CGFloat = 10
255
+ let l = abs(p.x - r.minX) < k, rt = abs(p.x - r.maxX) < k, b = abs(p.y - r.minY) < k, t = abs(p.y - r.maxY) < k
256
+ let inX = p.x > r.minX - k && p.x < r.maxX + k, inY = p.y > r.minY - k && p.y < r.maxY + k
257
+ if t && l { return .nw }
258
+ if t && rt { return .ne }
259
+ if b && l { return .sw }
260
+ if b && rt { return .se }
261
+ if t && inX { return .n }
262
+ if b && inX { return .s }
263
+ if l && inY { return .w }
264
+ if rt && inY { return .e }
265
+ return r.contains(p) ? .move : nil
266
+ }
267
+
268
+ func resized(_ r: NSRect, _ g: Grip, dx: CGFloat, dy: CGFloat) -> NSRect {
269
+ var x0 = r.minX, x1 = r.maxX, y0 = r.minY, y1 = r.maxY
270
+ switch g {
271
+ case .move: x0 += dx; x1 += dx; y0 += dy; y1 += dy
272
+ case .n: y1 += dy
273
+ case .s: y0 += dy
274
+ case .e: x1 += dx
275
+ case .w: x0 += dx
276
+ case .ne: y1 += dy; x1 += dx
277
+ case .nw: y1 += dy; x0 += dx
278
+ case .se: y0 += dy; x1 += dx
279
+ case .sw: y0 += dy; x0 += dx
280
+ }
281
+ return NSRect(x: min(x0, x1 - 32), y: min(y0, y1 - 32), width: max(32, x1 - x0), height: max(32, y1 - y0)).integral
282
+ }
283
+
284
+ /** the bar under the choice (above it when there is no room): record, mic, sound, cancel */
285
+ func placeBar() {
286
+ guard let c = chosen else { return }
287
+ let s = screenOf(c)
288
+ guard let i = NSScreen.screens.firstIndex(of: s), i < views.count else { return }
289
+ let host = views[i]
290
+ if bar == nil || bar?.superview !== host {
291
+ bar?.removeFromSuperview()
292
+ bar = makeBar()
293
+ host.addSubview(bar!)
294
+ }
295
+ let size = bar!.fittingSize
296
+ var o = NSPoint(x: c.midX - size.width / 2 - s.frame.minX, y: c.minY - size.height - 10 - s.frame.minY)
297
+ if o.y < 8 { o.y = c.maxY + 10 - s.frame.minY }
298
+ o.x = min(max(8, o.x), s.frame.width - size.width - 8)
299
+ bar!.setFrameOrigin(o)
300
+ bar!.setFrameSize(size)
301
+ }
302
+
303
+ func makeBar() -> NSView {
304
+ let rec = NSButton(title: "● Record", target: self, action: #selector(Chooser.recordClicked))
305
+ rec.bezelStyle = .rounded
306
+ rec.contentTintColor = .systemRed
307
+ rec.keyEquivalent = "\r"
308
+ let mic = NSButton(checkboxWithTitle: "Mic", target: self, action: #selector(Chooser.micClicked(_:)))
309
+ mic.state = self.mic ? .on : .off
310
+ let snd = NSButton(checkboxWithTitle: "System sound", target: self, action: #selector(Chooser.soundClicked(_:)))
311
+ snd.state = self.sound ? .on : .off
312
+ let x = NSButton(title: "Cancel", target: self, action: #selector(Chooser.cancelClicked))
313
+ x.bezelStyle = .rounded
314
+ let stack = NSStackView(views: [rec, mic, snd, x])
315
+ stack.edgeInsets = NSEdgeInsets(top: 6, left: 10, bottom: 6, right: 10)
316
+ stack.spacing = 12
317
+ stack.wantsLayer = true
318
+ stack.layer?.backgroundColor = NSColor.windowBackgroundColor.withAlphaComponent(0.95).cgColor
319
+ stack.layer?.cornerRadius = 8
320
+ return stack
321
+ }
322
+
323
+ @objc func recordClicked() { go() }
324
+ @objc func cancelClicked() { cancel() }
325
+ @objc func micClicked(_ b: NSButton) { mic = b.state == .on }
326
+ @objc func soundClicked(_ b: NSButton) { sound = b.state == .on }
327
+ }
328
+
329
+ /** a borderless window that still takes the keyboard */
330
+ final class KeyWindow: NSWindow {
331
+ override var canBecomeKey: Bool { true }
332
+ }
333
+
334
+ final class ChooseView: NSView {
335
+ unowned let chooser: Chooser
336
+ let screen: NSScreen
337
+
338
+ init(frame: NSRect, chooser: Chooser, screen: NSScreen) {
339
+ self.chooser = chooser
340
+ self.screen = screen
341
+ super.init(frame: frame)
342
+ addTrackingArea(NSTrackingArea(rect: .zero, options: [.mouseMoved, .activeAlways, .inVisibleRect], owner: self))
343
+ }
344
+
345
+ required init?(coder: NSCoder) { fatalError() }
346
+
347
+ override var acceptsFirstResponder: Bool { true }
348
+ override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
349
+
350
+ private func global(_ e: NSEvent) -> NSPoint {
351
+ let p = convert(e.locationInWindow, from: nil)
352
+ return NSPoint(x: p.x + screen.frame.minX, y: p.y + screen.frame.minY)
353
+ }
354
+
355
+ private func local(_ r: NSRect) -> NSRect { r.offsetBy(dx: -screen.frame.minX, dy: -screen.frame.minY) }
356
+
357
+ override func mouseMoved(with e: NSEvent) { MainActor.assumeIsolated { chooser.moved(global(e)) } }
358
+ override func mouseDown(with e: NSEvent) { MainActor.assumeIsolated { chooser.down(global(e)) } }
359
+ override func mouseDragged(with e: NSEvent) { MainActor.assumeIsolated { chooser.dragged(global(e)) } }
360
+ override func mouseUp(with e: NSEvent) { MainActor.assumeIsolated { chooser.up(global(e)) } }
361
+ override func keyDown(with e: NSEvent) {
362
+ MainActor.assumeIsolated {
363
+ if e.keyCode == UInt16(kVK_Escape) { chooser.cancel() }
364
+ if e.keyCode == UInt16(kVK_Return) { chooser.go() }
365
+ }
366
+ }
367
+
368
+ override func draw(_ dirty: NSRect) {
369
+ MainActor.assumeIsolated {
370
+ if let f = chooser.frozen, f.screen === screen { NSGraphicsContext.current?.cgContext.draw(f.image, in: bounds) }
371
+ let lit: NSRect? = chooser.chosen ?? chooser.dragRect ?? chooser.hover?.frame
372
+ let shade = NSBezierPath(rect: bounds)
373
+ if let lit { shade.append(NSBezierPath(rect: local(lit))); shade.windingRule = .evenOdd }
374
+ NSColor.black.withAlphaComponent(0.4).setFill()
375
+ shade.fill()
376
+ // the lit part is never fully clear: macOS sends a click on a clear pixel to the window below, so the
377
+ // window under the pointer took the click meant to choose it
378
+ if let lit {
379
+ NSColor.white.withAlphaComponent(0.03).setFill()
380
+ local(lit).fill()
381
+ }
382
+ let font: [NSAttributedString.Key: Any] = [.foregroundColor: NSColor.white, .font: NSFont.systemFont(ofSize: 13, weight: .semibold)]
383
+ if let lit {
384
+ let r = local(lit)
385
+ NSColor.systemOrange.setStroke()
386
+ let p = NSBezierPath(rect: r.insetBy(dx: -1, dy: -1))
387
+ p.lineWidth = 2
388
+ p.stroke()
389
+ let scale = screen.backingScaleFactor
390
+ let label = "\(Int(lit.width * scale)) × \(Int(lit.height * scale))\(chooser.hover != nil && chooser.chosen == nil ? " · click: this window" : chooser.chosenWin != nil ? " · the window" : "")" as NSString
391
+ label.draw(at: NSPoint(x: r.minX + 6, y: r.maxY + 6), withAttributes: font)
392
+ if chooser.chosen != nil {
393
+ NSColor.white.setFill()
394
+ for c in [NSPoint(x: r.minX, y: r.minY), NSPoint(x: r.maxX, y: r.minY), NSPoint(x: r.minX, y: r.maxY), NSPoint(x: r.maxX, y: r.maxY), NSPoint(x: r.midX, y: r.minY), NSPoint(x: r.midX, y: r.maxY), NSPoint(x: r.minX, y: r.midY), NSPoint(x: r.maxX, y: r.midY)] {
395
+ NSBezierPath(ovalIn: NSRect(x: c.x - 5, y: c.y - 5, width: 10, height: 10)).fill()
396
+ }
397
+ }
398
+ }
399
+ if chooser.chosen == nil && screen.frame.contains(NSEvent.mouseLocation) {
400
+ let hint = (chooser.frozen != nil ? "frozen · " : "") + (chooser.windowsToo ? "click a window, or drag an area" : "drag an area") + (chooser.video ? "" : " to capture") + " · Escape cancels" as NSString
401
+ let s = hint.size(withAttributes: font)
402
+ let r = NSRect(x: bounds.midX - s.width / 2 - 12, y: bounds.maxY - 80, width: s.width + 24, height: s.height + 12)
403
+ NSColor.black.withAlphaComponent(0.7).setFill()
404
+ NSBezierPath(roundedRect: r, xRadius: 8, yRadius: 8).fill()
405
+ hint.draw(at: NSPoint(x: r.minX + 12, y: r.minY + 6), withAttributes: font)
406
+ }
407
+ }
408
+ }
409
+ }
410
+
411
+ /** the person's choice: a window or an area, then (a video) adjusted and confirmed; nil when cancelled */
412
+ @MainActor func choose(video: Bool, windowsToo: Bool, mic: Bool, sound: Bool, frozen: (screen: NSScreen, image: CGImage)? = nil) async -> Picked? {
413
+ await withCheckedContinuation { k in
414
+ let c = Chooser(video: video, windowsToo: windowsToo, mic: mic, sound: sound, frozen: frozen)
415
+ c.done = { p in
416
+ k.resume(returning: p)
417
+ _ = c
418
+ }
419
+ c.open()
420
+ }
421
+ }
422
+
423
+ // MARK: while it records
424
+
425
+ /** while a take records, always: a red pill that pulses, « REC », the time and Stop, by the frame (or at the top of the
426
+ * screen when the take has none, a system picker's), draggable out of the way; and a red frame around what records */
427
+ @MainActor
428
+ final class RecordingFrame {
429
+ var windows: [NSWindow] = []
430
+ let label = NSTextField(labelWithString: "REC 0:00")
431
+ var onStop: (() -> Void)?
432
+
433
+ init(frame: NSRect?) {
434
+ if let frame {
435
+ let pad: CGFloat = 3
436
+ let edge = NSRect(x: frame.minX - pad, y: frame.minY - pad, width: frame.width + pad * 2, height: frame.height + pad * 2)
437
+ let border = NSWindow(contentRect: edge, styleMask: .borderless, backing: .buffered, defer: false)
438
+ border.level = .statusBar
439
+ border.isOpaque = false
440
+ border.backgroundColor = .clear
441
+ border.ignoresMouseEvents = true
442
+ border.isReleasedWhenClosed = false
443
+ border.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
444
+ let v = NSView(frame: NSRect(origin: .zero, size: edge.size))
445
+ v.wantsLayer = true
446
+ v.layer?.borderColor = NSColor.systemRed.cgColor
447
+ v.layer?.borderWidth = 3
448
+ border.contentView = v
449
+ border.orderFrontRegardless()
450
+ windows.append(border)
451
+ }
452
+
453
+ let dot = NSView(frame: NSRect(x: 0, y: 0, width: 12, height: 12))
454
+ dot.wantsLayer = true
455
+ dot.layer?.backgroundColor = NSColor.white.cgColor
456
+ dot.layer?.cornerRadius = 6
457
+ dot.widthAnchor.constraint(equalToConstant: 12).isActive = true
458
+ dot.heightAnchor.constraint(equalToConstant: 12).isActive = true
459
+ let pulse = CABasicAnimation(keyPath: "opacity")
460
+ pulse.fromValue = 1
461
+ pulse.toValue = 0.15
462
+ pulse.duration = 0.7
463
+ pulse.autoreverses = true
464
+ pulse.repeatCount = .infinity
465
+ dot.layer?.add(pulse, forKey: "pulse")
466
+ label.font = NSFont.monospacedDigitSystemFont(ofSize: 15, weight: .bold)
467
+ label.textColor = .white
468
+ let stop = NSButton(title: "■ Stop", target: nil, action: nil)
469
+ stop.bezelStyle = .rounded
470
+ let stack = NSStackView(views: [dot, label, stop])
471
+ stack.edgeInsets = NSEdgeInsets(top: 6, left: 14, bottom: 6, right: 8)
472
+ stack.spacing = 10
473
+ stack.wantsLayer = true
474
+ stack.layer?.backgroundColor = NSColor.systemRed.cgColor
475
+ stack.layer?.cornerRadius = 18
476
+ let size = stack.fittingSize
477
+ let screen = frame.flatMap { f in NSScreen.screens.first { $0.frame.intersects(f) } } ?? NSScreen.screens.first { $0.frame.contains(NSEvent.mouseLocation) } ?? NSScreen.main!
478
+ let vis = screen.visibleFrame
479
+ var o: NSPoint
480
+ if let frame {
481
+ o = NSPoint(x: frame.maxX - size.width, y: frame.minY - size.height - 10)
482
+ if o.y < vis.minY + 4 { o.y = min(frame.maxY + 10, vis.maxY - size.height - 4) }
483
+ } else {
484
+ o = NSPoint(x: vis.midX - size.width / 2, y: vis.maxY - size.height - 8)
485
+ }
486
+ o.x = min(max(vis.minX + 4, o.x), vis.maxX - size.width - 4)
487
+ let hud = KeyWindow(contentRect: NSRect(origin: o, size: size), styleMask: .borderless, backing: .buffered, defer: false)
488
+ hud.level = .statusBar
489
+ hud.isOpaque = false
490
+ hud.backgroundColor = .clear
491
+ hud.hasShadow = true
492
+ hud.isMovableByWindowBackground = true
493
+ hud.isReleasedWhenClosed = false
494
+ hud.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .stationary]
495
+ hud.contentView = stack
496
+ hud.orderFrontRegardless()
497
+ windows.append(hud)
498
+ stop.target = self
499
+ stop.action = #selector(RecordingFrame.stopClicked)
500
+ }
501
+
502
+ @objc func stopClicked() { onStop?() }
503
+
504
+ func tick(_ text: String) { label.stringValue = "REC \(text)" }
505
+
506
+ func close() {
507
+ for w in windows { w.orderOut(nil) }
508
+ windows = []
509
+ }
510
+ }
@@ -0,0 +1,107 @@
1
+ // a still on the clipboard the moment it is shot (wiki tools/shipkit/recordings.md, The recorder): the app writes it
2
+ // itself, before sk files the take, as one item that is a file and its image (a chat or a mail attaches the file, a
3
+ // terminal pastes the image). Its format is the menu's choice; WebP is encoded by cwebp (Homebrew's webp), since
4
+ // ImageIO writes no WebP. The file a paste reads lives in the app's own cache folder, the newest few kept
5
+ import AppKit
6
+
7
+ enum ClipFormat: String, CaseIterable {
8
+ case png
9
+ case webpLossless = "webp-lossless"
10
+ case webpHigh = "webp-hq"
11
+
12
+ static let KEY = "clipboardFormat"
13
+ static var current: ClipFormat {
14
+ get { ClipFormat(rawValue: UserDefaults.standard.string(forKey: KEY) ?? "") ?? .png }
15
+ set { UserDefaults.standard.set(newValue.rawValue, forKey: KEY) }
16
+ }
17
+
18
+ var label: String {
19
+ switch self {
20
+ case .png: "PNG"
21
+ case .webpLossless: "WebP lossless"
22
+ case .webpHigh: "WebP HQ"
23
+ }
24
+ }
25
+
26
+ // measured on a 3024x1898 still (PNG 7.2 MB): lossless -z 0 is 0.35 s and 4.3 MB, -z 1 is 1.8 s for 5% less;
27
+ // -q 90 -m 2 is 0.32 s and 0.67 MB
28
+ var cwebp: [String] { self == .webpLossless ? ["-lossless", "-z", "0"] : ["-q", "90", "-m", "2"] }
29
+ }
30
+
31
+ enum Clip {
32
+ static let KEEP = 20
33
+ static let WEBP_TYPE = NSPasteboard.PasteboardType("org.webmproject.webp")
34
+
35
+ static var dir: URL {
36
+ let d = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("dev.shipkit.recorder/clipboard", isDirectory: true)
37
+ try? FileManager.default.createDirectory(at: d, withIntermediateDirectories: true)
38
+ return d
39
+ }
40
+
41
+ /** the still copied where the clipboard's file lives, at once: sk moves the take's own file when it files it */
42
+ static func stage(_ png: URL) throws -> URL {
43
+ let f = DateFormatter()
44
+ f.dateFormat = "yyyyMMdd-HHmmss-SSS"
45
+ let to = dir.appendingPathComponent("screenshot-\(f.string(from: Date())).png")
46
+ try? FileManager.default.removeItem(at: to)
47
+ try FileManager.default.copyItem(at: png, to: to)
48
+ prune()
49
+ return to
50
+ }
51
+
52
+ /** the staged still on the clipboard in `format`; what it says, or the reason it could not */
53
+ @MainActor static func put(_ png: URL, _ format: ClipFormat) async throws -> String {
54
+ let data = try Data(contentsOf: png)
55
+ var file = png
56
+ var webp: Data?
57
+ if format != .png {
58
+ guard let bin = ["/opt/homebrew/bin/cwebp", "/usr/local/bin/cwebp"].first(where: { FileManager.default.isExecutableFile(atPath: $0) }) else {
59
+ throw Err("\(format.label) needs cwebp: brew install webp")
60
+ }
61
+ let out = png.deletingPathExtension().appendingPathExtension("webp")
62
+ let r = await run(bin, ["-quiet", "-mt"] + format.cwebp + [png.path, "-o", out.path])
63
+ if r.code != 0 { throw Err("cwebp: \(r.err.isEmpty ? "exit \(r.code)" : r.err)") }
64
+ webp = try Data(contentsOf: out)
65
+ file = out
66
+ }
67
+ let it = NSPasteboardItem()
68
+ it.setString(file.absoluteString, forType: .fileURL)
69
+ if let webp { it.setData(webp, forType: WEBP_TYPE) }
70
+ it.setData(data, forType: .png)
71
+ let pb = NSPasteboard.general
72
+ pb.clearContents()
73
+ if !pb.writeObjects([it]) { throw Err("the clipboard refused \(file.lastPathComponent)") }
74
+ let size = ByteCountFormatter.string(fromByteCount: Int64(webp?.count ?? data.count), countStyle: .file)
75
+ return "on the clipboard as \(format.label) (\(size))"
76
+ }
77
+
78
+ /** the newest KEEP stills and their WebP stay: a paste of an older clipboard still finds its file */
79
+ static func prune() {
80
+ let fm = FileManager.default
81
+ let all = (try? fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)) ?? []
82
+ let stems = Set(all.map { $0.deletingPathExtension().lastPathComponent }).sorted(by: >)
83
+ for stem in stems.dropFirst(KEEP) {
84
+ for f in all where f.deletingPathExtension().lastPathComponent == stem { try? fm.removeItem(at: f) }
85
+ }
86
+ }
87
+
88
+ static func run(_ bin: String, _ args: [String]) async -> (code: Int32, err: String) {
89
+ await withCheckedContinuation { k in
90
+ DispatchQueue.global().async {
91
+ let p = Process()
92
+ p.executableURL = URL(fileURLWithPath: bin)
93
+ p.arguments = args
94
+ let e = Pipe()
95
+ p.standardError = e
96
+ p.standardOutput = FileHandle.nullDevice
97
+ do { try p.run() } catch {
98
+ k.resume(returning: (127, error.localizedDescription))
99
+ return
100
+ }
101
+ let err = e.fileHandleForReading.readDataToEndOfFile()
102
+ p.waitUntilExit()
103
+ k.resume(returning: (p.terminationStatus, String(decoding: err, as: UTF8.self).trimmingCharacters(in: .whitespacesAndNewlines)))
104
+ }
105
+ }
106
+ }
107
+ }
@@ -0,0 +1,39 @@
1
+ // the recorder's editor window (wiki tools/shipkit/recordings.md, The recorder): the hub's editor alone for one capture
2
+ // (`/recordings?solo=1#<repo>/<id>`), in a window of its own, resizable, closable, where it was left: brought to the
3
+ // front when it opens, then a window like any other, behind what is picked next (only the tray floats). It opens after a take whose preset ends with open-editor (a screenshot, by default) and on a click of a
4
+ // capture in the tray, so a capture is annotated without leaving what was being done
5
+ import AppKit
6
+ import WebKit
7
+
8
+ @MainActor
9
+ final class EditorWindow: NSObject, NSWindowDelegate {
10
+ var window: NSWindow?
11
+ var web: WKWebView?
12
+
13
+ func open(_ url: URL) {
14
+ let w = window ?? make()
15
+ window = w
16
+ web?.load(URLRequest(url: url))
17
+ NSApp.activate()
18
+ w.makeKeyAndOrderFront(nil)
19
+ w.orderFrontRegardless()
20
+ }
21
+
22
+ private func make() -> NSWindow {
23
+ let config = WKWebViewConfiguration()
24
+ config.websiteDataStore = .default()
25
+ let v = WKWebView(frame: NSRect(x: 0, y: 0, width: 1100, height: 760), configuration: config)
26
+ v.setValue(false, forKey: "drawsBackground")
27
+ web = v
28
+ let w = NSWindow(contentRect: v.frame, styleMask: [.titled, .closable, .resizable, .miniaturizable], backing: .buffered, defer: false)
29
+ w.title = "Capture"
30
+ w.contentView = v
31
+ w.level = .normal
32
+ w.isReleasedWhenClosed = false
33
+ w.minSize = NSSize(width: 640, height: 420)
34
+ w.delegate = self
35
+ w.center()
36
+ w.setFrameAutosaveName("ShipkitCaptureEditor")
37
+ return w
38
+ }
39
+ }