Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

7.2 KB
Raw Blame
import CoreGraphics
import Foundation

enum MeetingOverlayPreference: String, Codable, CaseIterable, Equatable, Sendable {
    case pill
    case captions
}

enum MeetingOverlayPresentation: Equatable, Sendable {
    case pill
    case captions

    init(_ preference: MeetingOverlayPreference) {
        switch preference {
        case .pill:
            self = .pill
        case .captions:
            self = .captions
        }
    }

    var toggled: MeetingOverlayPresentation {
        switch self {
        case .pill:
            return .captions
        case .captions:
            return .pill
        }
    }

    /// Visible surface size for this presentation. Panel geometry and SwiftUI
    /// surface frames must both use this value; it is not the NSPanel frame.
    var visibleSize: CGSize {
        switch self {
        case .pill:
            return CGSize(width: 84, height: 32)
        case .captions:
            return CGSize(width: 420, height: 220)
        }
    }

    /// Canonical bottom footer height within the captions surface. Fixed so the footer never
    /// moves as caption content grows or shrinks.
    static let captionsFooterHeight: CGFloat = 35

    /// Persistent header row: FluidVoice mark, product name, collapse and stop controls.
    static let captionsControlsHeight: CGFloat = 38

    /// Canonical caption text viewport height: the remainder of the captions canvas above the
    /// footer. viewport + footer must equal `captions.visibleSize.height`.
    static let captionsViewportHeight: CGFloat = MeetingOverlayPresentation.captions.visibleSize.height
        - MeetingOverlayPresentation.captionsFooterHeight

    /// Canonical horizontal inset on both sides of the caption text content, inside the 420pt canvas.
    static let captionsHorizontalInset: CGFloat = 16

    /// Canonical inset above the caption text content, inside the caption viewport.
    static let captionsTopInset: CGFloat = 12

    /// Canonical inset below the caption text content, inside the caption viewport.
    static let captionsBottomInset: CGFloat = 7

    /// Caption text content width: the captions canvas width minus both horizontal insets.
    static let captionsContentWidth: CGFloat = MeetingOverlayPresentation.captions.visibleSize.width
        - (2 * MeetingOverlayPresentation.captionsHorizontalInset)

    /// Caption text content height: the viewport height minus the top and bottom insets. The live
    /// renderer selects complete recent lines within this rectangle; the empty state and safety
    /// clip use the same bounds so neither can consume the top or bottom inset.
    static let captionsContentHeight: CGFloat = MeetingOverlayPresentation.captionsViewportHeight
        - MeetingOverlayPresentation.captionsControlsHeight
        - MeetingOverlayPresentation.captionsTopInset
        - MeetingOverlayPresentation.captionsBottomInset
}

struct MeetingOverlayVisibleAnchor: Equatable, Sendable {
    var centerX: CGFloat
    var bottomY: CGFloat
}

struct MeetingOverlayPadding: Equatable, Sendable {
    var top: CGFloat
    var left: CGFloat
    var bottom: CGFloat
    var right: CGFloat

    static func uniform(_ value: CGFloat) -> MeetingOverlayPadding {
        MeetingOverlayPadding(top: value, left: value, bottom: value, right: value)
    }
}

struct MeetingOverlayLayout: Equatable, Sendable {
    var panelFrame: CGRect
    var visibleSurfaceFrame: CGRect
}

enum MeetingOverlayGeometry {
    /// Match bottom dictation placement without changing its position or saved settings.
    static func initialPillAnchor(
        screenFrame: CGRect,
        screenVisible: CGRect,
        bottomOffset: CGFloat
    ) -> MeetingOverlayVisibleAnchor {
        let minY = screenVisible.minY + 10
        let maxY = screenVisible.maxY - MeetingOverlayPresentation.pill.visibleSize.height - 40
        return MeetingOverlayVisibleAnchor(
            centerX: screenFrame.midX,
            bottomY: max(min(screenVisible.minY + bottomOffset, maxY), minY)
        )
    }

    static func layout(
        anchor: MeetingOverlayVisibleAnchor,
        visibleSize: CGSize,
        padding: CGFloat,
        screenVisible: CGRect
    ) -> MeetingOverlayLayout {
        self.layout(
            anchor: anchor,
            visibleSize: visibleSize,
            padding: .uniform(padding),
            screenVisible: screenVisible
        )
    }

    static func layout(
        anchor: MeetingOverlayVisibleAnchor,
        visibleSize: CGSize,
        padding: MeetingOverlayPadding,
        screenVisible: CGRect
    ) -> MeetingOverlayLayout {
        let visible = self.clampedVisibleSurface(
            proposedOrigin: CGPoint(
                x: anchor.centerX - visibleSize.width / 2,
                y: anchor.bottomY
            ),
            proposedSize: visibleSize,
            screenVisible: screenVisible
        )
        let panel = CGRect(
            x: visible.minX - padding.left,
            y: visible.minY - padding.bottom,
            width: visible.width + padding.left + padding.right,
            height: visible.height + padding.top + padding.bottom
        )
        return MeetingOverlayLayout(panelFrame: panel, visibleSurfaceFrame: visible)
    }

    private static func clampedVisibleSurface(
        proposedOrigin: CGPoint,
        proposedSize: CGSize,
        screenVisible: CGRect
    ) -> CGRect {
        let width = min(max(proposedSize.width, 0), max(screenVisible.width, 0))
        let height = min(max(proposedSize.height, 0), max(screenVisible.height, 0))
        let minX = screenVisible.minX
        let maxX = screenVisible.maxX - width
        let minY = screenVisible.minY
        let maxY = screenVisible.maxY - height
        let x = min(max(proposedOrigin.x, minX), max(minX, maxX))
        let y = min(max(proposedOrigin.y, minY), max(minY, maxY))
        return CGRect(x: x, y: y, width: width, height: height)
    }
}

enum MeetingOverlayPresentationEvent: Equatable, Sendable {
    case recordingStarted(sessionID: UUID)
    case recordingStateChanged
    case recordingDegraded
    case recordingStopped
    case preferenceChanged(MeetingOverlayPreference)
    case toggleRequested
}

struct MeetingOverlayPresentationReducer: Equatable, Sendable {
    private(set) var preference: MeetingOverlayPreference
    private(set) var presentation: MeetingOverlayPresentation?
    private(set) var sessionID: UUID?

    init(preference: MeetingOverlayPreference = .pill) {
        self.preference = preference
        self.presentation = nil
        self.sessionID = nil
    }

    mutating func apply(_ event: MeetingOverlayPresentationEvent) {
        switch event {
        case let .recordingStarted(sessionID):
            guard self.sessionID != sessionID else { return }
            self.sessionID = sessionID
            self.presentation = MeetingOverlayPresentation(self.preference)
        case .recordingStateChanged, .recordingDegraded:
            break
        case .recordingStopped:
            self.sessionID = nil
            self.presentation = nil
        case let .preferenceChanged(preference):
            self.preference = preference
        case .toggleRequested:
            guard self.sessionID != nil, let presentation else { return }
            self.presentation = presentation.toggled
        }
    }
}