dsh-task-cat 0.0.0-stage → 1.0.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/README.md +96 -2
- package/client.js +1486 -0
- package/cordis.patch.yml +3 -0
- package/desktop.js +559 -0
- package/icon.svg +15 -0
- package/index.js +561 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/overlay/TaskPetOverlay.swift +1566 -0
- package/package.json +50 -4
|
@@ -0,0 +1,1566 @@
|
|
|
1
|
+
// dsh-task-pet desktop overlay.
|
|
2
|
+
//
|
|
3
|
+
// A borderless, always-on-top companion window that lives outside the Harness
|
|
4
|
+
// app frame, so the pet keeps floating over every other application on the
|
|
5
|
+
// screen. The Host half spawns this binary with piped stdio and speaks JSON
|
|
6
|
+
// lines with it: state arrives on stdin, click intents leave on stdout. The
|
|
7
|
+
// pipe is the only channel — no socket, no token, nothing to authenticate.
|
|
8
|
+
//
|
|
9
|
+
// Rendered with AppKit/CoreGraphics so the binary ships no assets.
|
|
10
|
+
|
|
11
|
+
import AppKit
|
|
12
|
+
import Foundation
|
|
13
|
+
|
|
14
|
+
// MARK: - Wire payloads
|
|
15
|
+
|
|
16
|
+
/// One roster row. `meta` is composed by the page with the dictionary it already
|
|
17
|
+
/// loaded, so the desktop pet says exactly what the in-window panel says.
|
|
18
|
+
struct SessionEntry: Decodable {
|
|
19
|
+
let id: String
|
|
20
|
+
let title: String
|
|
21
|
+
let kind: String
|
|
22
|
+
let meta: String?
|
|
23
|
+
let done: Int?
|
|
24
|
+
let total: Int?
|
|
25
|
+
let current: Bool?
|
|
26
|
+
let currentLabel: String?
|
|
27
|
+
let active: Bool?
|
|
28
|
+
|
|
29
|
+
/// Completion in 0...1, or nil when the Session has no todo list to report.
|
|
30
|
+
var ratio: CGFloat? {
|
|
31
|
+
guard let done, let total, total > 0 else { return nil }
|
|
32
|
+
return min(1, max(0, CGFloat(done) / CGFloat(total)))
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
struct StateMessage: Decodable {
|
|
37
|
+
let type: String
|
|
38
|
+
let mood: String?
|
|
39
|
+
let badge: Int?
|
|
40
|
+
let attention: Int?
|
|
41
|
+
let running: Int?
|
|
42
|
+
let summary: String?
|
|
43
|
+
let locale: String?
|
|
44
|
+
let light: Bool?
|
|
45
|
+
let sessions: [SessionEntry]?
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// MARK: - Locale
|
|
49
|
+
|
|
50
|
+
/// Only the strings this binary owns; everything else is rendered by the page.
|
|
51
|
+
struct Strings {
|
|
52
|
+
let open: String
|
|
53
|
+
let hide: String
|
|
54
|
+
let moveHere: String
|
|
55
|
+
/// Card heading; the page's own panel uses the same words.
|
|
56
|
+
let title: String
|
|
57
|
+
/// One line under the heading, matching the panel's hint.
|
|
58
|
+
let hint: String
|
|
59
|
+
/// Label for the switch back into the app window.
|
|
60
|
+
let toWindow: String
|
|
61
|
+
/// "and N more conversations", matching the panel's list.more.
|
|
62
|
+
let more: (Int) -> String
|
|
63
|
+
/// Collapse label, matching the panel's list.less.
|
|
64
|
+
let less: String
|
|
65
|
+
/// Which language produced this table, so `apply` can skip a no-op rebuild.
|
|
66
|
+
let languageKey: String
|
|
67
|
+
|
|
68
|
+
init(open: String, moveHere: String, hide: String, title: String, hint: String,
|
|
69
|
+
toWindow: String, more: @escaping (Int) -> String, less: String, languageKey: String) {
|
|
70
|
+
self.languageKey = languageKey
|
|
71
|
+
self.open = open
|
|
72
|
+
self.moveHere = moveHere
|
|
73
|
+
self.hide = hide
|
|
74
|
+
self.title = title
|
|
75
|
+
self.hint = hint
|
|
76
|
+
self.toWindow = toWindow
|
|
77
|
+
self.more = more
|
|
78
|
+
self.less = less
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
static func forLanguage(_ raw: String) -> Strings {
|
|
82
|
+
if raw.hasPrefix("zh") {
|
|
83
|
+
return Strings(open: "打开 DeepSeek Harness",
|
|
84
|
+
moveHere: "移到当前屏幕",
|
|
85
|
+
hide: "隐藏桌面宠物",
|
|
86
|
+
title: "任务宠物",
|
|
87
|
+
hint: "桌面宠物会浮在其它软件之上,点它展开会话清单。",
|
|
88
|
+
toWindow: "收回窗口内",
|
|
89
|
+
more: { "还有 \($0) 个会话…" },
|
|
90
|
+
less: "收起",
|
|
91
|
+
languageKey: "zh")
|
|
92
|
+
}
|
|
93
|
+
if raw.hasPrefix("ja") {
|
|
94
|
+
return Strings(open: "DeepSeek Harness を開く",
|
|
95
|
+
moveHere: "この画面へ移動",
|
|
96
|
+
hide: "デスクトップペットを隠す",
|
|
97
|
+
title: "タスクペット",
|
|
98
|
+
hint: "デスクトップペットは他のアプリの上に浮かび、クリックで会話一覧を開きます。",
|
|
99
|
+
toWindow: "ウィンドウ内に戻す",
|
|
100
|
+
more: { "他 \($0) 件…" },
|
|
101
|
+
less: "閉じる",
|
|
102
|
+
languageKey: "ja")
|
|
103
|
+
}
|
|
104
|
+
if raw.hasPrefix("de") {
|
|
105
|
+
return Strings(open: "DeepSeek Harness öffnen",
|
|
106
|
+
moveHere: "Auf diesen Bildschirm",
|
|
107
|
+
hide: "Desktop-Haustier ausblenden",
|
|
108
|
+
title: "Aufgaben-Haustier",
|
|
109
|
+
hint: "Das Haustier schwebt über anderen Programmen; klicke darauf, um die Unterhaltungsliste zu öffnen.",
|
|
110
|
+
toWindow: "Zurück ins Fenster",
|
|
111
|
+
more: { "\($0) weitere…" },
|
|
112
|
+
less: "Weniger",
|
|
113
|
+
languageKey: "de")
|
|
114
|
+
}
|
|
115
|
+
if raw.hasPrefix("fr") {
|
|
116
|
+
return Strings(open: "Ouvrir DeepSeek Harness",
|
|
117
|
+
moveHere: "Sur cet écran",
|
|
118
|
+
hide: "Masquer la mascotte",
|
|
119
|
+
title: "Mascotte de tâches",
|
|
120
|
+
hint: "La mascotte flotte au-dessus des autres applications ; cliquez pour ouvrir la liste des conversations.",
|
|
121
|
+
toWindow: "Revenir dans la fenêtre",
|
|
122
|
+
more: { "\($0) de plus…" },
|
|
123
|
+
less: "Réduire",
|
|
124
|
+
languageKey: "fr")
|
|
125
|
+
}
|
|
126
|
+
if raw.hasPrefix("es") {
|
|
127
|
+
return Strings(open: "Abrir DeepSeek Harness",
|
|
128
|
+
moveHere: "A esta pantalla",
|
|
129
|
+
hide: "Ocultar la mascota",
|
|
130
|
+
title: "Mascota de tareas",
|
|
131
|
+
hint: "La mascota flota sobre otras aplicaciones; haz clic para ver la lista de conversaciones.",
|
|
132
|
+
toWindow: "Volver a la ventana",
|
|
133
|
+
more: { "\($0) más…" },
|
|
134
|
+
less: "Ver menos",
|
|
135
|
+
languageKey: "es")
|
|
136
|
+
}
|
|
137
|
+
if raw.hasPrefix("pt") {
|
|
138
|
+
return Strings(open: "Abrir o DeepSeek Harness",
|
|
139
|
+
moveHere: "Para este ecrã",
|
|
140
|
+
hide: "Ocultar a mascote",
|
|
141
|
+
title: "Mascote de tarefas",
|
|
142
|
+
hint: "A mascote flutua sobre os outros aplicativos; clique para abrir a lista de conversas.",
|
|
143
|
+
toWindow: "Voltar para a janela",
|
|
144
|
+
more: { "Mais \($0)…" },
|
|
145
|
+
less: "Mostrar menos",
|
|
146
|
+
languageKey: "pt")
|
|
147
|
+
}
|
|
148
|
+
if raw.hasPrefix("ru") {
|
|
149
|
+
return Strings(open: "Открыть DeepSeek Harness",
|
|
150
|
+
moveHere: "На этот экран",
|
|
151
|
+
hide: "Скрыть питомца",
|
|
152
|
+
title: "Питомец задач",
|
|
153
|
+
hint: "Питомец парит над другими программами; нажмите, чтобы открыть список диалогов.",
|
|
154
|
+
toWindow: "Вернуть в окно",
|
|
155
|
+
more: { "Ещё \($0)…" },
|
|
156
|
+
less: "Свернуть",
|
|
157
|
+
languageKey: "ru")
|
|
158
|
+
}
|
|
159
|
+
return Strings(open: "Open DeepSeek Harness",
|
|
160
|
+
moveHere: "Move to this screen",
|
|
161
|
+
hide: "Hide the desktop pet",
|
|
162
|
+
title: "Task pet",
|
|
163
|
+
hint: "The desktop pet floats above other apps; click it to open the session list.",
|
|
164
|
+
toWindow: "Bring it back into the window",
|
|
165
|
+
more: { "\($0) more…" },
|
|
166
|
+
less: "Show less",
|
|
167
|
+
languageKey: "en")
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let environmentLanguage = ProcessInfo.processInfo.environment["DSH_TASK_PET_LOCALE"] ?? "en"
|
|
172
|
+
/// The helper's own menu labels. The page tells us the app's active locale, which
|
|
173
|
+
/// beats the shell's `LANG` (often `C.UTF-8` on macOS and therefore English).
|
|
174
|
+
var strings = Strings.forLanguage(environmentLanguage)
|
|
175
|
+
|
|
176
|
+
// MARK: - Stdio
|
|
177
|
+
|
|
178
|
+
let outHandle = FileHandle.standardOutput
|
|
179
|
+
let outLock = NSLock()
|
|
180
|
+
|
|
181
|
+
/// Write one JSON line to the Host half.
|
|
182
|
+
func emit(_ object: [String: Any]) {
|
|
183
|
+
guard JSONSerialization.isValidJSONObject(object),
|
|
184
|
+
let data = try? JSONSerialization.data(withJSONObject: object),
|
|
185
|
+
var line = String(data: data, encoding: .utf8) else { return }
|
|
186
|
+
line += "\n"
|
|
187
|
+
outLock.lock()
|
|
188
|
+
outHandle.write(Data(line.utf8))
|
|
189
|
+
outLock.unlock()
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
func emitLog(_ message: String) {
|
|
193
|
+
emit(["type": "log", "message": message])
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// MARK: - Geometry
|
|
197
|
+
|
|
198
|
+
/// The artwork's own square; the window adds room for the badge and its shadow.
|
|
199
|
+
let petSize: CGFloat = 64
|
|
200
|
+
let canvas: CGFloat = 84
|
|
201
|
+
|
|
202
|
+
// MARK: - Pet view
|
|
203
|
+
|
|
204
|
+
final class PetView: NSView {
|
|
205
|
+
var mood: String = "sleeping"
|
|
206
|
+
var badge: Int = 0
|
|
207
|
+
var attention: Int = 0
|
|
208
|
+
var working: Bool = false
|
|
209
|
+
var phase: CGFloat = 0
|
|
210
|
+
/// Completion of the row a click would jump to, drawn as a ring; nil hides it.
|
|
211
|
+
var progress: CGFloat?
|
|
212
|
+
/// Draw the ring in the attention colour instead of the working colour.
|
|
213
|
+
var progressAlert = false
|
|
214
|
+
/// The headline the Host composed, spoken by assistive technology.
|
|
215
|
+
var summary: String = ""
|
|
216
|
+
|
|
217
|
+
/// The view is not flipped, so CoreGraphics coordinates match AppKit's.
|
|
218
|
+
override var isFlipped: Bool { false }
|
|
219
|
+
|
|
220
|
+
override func draw(_ dirtyRect: NSRect) {
|
|
221
|
+
guard let ctx = NSGraphicsContext.current?.cgContext else { return }
|
|
222
|
+
ctx.setShouldAntialias(true)
|
|
223
|
+
let bob = working ? sin(phase) * 1.5 : sin(phase * 0.35) * 0.7
|
|
224
|
+
ctx.saveGState()
|
|
225
|
+
ctx.translateBy(x: (bounds.width - petSize) / 2, y: (bounds.height - petSize) / 2 + bob)
|
|
226
|
+
drawPet(ctx)
|
|
227
|
+
ctx.restoreGState()
|
|
228
|
+
// The meter is a gauge for the whole overlay, so it stays put while the pet
|
|
229
|
+
// bobs, and it never competes with the count badge in the top-right corner.
|
|
230
|
+
drawProgressMeter(ctx)
|
|
231
|
+
if badge > 0 { drawBadge(ctx, text: badge > 9 ? "9+" : String(badge)) }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/// A slim meter under the pet showing how much of the target Session's plan is
|
|
235
|
+
/// done. The badge answers "how many need me"; this answers "how far along".
|
|
236
|
+
private func drawProgressMeter(_ ctx: CGContext) {
|
|
237
|
+
guard let progress else { return }
|
|
238
|
+
let track = CGRect(x: 16, y: 2, width: 52, height: 3.5)
|
|
239
|
+
let radius = track.height / 2
|
|
240
|
+
ctx.setFillColor(CGColor(srgbRed: 0.129, green: 0.231, blue: 0.2, alpha: 0.22))
|
|
241
|
+
ctx.addPath(CGPath(roundedRect: track, cornerWidth: radius, cornerHeight: radius, transform: nil))
|
|
242
|
+
ctx.fillPath()
|
|
243
|
+
let width = max(track.height, track.width * max(0, min(1, progress)))
|
|
244
|
+
let filled = CGRect(x: track.minX, y: track.minY, width: width, height: track.height)
|
|
245
|
+
ctx.setFillColor(progressAlert
|
|
246
|
+
? CGColor(srgbRed: 0.961, green: 0.729, blue: 0.196, alpha: 1)
|
|
247
|
+
: CGColor(srgbRed: 0.353, green: 0.663, blue: 0.553, alpha: 1))
|
|
248
|
+
ctx.addPath(CGPath(roundedRect: filled, cornerWidth: radius, cornerHeight: radius, transform: nil))
|
|
249
|
+
ctx.fillPath()
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private func fillRounded(_ ctx: CGContext, _ rect: CGRect, _ radius: CGFloat) {
|
|
253
|
+
ctx.addPath(CGPath(roundedRect: rect, cornerWidth: radius, cornerHeight: radius, transform: nil))
|
|
254
|
+
ctx.fillPath()
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/// Draw the pet in a 64x64 box with the origin at the bottom left.
|
|
258
|
+
///
|
|
259
|
+
/// A white long-haired cat, built from the same shape language as the mascot
|
|
260
|
+
/// this replaced: one big round head, oversized dark eyes with a sparkle, a
|
|
261
|
+
/// small mouth and soft blush. Fur is suggested with a few rounded tufts, not
|
|
262
|
+
/// spikes — spiky tufts plus ringed eyes were what made the first version read
|
|
263
|
+
/// as uncanny.
|
|
264
|
+
private func drawPet(_ ctx: CGContext) {
|
|
265
|
+
let fur = CGColor(srgbRed: 0.988, green: 0.980, blue: 0.969, alpha: 1)
|
|
266
|
+
let furShade = CGColor(srgbRed: 0.910, green: 0.886, blue: 0.855, alpha: 1)
|
|
267
|
+
let earInner = CGColor(srgbRed: 0.965, green: 0.769, blue: 0.804, alpha: 1)
|
|
268
|
+
let blush = CGColor(srgbRed: 0.976, green: 0.702, blue: 0.706, alpha: 0.45)
|
|
269
|
+
let ink = CGColor(srgbRed: 0.153, green: 0.137, blue: 0.129, alpha: 1)
|
|
270
|
+
let nose = CGColor(srgbRed: 0.925, green: 0.639, blue: 0.675, alpha: 1)
|
|
271
|
+
|
|
272
|
+
let alert = mood == "alert"
|
|
273
|
+
let alarmed = alert && badge == 0
|
|
274
|
+
let asleep = mood == "sleeping"
|
|
275
|
+
|
|
276
|
+
// Idle life: a slow breath, a swaying tail, the occasional blink.
|
|
277
|
+
let breath = 1 + sin(phase * 0.55) * 0.013
|
|
278
|
+
let tailSwing = sin(phase * (working ? 0.9 : 0.5)) * (working ? 12 : 7)
|
|
279
|
+
let cycle = phase.truncatingRemainder(dividingBy: working ? 3.4 : 4.8)
|
|
280
|
+
let blinking = !asleep && cycle < 0.11
|
|
281
|
+
let earTwitch = sin(phase * 2.7) > 0.992 ? 3.5 : 0.0
|
|
282
|
+
|
|
283
|
+
// Contact shadow, so a white cat still sits on a white page.
|
|
284
|
+
ctx.setFillColor(CGColor(srgbRed: 0.25, green: 0.23, blue: 0.21, alpha: 0.13))
|
|
285
|
+
ctx.fillEllipse(in: CGRect(x: 16, y: 1, width: 32, height: 5.5))
|
|
286
|
+
|
|
287
|
+
// Tail: a slim curl rather than a thick worm.
|
|
288
|
+
ctx.saveGState()
|
|
289
|
+
ctx.translateBy(x: 46, y: 12)
|
|
290
|
+
ctx.rotate(by: tailSwing * .pi / 180)
|
|
291
|
+
ctx.translateBy(x: -46, y: -12)
|
|
292
|
+
ctx.setLineCap(.round)
|
|
293
|
+
ctx.setStrokeColor(furShade)
|
|
294
|
+
ctx.setLineWidth(7)
|
|
295
|
+
ctx.move(to: CGPoint(x: 44, y: 10))
|
|
296
|
+
ctx.addCurve(to: CGPoint(x: 56, y: 26), control1: CGPoint(x: 55, y: 10), control2: CGPoint(x: 59, y: 18))
|
|
297
|
+
ctx.strokePath()
|
|
298
|
+
ctx.setStrokeColor(fur)
|
|
299
|
+
ctx.setLineWidth(5)
|
|
300
|
+
ctx.move(to: CGPoint(x: 44, y: 10))
|
|
301
|
+
ctx.addCurve(to: CGPoint(x: 56, y: 26), control1: CGPoint(x: 55, y: 10), control2: CGPoint(x: 59, y: 18))
|
|
302
|
+
ctx.strokePath()
|
|
303
|
+
ctx.restoreGState()
|
|
304
|
+
|
|
305
|
+
// Body: chubby, breathing, with a soft chest.
|
|
306
|
+
ctx.saveGState()
|
|
307
|
+
ctx.translateBy(x: 32, y: 16)
|
|
308
|
+
ctx.scaleBy(x: 1, y: breath)
|
|
309
|
+
ctx.translateBy(x: -32, y: -16)
|
|
310
|
+
ctx.setFillColor(fur)
|
|
311
|
+
fillRounded(ctx, CGRect(x: 17, y: 5, width: 30, height: 25), 13)
|
|
312
|
+
for tuft in [(CGFloat(22), CGFloat(7), CGFloat(4.6)), (30, 5.5, 5), (38, 5.5, 5), (44, 7, 4.6)] {
|
|
313
|
+
ctx.fillEllipse(in: CGRect(x: tuft.0 - tuft.2, y: tuft.1 - tuft.2, width: tuft.2 * 2, height: tuft.2 * 2))
|
|
314
|
+
}
|
|
315
|
+
ctx.setFillColor(CGColor(srgbRed: 1, green: 1, blue: 1, alpha: 0.85))
|
|
316
|
+
ctx.fillEllipse(in: CGRect(x: 26.5, y: 6.5, width: 11, height: 10))
|
|
317
|
+
ctx.restoreGState()
|
|
318
|
+
|
|
319
|
+
// Head: one clean circle, with a few pillowy tufts on top and at the cheeks.
|
|
320
|
+
let headCenter = CGPoint(x: 32, y: 38)
|
|
321
|
+
let headRadius: CGFloat = 17
|
|
322
|
+
ctx.setFillColor(fur)
|
|
323
|
+
ctx.fillEllipse(in: CGRect(x: headCenter.x - headRadius, y: headCenter.y - headRadius,
|
|
324
|
+
width: headRadius * 2, height: headRadius * 2))
|
|
325
|
+
for tuft in [(CGFloat(24.5), CGFloat(51.5), CGFloat(3.8)), (32, 54, 4.2), (39.5, 51.5, 3.8),
|
|
326
|
+
(18.5, 34.5, 3.6), (45.5, 34.5, 3.6), (20, 43, 3.4), (44, 43, 3.4)] {
|
|
327
|
+
ctx.fillEllipse(in: CGRect(x: tuft.0 - tuft.2, y: tuft.1 - tuft.2, width: tuft.2 * 2, height: tuft.2 * 2))
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Ears: small rounded triangles tucked into the outline.
|
|
331
|
+
for side in [CGFloat(-1), 1] {
|
|
332
|
+
ctx.saveGState()
|
|
333
|
+
let baseX = headCenter.x + side * 9
|
|
334
|
+
ctx.translateBy(x: baseX, y: headCenter.y + 11)
|
|
335
|
+
ctx.rotate(by: side * earTwitch * .pi / 180)
|
|
336
|
+
ctx.translateBy(x: -baseX, y: -(headCenter.y + 11))
|
|
337
|
+
ctx.setFillColor(fur)
|
|
338
|
+
ctx.beginPath()
|
|
339
|
+
ctx.move(to: CGPoint(x: baseX - 5, y: headCenter.y + 9))
|
|
340
|
+
ctx.addQuadCurve(to: CGPoint(x: baseX + side * 1.2, y: headCenter.y + 18),
|
|
341
|
+
control: CGPoint(x: baseX + side * 0.4, y: headCenter.y + 14))
|
|
342
|
+
ctx.addQuadCurve(to: CGPoint(x: baseX + 5, y: headCenter.y + 9),
|
|
343
|
+
control: CGPoint(x: baseX + 4, y: headCenter.y + 13))
|
|
344
|
+
ctx.closePath()
|
|
345
|
+
ctx.fillPath()
|
|
346
|
+
ctx.setFillColor(earInner)
|
|
347
|
+
ctx.beginPath()
|
|
348
|
+
ctx.move(to: CGPoint(x: baseX - 2.4, y: headCenter.y + 10))
|
|
349
|
+
ctx.addLine(to: CGPoint(x: baseX + side * 1, y: headCenter.y + 14.6))
|
|
350
|
+
ctx.addLine(to: CGPoint(x: baseX + 2.4, y: headCenter.y + 10))
|
|
351
|
+
ctx.closePath()
|
|
352
|
+
ctx.fillPath()
|
|
353
|
+
ctx.restoreGState()
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// A whisper of shadow where the head meets the body: without it a white cat
|
|
357
|
+
// on a white page is one silhouette.
|
|
358
|
+
ctx.setFillColor(CGColor(srgbRed: 0.25, green: 0.23, blue: 0.21, alpha: 0.07))
|
|
359
|
+
ctx.fillEllipse(in: CGRect(x: 21, y: 21.5, width: 22, height: 5.5))
|
|
360
|
+
|
|
361
|
+
// Eyes: big and dark with one sparkle — the friendly kind of big, not a ring
|
|
362
|
+
// around a pinprick pupil.
|
|
363
|
+
let eyeY: CGFloat = 37.5
|
|
364
|
+
let eyeDX: CGFloat = 6.4
|
|
365
|
+
if asleep || blinking {
|
|
366
|
+
ctx.setStrokeColor(ink)
|
|
367
|
+
ctx.setLineWidth(2)
|
|
368
|
+
ctx.setLineCap(.round)
|
|
369
|
+
for side in [CGFloat(-1), 1] {
|
|
370
|
+
let x = headCenter.x + side * eyeDX
|
|
371
|
+
ctx.move(to: CGPoint(x: x - 3.2, y: eyeY + 0.8))
|
|
372
|
+
ctx.addQuadCurve(to: CGPoint(x: x + 3.2, y: eyeY + 0.8), control: CGPoint(x: x, y: eyeY - 2.2))
|
|
373
|
+
ctx.strokePath()
|
|
374
|
+
}
|
|
375
|
+
} else {
|
|
376
|
+
for side in [CGFloat(-1), 1] {
|
|
377
|
+
let x = headCenter.x + side * eyeDX
|
|
378
|
+
let rx: CGFloat = alert ? 4.3 : 4.0
|
|
379
|
+
let ry: CGFloat = alert ? 4.6 : 4.3
|
|
380
|
+
ctx.setFillColor(ink)
|
|
381
|
+
ctx.fillEllipse(in: CGRect(x: x - rx, y: eyeY - ry, width: rx * 2, height: ry * 2))
|
|
382
|
+
ctx.setFillColor(CGColor(srgbRed: 1, green: 1, blue: 1, alpha: 1))
|
|
383
|
+
ctx.fillEllipse(in: CGRect(x: x - 2.1, y: eyeY + 0.9, width: 2.1, height: 2.1))
|
|
384
|
+
ctx.fillEllipse(in: CGRect(x: x + 1, y: eyeY - 1.9, width: 1.1, height: 1.1))
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Blush.
|
|
389
|
+
ctx.setFillColor(blush)
|
|
390
|
+
ctx.fillEllipse(in: CGRect(x: 17.5, y: 30.5, width: 6.5, height: 4))
|
|
391
|
+
ctx.fillEllipse(in: CGRect(x: 40, y: 30.5, width: 6.5, height: 4))
|
|
392
|
+
|
|
393
|
+
// Nose and the small "w" mouth.
|
|
394
|
+
ctx.setFillColor(nose)
|
|
395
|
+
ctx.beginPath()
|
|
396
|
+
ctx.move(to: CGPoint(x: 30.6, y: 31.8))
|
|
397
|
+
ctx.addQuadCurve(to: CGPoint(x: 33.4, y: 31.8), control: CGPoint(x: 32, y: 30.6))
|
|
398
|
+
ctx.closePath()
|
|
399
|
+
ctx.fillPath()
|
|
400
|
+
if !(alert && alarmed) {
|
|
401
|
+
ctx.setStrokeColor(ink)
|
|
402
|
+
ctx.setLineWidth(1.25)
|
|
403
|
+
ctx.setLineCap(.round)
|
|
404
|
+
ctx.move(to: CGPoint(x: 32, y: 30.2))
|
|
405
|
+
ctx.addQuadCurve(to: CGPoint(x: 29.4, y: 30.0), control: CGPoint(x: 30.6, y: 28.1))
|
|
406
|
+
ctx.move(to: CGPoint(x: 32, y: 30.2))
|
|
407
|
+
ctx.addQuadCurve(to: CGPoint(x: 34.6, y: 30.0), control: CGPoint(x: 33.4, y: 28.1))
|
|
408
|
+
ctx.strokePath()
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Whiskers: two a side, faint.
|
|
412
|
+
ctx.setStrokeColor(CGColor(srgbRed: 0.45, green: 0.42, blue: 0.39, alpha: 0.28))
|
|
413
|
+
ctx.setLineWidth(0.9)
|
|
414
|
+
for side in [CGFloat(-1), 1] {
|
|
415
|
+
for drop in [CGFloat(0), 2.4] {
|
|
416
|
+
let x0 = headCenter.x + side * 7.5
|
|
417
|
+
ctx.move(to: CGPoint(x: x0, y: 31.5 - drop * 0.5))
|
|
418
|
+
ctx.addLine(to: CGPoint(x: x0 + side * 7, y: 32.4 - drop))
|
|
419
|
+
ctx.strokePath()
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Mood marks, kept clear of the badge corner (top right).
|
|
424
|
+
if alarmed {
|
|
425
|
+
ctx.setFillColor(earInner)
|
|
426
|
+
ctx.fillEllipse(in: CGRect(x: 43, y: 44, width: 14, height: 14))
|
|
427
|
+
ctx.setFillColor(CGColor(srgbRed: 1, green: 1, blue: 1, alpha: 1))
|
|
428
|
+
fillRounded(ctx, CGRect(x: 49.1, y: 50.2, width: 1.8, height: 5.2), 0.9)
|
|
429
|
+
fillRounded(ctx, CGRect(x: 49.1, y: 47.2, width: 1.8, height: 1.8), 0.9)
|
|
430
|
+
} else if working && badge == 0 {
|
|
431
|
+
for (index, spot) in [(CGFloat(50), CGFloat(52), CGFloat(2.2)),
|
|
432
|
+
(56, 48, 1.6),
|
|
433
|
+
(60, 42, 1.1)].enumerated() {
|
|
434
|
+
ctx.setFillColor(CGColor(srgbRed: 0.298, green: 0.565, blue: 0.949,
|
|
435
|
+
alpha: 0.85 - CGFloat(index) * 0.25))
|
|
436
|
+
ctx.fillEllipse(in: CGRect(x: spot.0 - spot.2, y: spot.1 - spot.2,
|
|
437
|
+
width: spot.2 * 2, height: spot.2 * 2))
|
|
438
|
+
}
|
|
439
|
+
} else if asleep {
|
|
440
|
+
let z = NSAttributedString(string: "z", attributes: [
|
|
441
|
+
.font: NSFont.systemFont(ofSize: 9, weight: .semibold),
|
|
442
|
+
.foregroundColor: NSColor(srgbRed: 0.62, green: 0.60, blue: 0.58, alpha: 1),
|
|
443
|
+
])
|
|
444
|
+
z.draw(at: CGPoint(x: 50, y: 46))
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private func drawBadge(_ ctx: CGContext, text: String) {
|
|
449
|
+
let width: CGFloat = text.count > 1 ? 24 : 20
|
|
450
|
+
let rect = CGRect(x: bounds.width - width - 2, y: bounds.height - 22, width: width, height: 20)
|
|
451
|
+
ctx.setFillColor(attention > 0
|
|
452
|
+
? CGColor(srgbRed: 0.961, green: 0.729, blue: 0.196, alpha: 1)
|
|
453
|
+
: CGColor(srgbRed: 0.129, green: 0.231, blue: 0.2, alpha: 0.92))
|
|
454
|
+
fillRounded(ctx, rect, 10)
|
|
455
|
+
let string = NSAttributedString(string: text, attributes: [
|
|
456
|
+
.font: NSFont.systemFont(ofSize: 11, weight: .semibold),
|
|
457
|
+
.foregroundColor: attention > 0
|
|
458
|
+
? NSColor(srgbRed: 0.122, green: 0.102, blue: 0.031, alpha: 1)
|
|
459
|
+
: NSColor.white,
|
|
460
|
+
])
|
|
461
|
+
let size = string.size()
|
|
462
|
+
string.draw(at: CGPoint(x: rect.midX - size.width / 2, y: rect.midY - size.height / 2))
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// MARK: - Roster row
|
|
467
|
+
|
|
468
|
+
/// One roster row, drawn rather than titled: a coloured status dot, the title,
|
|
469
|
+
/// the page-composed status line (elapsed · 2/5 · tool), and a slim progress bar.
|
|
470
|
+
///
|
|
471
|
+
/// `NSMenu` only understands single-line titles, and "running 2 of 5" cannot be
|
|
472
|
+
/// told from "done" by a title plus one dot — which is exactly what the desktop
|
|
473
|
+
/// pet looked like before. A view-based item keeps the menu's dismissal, keyboard
|
|
474
|
+
/// handling and outside-click closing while allowing real content.
|
|
475
|
+
final class RosterRowView: NSView {
|
|
476
|
+
static let width: CGFloat = 320
|
|
477
|
+
static let rowHeight: CGFloat = 42
|
|
478
|
+
static let progressRowHeight: CGFloat = 52
|
|
479
|
+
|
|
480
|
+
private let entry: SessionEntry
|
|
481
|
+
/// Fired on click. A menu item's action was not enough: a view-based item
|
|
482
|
+
/// swallows the mouse, so rows opened nothing at all.
|
|
483
|
+
var onPick: (() -> Void)?
|
|
484
|
+
private var pressed = false
|
|
485
|
+
private var hovered = false {
|
|
486
|
+
didSet { if hovered != oldValue { needsDisplay = true } }
|
|
487
|
+
}
|
|
488
|
+
private var tracking: NSTrackingArea?
|
|
489
|
+
|
|
490
|
+
init(entry: SessionEntry, width: CGFloat = RosterRowView.width) {
|
|
491
|
+
self.entry = entry
|
|
492
|
+
let height = entry.ratio == nil ? Self.rowHeight : Self.progressRowHeight
|
|
493
|
+
super.init(frame: NSRect(x: 0, y: 0, width: width, height: height))
|
|
494
|
+
autoresizingMask = [.width]
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
@available(*, unavailable)
|
|
498
|
+
required init?(coder: NSCoder) { fatalError("not used") }
|
|
499
|
+
|
|
500
|
+
/// Colour for one roster kind; the same palette the in-window dot uses.
|
|
501
|
+
/// Running must not look like finished: they were both mint, and at 7pt the
|
|
502
|
+
/// solid/hollow difference read as nothing at all. Running is the brand blue,
|
|
503
|
+
/// finished is the success green, stalled progress keeps the mint.
|
|
504
|
+
private func kindColor() -> NSColor {
|
|
505
|
+
switch entry.kind {
|
|
506
|
+
case "running": return NSColor(srgbRed: 0.298, green: 0.565, blue: 0.949, alpha: 1)
|
|
507
|
+
case "attention": return NSColor(srgbRed: 0.961, green: 0.729, blue: 0.196, alpha: 1)
|
|
508
|
+
case "failed": return NSColor(srgbRed: 0.859, green: 0.278, blue: 0.278, alpha: 1)
|
|
509
|
+
case "finished": return NSColor(srgbRed: 0.302, green: 0.686, blue: 0.420, alpha: 1)
|
|
510
|
+
case "progress": return NSColor(srgbRed: 0.494, green: 0.769, blue: 0.655, alpha: 1)
|
|
511
|
+
default: return NSColor(srgbRed: 0.62, green: 0.66, blue: 0.68, alpha: 1)
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/// A running row also gets a halo that breathes, so "active" is visible
|
|
516
|
+
/// without relying on colour alone.
|
|
517
|
+
private func drawRunningHalo(in rect: CGRect, color: NSColor) {
|
|
518
|
+
let pulse = 0.5 + 0.5 * sin(CACurrentMediaTime() * 2.4)
|
|
519
|
+
color.withAlphaComponent(0.16 + 0.12 * pulse).setFill()
|
|
520
|
+
NSBezierPath(ovalIn: rect.insetBy(dx: -3.5, dy: -3.5)).fill()
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/// Filled for work that is moving, hollow for work that is not.
|
|
524
|
+
private func dotIsHollow() -> Bool {
|
|
525
|
+
entry.kind == "progress" || entry.kind == "idle"
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
override func updateTrackingAreas() {
|
|
529
|
+
super.updateTrackingAreas()
|
|
530
|
+
if let tracking { removeTrackingArea(tracking) }
|
|
531
|
+
let area = NSTrackingArea(rect: bounds, options: [.mouseEnteredAndExited, .activeAlways],
|
|
532
|
+
owner: self, userInfo: nil)
|
|
533
|
+
addTrackingArea(area)
|
|
534
|
+
tracking = area
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/// The card floats over another app, so the very first click on a row must be
|
|
538
|
+
/// delivered instead of being spent on making the window key.
|
|
539
|
+
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
|
540
|
+
|
|
541
|
+
override func mouseEntered(with event: NSEvent) { hovered = true }
|
|
542
|
+
override func mouseExited(with event: NSEvent) { hovered = false }
|
|
543
|
+
|
|
544
|
+
override func resetCursorRects() {
|
|
545
|
+
addCursorRect(bounds, cursor: .pointingHand)
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/// AppKit only delivers `mouseUp` to a view that claimed the press, so this
|
|
549
|
+
/// must exist even though the press itself has nothing to do: without it the
|
|
550
|
+
/// row never learns it was clicked at all — which is exactly how the list
|
|
551
|
+
/// looked when clicks "did nothing".
|
|
552
|
+
override func mouseDown(with event: NSEvent) {
|
|
553
|
+
pressed = true
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
override func mouseUp(with event: NSEvent) {
|
|
557
|
+
pressed = false
|
|
558
|
+
// Deliberately not gated on `mouseDown`: on a window that never becomes key
|
|
559
|
+
// AppKit delivers the release without a matching press, and gating on it is
|
|
560
|
+
// what made the list inert. The bounds check keeps a release that started
|
|
561
|
+
// elsewhere from picking a row.
|
|
562
|
+
guard bounds.contains(convert(event.locationInWindow, from: nil)) else { return }
|
|
563
|
+
onPick?()
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
override func draw(_ dirtyRect: NSRect) {
|
|
567
|
+
let highlighted = hovered
|
|
568
|
+
let titleColor = NSColor.labelColor
|
|
569
|
+
let metaColor = NSColor.secondaryLabelColor
|
|
570
|
+
let card = NSRect(x: 4, y: 1, width: bounds.width - 8, height: bounds.height - 2)
|
|
571
|
+
if entry.active == true {
|
|
572
|
+
// The row the user is looking at, as in the panel.
|
|
573
|
+
NSColor.labelColor.withAlphaComponent(0.08).setFill()
|
|
574
|
+
NSBezierPath(roundedRect: card, xRadius: 10, yRadius: 10).fill()
|
|
575
|
+
}
|
|
576
|
+
if highlighted {
|
|
577
|
+
NSColor.labelColor.withAlphaComponent(0.06).setFill()
|
|
578
|
+
NSBezierPath(roundedRect: card, xRadius: 10, yRadius: 10).fill()
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
let dotSize: CGFloat = 7
|
|
582
|
+
let dotX: CGFloat = 10
|
|
583
|
+
let dotY = bounds.height - 6 - 12
|
|
584
|
+
let color = kindColor()
|
|
585
|
+
let dotRect = NSRect(x: dotX, y: dotY, width: dotSize, height: dotSize)
|
|
586
|
+
if entry.kind == "running" {
|
|
587
|
+
drawRunningHalo(in: dotRect, color: color)
|
|
588
|
+
}
|
|
589
|
+
let dot = NSBezierPath(ovalIn: dotRect)
|
|
590
|
+
if dotIsHollow() {
|
|
591
|
+
color.setStroke()
|
|
592
|
+
dot.lineWidth = 1.6
|
|
593
|
+
dot.stroke()
|
|
594
|
+
} else {
|
|
595
|
+
color.setFill()
|
|
596
|
+
dot.fill()
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
let textX: CGFloat = 27
|
|
600
|
+
let textWidth = bounds.width - textX - 12
|
|
601
|
+
// Laid out from the top edge so the title, the status line and the bar keep
|
|
602
|
+
// their spacing at both row heights; fixed offsets collided once the bar
|
|
603
|
+
// became optional.
|
|
604
|
+
let titleY = bounds.height - 6 - 17
|
|
605
|
+
let metaY = titleY - 1 - 14
|
|
606
|
+
let title = entry.title as NSString
|
|
607
|
+
let titleAttrs: [NSAttributedString.Key: Any] = [
|
|
608
|
+
.font: NSFont.systemFont(ofSize: 12.5, weight: .regular),
|
|
609
|
+
.foregroundColor: titleColor,
|
|
610
|
+
]
|
|
611
|
+
let paragraph = NSMutableParagraphStyle()
|
|
612
|
+
paragraph.lineBreakMode = .byTruncatingTail
|
|
613
|
+
title.draw(in: NSRect(x: textX, y: titleY, width: textWidth, height: 17),
|
|
614
|
+
withAttributes: titleAttrs.merging([.paragraphStyle: paragraph]) { $1 })
|
|
615
|
+
|
|
616
|
+
// The panel marks the open Session with a trailing "· current"; the card
|
|
617
|
+
// says the same thing in the same place.
|
|
618
|
+
var metaText = entry.meta ?? ""
|
|
619
|
+
if entry.current == true, let label = entry.currentLabel, !label.isEmpty {
|
|
620
|
+
metaText = metaText.isEmpty ? label : "\(metaText) · \(label)"
|
|
621
|
+
}
|
|
622
|
+
let meta = metaText as NSString
|
|
623
|
+
if meta.length > 0 {
|
|
624
|
+
let metaAttrs: [NSAttributedString.Key: Any] = [
|
|
625
|
+
.font: NSFont.systemFont(ofSize: 11),
|
|
626
|
+
.foregroundColor: metaColor,
|
|
627
|
+
.paragraphStyle: paragraph,
|
|
628
|
+
]
|
|
629
|
+
meta.draw(in: NSRect(x: textX, y: metaY, width: textWidth, height: 14),
|
|
630
|
+
withAttributes: metaAttrs)
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
if let ratio = entry.ratio {
|
|
634
|
+
let track = NSRect(x: textX, y: 5, width: textWidth, height: 3)
|
|
635
|
+
let trackPath = NSBezierPath(roundedRect: track, xRadius: 1.5, yRadius: 1.5)
|
|
636
|
+
(highlighted ? NSColor.selectedMenuItemTextColor.withAlphaComponent(0.28)
|
|
637
|
+
: NSColor.quaternaryLabelColor).setFill()
|
|
638
|
+
trackPath.fill()
|
|
639
|
+
let filled = NSRect(x: track.minX, y: track.minY, width: max(2, track.width * ratio), height: 3)
|
|
640
|
+
color.setFill()
|
|
641
|
+
NSBezierPath(roundedRect: filled, xRadius: 1.5, yRadius: 1.5).fill()
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// MARK: - Panel
|
|
647
|
+
|
|
648
|
+
/// A non-activating panel: clicks never pull focus away from the app behind it.
|
|
649
|
+
/// The roster window: every Session, above the pet, scrollable.
|
|
650
|
+
///
|
|
651
|
+
/// This is a panel rather than an `NSMenu` for three reasons the menu could not
|
|
652
|
+
/// satisfy: it can be placed exactly above the pet, it can hold more rows than a
|
|
653
|
+
/// menu will fit on screen (so nothing is hidden behind "more…"), and its rows
|
|
654
|
+
/// receive ordinary clicks instead of a view-based item swallowing them.
|
|
655
|
+
/// The row that stands in for everything past the fifth: the panel's own
|
|
656
|
+
/// "and N more" affordance, so a long history never turns the card into a wall.
|
|
657
|
+
final class RosterFooterView: NSView {
|
|
658
|
+
var onTap: (() -> Void)?
|
|
659
|
+
private var label: String = ""
|
|
660
|
+
private var hovered = false { didSet { if hovered != oldValue { needsDisplay = true } } }
|
|
661
|
+
private var tracking: NSTrackingArea?
|
|
662
|
+
|
|
663
|
+
static let height: CGFloat = 28
|
|
664
|
+
|
|
665
|
+
init(width: CGFloat) {
|
|
666
|
+
super.init(frame: NSRect(x: 0, y: 0, width: width, height: Self.height))
|
|
667
|
+
autoresizingMask = [.width]
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
@available(*, unavailable)
|
|
671
|
+
required init?(coder: NSCoder) { fatalError("not used") }
|
|
672
|
+
|
|
673
|
+
func setLabel(_ text: String) {
|
|
674
|
+
label = text
|
|
675
|
+
needsDisplay = true
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
override func updateTrackingAreas() {
|
|
679
|
+
super.updateTrackingAreas()
|
|
680
|
+
if let tracking { removeTrackingArea(tracking) }
|
|
681
|
+
let area = NSTrackingArea(rect: bounds, options: [.mouseEnteredAndExited, .activeAlways],
|
|
682
|
+
owner: self, userInfo: nil)
|
|
683
|
+
addTrackingArea(area)
|
|
684
|
+
tracking = area
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
override func mouseEntered(with event: NSEvent) { hovered = true }
|
|
688
|
+
override func mouseExited(with event: NSEvent) { hovered = false }
|
|
689
|
+
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
|
690
|
+
override func resetCursorRects() { addCursorRect(bounds, cursor: .pointingHand) }
|
|
691
|
+
|
|
692
|
+
override func mouseDown(with event: NSEvent) {}
|
|
693
|
+
override func mouseUp(with event: NSEvent) {
|
|
694
|
+
guard bounds.contains(convert(event.locationInWindow, from: nil)) else { return }
|
|
695
|
+
onTap?()
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
override func draw(_ dirtyRect: NSRect) {
|
|
699
|
+
if hovered {
|
|
700
|
+
NSColor.labelColor.withAlphaComponent(0.06).setFill()
|
|
701
|
+
NSBezierPath(roundedRect: bounds.insetBy(dx: 4, dy: 2), xRadius: 8, yRadius: 8).fill()
|
|
702
|
+
}
|
|
703
|
+
let attrs: [NSAttributedString.Key: Any] = [
|
|
704
|
+
.font: NSFont.systemFont(ofSize: 11),
|
|
705
|
+
.foregroundColor: NSColor.secondaryLabelColor,
|
|
706
|
+
]
|
|
707
|
+
let size = (label as NSString).size(withAttributes: attrs)
|
|
708
|
+
let point = NSPoint(x: (bounds.width - size.width) / 2, y: (bounds.height - size.height) / 2)
|
|
709
|
+
(label as NSString).draw(at: point, withAttributes: attrs)
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
final class RosterPanel: NSPanel {
|
|
714
|
+
/// Like the pet itself, the card never becomes key: the pet receives clicks
|
|
715
|
+
/// reliably precisely because it never takes focus, and the card must behave
|
|
716
|
+
/// the same way. Dismissal is by pointer-leave, by picking a row, or by
|
|
717
|
+
/// tapping the pet again.
|
|
718
|
+
override var canBecomeKey: Bool { false }
|
|
719
|
+
override var canBecomeMain: Bool { false }
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/// Owns the roster panel: layout, scrolling, hover, click, dismissal.
|
|
723
|
+
final class RosterController: NSObject {
|
|
724
|
+
private let panel: RosterPanel
|
|
725
|
+
private let card = NSVisualEffectView()
|
|
726
|
+
private let scroll = NSScrollView()
|
|
727
|
+
private let document = FlippedView()
|
|
728
|
+
private let head = NSView()
|
|
729
|
+
private let headTitle = NSTextField(labelWithString: "")
|
|
730
|
+
private let headSummary = NSTextField(labelWithString: "")
|
|
731
|
+
private let headHint = NSTextField(labelWithString: "")
|
|
732
|
+
private let switchButton = NSButton()
|
|
733
|
+
private var onSwitch: (() -> Void)?
|
|
734
|
+
private var rows: [RosterRowView] = []
|
|
735
|
+
private var footer: RosterFooterView?
|
|
736
|
+
/// Everything the Host sent; the card shows the first few until expanded.
|
|
737
|
+
private var allEntries: [SessionEntry] = []
|
|
738
|
+
private var expanded = false
|
|
739
|
+
private var onPick: ((String) -> Void)?
|
|
740
|
+
private var header = (light: Optional<Bool>.none, title: "", summary: "", hint: "", more: { (_: Int) in "" }, less: "")
|
|
741
|
+
private var anchor = NSRect.zero
|
|
742
|
+
private var hoverTimer: Timer?
|
|
743
|
+
private var awaySince: Date?
|
|
744
|
+
|
|
745
|
+
private static let width: CGFloat = 340
|
|
746
|
+
/// How many conversations the card shows before it offers the rest.
|
|
747
|
+
static let collapsedRows = 5
|
|
748
|
+
private static let padding: CGFloat = 6
|
|
749
|
+
private static let gap: CGFloat = 8
|
|
750
|
+
|
|
751
|
+
override init() {
|
|
752
|
+
panel = RosterPanel(contentRect: NSRect(x: 0, y: 0, width: Self.width, height: 100),
|
|
753
|
+
styleMask: [.borderless, .nonactivatingPanel],
|
|
754
|
+
backing: .buffered,
|
|
755
|
+
defer: false)
|
|
756
|
+
panel.isFloatingPanel = true
|
|
757
|
+
panel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.statusWindow)) + 2)
|
|
758
|
+
panel.isOpaque = false
|
|
759
|
+
panel.backgroundColor = .clear
|
|
760
|
+
panel.hasShadow = true
|
|
761
|
+
panel.hidesOnDeactivate = false
|
|
762
|
+
panel.animationBehavior = .none
|
|
763
|
+
panel.isReleasedWhenClosed = false
|
|
764
|
+
panel.collectionBehavior = [.canJoinAllSpaces, .stationary, .fullScreenAuxiliary]
|
|
765
|
+
|
|
766
|
+
card.material = .popover
|
|
767
|
+
card.blendingMode = .behindWindow
|
|
768
|
+
card.state = .active
|
|
769
|
+
card.wantsLayer = true
|
|
770
|
+
card.layer?.cornerRadius = 16
|
|
771
|
+
card.layer?.masksToBounds = true
|
|
772
|
+
panel.contentView = card
|
|
773
|
+
|
|
774
|
+
switchButton.bezelStyle = .inline
|
|
775
|
+
switchButton.isBordered = false
|
|
776
|
+
switchButton.imagePosition = .imageOnly
|
|
777
|
+
switchButton.contentTintColor = .secondaryLabelColor
|
|
778
|
+
switchButton.action = #selector(RosterController.switchToWindow)
|
|
779
|
+
switchButton.toolTip = ""
|
|
780
|
+
head.addSubview(switchButton)
|
|
781
|
+
head.addSubview(headTitle)
|
|
782
|
+
head.addSubview(headSummary)
|
|
783
|
+
head.addSubview(headHint)
|
|
784
|
+
card.addSubview(head)
|
|
785
|
+
|
|
786
|
+
headTitle.font = NSFont.systemFont(ofSize: 13, weight: .semibold)
|
|
787
|
+
headTitle.textColor = .labelColor
|
|
788
|
+
headSummary.font = NSFont.systemFont(ofSize: 11)
|
|
789
|
+
headSummary.textColor = .secondaryLabelColor
|
|
790
|
+
headSummary.lineBreakMode = .byTruncatingTail
|
|
791
|
+
headHint.font = NSFont.systemFont(ofSize: 11)
|
|
792
|
+
headHint.textColor = .secondaryLabelColor
|
|
793
|
+
headHint.lineBreakMode = .byTruncatingTail
|
|
794
|
+
|
|
795
|
+
scroll.drawsBackground = false
|
|
796
|
+
scroll.hasVerticalScroller = true
|
|
797
|
+
scroll.autohidesScrollers = true
|
|
798
|
+
scroll.scrollerStyle = .overlay
|
|
799
|
+
scroll.documentView = document
|
|
800
|
+
card.addSubview(scroll)
|
|
801
|
+
super.init()
|
|
802
|
+
switchButton.target = self
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
var isVisible: Bool { panel.isVisible }
|
|
806
|
+
|
|
807
|
+
/// Show the roster above the pet, or below it when there is no room on top.
|
|
808
|
+
/// @param entries - roster rows, most urgent first.
|
|
809
|
+
/// @param petFrame - the pet's frame in screen coordinates.
|
|
810
|
+
/// @param light - the app's surface polarity, so the card matches it.
|
|
811
|
+
/// @param title - card heading.
|
|
812
|
+
/// @param summary - one-line state under the heading.
|
|
813
|
+
/// @param hint - one-line explanation under the summary.
|
|
814
|
+
/// @param onPick - receives the Session id of the clicked row.
|
|
815
|
+
func show(entries: [SessionEntry], above petFrame: NSRect, light: Bool?,
|
|
816
|
+
title: String, summary: String, hint: String, toWindow: String,
|
|
817
|
+
more: @escaping (Int) -> String, less: String,
|
|
818
|
+
onPick: @escaping (String) -> Void, onSwitch: @escaping () -> Void) {
|
|
819
|
+
allEntries = entries
|
|
820
|
+
expanded = false
|
|
821
|
+
self.onPick = onPick
|
|
822
|
+
anchor = petFrame
|
|
823
|
+
header = (light, title, summary, hint, more, less)
|
|
824
|
+
self.onSwitch = onSwitch
|
|
825
|
+
switchButton.toolTip = toWindow
|
|
826
|
+
switchButton.image = NSImage(systemSymbolName: "macwindow", accessibilityDescription: toWindow)
|
|
827
|
+
render()
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/// Hand the pet back to the app window.
|
|
831
|
+
@objc private func switchToWindow() {
|
|
832
|
+
onSwitch?()
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
/// Rebuild the card from `allEntries` and the current expansion.
|
|
836
|
+
private func render() {
|
|
837
|
+
let visible = expanded ? allEntries : Array(allEntries.prefix(Self.collapsedRows))
|
|
838
|
+
let hidden = allEntries.count - visible.count
|
|
839
|
+
let (light, title, summary, hint, more, less) = header
|
|
840
|
+
let onPick = self.onPick ?? { _ in }
|
|
841
|
+
|
|
842
|
+
rows.forEach { $0.removeFromSuperview() }
|
|
843
|
+
footer?.removeFromSuperview()
|
|
844
|
+
let petFrame = anchor
|
|
845
|
+
rows = visible.map { entry in
|
|
846
|
+
let row = RosterRowView(entry: entry, width: Self.width - Self.padding * 2 - 2)
|
|
847
|
+
row.onPick = { onPick(entry.id) }
|
|
848
|
+
return row
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
// The page's panel leads with a heading, a summary and one hint; the card
|
|
852
|
+
// carries the same three so both surfaces read as one product.
|
|
853
|
+
if let light, let appearance = NSAppearance(named: light ? .aqua : .darkAqua) {
|
|
854
|
+
card.appearance = appearance
|
|
855
|
+
} else {
|
|
856
|
+
card.appearance = nil
|
|
857
|
+
}
|
|
858
|
+
headTitle.stringValue = title
|
|
859
|
+
headSummary.stringValue = summary
|
|
860
|
+
headHint.stringValue = hint
|
|
861
|
+
headHint.isHidden = hint.isEmpty
|
|
862
|
+
let headHeight: CGFloat = hint.isEmpty ? 40 : 58
|
|
863
|
+
let headerWidth = Self.width - Self.padding * 2 - 4
|
|
864
|
+
head.frame = NSRect(x: Self.padding + 2, y: 0, width: headerWidth, height: headHeight)
|
|
865
|
+
headTitle.frame = NSRect(x: 4, y: headHeight - 22, width: headerWidth - 34, height: 17)
|
|
866
|
+
switchButton.frame = NSRect(x: headerWidth - 30, y: headHeight - 26, width: 26, height: 22)
|
|
867
|
+
headSummary.frame = NSRect(x: 4, y: headHeight - 39, width: headerWidth - 8, height: 15)
|
|
868
|
+
headHint.frame = NSRect(x: 4, y: headHeight - 56, width: headerWidth - 8, height: 15)
|
|
869
|
+
|
|
870
|
+
if hidden > 0 || expanded {
|
|
871
|
+
let foot = RosterFooterView(width: Self.width - Self.padding * 2 - 2)
|
|
872
|
+
foot.setLabel(expanded ? less : more(hidden))
|
|
873
|
+
foot.onTap = { [weak self] in
|
|
874
|
+
guard let self else { return }
|
|
875
|
+
self.expanded.toggle()
|
|
876
|
+
self.render()
|
|
877
|
+
}
|
|
878
|
+
footer = foot
|
|
879
|
+
} else {
|
|
880
|
+
footer = nil
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
let listHeight = rows.reduce(CGFloat(0)) { $0 + $1.frame.height } + (footer?.frame.height ?? 0) + 4
|
|
884
|
+
let screen = (NSScreen.screens.first { $0.frame.intersects(petFrame) } ?? NSScreen.main)?.visibleFrame
|
|
885
|
+
?? NSRect(x: 0, y: 0, width: 1440, height: 900)
|
|
886
|
+
// Never taller than the space a laptop user can see at once: scroll instead.
|
|
887
|
+
let maxHeight = min(listHeight, screen.height * 0.55)
|
|
888
|
+
let height = max(Self.padding * 2 + headHeight + 24, maxHeight + headHeight + Self.padding)
|
|
889
|
+
|
|
890
|
+
var y = petFrame.maxY + Self.gap
|
|
891
|
+
if y + height > screen.maxY {
|
|
892
|
+
// Not enough room above: drop below the pet and keep it on screen.
|
|
893
|
+
y = max(screen.minY + 4, petFrame.minY - Self.gap - height)
|
|
894
|
+
}
|
|
895
|
+
let x = min(max(screen.minX + 4, petFrame.midX - Self.width / 2), screen.maxX - Self.width - 4)
|
|
896
|
+
panel.setFrame(NSRect(x: x, y: y, width: Self.width, height: height), display: true)
|
|
897
|
+
|
|
898
|
+
// Laid out after the frame is final: an autoresizing mask would scale these
|
|
899
|
+
// against the panel's previous size and push the list past the card.
|
|
900
|
+
head.frame = NSRect(x: Self.padding + 2, y: height - headHeight - Self.padding / 2,
|
|
901
|
+
width: headerWidth, height: headHeight)
|
|
902
|
+
scroll.frame = NSRect(x: Self.padding, y: Self.padding,
|
|
903
|
+
width: Self.width - Self.padding * 2,
|
|
904
|
+
height: max(40, height - headHeight - Self.padding * 1.5))
|
|
905
|
+
|
|
906
|
+
document.frame = NSRect(x: 0, y: 0, width: Self.width - Self.padding * 2 - 2, height: listHeight)
|
|
907
|
+
var cursor = listHeight - Self.padding
|
|
908
|
+
for row in rows {
|
|
909
|
+
cursor -= row.frame.height
|
|
910
|
+
row.frame.origin = NSPoint(x: 0, y: cursor)
|
|
911
|
+
document.addSubview(row)
|
|
912
|
+
}
|
|
913
|
+
if let footer {
|
|
914
|
+
cursor -= footer.frame.height
|
|
915
|
+
footer.frame.origin = NSPoint(x: 0, y: cursor)
|
|
916
|
+
document.addSubview(footer)
|
|
917
|
+
}
|
|
918
|
+
scroll.documentView?.scroll(NSPoint(x: 0, y: 0))
|
|
919
|
+
|
|
920
|
+
panel.orderFrontRegardless()
|
|
921
|
+
startHoverWatch(petFrame: petFrame)
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/// Ask for key status without activating the app.
|
|
925
|
+
///
|
|
926
|
+
/// `NSApp.activate` is not an option here: this binary has no bundle, and
|
|
927
|
+
/// activating a bundle-less process from a background app is unreliable
|
|
928
|
+
/// enough to wedge the very window it is meant to raise.
|
|
929
|
+
private func makeKeyIfPossible() {
|
|
930
|
+
panel.becomesKeyOnlyIfNeeded = true
|
|
931
|
+
panel.makeKey()
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
func hide() {
|
|
935
|
+
// Reopening always starts collapsed, like the panel does.
|
|
936
|
+
expanded = false
|
|
937
|
+
hoverTimer?.invalidate()
|
|
938
|
+
hoverTimer = nil
|
|
939
|
+
awaySince = nil
|
|
940
|
+
guard panel.isVisible else { return }
|
|
941
|
+
panel.orderOut(nil)
|
|
942
|
+
rows.forEach { $0.removeFromSuperview() }
|
|
943
|
+
rows = []
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
/// Close when the pointer leaves both the card and the pet for a moment, the
|
|
947
|
+
/// way a hover palette behaves. The grace period covers the diagonal trip
|
|
948
|
+
/// between the two.
|
|
949
|
+
private func startHoverWatch(petFrame: NSRect) {
|
|
950
|
+
hoverTimer?.invalidate()
|
|
951
|
+
hoverTimer = Timer(timeInterval: 0.2, repeats: true) { [weak self] _ in
|
|
952
|
+
guard let self, self.panel.isVisible else { return }
|
|
953
|
+
let point = NSEvent.mouseLocation
|
|
954
|
+
if self.panel.frame.contains(point) || petFrame.contains(point) {
|
|
955
|
+
self.awaySince = nil
|
|
956
|
+
return
|
|
957
|
+
}
|
|
958
|
+
let now = Date()
|
|
959
|
+
if let since = self.awaySince {
|
|
960
|
+
if now.timeIntervalSince(since) > 0.8 { self.hide() }
|
|
961
|
+
} else {
|
|
962
|
+
self.awaySince = now
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
if let hoverTimer { RunLoop.main.add(hoverTimer, forMode: .common) }
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
func handleKey(_ event: NSEvent) -> Bool {
|
|
969
|
+
guard event.keyCode == 53 else { return false }
|
|
970
|
+
hide()
|
|
971
|
+
return true
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
/// A top-left origin container, so rows stack downwards the way they read.
|
|
976
|
+
final class FlippedView: NSView {
|
|
977
|
+
override var isFlipped: Bool { true }
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
/// The menu bar item: the piece of the in-window panel that has nowhere else to
|
|
981
|
+
/// live on the desktop — the heading, the summary line, and a way to reach the
|
|
982
|
+
/// pet's commands without hunting for the floating card.
|
|
983
|
+
final class StatusItemController {
|
|
984
|
+
private let item: NSStatusItem
|
|
985
|
+
private let menu = NSMenu()
|
|
986
|
+
private let summaryLine = NSMenuItem(title: "", action: nil, keyEquivalent: "")
|
|
987
|
+
private let hintLine = NSMenuItem(title: "", action: nil, keyEquivalent: "")
|
|
988
|
+
private var actions: [NSMenuItem] = []
|
|
989
|
+
|
|
990
|
+
init() {
|
|
991
|
+
item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
|
992
|
+
item.isVisible = false
|
|
993
|
+
summaryLine.isEnabled = false
|
|
994
|
+
hintLine.isEnabled = false
|
|
995
|
+
menu.autoenablesItems = false
|
|
996
|
+
menu.addItem(summaryLine)
|
|
997
|
+
menu.addItem(hintLine)
|
|
998
|
+
menu.addItem(.separator())
|
|
999
|
+
item.menu = menu
|
|
1000
|
+
if let button = item.button {
|
|
1001
|
+
button.imagePosition = .imageLeading
|
|
1002
|
+
button.font = NSFont.systemFont(ofSize: 11)
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/// Reflect the latest overlay state in the menu bar.
|
|
1007
|
+
/// @param summary - one-line state composed by the page.
|
|
1008
|
+
/// @param strings - the helper's own labels.
|
|
1009
|
+
func update(summary: String, badge: Int, strings: Strings) {
|
|
1010
|
+
if summaryLine.title != summary { summaryLine.title = summary }
|
|
1011
|
+
let count = badge > 0 ? " \(badge > 9 ? "9+" : String(badge))" : ""
|
|
1012
|
+
if item.button?.title != count { item.button?.title = count }
|
|
1013
|
+
if hintLine.title != strings.hint { hintLine.title = strings.hint }
|
|
1014
|
+
item.isVisible = true
|
|
1015
|
+
item.button?.toolTip = summary.isEmpty ? strings.title : summary
|
|
1016
|
+
if let button = item.button, button.image == nil {
|
|
1017
|
+
// A tiny pet, so the item reads as this feature rather than a stray digit.
|
|
1018
|
+
button.image = StatusItemController.petImage()
|
|
1019
|
+
button.image?.size = NSSize(width: 16, height: 16)
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/// Draw the same pet the overlay draws, at menu bar size.
|
|
1024
|
+
private static func petImage() -> NSImage {
|
|
1025
|
+
let view = PetView(frame: NSRect(x: 0, y: 0, width: canvas, height: canvas))
|
|
1026
|
+
view.mood = "idle"
|
|
1027
|
+
let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: 64, pixelsHigh: 64,
|
|
1028
|
+
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true,
|
|
1029
|
+
isPlanar: false, colorSpaceName: .deviceRGB,
|
|
1030
|
+
bytesPerRow: 0, bitsPerPixel: 0)
|
|
1031
|
+
guard let rep else { return NSImage(size: NSSize(width: 16, height: 16)) }
|
|
1032
|
+
rep.size = NSSize(width: 16, height: 16)
|
|
1033
|
+
NSGraphicsContext.saveGraphicsState()
|
|
1034
|
+
if let context = NSGraphicsContext(bitmapImageRep: rep) {
|
|
1035
|
+
NSGraphicsContext.current = context
|
|
1036
|
+
view.draw(NSRect(x: 0, y: 0, width: canvas, height: canvas))
|
|
1037
|
+
}
|
|
1038
|
+
NSGraphicsContext.restoreGraphicsState()
|
|
1039
|
+
let image = NSImage(size: NSSize(width: 16, height: 16))
|
|
1040
|
+
image.addRepresentation(rep)
|
|
1041
|
+
return image
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
/// Append the pet's commands to the menu bar menu.
|
|
1045
|
+
/// @param items - prepared items with targets already set.
|
|
1046
|
+
func setActions(_ items: [NSMenuItem]) {
|
|
1047
|
+
for action in actions where menu.items.contains(action) {
|
|
1048
|
+
menu.removeItem(action)
|
|
1049
|
+
}
|
|
1050
|
+
actions = items
|
|
1051
|
+
for action in items { menu.addItem(action) }
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
final class PetPanel: NSPanel {
|
|
1056
|
+
override var canBecomeKey: Bool { false }
|
|
1057
|
+
override var canBecomeMain: Bool { false }
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
/// The content view owns the mouse: a press that moves drags the panel, a press
|
|
1061
|
+
/// that does not is a tap that opens the most urgent conversation.
|
|
1062
|
+
final class PetContentView: NSView {
|
|
1063
|
+
weak var owner: PetWindowController?
|
|
1064
|
+
|
|
1065
|
+
/// Take the click that brings another app forward, so one tap on the pet is
|
|
1066
|
+
/// enough even while Harness sits in the background.
|
|
1067
|
+
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
|
1068
|
+
|
|
1069
|
+
/// Only the drawn pet is clickable. The canvas is 84pt around a 64pt pet, and
|
|
1070
|
+
/// a transparent margin that swallowed clicks would block whatever the user
|
|
1071
|
+
/// has underneath the corners.
|
|
1072
|
+
override func hitTest(_ point: NSPoint) -> NSView? {
|
|
1073
|
+
let inset = (canvas - petSize) / 2
|
|
1074
|
+
let art = NSRect(x: inset, y: inset, width: petSize, height: petSize)
|
|
1075
|
+
return art.contains(point) ? self : nil
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
override func accessibilityRole() -> NSAccessibility.Role? { .button }
|
|
1079
|
+
|
|
1080
|
+
override func accessibilityLabel() -> String? {
|
|
1081
|
+
owner?.accessibilityLabel() ?? ""
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
override func accessibilityPerformPress() -> Bool {
|
|
1085
|
+
owner?.toggleRoster()
|
|
1086
|
+
return true
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
override func mouseDown(with event: NSEvent) {
|
|
1090
|
+
owner?.pressBegan()
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
override func mouseDragged(with event: NSEvent) {
|
|
1094
|
+
owner?.pressDragged()
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
override func mouseUp(with event: NSEvent) {
|
|
1098
|
+
owner?.pressEnded()
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
override func rightMouseDown(with event: NSEvent) {
|
|
1102
|
+
owner?.showActions()
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
// MARK: - Controller
|
|
1107
|
+
|
|
1108
|
+
final class PetWindowController: NSObject {
|
|
1109
|
+
private let panel: PetPanel
|
|
1110
|
+
private let view = PetView()
|
|
1111
|
+
private let content = PetContentView()
|
|
1112
|
+
private var timer: Timer?
|
|
1113
|
+
private var phase: CGFloat = 0
|
|
1114
|
+
private var dragging = false
|
|
1115
|
+
private var dragAnchor: NSPoint = .zero
|
|
1116
|
+
private var pressAnchor: NSPoint = .zero
|
|
1117
|
+
/// Keep the menu bar menu's commands in step with the labels they carry.
|
|
1118
|
+
///
|
|
1119
|
+
/// Items are built directly rather than harvested from a scratch menu: an
|
|
1120
|
+
/// `NSMenuItem` belongs to exactly one menu, and re-parenting one trips an
|
|
1121
|
+
/// AppKit assertion that aborts the process.
|
|
1122
|
+
private var statusMenuKey = ""
|
|
1123
|
+
|
|
1124
|
+
private func refreshStatusMenu() {
|
|
1125
|
+
// Rebuilding three items on every state push is pure churn: the labels only
|
|
1126
|
+
// change when the language does.
|
|
1127
|
+
let key = strings.languageKey
|
|
1128
|
+
guard key != statusMenuKey else { return }
|
|
1129
|
+
statusMenuKey = key
|
|
1130
|
+
let toggle = NSMenuItem(title: strings.hide, action: #selector(menuHide), keyEquivalent: "")
|
|
1131
|
+
toggle.target = self
|
|
1132
|
+
let move = NSMenuItem(title: strings.moveHere, action: #selector(menuMoveHere), keyEquivalent: "")
|
|
1133
|
+
move.target = self
|
|
1134
|
+
let open = NSMenuItem(title: strings.open, action: #selector(menuOpen), keyEquivalent: "")
|
|
1135
|
+
open.target = self
|
|
1136
|
+
statusItem.setActions([toggle, move, open])
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/// What the Host last said the overlay is doing, for the menu bar item.
|
|
1140
|
+
private var summaryText = ""
|
|
1141
|
+
/// Whether the app draws a light surface; nil keeps the system material.
|
|
1142
|
+
private var usesLightSurface: Bool?
|
|
1143
|
+
private var sessions: [SessionEntry] = []
|
|
1144
|
+
private let roster = RosterController()
|
|
1145
|
+
private let statusItem = StatusItemController()
|
|
1146
|
+
|
|
1147
|
+
override init() {
|
|
1148
|
+
panel = PetPanel(contentRect: NSRect(x: 0, y: 0, width: canvas, height: canvas),
|
|
1149
|
+
styleMask: [.borderless, .nonactivatingPanel],
|
|
1150
|
+
backing: .buffered,
|
|
1151
|
+
defer: false)
|
|
1152
|
+
super.init()
|
|
1153
|
+
content.owner = self
|
|
1154
|
+
content.addSubview(view)
|
|
1155
|
+
view.frame = content.bounds
|
|
1156
|
+
view.autoresizingMask = [.width, .height]
|
|
1157
|
+
panel.contentView = content
|
|
1158
|
+
panel.isOpaque = false
|
|
1159
|
+
panel.backgroundColor = .clear
|
|
1160
|
+
panel.hasShadow = false
|
|
1161
|
+
// Order matters: `isFloatingPanel` resets `level` to `.floating`, so the
|
|
1162
|
+
// final level is set last. `.statusBar` + 1 keeps the pet above other
|
|
1163
|
+
// applications' normal and floating windows without covering system UI.
|
|
1164
|
+
panel.isFloatingPanel = true
|
|
1165
|
+
panel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.statusWindow)) + 1)
|
|
1166
|
+
panel.hidesOnDeactivate = false
|
|
1167
|
+
panel.ignoresMouseEvents = false
|
|
1168
|
+
panel.isMovableByWindowBackground = false
|
|
1169
|
+
panel.animationBehavior = .none
|
|
1170
|
+
panel.isReleasedWhenClosed = false
|
|
1171
|
+
panel.collectionBehavior = [.canJoinAllSpaces, .stationary, .fullScreenAuxiliary]
|
|
1172
|
+
placeAtSavedOrigin()
|
|
1173
|
+
panel.orderFrontRegardless()
|
|
1174
|
+
startAnimating()
|
|
1175
|
+
NotificationCenter.default.addObserver(
|
|
1176
|
+
self,
|
|
1177
|
+
selector: #selector(screensChanged),
|
|
1178
|
+
name: NSApplication.didChangeScreenParametersNotification,
|
|
1179
|
+
object: nil
|
|
1180
|
+
)
|
|
1181
|
+
startHeartbeat()
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
// MARK: Placement
|
|
1185
|
+
|
|
1186
|
+
/// Where the drag position lives. A plain file keeps every artifact of this
|
|
1187
|
+
/// helper under one directory instead of scattering a stray defaults domain.
|
|
1188
|
+
private var positionFile: String {
|
|
1189
|
+
let home = ProcessInfo.processInfo.environment["DSH_HOME"]
|
|
1190
|
+
?? ((ProcessInfo.processInfo.environment["HOME"] ?? NSHomeDirectory()) + "/.dsh")
|
|
1191
|
+
return home + "/cache/dsh-task-pet/position.txt"
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/// A saved origin is only usable while it still lands inside a visible frame;
|
|
1195
|
+
/// a display that was unplugged, or a window dragged past an edge, must not
|
|
1196
|
+
/// leave the pet unreachable.
|
|
1197
|
+
private func savedOrigin() -> NSPoint? {
|
|
1198
|
+
guard let raw = try? String(contentsOfFile: positionFile, encoding: .utf8) else { return nil }
|
|
1199
|
+
let parts = raw.split(separator: ",").compactMap { Double($0.trimmingCharacters(in: .whitespacesAndNewlines)) }
|
|
1200
|
+
guard parts.count == 2 else { return nil }
|
|
1201
|
+
let rect = NSRect(x: parts[0], y: parts[1], width: canvas, height: canvas)
|
|
1202
|
+
let onScreen = NSScreen.screens.contains { screen in
|
|
1203
|
+
screen.visibleFrame.insetBy(dx: 1, dy: 1).contains(rect)
|
|
1204
|
+
}
|
|
1205
|
+
return onScreen ? rect.origin : nil
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/// Bottom right of the screen the pointer is on — the one the user is looking
|
|
1209
|
+
/// at — clear of the Dock.
|
|
1210
|
+
private func defaultOrigin() -> NSPoint? {
|
|
1211
|
+
let pointer = NSEvent.mouseLocation
|
|
1212
|
+
let screen = NSScreen.screens.first { $0.frame.contains(pointer) }
|
|
1213
|
+
?? NSScreen.main
|
|
1214
|
+
?? NSScreen.screens.first
|
|
1215
|
+
guard let visible = screen?.visibleFrame else { return nil }
|
|
1216
|
+
return NSPoint(x: visible.maxX - canvas - 24, y: visible.minY + 96)
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
private func placeAtSavedOrigin() {
|
|
1220
|
+
guard let origin = savedOrigin() ?? defaultOrigin() else { return }
|
|
1221
|
+
panel.setFrameOrigin(clamp(origin))
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
/// Keep the whole pet inside one visible screen frame.
|
|
1225
|
+
private func clamp(_ origin: NSPoint) -> NSPoint {
|
|
1226
|
+
let center = NSPoint(x: origin.x + canvas / 2, y: origin.y + canvas / 2)
|
|
1227
|
+
let screen = NSScreen.screens.first { $0.frame.contains(center) } ?? NSScreen.main
|
|
1228
|
+
guard let visible = screen?.visibleFrame else { return origin }
|
|
1229
|
+
return NSPoint(x: min(max(origin.x, visible.minX), visible.maxX - canvas),
|
|
1230
|
+
y: min(max(origin.y, visible.minY), visible.maxY - canvas))
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
/// Keep the pet on a screen that still exists after a display change.
|
|
1234
|
+
@objc private func screensChanged() {
|
|
1235
|
+
roster.hide()
|
|
1236
|
+
panel.setFrameOrigin(clamp(panel.frame.origin))
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
private func remember() {
|
|
1240
|
+
let origin = panel.frame.origin
|
|
1241
|
+
let file = positionFile
|
|
1242
|
+
try? FileManager.default.createDirectory(
|
|
1243
|
+
atPath: (file as NSString).deletingLastPathComponent,
|
|
1244
|
+
withIntermediateDirectories: true
|
|
1245
|
+
)
|
|
1246
|
+
try? "\(origin.x),\(origin.y)".write(toFile: file, atomically: true, encoding: .utf8)
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
/// Exit when nothing has reported for a long while: a plugin reload, a crash,
|
|
1250
|
+
/// or a closed window would otherwise leave a pet that cannot be dismissed.
|
|
1251
|
+
/// The budget is generous because a backgrounded page throttles its timers to
|
|
1252
|
+
/// roughly one tick a minute.
|
|
1253
|
+
private func startHeartbeat() {
|
|
1254
|
+
let guardTimer = Timer(timeInterval: 30, repeats: true) { [weak self] _ in
|
|
1255
|
+
guard let self else { return }
|
|
1256
|
+
if Date().timeIntervalSince(self.lastStateAt) > 300 {
|
|
1257
|
+
emitLog("no state for five minutes; exiting")
|
|
1258
|
+
NSApp.terminate(nil)
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
RunLoop.main.add(guardTimer, forMode: .common)
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
// MARK: Animation
|
|
1265
|
+
|
|
1266
|
+
private func startAnimating() {
|
|
1267
|
+
var idleTicks = 0
|
|
1268
|
+
let tick = Timer(timeInterval: 1.0 / 24.0, repeats: true) { [weak self] _ in
|
|
1269
|
+
guard let self else { return }
|
|
1270
|
+
self.phase += 0.075
|
|
1271
|
+
if self.phase > 10_000 { self.phase = 0 }
|
|
1272
|
+
// A working pet breathes fast and repaints every tick; an idle one only
|
|
1273
|
+
// needs a slow drift, so an idle dock gets 4 frames a second instead of 24.
|
|
1274
|
+
idleTicks += 1
|
|
1275
|
+
guard self.view.working || idleTicks >= 6 else { return }
|
|
1276
|
+
idleTicks = 0
|
|
1277
|
+
self.view.phase = self.phase
|
|
1278
|
+
self.view.needsDisplay = true
|
|
1279
|
+
}
|
|
1280
|
+
timer = tick
|
|
1281
|
+
// `.common` keeps the pet moving while a menu or a drag tracks the run loop.
|
|
1282
|
+
RunLoop.main.add(tick, forMode: .common)
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
// MARK: State
|
|
1286
|
+
|
|
1287
|
+
/// Set while the page keeps reporting. A helper whose page is gone must not
|
|
1288
|
+
/// keep floating a frozen pet over every window.
|
|
1289
|
+
private var lastStateAt = Date()
|
|
1290
|
+
|
|
1291
|
+
func apply(_ message: StateMessage) {
|
|
1292
|
+
lastStateAt = Date()
|
|
1293
|
+
if let locale = message.locale, !locale.isEmpty {
|
|
1294
|
+
let next = Strings.forLanguage(locale)
|
|
1295
|
+
if next.languageKey != strings.languageKey { strings = next }
|
|
1296
|
+
}
|
|
1297
|
+
if let summary = message.summary { view.summary = summary }
|
|
1298
|
+
if let mood = message.mood { view.mood = mood }
|
|
1299
|
+
if let badge = message.badge { view.badge = badge }
|
|
1300
|
+
if let attention = message.attention { view.attention = attention }
|
|
1301
|
+
if let light = message.light { usesLightSurface = light }
|
|
1302
|
+
if let summary = message.summary { summaryText = summary }
|
|
1303
|
+
if !summaryText.isEmpty || !sessions.isEmpty {
|
|
1304
|
+
statusItem.update(summary: summaryText, badge: view.badge, strings: strings)
|
|
1305
|
+
refreshStatusMenu()
|
|
1306
|
+
}
|
|
1307
|
+
if let sessions = message.sessions {
|
|
1308
|
+
self.sessions = sessions
|
|
1309
|
+
// The ring around the pet is the progress of the row the user would jump
|
|
1310
|
+
// to: the pet is useless as a gauge if the number is only in the menu.
|
|
1311
|
+
let lead = sessions.first { $0.kind == "running" && $0.ratio != nil }
|
|
1312
|
+
?? sessions.first { $0.ratio != nil && $0.kind != "finished" }
|
|
1313
|
+
view.progress = lead?.ratio
|
|
1314
|
+
view.progressAlert = lead?.kind == "attention"
|
|
1315
|
+
}
|
|
1316
|
+
view.working = (message.running ?? 0) > 0
|
|
1317
|
+
view.needsDisplay = true
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
// MARK: Mouse
|
|
1321
|
+
|
|
1322
|
+
func pressBegan() {
|
|
1323
|
+
dragging = false
|
|
1324
|
+
pressAnchor = panel.frame.origin
|
|
1325
|
+
dragAnchor = NSEvent.mouseLocation
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
func pressDragged() {
|
|
1329
|
+
let now = NSEvent.mouseLocation
|
|
1330
|
+
let dx = now.x - dragAnchor.x
|
|
1331
|
+
let dy = now.y - dragAnchor.y
|
|
1332
|
+
if !dragging && abs(dx) + abs(dy) > 3 {
|
|
1333
|
+
// The card is anchored to the pet, so it must go when the pet moves.
|
|
1334
|
+
dragging = true
|
|
1335
|
+
roster.hide()
|
|
1336
|
+
}
|
|
1337
|
+
guard dragging else { return }
|
|
1338
|
+
panel.setFrameOrigin(clamp(NSPoint(x: pressAnchor.x + dx, y: pressAnchor.y + dy)))
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
func pressEnded() {
|
|
1342
|
+
if dragging {
|
|
1343
|
+
dragging = false
|
|
1344
|
+
remember()
|
|
1345
|
+
return
|
|
1346
|
+
}
|
|
1347
|
+
toggleRoster()
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
/// Left click: every Session, in one scrollable list above the pet.
|
|
1351
|
+
func toggleRoster() {
|
|
1352
|
+
if roster.isVisible {
|
|
1353
|
+
roster.hide()
|
|
1354
|
+
return
|
|
1355
|
+
}
|
|
1356
|
+
guard !sessions.isEmpty else {
|
|
1357
|
+
emit(["type": "activate"])
|
|
1358
|
+
activateHarness()
|
|
1359
|
+
return
|
|
1360
|
+
}
|
|
1361
|
+
roster.show(entries: sessions, above: panel.frame, light: usesLightSurface,
|
|
1362
|
+
title: strings.title, summary: summaryText, hint: strings.hint,
|
|
1363
|
+
toWindow: strings.toWindow, more: strings.more, less: strings.less) { [weak self] id in
|
|
1364
|
+
self?.roster.hide()
|
|
1365
|
+
emit(["type": "jump", "id": id])
|
|
1366
|
+
self?.activateHarness()
|
|
1367
|
+
} onSwitch: { [weak self] in
|
|
1368
|
+
// Switching surfaces is a durable choice: the window pet takes over.
|
|
1369
|
+
self?.roster.hide()
|
|
1370
|
+
emit(["type": "detach"])
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
/// The Host ranks the roster, so the first entry is the most urgent one.
|
|
1375
|
+
private func jumpTarget() -> SessionEntry? {
|
|
1376
|
+
sessions.first { $0.kind == "attention" }
|
|
1377
|
+
?? sessions.first { $0.kind == "running" }
|
|
1378
|
+
?? sessions.first
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
/// Bring the Harness window forward after a jump. `.activateIgnoringOtherApps`
|
|
1382
|
+
/// is what actually raises it on current macOS; `activate` alone only marks
|
|
1383
|
+
/// the app as active for the next click.
|
|
1384
|
+
private func activateHarness() {
|
|
1385
|
+
let bundleId = ProcessInfo.processInfo.environment["DSH_DESKTOP_BUNDLE"] ?? "com.deepseek.dsh"
|
|
1386
|
+
let candidates = NSWorkspace.shared.runningApplications.filter {
|
|
1387
|
+
$0.bundleIdentifier == bundleId || $0.localizedName == "DeepSeek Harness"
|
|
1388
|
+
}
|
|
1389
|
+
guard let app = candidates.first(where: { !$0.isTerminated }) else {
|
|
1390
|
+
// Not running yet: launch it so a click on the pet is never a dead end.
|
|
1391
|
+
if let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleId) {
|
|
1392
|
+
NSWorkspace.shared.openApplication(at: url, configuration: NSWorkspace.OpenConfiguration())
|
|
1393
|
+
}
|
|
1394
|
+
return
|
|
1395
|
+
}
|
|
1396
|
+
if #available(macOS 14.0, *) {
|
|
1397
|
+
app.activate()
|
|
1398
|
+
} else {
|
|
1399
|
+
app.activate(options: [.activateIgnoringOtherApps, .activateAllWindows])
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
/// What VoiceOver reads for the pet: its role and the headline fact.
|
|
1404
|
+
func accessibilityLabel() -> String {
|
|
1405
|
+
guard let first = sessions.first else { return strings.open }
|
|
1406
|
+
return "\(view.summary.isEmpty ? strings.open : view.summary) — \(first.title)"
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
// MARK: Menu
|
|
1410
|
+
|
|
1411
|
+
/// The roster, newest urgency first. Rows carry their own target so a click
|
|
1412
|
+
/// is unambiguous even while the roster changes underneath the open menu.
|
|
1413
|
+
/// Right click: the pet's own actions. Plain titles, so clicks behave.
|
|
1414
|
+
func showActions() {
|
|
1415
|
+
let menu = NSMenu()
|
|
1416
|
+
menu.autoenablesItems = false
|
|
1417
|
+
let move = NSMenuItem(title: strings.moveHere, action: #selector(menuMoveHere), keyEquivalent: "")
|
|
1418
|
+
move.target = self
|
|
1419
|
+
menu.addItem(move)
|
|
1420
|
+
let open = NSMenuItem(title: strings.open, action: #selector(menuOpen), keyEquivalent: "")
|
|
1421
|
+
open.target = self
|
|
1422
|
+
menu.addItem(open)
|
|
1423
|
+
let hide = NSMenuItem(title: strings.hide, action: #selector(menuHide), keyEquivalent: "")
|
|
1424
|
+
hide.target = self
|
|
1425
|
+
menu.addItem(hide)
|
|
1426
|
+
// Open above the pet so the list never runs off the bottom of the screen.
|
|
1427
|
+
let anchor = NSPoint(x: 0, y: panel.frame.height)
|
|
1428
|
+
menu.popUp(positioning: nil, at: anchor, in: content)
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
/// Put the pet on the screen the pointer is on — the only way back for a pet
|
|
1432
|
+
/// dragged onto a display the user then disconnected the mind from.
|
|
1433
|
+
@objc private func menuMoveHere() {
|
|
1434
|
+
guard let visible = (NSScreen.screens.first { $0.frame.contains(NSEvent.mouseLocation) }
|
|
1435
|
+
?? NSScreen.main)?.visibleFrame else { return }
|
|
1436
|
+
panel.setFrameOrigin(clamp(NSPoint(x: visible.maxX - canvas - 24, y: visible.minY + 96)))
|
|
1437
|
+
remember()
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
@objc private func menuOpen() { activateHarness() }
|
|
1441
|
+
|
|
1442
|
+
@objc private func menuJump() {
|
|
1443
|
+
guard let target = jumpTarget() else { return }
|
|
1444
|
+
emit(["type": "jump", "id": target.id])
|
|
1445
|
+
activateHarness()
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
@objc private func menuHide() {
|
|
1449
|
+
emit(["type": "hidden"])
|
|
1450
|
+
NSApp.terminate(nil)
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
// MARK: Stdio
|
|
1454
|
+
|
|
1455
|
+
/// Read state lines until the Host closes the pipe; EOF means it is gone.
|
|
1456
|
+
func startReading() {
|
|
1457
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
1458
|
+
let decoder = JSONDecoder()
|
|
1459
|
+
while let line = readLine(strippingNewline: true) {
|
|
1460
|
+
guard !line.isEmpty, let data = line.data(using: .utf8) else { continue }
|
|
1461
|
+
guard let message = try? decoder.decode(StateMessage.self, from: data) else { continue }
|
|
1462
|
+
switch message.type {
|
|
1463
|
+
case "state":
|
|
1464
|
+
DispatchQueue.main.async { self.apply(message) }
|
|
1465
|
+
case "ping":
|
|
1466
|
+
emit(["type": "pong"])
|
|
1467
|
+
case "quit":
|
|
1468
|
+
DispatchQueue.main.async { NSApp.terminate(nil) }
|
|
1469
|
+
default:
|
|
1470
|
+
continue
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
emitLog("stdin closed")
|
|
1474
|
+
DispatchQueue.main.async { NSApp.terminate(nil) }
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
// MARK: - Single instance
|
|
1480
|
+
|
|
1481
|
+
/// One pet per DSH home. A second Harness window (or a restart that raced the
|
|
1482
|
+
/// old process) must not stack a second floating pet on the same screen.
|
|
1483
|
+
enum InstanceLock {
|
|
1484
|
+
static func path() -> String {
|
|
1485
|
+
if let home = ProcessInfo.processInfo.environment["DSH_HOME"], !home.isEmpty {
|
|
1486
|
+
return home + "/cache/dsh-task-pet/instance.pid"
|
|
1487
|
+
}
|
|
1488
|
+
let base = ProcessInfo.processInfo.environment["HOME"] ?? NSHomeDirectory()
|
|
1489
|
+
return base + "/.dsh/cache/dsh-task-pet/instance.pid"
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
/// Claim the singleton slot, or report the live owner.
|
|
1493
|
+
/// @returns true when this process owns the slot.
|
|
1494
|
+
static func claim() -> Bool {
|
|
1495
|
+
let file = path()
|
|
1496
|
+
if let text = try? String(contentsOfFile: file, encoding: .utf8),
|
|
1497
|
+
let existing = pid_t(text.trimmingCharacters(in: .whitespacesAndNewlines)) {
|
|
1498
|
+
// `kill(pid, 0)` succeeds while that process exists; a stale file from a
|
|
1499
|
+
// crash reports ESRCH and is simply taken over.
|
|
1500
|
+
if existing != getpid(), kill(existing, 0) == 0 { return false }
|
|
1501
|
+
}
|
|
1502
|
+
try? FileManager.default.createDirectory(
|
|
1503
|
+
atPath: (file as NSString).deletingLastPathComponent,
|
|
1504
|
+
withIntermediateDirectories: true
|
|
1505
|
+
)
|
|
1506
|
+
try? String(getpid()).write(toFile: file, atomically: true, encoding: .utf8)
|
|
1507
|
+
return true
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
/// Drop the slot when this owner exits.
|
|
1511
|
+
static func release() {
|
|
1512
|
+
let file = path()
|
|
1513
|
+
if let text = try? String(contentsOfFile: file, encoding: .utf8),
|
|
1514
|
+
pid_t(text.trimmingCharacters(in: .whitespacesAndNewlines)) != getpid() { return }
|
|
1515
|
+
try? FileManager.default.removeItem(atPath: file)
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
// MARK: - Preference gate
|
|
1520
|
+
|
|
1521
|
+
/// The durable opt-in, read straight from disk.
|
|
1522
|
+
///
|
|
1523
|
+
/// The Host normally decides whether to spawn this helper, but a stale Host
|
|
1524
|
+
/// (or a supervisor whose state went out of sync) can respawn it after the user
|
|
1525
|
+
/// turned desktop mode off. Consulting the same file here makes "off" mean off,
|
|
1526
|
+
/// no matter who is doing the spawning: a helper that boots without an explicit
|
|
1527
|
+
/// `enabled: true` reports why and exits immediately.
|
|
1528
|
+
enum Preference {
|
|
1529
|
+
static func path() -> String {
|
|
1530
|
+
if let home = ProcessInfo.processInfo.environment["DSH_HOME"], !home.isEmpty {
|
|
1531
|
+
return home + "/storages/dsh-task-pet/desktop.json"
|
|
1532
|
+
}
|
|
1533
|
+
let base = ProcessInfo.processInfo.environment["HOME"] ?? NSHomeDirectory()
|
|
1534
|
+
return base + "/.dsh/storages/dsh-task-pet/desktop.json"
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
/// True only for an explicit, readable `{"enabled": true}`.
|
|
1538
|
+
static func enabled() -> Bool {
|
|
1539
|
+
guard let data = FileManager.default.contents(atPath: path()),
|
|
1540
|
+
let object = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
|
1541
|
+
let flag = object["enabled"] as? Bool else { return false }
|
|
1542
|
+
return flag
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
// MARK: - Boot
|
|
1547
|
+
|
|
1548
|
+
// NSApplication must exist before any NSWindow is constructed.
|
|
1549
|
+
let app = NSApplication.shared
|
|
1550
|
+
if !Preference.enabled() {
|
|
1551
|
+
// Nobody opted in. Reporting it lets the Host correct its own state instead
|
|
1552
|
+
// of counting this as a crash.
|
|
1553
|
+
emit(["type": "disabled", "pid": ProcessInfo.processInfo.processIdentifier])
|
|
1554
|
+
exit(0)
|
|
1555
|
+
}
|
|
1556
|
+
if !InstanceLock.claim() {
|
|
1557
|
+
// Another Harness instance already floats a pet for this DSH home.
|
|
1558
|
+
emit(["type": "duplicate", "pid": ProcessInfo.processInfo.processIdentifier])
|
|
1559
|
+
exit(0)
|
|
1560
|
+
}
|
|
1561
|
+
atexit { InstanceLock.release() }
|
|
1562
|
+
app.setActivationPolicy(.accessory)
|
|
1563
|
+
let controller = PetWindowController()
|
|
1564
|
+
controller.startReading()
|
|
1565
|
+
emit(["type": "ready", "pid": ProcessInfo.processInfo.processIdentifier])
|
|
1566
|
+
app.run()
|