1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
// dingger menu bar: herdr's agent panel as a macOS status item, plus a
// floating always-on-top panel (panel.swift) toggled by a global shortcut
// (hotkey.swift). Rows and colors come from model.swift.
//
// Reads the pane options the dingger daemon writes and renders herdr's
// Symbols style with herdr's palette:
//
// blocked × red done ✓ teal working ◐ yellow idle ○ green
//
// in herdr's priority order, oldest waiting first. Clicking a row runs
// `dingger focus --pane`, the same tested code path as the tmux status-line
// click.
//
// Debug flags (used by tests/e2e.sh):
// --print print the title and rows the menu would show, then exit
// (rows: "<symbol> <status>\t<title>\t<subtitle>")
// --print-panel print what the floating panel would show, then exit
// --click <pane> perform a row click for <pane>, then exit
// --selftest check the panel visibility rules, then exit
import AppKit
func log(_ message: String) {
FileHandle.standardError.write(Data("\(Date()) \(message)\n".utf8))
}
final class Menubar: NSObject, NSApplicationDelegate, NSMenuDelegate {
private let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
private let menu = NSMenu()
private var panel: PanelController!
private var visibility = PanelVisibility(
mode: PanelMode(rawValue: UserDefaults.standard.string(forKey: "panelMode") ?? "") ?? .auto)
private var settings = Settings()
private var settingsModified: Date?
private var hotkey: Hotkey?
private var hotkeyRegistered = false
private var rows: [Row] = []
func applicationDidFinishLaunching(_: Notification) {
menu.delegate = self
item.menu = menu
panel = PanelController { [weak self] paneId in self?.jump(to: paneId) }
Settings.ensureFile()
reloadSettings()
refresh()
let timer = Timer(timeInterval: 1.0, repeats: true) { [weak self] _ in self?.refresh() }
RunLoop.main.add(timer, forMode: .common)
}
// MARK: settings and shortcut
private func reloadSettings() {
settingsModified = Settings.modified()
settings = Settings.load()
guard let parsed = Hotkey.parse(settings.hotkey) else {
GlobalHotkey.unregister()
hotkey = nil
hotkeyRegistered = false
log("settings: invalid hotkey \"\(settings.hotkey)\"")
return
}
hotkey = parsed
hotkeyRegistered = GlobalHotkey.register(parsed) { [weak self] in self?.togglePanel() }
log("settings: hotkey \(parsed.display) \(hotkeyRegistered ? "registered" : "in use by another app")")
}
// MARK: refresh
private func refresh() {
if Settings.modified() != settingsModified { reloadSettings() }
rows = fetchRows()
item.button?.attributedTitle = title(for: rows)
var tip: [String] = []
for status in [Status.blocked, .done, .working, .idle] {
let count = rows.filter { $0.status == status }.count
if count > 0 { tip.append("\(count) \(status.rawValue)") }
}
let tipText = tip.isEmpty ? "No agents" : tip.joined(separator: " · ")
item.button?.toolTip = tipText
item.button?.setAccessibilityLabel("dingger, " + tipText)
visibility.update(waiting: Set(rows.filter(\.waiting).map(\.paneId)))
applyPanel()
}
private func applyPanel() {
if visibility.visible {
panel.render(PanelContent(rows: rows))
if !panel.isVisible {
panel.show()
log("panel: shown (\(visibility.mode.rawValue), forced: \(String(describing: visibility.forced)))")
}
} else if panel.isVisible {
panel.hide()
log("panel: hidden (\(visibility.mode.rawValue), forced: \(String(describing: visibility.forced)))")
}
}
@objc private func togglePanel() {
visibility.toggle()
applyPanel()
}
@objc private func selectMode(_ sender: NSMenuItem) {
guard let raw = sender.representedObject as? String, let mode = PanelMode(rawValue: raw) else { return }
visibility.setMode(mode)
UserDefaults.standard.set(mode.rawValue, forKey: "panelMode")
applyPanel()
}
@objc private func openSettings() {
Settings.ensureFile()
_ = run("/usr/bin/open", ["-t", Settings.path])
}
private func jump(to paneId: String) {
DispatchQueue.global().async {
focus(paneId)
DispatchQueue.main.async { self.refresh() }
}
}
// MARK: menu (rebuilt from fresh data every time it opens)
func menuNeedsUpdate(_ menu: NSMenu) {
menu.removeAllItems()
rows = fetchRows()
if rows.isEmpty {
let none = NSMenuItem(title: "No agents", action: nil, keyEquivalent: "")
none.isEnabled = false
menu.addItem(none)
}
for (index, row) in rows.enumerated() {
// One separator between the agents waiting for you and the rest.
if index > 0, rows[index - 1].waiting, !row.waiting {
menu.addItem(.separator())
}
let entry = NSMenuItem(title: row.label, action: #selector(rowClicked(_:)), keyEquivalent: "")
entry.target = self
entry.attributedTitle = rowTitle(row)
entry.subtitle = row.subtitle
entry.toolTip = row.name.isEmpty ? nil : row.name
entry.representedObject = row.paneId
menu.addItem(entry)
}
menu.addItem(.separator())
let toggle = NSMenuItem(title: visibility.visible ? "Hide Panel" : "Show Panel",
action: #selector(togglePanel), keyEquivalent: "")
toggle.target = self
if let hotkey, hotkeyRegistered {
toggle.keyEquivalent = hotkey.key.count == 1 ? hotkey.key : ""
toggle.keyEquivalentModifierMask = hotkey.modifierFlags
}
menu.addItem(toggle)
let modeMenu = NSMenu()
for mode in PanelMode.allCases {
let option = NSMenuItem(title: mode.label, action: #selector(selectMode(_:)), keyEquivalent: "")
option.target = self
option.representedObject = mode.rawValue
option.state = visibility.mode == mode ? .on : .off
modeMenu.addItem(option)
}
let modeItem = NSMenuItem(title: "Show Panel Automatically", action: nil, keyEquivalent: "")
modeItem.submenu = modeMenu
menu.addItem(modeItem)
if let hotkey, !hotkeyRegistered {
let clash = NSMenuItem(title: "Shortcut \(hotkey.display) is used by another app", action: nil,
keyEquivalent: "")
clash.isEnabled = false
menu.addItem(clash)
} else if hotkey == nil {
let invalid = NSMenuItem(title: "Shortcut \"\(settings.hotkey)\" is not valid", action: nil,
keyEquivalent: "")
invalid.isEnabled = false
menu.addItem(invalid)
}
let settingsItem = NSMenuItem(title: "Settings…", action: #selector(openSettings), keyEquivalent: ",")
settingsItem.target = self
menu.addItem(settingsItem)
menu.addItem(.separator())
menu.addItem(NSMenuItem(title: "Quit dingger", action: #selector(NSApplication.terminate(_:)),
keyEquivalent: "q"))
}
@objc private func rowClicked(_ sender: NSMenuItem) {
guard let paneId = sender.representedObject as? String else { return }
jump(to: paneId)
}
}
let arguments = CommandLine.arguments
if arguments.contains("--selftest") {
var failures = panelSelftest()
if Hotkey.parse("ctrl+opt+cmd+e")?.display != "⌃⌥⌘E" { failures.append("hotkey: parse ctrl+opt+cmd+e") }
if Hotkey.parse("e") != nil { failures.append("hotkey: a key without modifiers is rejected") }
if Hotkey.parse("ctrl+opt+bogus") != nil { failures.append("hotkey: unknown keys are rejected") }
if Hotkey.parse("cmd+shift+f12")?.display != "⇧⌘F12" { failures.append("hotkey: parse cmd+shift+f12") }
failures.forEach { print("FAIL \($0)") }
print(failures.isEmpty ? "selftest ok" : "selftest: \(failures.count) failed")
exit(failures.isEmpty ? 0 : 1)
}
if arguments.contains("--print") {
let rows = fetchRows()
print("title: \(plainTitle(for: rows))")
for row in rows {
print("\(row.status.symbol) \(row.status.rawValue)\t\(row.label)\t\(row.subtitle)")
}
exit(0)
}
if arguments.contains("--print-panel") {
print(PanelContent(rows: fetchRows()).plainText)
exit(0)
}
if let index = arguments.firstIndex(of: "--click"), index + 1 < arguments.count {
focus(arguments[index + 1])
exit(0)
}
let app = NSApplication.shared
let delegate = Menubar()
app.delegate = delegate
app.setActivationPolicy(.accessory)
app.run()
|