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
22 changes: 14 additions & 8 deletions Sources/NotebookApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions Sources/NotebookApp/ReadAloud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 }
Expand Down
2 changes: 2 additions & 0 deletions Sources/NotebookApp/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions Sources/NotebookApp/VoiceSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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 {
Expand Down
Loading