知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.
Swift androidlinuxlive-captionsmacosmeeting-notesmenubar-appsimultaneous-interpretationspeech-recognitionsubtitlesswiftswiftuitranslationwindows
fix: a click on the words of a transcript line plays the recording from there, and the play button comes back when it ends
wquguru7c11a14dc897
diff --git a/Sources/Cicada/ImportViews.swift b/Sources/Cicada/ImportViews.swiftindex 3dce42f..292caa4 100644--- a/Sources/Cicada/ImportViews.swift+++ b/Sources/Cicada/ImportViews.swift@@ -21,6 +21,8 @@ final class RecordingPlayer: ObservableObject {var ownScroll = Date.distantPastprivate var player: AVPlayer?private var observer: Any?+ /// The end of the recording: the time observer stops firing there, so it would still say playing.+ private var ended: NSObjectProtocol?var ready: Bool { player != nil }@@ -40,6 +42,9 @@ final class RecordingPlayer: ObservableObject {}let p = AVPlayer(url: url)player = p+ ended = NotificationCenter.default.addObserver(forName: AVPlayerItem.didPlayToEndTimeNotification, object: p.currentItem, queue: .main) { [weak self] _ in+ MainActor.assumeIsolated { self?.playing = false }+ }observer = p.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 4), queue: .main) { [weak self] t inMainActor.assumeIsolated {guard let self, let p = self.player else { return }@@ -58,6 +63,8 @@ final class RecordingPlayer: ObservableObject {func unload() {if let observer { player?.removeTimeObserver(observer) }+ if let ended { NotificationCenter.default.removeObserver(ended) }+ ended = nilplayer?.pause()observer = nilplayer = nil@@ -84,6 +91,7 @@ final class RecordingPlayer: ObservableObject {func play(from t: Double) {guard let player else { return }+ debugLog("play from \(String(format: "%.1f", t))")seek(t)player.playImmediately(atRate: rate)playing = truediff --git a/Sources/Cicada/Views.swift b/Sources/Cicada/Views.swiftindex 993d771..5d1db79 100644--- a/Sources/Cicada/Views.swift+++ b/Sources/Cicada/Views.swift@@ -840,6 +840,7 @@ struct MainWindowView: View {@State private var editingLine: String?@State private var editingStart = ""@State private var hoveredLine: String?+ @State private var wordClick = WordClick()/// Wide enough for the notes and the bilingual transcript side by side.private var sideBySide: Bool { showQuotes && wideEnough }@@ -891,6 +892,15 @@ struct MainWindowView: View {.onChange(of: store.liveID) { _, _ in loadPlayer() }// Opened afresh: a file opened before the room was mixed in (#19) would play one side only..onChange(of: store.audioFinished) { _, id in if id == current?.id { player.unload(); loadPlayer() } }+ // A click on a line's words: its selectable text takes the mouse before the row's tap.+ .onAppear {+ wordClick.install(line: { hoveredLine.flatMap { id in editingLine == id ? nil : id } }) { id in+ guard let s = current, player.sessionID == s.id, player.ready,+ let l = s.lines.first(where: { $0.id == id }) else { return }+ player.play(from: l.t)+ }+ }+ .onDisappear { wordClick.remove() }.onReceive(NotificationCenter.default.publisher(for: .debugPlay)) { n inif let t = n.object as? Double { player.play(from: t) }}@@ -1818,3 +1828,46 @@ extension Notification.Name {/// Debug `shareSheet:<id>`: the 分享… sheet opens for that record (no id: it closes).static let debugShareSheet = Notification.Name("cicada.debugShareSheet")}++/// A click on the words of a transcript line plays from it, as a click elsewhere on the line does:+/// selectable text takes the mouse before SwiftUI's tap. A drag (selecting) or a double click+/// (a word) does nothing; the time and 改这句… keep their own buttons.+final class WordClick {+ private var monitor: Any?++ func install(line: @escaping () -> String?, play: @escaping (String) -> Void) {+ guard monitor == nil else { return }+ monitor = NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { e in+ guard e.clickCount == 1, let id = line(), Self.onText(e) else { return e }+ // The text tracks the mouse until it is let go, and its mouse-up never reaches a+ // monitor: look once that tracking is over.+ let start = NSEvent.mouseLocation+ Self.whenReleased(within: 1) {+ let now = NSEvent.mouseLocation+ guard hypot(now.x - start.x, now.y - start.y) < 3, line() == id else { return }+ play(id)+ }+ return e+ }+ }++ /// Once the button is up (a click, not a press held to select): checked every 50 ms.+ private static func whenReleased(within seconds: Double, _ action: @escaping () -> Void) {+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {+ if NSEvent.pressedMouseButtons & 1 == 0 { action() } else if seconds > 0.05 { whenReleased(within: seconds - 0.05, action) }+ }+ }++ func remove() {+ if let monitor { NSEvent.removeMonitor(monitor) }+ monitor = nil+ }++ /// The mouse went down on selectable text, not on a button or the row's empty space (which+ /// the row's own tap handles).+ private static func onText(_ e: NSEvent) -> Bool {+ guard let content = e.window?.contentView,+ let hit = content.hitTest(content.convert(e.locationInWindow, from: nil)) else { return false }+ return hit is NSText || String(describing: type(of: hit)).contains("TextField")+ }+}