Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <folder>` 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 <folder>` 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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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-<display>`, `camera-0`, `mic-0`, `speaker-0`) is one file in the mind's files, named by
Expand Down Expand Up @@ -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
```
Expand Down
133 changes: 133 additions & 0 deletions Sources/VoiceMeetCore/RecordRule.swift
Original file line number Diff line number Diff line change
@@ -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<RecordSwitch>, 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<RecordSwitch>, 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
}
}
}
28 changes: 18 additions & 10 deletions Sources/VoiceMeetMac/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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,
Expand Down Expand Up @@ -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)")
}
}

Expand Down
Loading
Loading