知了 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
refactor: the subtitle bar's visibility is one enum instead of five flags
wqugurubdaa29121e8b
diff --git a/Sources/Cicada/Overlay.swift b/Sources/Cicada/Overlay.swiftindex 128af27..3967955 100644--- a/Sources/Cicada/Overlay.swift+++ b/Sources/Cicada/Overlay.swift@@ -36,16 +36,42 @@ final class OverlayController {view.micOn = onview.needsDisplay = true}- private(set) var expanded = true- private(set) var manualHidden = false- private(set) var dimmed = false- private var peeking = false+ /// Where the pointer is while the bar is open.+ enum Pointer: Equatable {+ case away+ /// Passing over: faded so the app underneath stays usable.+ case passing+ /// Rested on it: the bar takes clicks (drag, resize, the mic button) until the pointer leaves.+ case editing+ }++ enum Visibility: Equatable {+ case hidden+ case open(Pointer)+ /// Tucked away to a dot after a quiet spell (自动).+ case tucked+ /// Opened from the dot by hovering it: tucks again once the pointer leaves.+ case peeking+ /// Put away with the toggle shortcut until it is pressed again.+ case manuallyHidden+ }++ private(set) var visibility = Visibility.hidden+ var isShown: Bool { visibility != .hidden }+ var expanded: Bool {+ switch visibility {+ case .open, .peeking, .manuallyHidden: true+ case .hidden, .tucked: false+ }+ }+ var manualHidden: Bool { visibility == .manuallyHidden }+ var dimmed: Bool { visibility == .open(.passing) }+ private var peeking: Bool { visibility == .peeking }private var lastContentAt = Date()private var lastMouse = NSPoint.zeroprivate var stillFor = 0.0private static let pollInterval = 0.05- private(set) var isShown = falseprivate var width: CGFloat = 900private var fontSize: CGFloat = 28private var origin = NSPoint.zero@@ -153,8 +179,6 @@ final class OverlayController {func show() {restore()- isShown = true- manualHidden = false// Auto mode starts tucked away until someone speaks.setExpanded(!mode().auto, animated: false)layout()@@ -169,7 +193,7 @@ final class OverlayController {}func hide() {- isShown = false+ visibility = .hiddenhoverTimer?.invalidate()hoverTimer = nilsetHover(false)@@ -184,17 +208,16 @@ final class OverlayController {}private func setExpanded(_ on: Bool, animated: Bool) {- expanded = onif !on {onCollapsed?()- dimmed = falsestillFor = 0setHover(false)}+ visibility = on ? .open(.away) : .tuckedguard animated else {view.collapsed = !onview.needsDisplay = true- panel.alphaValue = manualHidden ? 0 : 1+ panel.alphaValue = 1return}if on {@@ -211,7 +234,7 @@ final class OverlayController {panel.animator().alphaValue = 0}, completionHandler: { [weak self] inMainActor.assumeIsolated {- guard let self, !self.expanded else { return }+ guard let self, self.visibility == .tucked else { return }self.view.collapsed = trueself.view.needsDisplay = trueNSAnimationContext.runAnimationGroup { ctx in@@ -227,12 +250,11 @@ final class OverlayController {func toggleManual() {guard isShown else { return }if manualHidden || !expanded {- manualHidden = falselastContentAt = Date()setExpanded(true, animated: true)} else {- manualHidden = truesetHover(false)+ visibility = .manuallyHiddenNSAnimationContext.runAnimationGroup { ctx inctx.duration = 0.2panel.animator().alphaValue = 0@@ -336,8 +358,8 @@ final class OverlayController {}private func setDimmed(_ on: Bool) {- guard dimmed != on else { return }- dimmed = on+ guard dimmed != on, case .open = visibility else { return }+ visibility = .open(on ? .passing : .away)NSAnimationContext.runAnimationGroup { ctx inctx.duration = on ? 0.12 : 0.25panel.animator().alphaValue = on ? 0.15 : 1@@ -345,8 +367,7 @@ final class OverlayController {}private func pollHover() {- guard isShown, !manualHidden else { return }- if view.dragMode != nil { return }+ guard isShown, !manualHidden, view.dragMode == nil else { return }let mouse = NSEvent.mouseLocationlet moved = hypot(mouse.x - lastMouse.x, mouse.y - lastMouse.y) > 2lastMouse = mouse@@ -355,14 +376,14 @@ final class OverlayController {// Tucked away: hovering the dot peeks at the last line.if !expanded {if pillFrame.contains(mouse) {- peeking = truesetExpanded(true, animated: true)+ visibility = .peeking}return}let overBar = barFrame.insetBy(dx: -4, dy: -4).contains(mouse)- if view.hover {+ if visibility == .open(.editing) {// Editing: stay until the pointer leaves.if !overBar { setHover(false); stillFor = 0 }} else if overBar && !peeking {@@ -380,21 +401,21 @@ final class OverlayController {if peeking {if !overBar && !pillFrame.contains(mouse) {- peeking = false- if mode().auto { setExpanded(false, animated: true) }+ if mode().auto { setExpanded(false, animated: true) } else { visibility = .open(.away) }}return}// Quiet long enough: tuck away.let m = mode()- if m.auto, !view.hover, !dimmed, view.speaking == nil,+ if m.auto, visibility == .open(.away), view.speaking == nil,Date().timeIntervalSince(lastContentAt) > m.hideAfter {setExpanded(false, animated: true)}}private func setHover(_ on: Bool) {+ if case .open = visibility { visibility = .open(on ? .editing : .away) }view.hover = onpanel.ignoresMouseEvents = !onview.needsDisplay = true