rvlib-shipkit 2.21.0 → 2.22.1
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.
- package/CHANGELOG.md +23 -0
- package/package.json +1 -1
- package/src/asks/asks-queue.ts +8 -7
- package/src/assets/kits/identity-kit.ts +158 -2
- package/src/cli/cmd_Bump.ts +3 -3
- package/src/cli/cmd_Rec.ts +349 -0
- package/src/cli/cmd_Releases.ts +4 -1
- package/src/cli/cmd_Secret.ts +10 -3
- package/src/cli/cmd_Sign.ts +24 -0
- package/src/cli/cmd_Social.ts +44 -1
- package/src/cli/family-pages.ts +1 -0
- package/src/cli/registry.ts +4 -0
- package/src/core/agent-spawn.ts +4 -17
- package/src/core/code-lines.ts +49 -0
- package/src/core/discover-checks.ts +27 -3
- package/src/core/hard-reset.ts +34 -0
- package/src/core/raw-access.ts +3 -13
- package/src/core/throwaway-worktree.ts +72 -0
- package/src/home/app-sign.ts +98 -0
- package/src/home/gui-app.ts +31 -12
- package/src/home/home-routes.ts +6 -0
- package/src/home/serve-web.ts +1 -1
- package/src/identities/identities.ts +6 -0
- package/src/media-kinds.ts +5 -3
- package/src/media-store/forget.ts +54 -0
- package/src/media-store/put.ts +4 -3
- package/src/media-store/space.ts +63 -24
- package/src/recordings/delete.ts +19 -0
- package/src/recordings/edit.ts +231 -0
- package/src/recordings/load.ts +37 -0
- package/src/recordings/marks-draw.ts +111 -0
- package/src/recordings/marks-png.ts +25 -0
- package/src/recordings/marks.ts +138 -0
- package/src/recordings/media-kind.ts +67 -0
- package/src/recordings/model.ts +170 -0
- package/src/recordings/open.ts +33 -0
- package/src/recordings/place.ts +89 -0
- package/src/recordings/presets.ts +99 -0
- package/src/recordings/recorder-app/Capture.swift +510 -0
- package/src/recordings/recorder-app/Clip.swift +162 -0
- package/src/recordings/recorder-app/Editor.swift +39 -0
- package/src/recordings/recorder-app/Tray.swift +298 -0
- package/src/recordings/recorder-app/main.swift +862 -0
- package/src/recordings/recorder-app.ts +46 -0
- package/src/recordings/recorder.shipkit.ts +11 -0
- package/src/recordings/routes.ts +140 -0
- package/src/recordings/run-recorder.ts +40 -0
- package/src/recordings/share.ts +107 -0
- package/src/recordings/take.ts +146 -0
- package/src/recordings/view.ts +40 -0
- package/src/release/gate-checkout.ts +74 -0
- package/src/release/release-scope.ts +35 -26
- package/src/release/run-bump.ts +79 -37
- package/src/release/run-release.ts +67 -53
- package/src/releases/add.ts +7 -2
- package/src/releases/from-card.ts +3 -3
- package/src/releases/model.ts +29 -7
- package/src/repo-config.ts +7 -3
- package/src/review/trailer-desk.ts +1 -0
- package/src/review/trailer-model.ts +2 -0
- package/src/secrets/manager.ts +18 -1
- package/src/serve/hub-app/home-st.ts +15 -5
- package/src/serve/hub-app/home.html +1 -0
- package/src/serve/hub-app/hub-app.tsx +6 -1
- package/src/serve/hub-app/icons.tsx +2 -0
- package/src/serve/hub-app/palette-st.ts +1 -0
- package/src/serve/hub-app/project-list.tsx +1 -0
- package/src/serve/hub-app/projects-svc.ts +1 -1
- package/src/serve/hub-app/recordings-st.ts +641 -0
- package/src/serve/hub-app/recordings-view.tsx +504 -0
- package/src/serve/hub-app/recordings.css +82 -0
- package/src/serve/hub-app/repo-st.ts +1 -1
- package/src/serve/hub-app/review/trailer-page.tsx +1 -0
- package/src/serve/hub-app/review/trailer-st.ts +16 -0
- package/src/serve/hub-app/review/trailer-timeline.tsx +3 -0
- package/src/serve/hub-app/review.css +2 -0
- package/src/serve/hub-app/social/post-card.tsx +31 -3
- package/src/serve/hub-app/social/social-model.ts +4 -0
- package/src/serve/hub-app/social/social-st.ts +12 -0
- package/src/serve/hub-app/social.css +4 -0
- package/src/shipkit.ts +2 -2
- package/src/shot/box.ts +23 -17
- package/src/shot/browser-slot.ts +13 -3
- package/src/shot/film-box.ts +193 -117
- package/src/shot/shooter.ts +5 -3
- package/src/smoke/run-smoke.ts +8 -6
- package/src/social/compose.ts +14 -0
- package/src/social/drafts.ts +1 -1
- package/src/social/load.ts +16 -2
- package/src/social/publish/index.ts +3 -1
- package/src/social/publish/secret.ts +12 -4
- package/src/social/release-card.ts +69 -0
- package/src/social/release-stage.ts +49 -10
- package/src/social/routes.ts +5 -0
- package/src/social/stage.ts +9 -5
- package/src/trailer/build.ts +24 -0
- package/src/trailer/ffmpeg.ts +8 -0
- package/src/trailer/grammar.ts +9 -2
- package/src/trailer/look.ts +7 -0
- 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,162 @@
|
|
|
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). The item is written at once with its data PROMISED: the PNG, the file and the WebP are
|
|
4
|
+
// made behind it, and a paste that comes first waits for them, so a ⌘V right after the shot never pastes the clipboard
|
|
5
|
+
// before it. Its format is the menu's choice; WebP is encoded by cwebp (Homebrew's webp), since ImageIO writes no
|
|
6
|
+
// WebP. The file a paste reads lives in the app's own cache folder, the newest few kept
|
|
7
|
+
import AppKit
|
|
8
|
+
|
|
9
|
+
enum ClipFormat: String, CaseIterable {
|
|
10
|
+
case png
|
|
11
|
+
case webpLossless = "webp-lossless"
|
|
12
|
+
case webpHigh = "webp-hq"
|
|
13
|
+
|
|
14
|
+
static let KEY = "clipboardFormat"
|
|
15
|
+
static var current: ClipFormat {
|
|
16
|
+
get { ClipFormat(rawValue: UserDefaults.standard.string(forKey: KEY) ?? "") ?? .png }
|
|
17
|
+
set { UserDefaults.standard.set(newValue.rawValue, forKey: KEY) }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
var label: String {
|
|
21
|
+
switch self {
|
|
22
|
+
case .png: "PNG"
|
|
23
|
+
case .webpLossless: "WebP lossless"
|
|
24
|
+
case .webpHigh: "WebP HQ"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 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;
|
|
29
|
+
// -q 90 -m 2 is 0.32 s and 0.67 MB
|
|
30
|
+
var cwebp: [String] { self == .webpLossless ? ["-lossless", "-z", "0"] : ["-q", "90", "-m", "2"] }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** one still's clipboard data, made off the main thread and handed to the clipboard as it asks: the image encoded or
|
|
34
|
+
* read, its file in the cache folder, its WebP; a WebP that fails falls back to the PNG, said */
|
|
35
|
+
final class ClipJob: NSObject, NSPasteboardItemDataProvider, @unchecked Sendable {
|
|
36
|
+
/** the jobs the clipboard still holds a promise of: kept alive until it lets go */
|
|
37
|
+
@MainActor static var live: [ClipJob] = []
|
|
38
|
+
|
|
39
|
+
let format: ClipFormat
|
|
40
|
+
private let ready = DispatchGroup()
|
|
41
|
+
private(set) var png = Data()
|
|
42
|
+
private(set) var file: URL?
|
|
43
|
+
private(set) var webp: Data?
|
|
44
|
+
/** why the chosen format could not be made; the PNG stands in */
|
|
45
|
+
private(set) var failure: String?
|
|
46
|
+
|
|
47
|
+
/** from the image itself (a capture), or from a PNG on the disk (a tray copy) */
|
|
48
|
+
init(image: CGImage? = nil, pngFile: URL? = nil, format: ClipFormat) {
|
|
49
|
+
self.format = format
|
|
50
|
+
super.init()
|
|
51
|
+
ready.enter()
|
|
52
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
53
|
+
self.make(image, pngFile)
|
|
54
|
+
self.ready.leave()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private func make(_ image: CGImage?, _ pngFile: URL?) {
|
|
59
|
+
let f = DateFormatter()
|
|
60
|
+
f.dateFormat = "yyyyMMdd-HHmmss-SSS"
|
|
61
|
+
let base = Clip.dir.appendingPathComponent("screenshot-\(f.string(from: Date()))")
|
|
62
|
+
let at = base.appendingPathExtension("png")
|
|
63
|
+
do {
|
|
64
|
+
if let image {
|
|
65
|
+
let d = NSMutableData()
|
|
66
|
+
guard let dest = CGImageDestinationCreateWithData(d, "public.png" as CFString, 1, nil) else { throw Err("no PNG encoder") }
|
|
67
|
+
CGImageDestinationAddImage(dest, image, nil)
|
|
68
|
+
guard CGImageDestinationFinalize(dest) else { throw Err("the PNG did not encode") }
|
|
69
|
+
png = d as Data
|
|
70
|
+
} else if let pngFile {
|
|
71
|
+
png = try Data(contentsOf: pngFile)
|
|
72
|
+
}
|
|
73
|
+
try png.write(to: at)
|
|
74
|
+
file = at
|
|
75
|
+
Clip.prune()
|
|
76
|
+
} catch {
|
|
77
|
+
failure = "the clipboard's file: \(error.localizedDescription)"
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
guard format != .png else { return }
|
|
81
|
+
guard let bin = ["/opt/homebrew/bin/cwebp", "/usr/local/bin/cwebp"].first(where: { FileManager.default.isExecutableFile(atPath: $0) }) else {
|
|
82
|
+
failure = "\(format.label) needs cwebp (brew install webp): the clipboard has the PNG"
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
let out = base.appendingPathExtension("webp")
|
|
86
|
+
let r = Clip.runNow(bin, ["-quiet", "-mt"] + format.cwebp + [at.path, "-o", out.path])
|
|
87
|
+
guard r.code == 0, let d = try? Data(contentsOf: out) else {
|
|
88
|
+
failure = "cwebp: \(r.err.isEmpty ? "exit \(r.code)" : r.err); the clipboard has the PNG"
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
webp = d
|
|
92
|
+
file = out
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** waits for the data, then runs `done` on the main thread with what it says */
|
|
96
|
+
func whenReady(_ done: @escaping @MainActor (String, String?) -> Void) {
|
|
97
|
+
ready.notify(queue: .main) {
|
|
98
|
+
MainActor.assumeIsolated {
|
|
99
|
+
let size = ByteCountFormatter.string(fromByteCount: Int64(self.webp?.count ?? self.png.count), countStyle: .file)
|
|
100
|
+
done("on the clipboard as \(self.webp != nil ? self.format.label : "PNG") (\(size))", self.failure)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
func pasteboard(_ pasteboard: NSPasteboard?, item: NSPasteboardItem, provideDataForType type: NSPasteboard.PasteboardType) {
|
|
106
|
+
// a paste that comes before the data waits for it: never the clipboard's older content
|
|
107
|
+
ready.wait()
|
|
108
|
+
if type == .fileURL, let file { item.setString(file.absoluteString, forType: .fileURL) }
|
|
109
|
+
else if type == .png { item.setData(png, forType: .png) }
|
|
110
|
+
else if type == Clip.WEBP_TYPE, let webp { item.setData(webp, forType: type) }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
func pasteboardFinishedWithDataProvider(_ pasteboard: NSPasteboard) {
|
|
114
|
+
DispatchQueue.main.async { MainActor.assumeIsolated { ClipJob.live.removeAll { $0 === self } } }
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
enum Clip {
|
|
119
|
+
static let KEEP = 20
|
|
120
|
+
static let WEBP_TYPE = NSPasteboard.PasteboardType("org.webmproject.webp")
|
|
121
|
+
|
|
122
|
+
static var dir: URL {
|
|
123
|
+
let d = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("dev.shipkit.recorder/clipboard", isDirectory: true)
|
|
124
|
+
try? FileManager.default.createDirectory(at: d, withIntermediateDirectories: true)
|
|
125
|
+
return d
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** the job's item on the clipboard now, every type promised: the file, the PNG and, for a WebP format, the WebP */
|
|
129
|
+
@MainActor static func put(_ job: ClipJob) throws {
|
|
130
|
+
let it = NSPasteboardItem()
|
|
131
|
+
let types: [NSPasteboard.PasteboardType] = job.format == .png ? [.fileURL, .png] : [.fileURL, WEBP_TYPE, .png]
|
|
132
|
+
guard it.setDataProvider(job, forTypes: types) else { throw Err("the clipboard took no promise") }
|
|
133
|
+
let pb = NSPasteboard.general
|
|
134
|
+
pb.clearContents()
|
|
135
|
+
if !pb.writeObjects([it]) { throw Err("the clipboard refused the still") }
|
|
136
|
+
ClipJob.live.append(job)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** the newest KEEP stills and their WebP stay: a paste of an older clipboard still finds its file */
|
|
140
|
+
static func prune() {
|
|
141
|
+
let fm = FileManager.default
|
|
142
|
+
let all = (try? fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)) ?? []
|
|
143
|
+
let stems = Set(all.map { $0.deletingPathExtension().lastPathComponent }).sorted(by: >)
|
|
144
|
+
for stem in stems.dropFirst(KEEP) {
|
|
145
|
+
for f in all where f.deletingPathExtension().lastPathComponent == stem { try? fm.removeItem(at: f) }
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** a tool run to its end on the calling thread: its exit code and what it said */
|
|
150
|
+
static func runNow(_ bin: String, _ args: [String]) -> (code: Int32, err: String) {
|
|
151
|
+
let p = Process()
|
|
152
|
+
p.executableURL = URL(fileURLWithPath: bin)
|
|
153
|
+
p.arguments = args
|
|
154
|
+
let e = Pipe()
|
|
155
|
+
p.standardError = e
|
|
156
|
+
p.standardOutput = FileHandle.nullDevice
|
|
157
|
+
do { try p.run() } catch { return (127, error.localizedDescription) }
|
|
158
|
+
let err = e.fileHandleForReading.readDataToEndOfFile()
|
|
159
|
+
p.waitUntilExit()
|
|
160
|
+
return (p.terminationStatus, String(decoding: err, as: UTF8.self).trimmingCharacters(in: .whitespacesAndNewlines))
|
|
161
|
+
}
|
|
162
|
+
}
|