read text aloud

This commit is contained in:
2026-10-23 09:40:20 +02:00
parent 281f5465a6
commit 92b90c0e13
20 changed files with 471 additions and 18 deletions
@@ -108,10 +108,10 @@ struct ContextMenuOverlay: View {
nonisolated static func tint(for action: ContextAction) -> MenuIconTint {
switch action {
case .copyReference, .copyTerminalSelection, .pasteIntoTerminal: .plain
case .copyReference, .copyTerminalSelection, .pasteIntoTerminal, .readAloudFile, .readAloudText: .plain
case .newFile, .newFolder, .stage: .add
case .copyFileName, .unstage, .revealInFinder: .modify
case .passOnReference, .passOnFileName, .bouwen, .openDiff, .showDiff, .rename: .rename
case .passOnReference, .passOnFileName, .bouwen, .waitWhat, .openDiff, .showDiff, .rename: .rename
case .discard, .delete: .danger
}
}
@@ -120,6 +120,8 @@ struct ContextMenuOverlay: View {
switch action {
case .passOnReference, .passOnFileName: ContextMenuRow.spark
case .bouwen: "hammer"
case .waitWhat: "questionmark.bubble"
case .readAloudFile, .readAloudText: "speaker.wave.2"
case .copyReference, .copyFileName, .copyTerminalSelection: "doc.on.doc"
case .pasteIntoTerminal: "doc.on.clipboard"
case .newFile: "doc.badge.plus"
+5
View File
@@ -129,6 +129,11 @@ public enum Command: Equatable, Sendable {
/// The clean-copy popup's Copy and ⌘↩: copies its text and closes it.
case copyCleanCopy
// MARK: Read aloud
/// The title bar's Stop button.
case stopReading
// MARK: Project and window
/// ⌘O
+9 -1
View File
@@ -132,13 +132,14 @@ private struct PaneToggles: View {
}
/// Search ⌘F · Fluid ⌘L · Hidden ⌘. · Note ⌘N, then `?`. Search and Note are actions; Fluid and Hidden toggle.
/// While Helder reads aloud, a Stop toggle leads the group; switching it off stops the reading.
private struct TitleToolbar: View {
let model: WorkspaceModel
var body: some View {
let layout = model.layout
HStack(spacing: Metrics.toolbarIconGap) {
ButtonGroup([
ButtonGroup(stop + [
GroupItem("Search", keys: "⌘F", kind: .action,
tip: Text("Search the open file, the project and the file names.")) { model.perform(.showSearch(seed: "")) },
GroupItem("Fluid", keys: "⌘L", kind: .toggle(isOn: layout.fluid),
@@ -161,4 +162,11 @@ private struct TitleToolbar: View {
}
.fixedSize()
}
private var stop: [GroupItem] {
guard model.readAloud.isReading else { return [] }
return [GroupItem("Stop", kind: .toggle(isOn: true), tip: Tooltip.stated("Read aloud: ", "playing", ". Click to stop.")) {
model.perform(.stopReading)
}]
}
}
@@ -0,0 +1,32 @@
import Foundation
import HelderCore
import HelderServices
import Observation
/// Read aloud: whether Helder is reading, for the title bar's Stop button.
@Observable
@MainActor
public final class ReadAloudStore {
public private(set) var isReading = false
@ObservationIgnored private let speech: SpeechServicing
public init(speech: SpeechServicing) {
self.speech = speech
speech.onFinish = { [weak self] in self?.isReading = false }
}
/// Starts reading, replacing what is being read; false for blank text, which reads nothing.
@discardableResult
public func read(_ text: String) -> Bool {
guard !text.allSatisfy(\.isWhitespace) else { return false }
speech.speak(MarkdownSpeech.spoken(text))
isReading = true
return true
}
public func stop() {
speech.stop()
isReading = false
}
}
@@ -95,6 +95,14 @@ extension WorkspaceModel {
overlay.present(.passOn(PassOnRequest(reference: name, anchor: anchor)))
case let .bouwen(name):
passToAgent("/bouwen \(name)", toast: "/bouwen \(name)")
case let .waitWhat(kind):
if let text = terminal.selection(kind)?.trimmingCharacters(in: .whitespacesAndNewlines), !text.isEmpty {
passToAgent("/wait-what `\(text)`", toast: "/wait-what")
}
case let .readAloudText(text):
readAloud.read(text)
case let .readAloudFile(path):
readAloudFile(path)
case let .newFile(directory):
overlay.present(.namePrompt(NamePromptRequest(purpose: .newFile(inDirectory: directory), anchor: anchor)))
case let .newFolder(directory):
@@ -129,6 +137,19 @@ extension WorkspaceModel {
}
}
/// The open file is read from its buffer, unsaved edits included; any other file from disk.
private func readAloudFile(_ path: String) {
let live = editor.flatMap { $0.activePath == path && $0.isLoaded(path) ? $0.text(for: path) : nil }
let files = services?.files
track {
var text = live
if text == nil, case let .text(disk)? = try? await files?.readText(path) { text = disk }
guard !Task.isCancelled else { return }
let prose = MarkdownSpeech.plainText(MarkdownParser.parse(text ?? ""))
if !self.readAloud.read(prose) { self.toasts.show("Nothing to read", detail: path) }
}
}
func submitName(_ name: String) {
guard case let .namePrompt(request) = overlay.current else { return }
overlay.dismiss()
@@ -115,7 +115,7 @@ extension WorkspaceModel {
}
/// Stops the watchers and polling, waits for the work still running against this project,
/// then saves the note and the session and ends the terminals.
/// then stops reading aloud, saves the note and the session and ends the terminals.
func closeProject() async {
watcher?.stop()
watcher = nil
@@ -132,6 +132,7 @@ extension WorkspaceModel {
await refresh?.value
await symbols?.value
for task in running { await task.value }
readAloud.stop()
await editor?.flushAutoSaves()
await note?.save()
await editor?.persist()
@@ -79,6 +79,7 @@ public final class WorkspaceModel {
public let layout: LayoutStore
public let overlay: OverlayStore
public let toasts: ToastStore
public let readAloud: ReadAloudStore
public internal(set) var sourceControl: SourceControlStore?
public internal(set) var explorer: ExplorerStore?
public internal(set) var editor: EditorStore?
@@ -114,6 +115,7 @@ public final class WorkspaceModel {
layout = LayoutStore(session: app.session, persistence: app.layouts)
overlay = OverlayStore()
toasts = ToastStore()
readAloud = ReadAloudStore(speech: app.speech)
terminal = TerminalStore()
wireTerminal()
overlay.onChange = { [weak self] old, new in self?.overlayChanged(from: old, to: new) }
@@ -189,6 +191,8 @@ public final class WorkspaceModel {
case .passNote: passNote()
case .copyCleanCopy: copyCleanCopy()
case .stopReading: readAloud.stop()
case .openFolder:
switchProject {
guard let url = await self.app.system.chooseFolder() else { return }
@@ -46,17 +46,19 @@ public struct AppServices {
public var layouts: LayoutPersisting
/// Dialogs, pasteboard, Finder.
public var system: SystemActing
public var speech: SpeechServicing
/// Builds the services for a project folder.
public var makeWorkspace: (URL) -> WorkspaceServices
public var makeWatcher: () -> FileWatching
/// Every store is passed in, so a test can never reach the user's real recents, sessions or layouts.
public init(recentProjects: RecentProjectsStoring, session: SessionStoring, layouts: LayoutPersisting, system: SystemActing,
makeWorkspace: @escaping (URL) -> WorkspaceServices, makeWatcher: @escaping () -> FileWatching) {
speech: SpeechServicing, makeWorkspace: @escaping (URL) -> WorkspaceServices, makeWatcher: @escaping () -> FileWatching) {
self.recentProjects = recentProjects
self.session = session
self.layouts = layouts
self.system = system
self.speech = speech
self.makeWorkspace = makeWorkspace
self.makeWatcher = makeWatcher
}
@@ -66,6 +68,7 @@ public struct AppServices {
session: SessionStore(),
layouts: UserDefaultsLayoutStore(),
system: LiveSystemActions(),
speech: SpeechService(),
makeWorkspace: WorkspaceServices.live(root:),
makeWatcher: { FSEventsWatcher() }
)