diff --git a/README.md b/README.md index dc84d43..5bfaecc 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,10 @@ of state is one line in the app's log. The ๐‘– in the menu bar shows the lend at a glance: plain while live, dimmed while it connects or reconnects, and dimmed with a slash through it while it is not lent (no mind, stopped, not allowed, -incompatible). Green, red and orange (a banner, a use, a capture) show over it while they last. -`--render-icons ` draws each mark on a light and a dark bar. +incompatible). Green, red and orange (a banner, a use, the microphone or a camera on) show over it +while they last. A small red dot at its lower right is the record: filled while it records, a ring +while it is paused, none while it is off. `--render-icons ` draws each mark on a light and +a dark bar, and `marks-light.png` and `marks-dark.png`: each colour of the mark with each dot. Needs macOS 14 or later and Node 22.18 or later. @@ -168,6 +170,8 @@ await mac.banner.show({ }); // { pressed: "Join" }, or { pressed: null, why: "no answer" | "dismissed" | "replaced" | "off", words } await mac.call.ring({ agent: "/agents/lunch", from: "Lunch planner", reason: "Friday's table" }); // { accepted: true }, or { accepted: false, why: "declined" | "no answer" | "replaced" | "off", words } +await mac.record.state(); // { sources: { screens, cameras, mics, speakers }, windowText, recording, paused, line, tracks, โ€ฆ } +await mac.record.set({ screens: true, mics: false }); // the new state; "on" is a use, as a snapshot is ``` ### Presence @@ -270,8 +274,19 @@ to a mind, into that mind, and starts again by itself when the app starts. A sou permission macOS has not given says so in its row and stays off. Screens and cameras pause as soon as the Mac is locked or its displays sleep, and start again by themselves: a paused camera is off, its light too. Microphones and speakers go on. The line under the -switches says what happens ("Recording 4 tracks ยท 2 slots waiting"), and the ๐‘– in the menu bar is -orange while the Mac records. +switches says what happens ("Recording 4 tracks ยท 2 slots waiting"), and the ๐‘– in the menu bar has +a red dot at its lower right while the Mac records, a red ring while the record is paused. The +record does not change the colour of the ๐‘–: orange still says that a call or a capture has the +microphone or a camera on. + +A mind's agent reads and sets the same switches: `mac.record.state()` (no use of the Mac's powers) +and `mac.record.set({ screens, cameras, mics, speakers, windowText })`, which answers the new +state. Turning a source off is always allowed. Turning one on is a use of its power under your +choice for it, as a snapshot or a listen is: **Screens** and **Speakers** go by each display's +choice, **Camera** by each camera's, **Microphone** by the microphone's, **Window text** by the +window text's. Off refuses, Ask asks you with Touch ID, Shared does it, and Activity shows it +("Record the screens until switched off"). Your own hand on the panel's switches asks nothing. +`mac.state()` has the record's short state too (`record: { recording, paused, line }`). Time is cut into **slots** of 30 seconds on the UTC clock. For each slot each **track** (`screen-`, `camera-0`, `mic-0`, `speaker-0`) is one file in the mind's files, named by @@ -410,7 +425,7 @@ script of the bridge parses. Every push to `main` publishes a version to npm ./run-on-mac.sh # build, install in /Applications, open, follow the logs ./run-on-mac.sh --build-only # build and sign .build/iterate.app only ./run-on-mac.sh --logs # follow the app's log -swift test # the state machines' tests (needs Xcode): microphone, camera, lend, banner, the record +swift test # the state machines' tests (needs Xcode): microphone, camera, lend, banner, the record, the mark and record.set's rule npx . # the installer from this folder npx . init /tmp/mac --no-install # a copy, without building or installing it ``` diff --git a/Sources/VoiceMeetCore/RecordRule.swift b/Sources/VoiceMeetCore/RecordRule.swift new file mode 100644 index 0000000..71461ed --- /dev/null +++ b/Sources/VoiceMeetCore/RecordRule.swift @@ -0,0 +1,133 @@ +/// The record's dot on the menu-bar mark, the mark's ink, and the rule by which a caller of the +/// endpoint may turn a switch of the record on. Pure: the app (`AppDelegate`, `AppModel`) reads +/// macOS and the person's choices, and asks these. + +/// The small red dot at the mark's lower right. +public enum RecordDot: String, Equatable, Sendable, CaseIterable { + /// The record is off: no dot. + case none + /// The record runs: a filled dot. + case recording + /// The record is on and paused (the Mac is locked or its displays sleep): a ring. + case paused + + public static func of(isRecording: Bool, isPaused: Bool) -> RecordDot { + isRecording ? (isPaused ? .paused : .recording) : .none + } + + /// The words that follow the mark's own in its tooltip: "recording 4 tracks", "recording paused: locked". + public func words(tracks: Int, pausedBecause: String?) -> String? { + switch self { + case .none: return nil + case .recording: return "recording \(tracks) track\(tracks == 1 ? "" : "s")" + case .paused: return "recording paused" + (pausedBecause.map { ": \($0)" } ?? "") + } + } +} + +/// What the mark in the menu bar shows: its ink and the record's dot. The record never changes the +/// ink: orange says that the microphone or a camera is on for a call or a capture, and the dot +/// says that the record runs, so a camera turned on during an all-day record still shows. +public struct MenuBarMark: Equatable, Sendable { + public enum Ink: String, Equatable, Sendable, CaseIterable { + /// A banner shows, a call's among them. + case green + /// A mind used the Mac in the last 3 seconds. + case red + /// The microphone or a camera is on, or the person's capture runs. + case orange + /// Else the lend at a glance (`LendGlance`): plain, dimmed, dimmed and slashed. + case live, connecting, down + } + + public let ink: Ink + public let dot: RecordDot + + public init(ink: Ink, dot: RecordDot) { + self.ink = ink + self.dot = dot + } + + public static func of( + bannerShows: Bool, inUse: Bool, microphoneOrCameraOn: Bool, glance: LendGlance, isRecording: Bool, isPaused: Bool + ) -> MenuBarMark { + let ink: Ink + if bannerShows { + ink = .green + } else if inUse { + ink = .red + } else if microphoneOrCameraOn { + ink = .orange + } else { + switch glance { + case .live: ink = .live + case .connecting: ink = .connecting + case .down: ink = .down + } + } + return MenuBarMark(ink: ink, dot: .of(isRecording: isRecording, isPaused: isPaused)) + } + + /// One word for the log: "orange", "live+recording". + public var name: String { dot == .none ? ink.rawValue : "\(ink.rawValue)+\(dot.rawValue)" } +} + +/// A switch of the record that a caller can set: `record.set` on the endpoint. +public enum RecordSwitch: String, Equatable, Hashable, Sendable, CaseIterable { + case screens, cameras, mics, speakers, windowText + + /// The power whose choice in the panel decides whether a caller may turn this switch on. + /// The speakers are recorded through the screen's capture, so the screen's choice decides. + public var power: String { + switch self { + case .screens, .speakers: return "screen" + case .cameras: return "camera" + case .mics: return "microphone" + case .windowText: return "accessibility" + } + } + + /// What the record does, for the approval and for Activity: "record the screens". + public var does: String { + switch self { + case .screens: return "record the screens" + case .cameras: return "record the cameras" + case .mics: return "record the microphone" + case .speakers: return "record the sound of the speakers" + case .windowText: return "record the text of the window in front" + } + } + + /// The switches a caller turns on with `wanted`: those it sets true that are not on now. Only + /// these are a use of the Mac's powers; a switch turned off, or left as it is, is none. + public static func turnedOn(from on: Set, by wanted: [RecordSwitch: Bool]) -> [RecordSwitch] { + allCases.filter { wanted[$0] == true && !on.contains($0) } + } + + /// The switches that `wanted` turns off. + public static func turnedOff(from on: Set, by wanted: [RecordSwitch: Bool]) -> [RecordSwitch] { + allCases.filter { wanted[$0] == false && on.contains($0) } + } +} + +/// How the endpoint answers a caller's use of one thing: the rule of `AppModel.use`, for a +/// snapshot, a listen and a switch of the record turned on alike. +public enum UseGate: Equatable, Sendable { + /// The owner has the thing off. + case off + /// macOS has not given the app the permission the thing needs. + case notPermitted + /// The choice is Ask and no approval holds: the person is asked. + case ask + /// Shared, or Ask with an approval that holds. + case go + + /// `sharing` is the panel's choice: "off", "ask" or "shared"; anything else is off. + public static func decide(sharing: String, permitted: Bool, approved: Bool) -> UseGate { + switch sharing { + case "shared": return permitted ? .go : .notPermitted + case "ask": return !permitted ? .notPermitted : approved ? .go : .ask + default: return .off + } + } +} diff --git a/Sources/VoiceMeetMac/AppDelegate.swift b/Sources/VoiceMeetMac/AppDelegate.swift index ade3b97..19eb975 100644 --- a/Sources/VoiceMeetMac/AppDelegate.swift +++ b/Sources/VoiceMeetMac/AppDelegate.swift @@ -12,6 +12,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate { private let lend = Lend() private let popover = NSPopover() private var statusItem: NSStatusItem? + private var recordDot: RecordDotView? private var shownStatusIcon = "" private var terminationSignal: DispatchSourceSignal? private var grantWatch: Timer? @@ -29,6 +30,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate { statusItem.button?.target = self statusItem.button?.action = #selector(togglePanel) self.statusItem = statusItem + if let button = statusItem.button { recordDot = RecordDotView.attach(to: button, imageSize: IterateIcon.plain.size) } let panel = Panel( state: panelState, @@ -239,33 +241,39 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate { /// The iterate mark: green while a banner shows, red for 3 seconds at each use by a /// mind, orange while the microphone or a camera is on. Else the lend at a glance: plain while - /// live, dimmed while it connects, dimmed and slashed while it is not lent. + /// live, dimmed while it connects, dimmed and slashed while it is not lent. The record adds a + /// red dot at its lower right, filled while it records, a ring while it is paused, and does not + /// change the ink (`MenuBarMark`). private func updateStatusButton() { guard let button = statusItem?.button else { return } let isInUse = model.lastUse.map { Date().timeIntervalSince($0.at) < 3 } ?? false if isInUse { DispatchQueue.main.asyncAfter(deadline: .now() + 3.1) { [weak self] in self?.updateStatusButton() } } - let isCapturing = model.microphone.isRunning || model.isAnyCameraRunning || self.isCapturing || model.record.isRecording + let record = model.record + let isCapturing = model.microphone.isRunning || model.isAnyCameraRunning || self.isCapturing let banner = model.banners.state.showing let glance = lend.state.phase.glance - let shown = banner != nil ? "green" : isInUse ? "red" : isCapturing ? "orange" : glance.rawValue - button.image = - banner != nil ? IterateIcon.green : isInUse ? IterateIcon.red : isCapturing ? IterateIcon.orange : IterateIcon.lend(glance) + let mark = MenuBarMark.of( + bannerShows: banner != nil, inUse: isInUse, microphoneOrCameraOn: isCapturing, glance: glance, + isRecording: record.isRecording, isPaused: record.isPaused) + button.image = IterateIcon.image(mark) + recordDot?.dot = mark.dot let lendWords: String switch glance { case .live: lendWords = "iterate: lent" case .connecting: lendWords = "iterate: connecting" case .down: lendWords = "iterate: not lent" } - let label = + let words = banner.map { $0.isCall ? "iterate: \($0.title) calls" : "iterate: \($0.title)" } - ?? (isInUse ? "iterate: a mind uses this Mac now" : self.isCapturing ? "iterate: your capture runs" : model.record.isRecording ? "iterate: recording into the mind" : isCapturing ? "iterate: microphone or camera on" : lendWords) + ?? (isInUse ? "iterate: a mind uses this Mac now" : self.isCapturing ? "iterate: your capture runs" : isCapturing ? "iterate: microphone or camera on" : lendWords) + let label = [words, mark.dot.words(tracks: record.tracks.count, pausedBecause: record.pausedBecause)].compactMap { $0 }.joined(separator: ", ") button.toolTip = label button.setAccessibilityLabel(label) - if shown != shownStatusIcon { - shownStatusIcon = shown - log.info("menu-bar icon: the iterate mark, \(shown)") + if mark.name != shownStatusIcon { + shownStatusIcon = mark.name + log.info("menu-bar icon: the iterate mark, \(mark.name)") } } diff --git a/Sources/VoiceMeetMac/AppModel.swift b/Sources/VoiceMeetMac/AppModel.swift index 1c6d603..4a44b54 100644 --- a/Sources/VoiceMeetMac/AppModel.swift +++ b/Sources/VoiceMeetMac/AppModel.swift @@ -1,6 +1,7 @@ import AVFoundation import AppKit import Combine +import Record import VoiceMeetCore /// Answers the local endpoint's callers (a mind, through `iterate provide`) from the Mac's screen, @@ -117,7 +118,12 @@ final class AppModel { }, "microphone": microphone.statusLine, "presence": Self.presence().dictionary, + "record": ["recording": record.isRecording, "paused": record.isPaused, "line": record.line], ] + case "record.state": + return recordState() + case "record.set": + return try await setRecord(parameters) case "screen.snapshot": let display = try Screen.resolve(parameters["display"] as? Int) return try await act(display, what: "A still picture of the display \"\(display.title)\".", doing: "Taking a picture of the display โ€œ\(display.title)โ€", did: "Took a picture of the display โ€œ\(display.title)โ€") { @@ -202,7 +208,7 @@ final class AppModel { return [:] default: throw EndpointRefusal( - "No method \"\(method)\". The methods are: state, screen.snapshot, camera.snapshot, microphone.listen, speaker.play, speaker.say, accessibility.frontWindow, run, banner.show, call.ring." + "No method \"\(method)\". The methods are: state, screen.snapshot, camera.snapshot, microphone.listen, speaker.play, speaker.say, accessibility.frontWindow, run, banner.show, call.ring, record.state, record.set." ) } } @@ -210,21 +216,90 @@ final class AppModel { /// Refuses a thing that is off or not permitted, asks the person first when its choice is /// "Ask", then counts the use, so that the menu bar and the panel show it. private func use(_ thing: Shareable, what: String) async throws { - let sharing = thing.sharing - guard sharing != .off else { + switch UseGate.decide(sharing: thing.sharing.rawValue, permitted: thing.isPermitted, approved: approval.isApproved(thing) != nil) { + case .off: throw EndpointRefusal("The Mac's owner has this off in the iterate panel: \(thing.title). A mind cannot \(thing.does).") - } - if let permission = thing.power.permission, !permission.isGranted { + case .notPermitted: throw EndpointRefusal( - "macOS has not given this app \(permission.title). The Mac's owner gives it from the iterate panel, at \(thing.title)." + "macOS has not given this app \(thing.power.permission?.title ?? "a permission"). The Mac's owner gives it from the iterate panel, at \(thing.title)." ) - } - if sharing == .ask, approval.isApproved(thing) == nil { + case .ask: try await approval.ask(for: thing, what: what) + case .go: + break } count(thing) } + // MARK: - The record's switches + + /// The record's switches and its state, for `state()` and `record.state()`. Reading it is no use. + private func recordState() -> [String: Any] { + var answer = record.state() + answer["windowText"] = deviceWindow.readsWindowText + return answer + } + + private var recordOn: Set { + var on = Set(record.on.compactMap { RecordSwitch(rawValue: $0.rawValue) }) + if deviceWindow.readsWindowText { on.insert(.windowText) } + return on + } + + /// `record.set`: a switch turned off is off at once; a switch turned on is a use of its power, + /// under that power's choice in the panel (`use`), for each display or camera it records. The + /// switches turned off stay off when a use is then refused; no switch is turned on then. + private func setRecord(_ parameters: [String: Any]) async throws -> [String: Any] { + var wanted: [RecordSwitch: Bool] = [:] + for item in RecordSwitch.allCases { + guard let value = parameters[item.rawValue] else { continue } + guard let on = value as? Bool else { throw EndpointRefusal("record.set: \(item.rawValue) is true or false.") } + wanted[item] = on + } + let unknown = Set(parameters.keys).subtracting(RecordSwitch.allCases.map(\.rawValue) + ["method"]) + guard unknown.isEmpty else { + throw EndpointRefusal("record.set knows screens, cameras, mics, speakers and windowText, not \(unknown.sorted().joined(separator: ", ")).") + } + let on = recordOn + for item in RecordSwitch.turnedOff(from: on, by: wanted) { setRecordSwitch(item, false) } + let turnedOn = RecordSwitch.turnedOn(from: on, by: wanted) + for item in turnedOn { + let things = recordThings(item) + guard !things.isEmpty else { throw EndpointRefusal("This Mac has nothing to \(item.does) now.") } + for thing in things { + try await use(thing, what: "To \(item.does) into the mind, \(thing.title) among them, until it is switched off in the panel or by a call.") + } + } + for item in turnedOn { + setRecordSwitch(item, true) + let symbol = DeviceRecord.switches.first { $0.source.rawValue == item.rawValue }?.symbol ?? "text.viewfinder" + let act = Shareable(id: "record.\(item.rawValue)", power: .screen, title: item.rawValue, does: item.does, symbol: symbol) + let words = item.does.prefix(1).uppercased() + item.does.dropFirst() + acts.ended(acts.began(act, doing: "\(words) until switched off", did: "Turned on: \(item.does) until switched off")) + } + return recordState() + } + + private func setRecordSwitch(_ item: RecordSwitch, _ value: Bool) { + log.info("endpoint: a caller turns the record's \(item.rawValue) \(value ? "on" : "off")") + if item == .windowText { + deviceWindow.readsWindowText = value + } else if let source = RecordSource(rawValue: item.rawValue) { + record.set(source, value) + } + } + + /// The things whose choice decides a switch: every display for the screens and the speakers + /// (the record has them all), every camera, the microphone, the window text. + private func recordThings(_ item: RecordSwitch) -> [Shareable] { + switch item.power { + case "screen": return Screen.displays.map(Shareable.display) + case "camera": return Camera.devices.map(Shareable.camera) + case "microphone": return [.microphone] + default: return [.accessibility] + } + } + /// One act of a caller with `thing`: `use` first, then `body`, and the person sees it in the /// pill on the screen and in the panel's list, from its start to its end. A refusal before /// the act (the thing is off, the person said no) is no act. diff --git a/Sources/VoiceMeetMac/DeviceRecord.swift b/Sources/VoiceMeetMac/DeviceRecord.swift index 9ebbc95..9207874 100644 --- a/Sources/VoiceMeetMac/DeviceRecord.swift +++ b/Sources/VoiceMeetMac/DeviceRecord.swift @@ -44,7 +44,14 @@ final class DeviceRecord: ObservableObject { RecordSource.allCases.filter { UserDefaults.standard.bool(forKey: "record.\($0.rawValue)") }) /// The quiet line of state under the switches. @Published private(set) var line = "Off" + /// A recorder runs (the dot on the mark); it may be paused. @Published private(set) var isRecording = false + /// The recorder runs and is paused: the Mac is locked or its displays sleep (a ring on the mark). + @Published private(set) var isPaused = false + /// Why it is paused: the presence, "locked" or "asleep". + private(set) var pausedBecause: String? + /// The tracks the recorder writes now: "screen-1", "mic-1". + private(set) var tracks: [String] = [] var destination: () -> Destination? = { nil } /// The device's presence: "active", "idle", "locked", "asleep" (`DeviceEvents.presence`). @@ -62,11 +69,26 @@ final class DeviceRecord: ObservableObject { private var uploaderInput: FileHandle? private var uploaderEndedAt = Date.distantPast private var uploadFailure: String? + private var waiting = 0 private var pending = "" private var timer: Timer? func isOn(_ source: RecordSource) -> Bool { on.contains(source) } + /// For `record.state()` on the endpoint (`AppModel` adds the window text). + func state() -> [String: Any] { + var answer: [String: Any] = [ + "sources": Dictionary(uniqueKeysWithValues: Self.switches.map { ($0.source.rawValue, on.contains($0.source)) }), + "recording": isRecording, "paused": isPaused, "line": line, "tracks": tracks, "waitingSlots": waiting, + ] + if let uploadFailure, waiting > 0 { answer["uploadFailure"] = uploadFailure } + if let recordsTo { + answer["mind"] = recordsTo.mind + answer["device"] = recordsTo.name + } + return answer + } + func set(_ source: RecordSource, _ value: Bool) { UserDefaults.standard.set(value, forKey: "record.\(source.rawValue)") if value { on.insert(source) } else { on.remove(source) } @@ -149,11 +171,16 @@ final class DeviceRecord: ObservableObject { if let uploadFailure, waiting > 0 { parts.append("upload failing: \(uploadFailure)") } let line = parts.joined(separator: " ยท ") let isRecording = recorder != nil + let isPaused = isRecording && paused + let tracks = recorder?.trackIDs ?? [] + let pausedBecause = isPaused ? presence() ?? "locked" : nil + // the mark shows these: it changes with them + let markChanges = isRecording != self.isRecording || isPaused != self.isPaused || tracks.count != self.tracks.count || pausedBecause != self.pausedBecause + (self.waiting, self.tracks, self.pausedBecause) = (waiting, tracks, pausedBecause) if line != self.line { self.line = line } - if isRecording != self.isRecording { - self.isRecording = isRecording - onChange() - } + if isRecording != self.isRecording { self.isRecording = isRecording } + if isPaused != self.isPaused { self.isPaused = isPaused } + if markChanges { onChange() } } /// The slots in the spool that wait for the uploader. diff --git a/Sources/VoiceMeetMac/IterateIcon.swift b/Sources/VoiceMeetMac/IterateIcon.swift index 44e9ad9..2950b3a 100644 --- a/Sources/VoiceMeetMac/IterateIcon.swift +++ b/Sources/VoiceMeetMac/IterateIcon.swift @@ -19,27 +19,69 @@ enum IterateIcon { /// A template image: the system draws it light or dark with the menu bar. static let plain = mark(height: menuBarHeight, color: nil) - /// The lend at a glance, as Apple's own items show a state: the plain mark while lent; dimmed - /// while it connects; dimmed with a slash through it while it is not lent (as `wifi.slash` - /// and `mic.slash` do). All are template images, readable in a light and a dark bar. - static let connecting = mark(height: menuBarHeight, color: nil, alpha: 0.4) - static let down = mark(height: menuBarHeight, color: nil, alpha: 0.4, slash: true) - - static func lend(_ glance: LendGlance) -> NSImage { - switch glance { - case .live: return plain - case .connecting: return connecting - case .down: return down + + /// Each ink of the mark (`MenuBarMark.Ink`), with and without the gap where the record's dot + /// sits. The lend at a glance, as Apple's own items show a state: plain while lent; dimmed while + /// it connects; dimmed with a slash through it while it is not lent (as `wifi.slash` and + /// `mic.slash` do), all template images, readable in a light and a dark bar. Red, orange and + /// green are in colour. + private static let marks: [String: NSImage] = { + var marks: [String: NSImage] = [:] + for ink in MenuBarMark.Ink.allCases { + for gap in [false, true] { + let image: NSImage + switch ink { + case .green: image = mark(height: menuBarHeight, color: .systemGreen, dotGap: gap) + case .red: image = mark(height: menuBarHeight, color: .systemRed, dotGap: gap) + case .orange: image = mark(height: menuBarHeight, color: .systemOrange, dotGap: gap) + case .live: image = mark(height: menuBarHeight, color: nil, dotGap: gap) + case .connecting: image = mark(height: menuBarHeight, color: nil, alpha: 0.4, dotGap: gap) + case .down: image = mark(height: menuBarHeight, color: nil, alpha: 0.4, slash: true, dotGap: gap) + } + marks["\(ink.rawValue)\(gap)"] = image + } + } + return marks + }() + + /// The mark's image for `mark`: its ink, with a gap cut where the dot sits when it has one. + /// The dot itself is drawn by `RecordDotView` over the image, so that a template image stays + /// one: the system draws it light or dark with the bar, and the dot stays red. + static func image(_ mark: MenuBarMark) -> NSImage { + marks["\(mark.ink.rawValue)\(mark.dot != .none)"] ?? plain + } + + // MARK: - The record's dot + + /// The dot's centre in the mark's image (y down), and its sizes. It sits at the lower right, + /// beside the foot of the "i", inside the mark's width. + static let dotCenter = NSPoint(x: 9.2, y: 10.6) + static let dotRadius: CGFloat = 2.3 + /// The gap cut out of the mark around the dot, so that the dot reads beside every ink. + private static let dotGapRadius: CGFloat = 3.3 + /// The red of macOS's own recording marks. + static let dotColor = NSColor.systemRed + + /// Draws the dot around `center`, in the current context: filled while it records, a ring while paused. + static func drawDot(_ dot: RecordDot, at center: NSPoint, scale: CGFloat = 1) { + let lineWidth = 1.3 * scale + switch dot { + case .none: + return + case .recording: + let r = dotRadius * scale + dotColor.setFill() + NSBezierPath(ovalIn: NSRect(x: center.x - r, y: center.y - r, width: 2 * r, height: 2 * r)).fill() + case .paused: + let r = dotRadius * scale - lineWidth / 2 + let ring = NSBezierPath(ovalIn: NSRect(x: center.x - r, y: center.y - r, width: 2 * r, height: 2 * r)) + ring.lineWidth = lineWidth + dotColor.setStroke() + ring.stroke() } } - /// While the screen is shared. - static let red = mark(height: menuBarHeight, color: .systemRed) - /// While only the microphone is on. - static let orange = mark(height: menuBarHeight, color: .systemOrange) - /// While a banner shows, a call's among them. - static let green = mark(height: menuBarHeight, color: .systemGreen) - - static func mark(height: CGFloat, color: NSColor?, alpha: CGFloat = 1, slash: Bool = false) -> NSImage { + + static func mark(height: CGFloat, color: NSColor?, alpha: CGFloat = 1, slash: Bool = false, dotGap: Bool = false) -> NSImage { let glyph = glyph() let bounds = glyph.bounds let scale = height / bounds.height @@ -67,6 +109,12 @@ enum IterateIcon { line.lineWidth = 1.3 line.stroke() } + if dotGap { + let r = dotGapRadius + NSGraphicsContext.current?.compositingOperation = .clear + NSBezierPath(ovalIn: NSRect(x: dotCenter.x - r, y: dotCenter.y - r, width: 2 * r, height: 2 * r)).fill() + NSGraphicsContext.current?.compositingOperation = .sourceOver + } return true } image.isTemplate = color == nil @@ -75,39 +123,72 @@ enum IterateIcon { /// Each lend mark on a light and a dark menu bar, as the system tints a template image, 8 times /// its size, one PNG each in `folder` (`--render-icons `), for a look with no Mac bar. + /// And `marks-light.png`, `marks-dark.png`: each ink (a row: plain, dimmed, slashed, red, + /// orange, green) with each record's dot (a column: none, recording, paused). static func render(to folder: URL) -> Bool { let zoom: CGFloat = 8 - let bar = NSSize(width: 40, height: 24) - for glance in LendGlance.allCases { - for (look, background, ink) in [("light", NSColor(white: 0.93, alpha: 1), NSColor.black), ("dark", NSColor(white: 0.16, alpha: 1), NSColor.white)] { - let mark = lend(glance) - guard let bitmap = NSBitmapImageRep( - bitmapDataPlanes: nil, pixelsWide: Int(bar.width * zoom), pixelsHigh: Int(bar.height * zoom), bitsPerSample: 8, - samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0) - else { return false } - NSGraphicsContext.saveGraphicsState() - NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bitmap) - background.setFill() - NSRect(x: 0, y: 0, width: bar.width * zoom, height: bar.height * zoom).fill() - // a template image: its alpha, in the bar's ink - let size = NSSize(width: mark.size.width * zoom, height: mark.size.height * zoom) - let place = NSRect(x: (bar.width * zoom - size.width) / 2, y: (bar.height * zoom - size.height) / 2, width: size.width, height: size.height) - let tinted = NSImage(size: size, flipped: false) { rect in - mark.draw(in: rect) - ink.setFill() - rect.fill(using: .sourceAtop) - return true - } - tinted.draw(in: place) - NSGraphicsContext.restoreGraphicsState() - guard let png = bitmap.representation(using: .png, properties: [:]), - (try? png.write(to: folder.appendingPathComponent("mark-\(glance.rawValue)-\(look).png"))) != nil + let cell = NSSize(width: 40 * zoom, height: 24 * zoom) + let inks: [MenuBarMark.Ink] = [.live, .connecting, .down, .red, .orange, .green] + for (look, background, ink) in [("light", NSColor(white: 0.93, alpha: 1), NSColor.black), ("dark", NSColor(white: 0.16, alpha: 1), NSColor.white)] { + for glance in LendGlance.allCases { + let mark = MenuBarMark.of(bannerShows: false, inUse: false, microphoneOrCameraOn: false, glance: glance, isRecording: false, isPaused: false) + guard + write(size: cell, to: folder.appendingPathComponent("mark-\(glance.rawValue)-\(look).png"), draw: { + background.setFill() + NSRect(origin: .zero, size: cell).fill() + draw(mark, in: NSRect(origin: .zero, size: cell), zoom: zoom, ink: ink) + }) else { return false } } + let sheet = NSSize(width: cell.width * CGFloat(RecordDot.allCases.count), height: cell.height * CGFloat(inks.count)) + guard + write(size: sheet, to: folder.appendingPathComponent("marks-\(look).png"), draw: { + background.setFill() + NSRect(origin: .zero, size: sheet).fill() + for (row, markInk) in inks.enumerated() { + for (column, dot) in RecordDot.allCases.enumerated() { + let place = NSRect(x: CGFloat(column) * cell.width, y: sheet.height - CGFloat(row + 1) * cell.height, width: cell.width, height: cell.height) + draw(MenuBarMark(ink: markInk, dot: dot), in: place, zoom: zoom, ink: ink) + } + } + }) + else { return false } } return true } + /// One mark, centred in `cell`, `zoom` times its size: a template image in the bar's `ink`, a + /// coloured one as it is, then the dot, as `RecordDotView` draws it over the button. + private static func draw(_ mark: MenuBarMark, in cell: NSRect, zoom: CGFloat, ink: NSColor) { + let image = image(mark) + let size = NSSize(width: image.size.width * zoom, height: image.size.height * zoom) + let place = NSRect(x: cell.midX - size.width / 2, y: cell.midY - size.height / 2, width: size.width, height: size.height) + let tinted = NSImage(size: size, flipped: false) { rect in + image.draw(in: rect) + if image.isTemplate { + ink.setFill() + rect.fill(using: .sourceAtop) + } + return true + } + tinted.draw(in: place) + // the image's y runs down; the bitmap's runs up + drawDot(mark.dot, at: NSPoint(x: place.minX + dotCenter.x * zoom, y: place.maxY - dotCenter.y * zoom), scale: zoom) + } + + private static func write(size: NSSize, to file: URL, draw: () -> Void) -> Bool { + guard let bitmap = NSBitmapImageRep( + bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height), bitsPerSample: 8, + samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0) + else { return false } + NSGraphicsContext.saveGraphicsState() + NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: bitmap) + draw() + NSGraphicsContext.restoreGraphicsState() + guard let png = bitmap.representation(using: .png, properties: [:]) else { return false } + return (try? png.write(to: file)) != nil + } + static func glyph() -> NSBezierPath { let glyph = NSBezierPath() for path in paths { @@ -152,3 +233,36 @@ enum IterateIcon { return path } } + +/// The record's dot over the mark in the menu bar (`IterateIcon.drawDot`). A view of its own on +/// the status button, so that the mark's template image stays a template. +final class RecordDotView: NSView { + var dot: RecordDot = .none { + didSet { if dot != oldValue { needsDisplay = true } } + } + + override var isFlipped: Bool { true } + + /// Clicks go to the button under it. + override func hitTest(_ point: NSPoint) -> NSView? { nil } + + override func draw(_ dirtyRect: NSRect) { + IterateIcon.drawDot(dot, at: NSPoint(x: bounds.midX, y: bounds.midY)) + } + + /// Puts the view on `button`, its centre where the dot sits in the mark's image (which the + /// button centres). + static func attach(to button: NSStatusBarButton, imageSize: NSSize) -> RecordDotView { + let view = RecordDotView() + view.translatesAutoresizingMaskIntoConstraints = false + button.addSubview(view) + let side = IterateIcon.dotRadius * 2 + 2 + NSLayoutConstraint.activate([ + view.widthAnchor.constraint(equalToConstant: side), + view.heightAnchor.constraint(equalToConstant: side), + view.centerXAnchor.constraint(equalTo: button.centerXAnchor, constant: IterateIcon.dotCenter.x - imageSize.width / 2), + view.centerYAnchor.constraint(equalTo: button.centerYAnchor, constant: IterateIcon.dotCenter.y - imageSize.height / 2), + ]) + return view + } +} diff --git a/Sources/VoiceMeetMac/Panel.swift b/Sources/VoiceMeetMac/Panel.swift index 3d7ae1d..e5fd09c 100644 --- a/Sources/VoiceMeetMac/Panel.swift +++ b/Sources/VoiceMeetMac/Panel.swift @@ -311,6 +311,8 @@ struct Panel: View { - `await mac.banner.show({ title, body?, buttons?, sound?, seconds? })`: a banner on the Mac's screen, at the top right. `buttons` is one to three `{ label, style?: "accept" | "decline" | "plain", symbol?, open? }` (default one "OK"); a button with `open` opens that page in the browser after its press, and the page must be on this mind's domain. `sound` is "chime" (default), "ring" or "none"; `seconds` is 30 unless said (5 to 120). It answers `{ pressed: "