diff --git a/Sources/NotebookApp/ContentView.swift b/Sources/NotebookApp/ContentView.swift index 5fb17f2..e1071b6 100644 --- a/Sources/NotebookApp/ContentView.swift +++ b/Sources/NotebookApp/ContentView.swift @@ -1286,7 +1286,6 @@ struct AskBar: View { private func readBack(_ answered: NotebookModel.Answered?) { guard let answered, answered.generated else { return } switch VoiceSettings.readAloud { - case .never: return case .whenSpoken: guard askedByVoice else { return } case .always: break } @@ -1389,14 +1388,21 @@ struct AskBar: View { .buttonStyle(.plain) .help("Clear the question") } - if readAloud.speaking { - Button { readAloud.stop() } label: { - Image(systemName: "speaker.slash.fill").font(.title2) - .foregroundStyle(Palette.inkSecondary) - } - .buttonStyle(.plain) - .help("Stop reading the answer") + // Always here, not only while an answer is being read. It + // appeared only mid-answer, which meant the time to decide + // answers should be silent was after one had started talking. + Button { readAloud.muted.toggle() } label: { + Image(systemName: readAloud.muted ? "speaker.slash.fill" + : "speaker.wave.2.fill") + .font(.title2) + .foregroundStyle(readAloud.speaking ? Palette.accent + : Palette.inkSecondary) + .symbolEffect(.variableColor.iterative, isActive: readAloud.speaking) + .contentTransition(.symbolEffect(.replace)) } + .buttonStyle(.plain) + .help(readAloud.muted ? "Unmute: read answers aloud" + : readAloud.speaking ? "Stop reading and mute" : "Mute answers") // Present while a question is out, only disabled: taking it away // would drop the keyboard monitor with it and build a new one, // microphone state and all, every time a question was asked. diff --git a/Sources/NotebookApp/ReadAloud.swift b/Sources/NotebookApp/ReadAloud.swift index 1a09a16..b94991b 100644 --- a/Sources/NotebookApp/ReadAloud.swift +++ b/Sources/NotebookApp/ReadAloud.swift @@ -16,6 +16,14 @@ final class ReadAloud { private(set) var speaking = false + /// Muted means nothing is read, and whatever is being read stops. + var muted = VoiceSettings.muted { + didSet { + VoiceSettings.muted = muted + if muted { stop() } + } + } + @ObservationIgnored private let synthesizer = AVSpeechSynthesizer() @ObservationIgnored private let watcher = Watcher() @@ -28,6 +36,7 @@ final class ReadAloud { /// Read a turn's answer, citations and markdown removed. func speak(_ turn: NotebookPackage.Turn) { + guard !muted else { return } let text = SpeakableText.from(answer: turn.answer, citations: turn.citations.map(\.citation)) guard !text.isEmpty else { return } diff --git a/Sources/NotebookApp/SettingsView.swift b/Sources/NotebookApp/SettingsView.swift index f55d2b1..e93ec50 100644 --- a/Sources/NotebookApp/SettingsView.swift +++ b/Sources/NotebookApp/SettingsView.swift @@ -201,6 +201,8 @@ struct SettingsView: View { ForEach(VoiceSettings.ReadAloud.allCases) { Text($0.label).tag($0) } } .onChange(of: readAloud) { _, new in VoiceSettings.readAloud = new } + Text("The speaker button beside the Ask field mutes and unmutes.") + .font(.caption).foregroundStyle(.secondary) Picker("Voice", selection: $voiceID) { Text("System default").tag("") ForEach(ReadAloud.voices(), id: \.identifier) { v in diff --git a/Sources/NotebookApp/VoiceSettings.swift b/Sources/NotebookApp/VoiceSettings.swift index 376dd5a..5bcb0b6 100644 --- a/Sources/NotebookApp/VoiceSettings.swift +++ b/Sources/NotebookApp/VoiceSettings.swift @@ -4,18 +4,19 @@ import Foundation /// `GatewaySettings`. enum VoiceSettings { - /// When an answer is read aloud. + /// Which answers are read aloud, when not muted. /// - /// Three states rather than a switch, because the useful default is neither - /// on nor off. Somebody who spoke a question is usually not looking at the - /// screen and wants the answer spoken back; somebody who typed one is - /// reading already, and a voice starting up unasked is an interruption. + /// Somebody who spoke a question is usually not looking at the screen and + /// wants the answer spoken back; somebody who typed one is reading already, + /// and a voice starting up unasked is an interruption. Whether anything is + /// read at all is the speaker button's job, not this setting's: "Never" was + /// a third option here, and a mute somebody could only reach through + /// Settings was a mute nobody reached for mid-answer. enum ReadAloud: String, CaseIterable, Identifiable { - case never, whenSpoken, always + case whenSpoken, always var id: String { rawValue } var label: String { switch self { - case .never: "Never" case .whenSpoken: "When asked by voice" case .always: "Always" } @@ -30,6 +31,19 @@ enum VoiceSettings { set { UserDefaults.standard.set(newValue.rawValue, forKey: "voice.readAloud") } } + /// The speaker button in the Ask bar. Kept across launches: somebody who + /// muted the app in a shared office wants it still muted tomorrow. + /// + /// Anybody who had chosen "Never" before the button existed starts muted, + /// which is what that choice meant. + static var muted: Bool { + get { + if let set = UserDefaults.standard.object(forKey: "voice.muted") as? Bool { return set } + return UserDefaults.standard.string(forKey: "voice.readAloud") == "never" + } + set { UserDefaults.standard.set(newValue, forKey: "voice.muted") } + } + /// Whether holding space in an empty Ask field dictates. On by default: it /// only ever acts where a space would have done nothing useful. static var holdSpace: Bool {