read text aloud
This commit is contained in:
@@ -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() }
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user