From 1c78f428dfd66f9197a4172cf9ff2864c241e151 Mon Sep 17 00:00:00 2001 From: "a.sakhatskii" Date: Wed, 18 Nov 2026 11:05:14 +0400 Subject: [PATCH 1/8] Pill geometry: one placement for settled and dragged panels, settle timing PillGeometry.dockedOrigin places a docked panel (anchor + clamp) for both the settled pill and a drag, so a press that has not moved moves nothing (the drag skipped the clamp: the peek dock dropped 11 pt). Drag threshold, grab offset and drag-along helpers, and PillMotion (spring durations + settleDelay), all tested. --- .../KleothCore/Dictation/PillGeometry.swift | 76 ++++++++++ Tests/KleothCoreTests/PillDragTests.swift | 138 ++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 Tests/KleothCoreTests/PillDragTests.swift diff --git a/Sources/KleothCore/Dictation/PillGeometry.swift b/Sources/KleothCore/Dictation/PillGeometry.swift index 9611cf2..95bd8b2 100644 --- a/Sources/KleothCore/Dictation/PillGeometry.swift +++ b/Sources/KleothCore/Dictation/PillGeometry.swift @@ -182,6 +182,51 @@ public enum PillGeometry { } } + /// The origin of a panel docked on `edge` at `along`: centred on the + /// anchor (`dockedCenter` resolved against `referenceSize`, the one size + /// every phase shares so a pill near a corner never creeps), then clamped + /// into `bounds` with its OWN size — which lifts a panel thicker than the + /// reference (the peek dock) off the edge. The settled pill and a drag both + /// place the panel with this, so a press that has not moved yet moves + /// nothing (the drag used to skip the clamp: the dock dropped 11 pt). + public static func dockedOrigin( + edge: Edge, along: CGFloat, panelSize: CGSize, referenceSize: CGSize, shadowPadding: CGFloat, in bounds: CGRect + ) -> CGPoint { + let center = dockedCenter( + edge: edge, along: along, panelSize: referenceSize, shadowPadding: shadowPadding, in: bounds + ) + let origin = CGPoint(x: center.x - panelSize.width / 2, y: center.y - panelSize.height / 2) + return clamp(origin, panelSize: panelSize, in: bounds) + } + + // MARK: Drag + + /// How far (points) a press on the pill must travel before it is a drag. + /// Below it the press is a click — a hand that drifts while clicking Stop + /// must stop the recording, not move the pill. + public static let dragThreshold: CGFloat = 6 + + /// Whether a press that has moved by `translation` is a drag. A + /// non-finite translation is not. + public static func isDrag(translation: CGSize) -> Bool { + let distance = (translation.width * translation.width + translation.height * translation.height).squareRoot() + return distance.isFinite && distance >= dragThreshold + } + + /// Where along `edge` the pointer grabbed a panel centred at + /// `panelCenter`: its offset from that centre on the edge's axis (x on + /// bottom/top, y on the sides). Kept for the whole drag, so the grabbed + /// point stays under the cursor instead of the centre snapping to it. + public static func grabOffset(pointer: CGPoint, panelCenter: CGPoint, edge: Edge) -> CGFloat { + edge.isVertical ? pointer.y - panelCenter.y : pointer.x - panelCenter.x + } + + /// The along-axis coordinate a drag puts the panel's centre at: the + /// pointer's, minus the grab. + public static func dragAlong(pointer: CGPoint, grabOffset: CGFloat, edge: Edge) -> CGFloat { + (edge.isVertical ? pointer.y : pointer.x) - grabOffset + } + /// The along-axis coordinate a saved placement encodes for `edge`. public static func along(for placement: PillPlacement, edge: Edge, in bounds: CGRect) -> CGFloat { edge.isVertical @@ -389,3 +434,34 @@ public enum PillGeometry { return min(max(value, 0), 1) } } + +/// The pill's motion timing (`DictationPillController`): a transition moves +/// the capsule on one spring and reshapes it on another, and the panel +/// SETTLES (shrinks back to the capsule, offset zero) once the last of them +/// has ended. The controller builds its springs from these durations, so the +/// settle can never come before a beat it waits for. +public enum PillMotion { + /// The move spring (a rise out of the edge, a sink, a slide). + public static let moveDuration: TimeInterval = 0.34 + /// The shape spring (size, phase, the tumble between upright and flat). + public static let shapeDuration: TimeInterval = 0.3 + /// A peek: the short hop of the resting pill under the pointer. + public static let peekDuration: TimeInterval = 0.22 + /// The beat between the two springs when one waits for the other (a sink + /// shrinks first, then slides into the edge). + public static let stagger: TimeInterval = 0.06 + + /// How long after a transition starts the panel may settle: the end of + /// its last beat. `staggeredMove` = the move starts a `stagger` late; a + /// `peek` moves on the peek spring (never staggered). Zero when nothing + /// animates. + /// + /// A timer, not SwiftUI's `withAnimation(completion:)`: on the pill that + /// callback did not fire for a move and a reshape started in the same + /// turn (every rise and sink), so the panel stayed a stage — and the + /// dock's surface stayed held — until the next morph. + public static func settleDelay(moves: Bool, reshapes: Bool, staggeredMove: Bool, peek: Bool) -> TimeInterval { + let moveEnd = peek ? peekDuration : (staggeredMove ? stagger : 0) + moveDuration + return max(moves ? moveEnd : 0, reshapes ? shapeDuration : 0) + } +} diff --git a/Tests/KleothCoreTests/PillDragTests.swift b/Tests/KleothCoreTests/PillDragTests.swift new file mode 100644 index 0000000..b4edc91 --- /dev/null +++ b/Tests/KleothCoreTests/PillDragTests.swift @@ -0,0 +1,138 @@ +import Testing +import Foundation +import CoreGraphics +@testable import KleothCore + +/// Dragging the pill and settling its motion (2026-11-18, the user: the drag +/// was "very glitchy", the dock "collapsed and jumped" when dragged, and an +/// active pill sometimes rendered empty). Cocoa screen coordinates, y up. +@Suite struct PillDragTests { + private let shadowPadding: CGFloat = 18 + private let bounds = CGRect(x: 0, y: 0, width: 1600, height: 975) + /// The anchor's reference panel on the bottom edge: the longest shapes' + /// length and the thickest text phase's height (`referenceSize`). + private let reference = CGSize(width: 331, height: 74) + /// The peek dock: THICKER than the reference (its 60 pt capsule + margin). + private let dock = CGSize(width: 331, height: 96) + /// The meeting bar: thinner than the reference. + private let bar = CGSize(width: 296, height: 66) + + // MARK: No jump at drag start + + /// The drag places the panel with the SAME function the settled pill uses, + /// so a press that has not moved yet leaves the panel exactly where it + /// was — the dock used to drop 11 pt on the first drag frame, because the + /// drag skipped the clamp that lifts a panel thicker than the reference. + @Test func dragAtTheSettledSpotMovesNothing() { + for edge in [PillGeometry.Edge.bottom, .top, .left, .right] { + let ref = edge.isVertical ? CGSize(width: reference.height, height: reference.width) : reference + for size in [dock, bar, reference] { + let panel = edge.isVertical ? CGSize(width: size.height, height: size.width) : size + let along: CGFloat = edge.isVertical ? 400 : 700 + let settled = PillGeometry.dockedOrigin( + edge: edge, along: along, panelSize: panel, referenceSize: ref, + shadowPadding: shadowPadding, in: bounds + ) + let frame = CGRect(origin: settled, size: panel) + // The press: anywhere on the panel; the grab is measured + // against its centre, along the edge. + let pointer = CGPoint(x: frame.midX + 23, y: frame.midY - 9) + let grab = PillGeometry.grabOffset(pointer: pointer, panelCenter: CGPoint(x: frame.midX, y: frame.midY), edge: edge) + let dragged = PillGeometry.dockedOrigin( + edge: edge, along: PillGeometry.dragAlong(pointer: pointer, grabOffset: grab, edge: edge), + panelSize: panel, referenceSize: ref, shadowPadding: shadowPadding, in: bounds + ) + #expect(dragged == settled, "edge \(edge) panel \(panel)") + } + } + } + + /// The dock is lifted off the edge by the clamp exactly as when it peeks: + /// its near side `edgeMargin` in, never cut by the screen edge. + @Test func aPanelThickerThanTheReferenceIsClampedInward() { + let origin = PillGeometry.dockedOrigin( + edge: .bottom, along: 800, panelSize: dock, referenceSize: reference, + shadowPadding: shadowPadding, in: bounds + ) + #expect(origin.y == bounds.minY + PillGeometry.edgeMargin) + #expect(origin.x == 800 - dock.width / 2) + // A thinner bar is centred on the anchor, not clamped. + let barOrigin = PillGeometry.dockedOrigin( + edge: .bottom, along: 800, panelSize: bar, referenceSize: reference, + shadowPadding: shadowPadding, in: bounds + ) + let anchor = PillGeometry.dockedCenter(edge: .bottom, along: 800, panelSize: reference, shadowPadding: shadowPadding, in: bounds) + #expect(barOrigin.y + bar.height / 2 == anchor.y) + } + + /// The pill follows the pointer 1:1 along its edge with the grab kept: the + /// point that was grabbed stays under the cursor. + @Test func theGrabbedPointStaysUnderThePointer() { + let start = PillGeometry.dockedOrigin( + edge: .bottom, along: 700, panelSize: bar, referenceSize: reference, + shadowPadding: shadowPadding, in: bounds + ) + let frame = CGRect(origin: start, size: bar) + let press = CGPoint(x: frame.minX + 40, y: frame.midY) + let grab = PillGeometry.grabOffset(pointer: press, panelCenter: CGPoint(x: frame.midX, y: frame.midY), edge: .bottom) + #expect(grab == 40 - bar.width / 2) + for dx: CGFloat in [-200, -13, 0, 9, 250] { + // Orthogonal motion is ignored: the pill stays docked. + let pointer = CGPoint(x: press.x + dx, y: press.y + 70) + let origin = PillGeometry.dockedOrigin( + edge: .bottom, along: PillGeometry.dragAlong(pointer: pointer, grabOffset: grab, edge: .bottom), + panelSize: bar, referenceSize: reference, shadowPadding: shadowPadding, in: bounds + ) + #expect(origin.x == start.x + dx) + #expect(origin.y == start.y) + } + // Side edges slide along y. + #expect(PillGeometry.grabOffset(pointer: CGPoint(x: 5, y: 130), panelCenter: CGPoint(x: 40, y: 100), edge: .left) == 30) + #expect(PillGeometry.dragAlong(pointer: CGPoint(x: 900, y: 300), grabOffset: 30, edge: .right) == 270) + } + + // MARK: Click or drag + + /// A press becomes a drag only past the threshold, in any direction; a + /// hand that drifts a few points while clicking still clicks. + @Test func aPressBecomesADragOnlyPastTheThreshold() { + #expect(!PillGeometry.isDrag(translation: .zero)) + #expect(!PillGeometry.isDrag(translation: CGSize(width: 3, height: -4))) // 5 pt + #expect(PillGeometry.isDrag(translation: CGSize(width: 0, height: PillGeometry.dragThreshold))) + #expect(PillGeometry.isDrag(translation: CGSize(width: -5, height: 5))) // 7.1 pt + #expect(!PillGeometry.isDrag(translation: CGSize(width: CGFloat.nan, height: 40))) + } + + // MARK: Settling + + /// The panel settles once the LAST beat of a transition has ended — from + /// the durations, never from SwiftUI's completion callback, which did not + /// fire for a rise or a sink (the panel then stayed a stage for the whole + /// meeting, and the dock's surface stayed under the bar). + @Test func settleWaitsForTheLastBeat() { + let move = PillMotion.moveDuration, shape = PillMotion.shapeDuration + // Rise: move and shape together. + #expect(PillMotion.settleDelay(moves: true, reshapes: true, staggeredMove: false, peek: false) == max(move, shape)) + // Sink: shape first, the move a stagger later. + #expect(PillMotion.settleDelay(moves: true, reshapes: true, staggeredMove: true, peek: false) == PillMotion.stagger + move) + // Morph in place. + #expect(PillMotion.settleDelay(moves: false, reshapes: true, staggeredMove: false, peek: false) == shape) + // A peek: the quick hop, but never before the shape spring is done. + #expect(PillMotion.settleDelay(moves: true, reshapes: true, staggeredMove: false, peek: true) == max(PillMotion.peekDuration, shape)) + #expect(PillMotion.settleDelay(moves: true, reshapes: false, staggeredMove: false, peek: true) == PillMotion.peekDuration) + // Nothing to animate: settle now. + #expect(PillMotion.settleDelay(moves: false, reshapes: false, staggeredMove: false, peek: false) == 0) + // Every answer covers every beat it runs. + for moves in [false, true] { + for reshapes in [false, true] { + for staggered in [false, true] { + for peek in [false, true] { + let delay = PillMotion.settleDelay(moves: moves, reshapes: reshapes, staggeredMove: staggered, peek: peek) + if reshapes { #expect(delay >= shape) } + if moves { #expect(delay >= (peek ? PillMotion.peekDuration : move)) } + } + } + } + } + } +} From e1fe8278cd7b4885194e54a5c99655245d69f802 Mon Sep 17 00:00:00 2001 From: "a.sakhatskii" Date: Wed, 18 Nov 2026 11:05:21 +0400 Subject: [PATCH 2/8] Pill: drag from anywhere, keep its state while dragged, never render empty - Every point of the capsule but a real button drags past a 6 pt threshold; a press that does not travel is a click. The peek dock's fields were Buttons, so the dock could not be dragged at all and the pointer slid off it. - A drag keeps the pill's phase, size and peek until the drop: requests that arrive meanwhile (a dictation ending, a meeting starting) land at the drop; a hover exit no longer collapses the dock mid-drag. - Phase changes during a drag drew the capsule outside its panel (empty or half-cut) and the settle threw the panel back to where the drag began. - The panel settles on a timer (PillMotion): SwiftUI's completion never fired for a rise or a sink, so the panel stayed a stage with the dock's surface held. - settle() lands a phase still waiting for its turn instead of dropping it. - A start from the dock morphs instead of rising, whose hidden content showed a dock-sized empty capsule. - Open-hand cursor on the body that drags, closed while dragging. --- .../DictationPillController.swift | 389 ++++++++++++------ .../KleothPillUI/DictationPillModel.swift | 93 +++++ .../KleothPillUI/DictationPillView.swift | 342 ++++++++------- 3 files changed, 545 insertions(+), 279 deletions(-) diff --git a/app/Sources/KleothPillUI/DictationPillController.swift b/app/Sources/KleothPillUI/DictationPillController.swift index 3518bf2..e91925f 100644 --- a/app/Sources/KleothPillUI/DictationPillController.swift +++ b/app/Sources/KleothPillUI/DictationPillController.swift @@ -53,15 +53,14 @@ public final class DictationPillController: DictationPillPresenting { /// lands during the stagger as anticipation. /// Tuned short (was 0.55 / 0.5 s): the user reads anything slower as lag /// between the key press and the pill. - static let moveSpring: Animation = .spring(duration: 0.34, bounce: 0.25) - static let shapeSpring: Animation = .spring(duration: 0.3, bounce: 0.18) + /// The durations live in `PillMotion` (KleothCore), which also decides + /// when a transition has ended and the panel settles. + static let moveSpring: Animation = .spring(duration: PillMotion.moveDuration, bounce: 0.25) + static let shapeSpring: Animation = .spring(duration: PillMotion.shapeDuration, bounce: 0.18) /// A peek (hover, or the chord's first frame) is a short hop out of the /// edge and back: quicker and tighter than a rise. - static let peekSpring: Animation = .spring(duration: 0.22, bounce: 0.2) - static let stagger: TimeInterval = 0.06 - /// Nominal durations of the two springs, for picking the beat that ends last. - static let moveDuration: TimeInterval = 0.34 - static let shapeDuration: TimeInterval = 0.3 + static let peekSpring: Animation = .spring(duration: PillMotion.peekDuration, bounce: 0.2) + static let stagger: TimeInterval = PillMotion.stagger /// What the SwiftUI content renders. let model = DictationPillModel() @@ -75,7 +74,7 @@ public final class DictationPillController: DictationPillPresenting { private(set) var menuOpen = false /// Peeking or the menu is open: an offer would pull the dock from under /// the pointer, so the meeting side waits (meetings design §3.2.3). - public var isInteracting: Bool { peeking || menuOpen } + public var isInteracting: Bool { peeking || menuOpen || isDragging } /// The pointer is on the CAPSULE (any phase): an offer's lifetime waits. /// Answered from geometry, like `closeMenu`'s and `reconsiderPointer`'s /// re-checks — never from `model.hovered` alone: the tracking area sends no @@ -114,11 +113,20 @@ public final class DictationPillController: DictationPillPresenting { /// The rect the panel settles to when the in-flight transition completes; /// nil when the panel is settled (frame == capsule rect, offset == zero). private var pendingFrame: CGRect? - /// The phase an in-flight transition has yet to apply: `transition` puts - /// `model.phase` on the NEXT main-queue turn, so for that turn the model - /// still shows the old phase. nil once applied, or when the transition was - /// superseded by a settle or a hide (its phase will never land). - private var pendingPhase: DictationPillState? + /// The shape an in-flight transition has yet to apply: `transition` puts + /// `model.phase` (with its capsule size and flatness) on the NEXT + /// main-queue turn, so for that turn the model still shows the old phase. + /// nil once applied — by the transition's turn, or by a `settle()` that + /// came first (a drag starting in that turn: the phase lands at once + /// instead of never) — or when a hide superseded it. + private var pendingShape: PendingShape? + private struct PendingShape { + var phase: DictationPillState + var capsule: CGSize + var flat: Bool + } + /// The phase `pendingShape` carries. + private var pendingPhase: DictationPillState? { pendingShape?.phase } /// Bumped by every transition, settle, and hide so a stale spring /// completion can never shrink the panel to a rect that is no longer the /// destination. @@ -127,6 +135,34 @@ public final class DictationPillController: DictationPillPresenting { /// anchor center, so a drag does not snap the pill's center to the cursor. /// Reset to zero when a drag re-docks to an edge with the other axis. private var dragGrabOffset: CGFloat = 0 + /// A drag is in progress (`beginDrag` … `commitDraggedPlacement`). The + /// pill keeps the phase, size and peek it was grabbed in until the drop: + /// a transition started under a drag springs a STAGE that the drag then + /// overwrote with the dragged frame — the capsule, still offset on that + /// stage, was drawn outside its own panel (an empty or half-cut pill), and + /// the spring's settle then threw the panel back to where the drag began. + /// And a hover exit no longer collapses the dock out from under the drag. + private(set) var isDragging = false + /// What the pill was asked to do while dragged — the LATEST request wins + /// (as it would have without the drag) and lands at the drop. + private var deferredDuringDrag: DeferredRequest? + private enum DeferredRequest { + case show(DictationPillState) + case hide + } + /// The phase the pill is heading for: one asked for during a drag, else + /// the one a transition has yet to apply, else what it shows. Every "is + /// the pill resting?" decision reads this — `model.phase` alone answered + /// for a phase that was already being replaced. + private var upcomingPhase: DictationPillState { deferredPhase ?? pendingPhase ?? model.phase } + /// The phase a drag is holding for the drop, if any. + private var deferredPhase: DictationPillState? { + switch deferredDuringDrag { + case .show(let state): return state + case .hide: return .hidden + case nil: return nil + } + } public init(defaults: UserDefaults = .standard) { self.defaults = defaults @@ -146,6 +182,16 @@ public final class DictationPillController: DictationPillPresenting { public func show(_ state: DictationPillState) { hideTask?.cancel() hideTask = nil + endDragIfReleased() + if isDragging { + // Held for the drop (see `isDragging`). A `.hidden` still goes + // through `dismiss()`, whose backdrop decides — and is held too. + if state == .hidden { dismiss() } else { deferredDuringDrag = .show(state) } + return + } + // The dock is out under the pointer: whatever replaces it starts from + // a capsule that is already fully on screen (a start from the pill). + let fromDock = state != .idle && model.phase == .idle && (peeking || menuOpen) if state != .idle, peeking { peekTask?.cancel() peeking = false @@ -173,7 +219,7 @@ public final class DictationPillController: DictationPillPresenting { model.apply(labelWidth: layout.labelWidth) if alreadyUp { - transition(to: target, phase: state, capsule: layout.capsuleSize, flat: layout.flat) + transition(to: target, phase: state, capsule: layout.capsuleSize, flat: layout.flat, fromDock: fromDock) } else { // A fresh show starts from the tucked spot and emerges, so even the // first pill of the day comes in from the edge rather than popping. @@ -260,16 +306,18 @@ public final class DictationPillController: DictationPillPresenting { public func setBackdrop(_ newBackdrop: DictationPillBackdrop) { guard backdrop != newBackdrop else { return } backdrop = newBackdrop - guard Self.isRestingFamily(pendingPhase ?? model.phase) else { return } + guard Self.isRestingFamily(upcomingPhase) else { return } if let state = newBackdrop.state { show(state) - } else if model.phase != .hidden { + } else if upcomingPhase != .hidden { hideCompletely() } } - /// What the pill is showing right now. - public var currentState: DictationPillState { dismissingState ?? model.phase } + /// What the pill is showing right now — or, during a drag, what it will + /// show at the drop (`isDragging`): the coordinator decides what to + /// dismiss from this, and a phase held for the drop is already its owner's. + public var currentState: DictationPillState { dismissingState ?? deferredPhase ?? model.phase } private var dismissingState: DictationPillState? /// What the pill shows once the transition in flight lands: `currentState`, @@ -277,7 +325,8 @@ public final class DictationPillController: DictationPillPresenting { /// counts (`model.phase` changes a turn later on a panel that is up) — the /// coordinator's "is a dictation phase live?" input. During a ✕ it is still /// the dismissed phase, like `currentState`. - public var upcomingState: DictationPillState { dismissingState ?? pendingPhase ?? model.phase } + /// A phase asked for during a drag counts too: it lands at the drop. + public var upcomingState: DictationPillState { dismissingState ?? upcomingPhase } public func setResting(_ visible: Bool) { setBackdrop(visible ? .idle : .hidden) @@ -311,7 +360,7 @@ public final class DictationPillController: DictationPillPresenting { // land a turn later with nothing left to take it down. Showing the // backdrop supersedes that transition (the generation check), so the // phase never appears. - if model.phase == state, pendingPhase == nil || pendingPhase == state, + if model.phase == state, upcomingPhase == state, panel?.isVisible == true, model.isPresented { return } show(state) } @@ -347,6 +396,11 @@ public final class DictationPillController: DictationPillPresenting { } private func hideCompletely() { + endDragIfReleased() + if isDragging { + deferredDuringDrag = .hide + return + } guard let panel, panel.isVisible else { model.isPresented = false model.apply(phase: .hidden) @@ -373,7 +427,8 @@ public final class DictationPillController: DictationPillPresenting { /// "Reset pill position". public func resetPosition() { defaults.removeObject(forKey: Self.placementDefaultsKey) - guard let panel, panel.isVisible, let screen = defaultScreen() else { return } + // A drag in progress writes its own placement at the drop. + guard !isDragging, let panel, panel.isVisible, let screen = defaultScreen() else { return } currentDisplayId = screen.kleothDisplayId let edge = dockEdge(on: screen) let layout = layout(for: model.phase, edge: edge, on: screen) @@ -385,35 +440,48 @@ public final class DictationPillController: DictationPillPresenting { // MARK: View-facing API (DictationPillView) - /// Called once at drag start: finishes any in-flight transition (so the - /// panel is exactly the capsule and the drag moves what the user sees) and - /// remembers where along the edge the pointer grabbed it. + /// Called once, when a press on the pill has travelled past + /// `PillGeometry.dragThreshold`: finishes any in-flight transition (so the + /// panel is exactly the capsule and the drag moves what the user sees — + /// a phase still waiting for its turn lands now, it is never dropped) and + /// remembers where along the edge the pointer grabbed it. From here to + /// the drop the pill keeps its phase, size and peek (`isDragging`). /// - /// The grab is measured against the PANEL's own centre rather than - /// `anchorCenter`: settled, the two are the same for an upright phase, and + /// The grab is measured against the PANEL's own centre rather than the + /// anchor's: settled, the two are the same for an upright phase, and /// for a flat recording bar on a side edge the panel is the only thing /// that knows where the along-axis clamp actually put it. func beginDrag() { + guard !isDragging else { return } closeMenu() settle() + isDragging = true + deferredDuringDrag = nil + model.apply(dragging: true) + // A collapse already counting down (the pointer left the dock just + // before the press moved) must not fire under the drag. + peekTask?.cancel() + peekTask = nil guard let panel else { return } - let mouse = NSEvent.mouseLocation - let center = CGPoint(x: panel.frame.midX, y: panel.frame.midY) - dragGrabOffset = model.edge.isVertical ? mouse.y - center.y : mouse.x - center.x + dragGrabOffset = PillGeometry.grabOffset( + pointer: dragPointerLocation, panelCenter: CGPoint(x: panel.frame.midX, y: panel.frame.midY), edge: model.edge + ) } /// Live drag, driven by `NSEvent.mouseLocation` (never /// `DragGesture.Value.translation`, which double-counts as the window /// moves under the cursor). The pill slides ALONG its edge only: the /// pointer's along-axis coordinate (minus the grab offset) becomes the - /// anchor's, the orthogonal one is the edge's fixed inset. A resting pill - /// pops fully on screen while dragged and tucks back on release. Dragging - /// clearly toward another edge — past the corner diagonal by - /// `PillGeometry.redockHysteresis` — re-docks there, re-laying the panel - /// out on the spot (a side edge stands it up). + /// anchor's, the orthogonal one is the edge's fixed inset — placed by + /// `activeOrigin`, the function the settled pill uses, so the first frame + /// of a drag moves nothing. A resting pill pops fully on screen while + /// dragged and tucks back on release. Dragging clearly toward another + /// edge — past the corner diagonal by `PillGeometry.redockHysteresis` — + /// re-docks there, re-laying the panel out on the spot (a side edge + /// stands it up). func dragMoved() { - guard let panel else { return } - let mouse = NSEvent.mouseLocation + guard isDragging, let panel else { return } + let mouse = dragPointerLocation guard let screen = NSScreen.screens.first(where: { $0.frame.contains(mouse) }) ?? panelScreen() ?? defaultScreen() else { return } let edge = PillGeometry.dragEdge(current: model.edge, pointer: mouse, in: screen.frame) @@ -426,25 +494,35 @@ public final class DictationPillController: DictationPillPresenting { model.apply(edge: edge) model.apply(labelWidth: layout.labelWidth) } - let along = (edge.isVertical ? mouse.y : mouse.x) - dragGrabOffset - let size = layout.panelSize - let center = PillGeometry.dockedCenter( - edge: edge, along: along, - panelSize: Self.dockReferenceSize(panelSize: size, edge: edge, flat: layout.flat, dock: model.dock, on: screen), - shadowPadding: Self.shadowPadding, in: Self.bounds(of: screen) - ) - panel.setFrame( - CGRect(x: center.x - size.width / 2, y: center.y - size.height / 2, width: size.width, height: size.height), - display: true + let origin = activeOrigin( + panelSize: layout.panelSize, edge: edge, flat: layout.flat, on: screen, + along: PillGeometry.dragAlong(pointer: mouse, grabOffset: dragGrabOffset, edge: edge) ) + var still = Transaction() + still.disablesAnimations = true + withTransaction(still) { + panel.setFrame(CGRect(origin: origin, size: layout.panelSize), display: true) + } } - /// Persists where the user dropped the pill: the edge it is docked on and - /// its fraction along it (see `PillPlacement`). The pill then springs to - /// its phase's spot — tucked into the edge if resting. + /// The drop: persists where the user left the pill — the edge it is + /// docked on and its fraction along it (see `PillPlacement`) — then lets + /// the pill go on from the new spot: to whatever it was asked to show + /// meanwhile (`deferredDuringDrag`), else to its own phase's spot, tucked + /// into the edge if resting. The pointer is read again last: a dock + /// dropped with the pointer off it collapses (after `peekLinger`) at its + /// NEW place, not halfway through the drag. func commitDraggedPlacement() { - guard let panel else { return } - guard let screen = panelScreen() ?? defaultScreen() else { return } + guard isDragging else { return } + isDragging = false + model.apply(dragging: false) + let deferred = deferredDuringDrag + deferredDuringDrag = nil + defer { reconsiderPointerAfterDrag() } + guard let panel, let screen = panelScreen() ?? defaultScreen() else { + replay(deferred) + return + } currentDisplayId = screen.kleothDisplayId // The dragged frame is the phase's panel centered on the anchor, so // its center fractions ARE the anchor's. @@ -459,6 +537,10 @@ public final class DictationPillController: DictationPillPresenting { if let data = try? JSONEncoder().encode(placement) { defaults.set(data, forKey: Self.placementDefaultsKey) } + guard deferred == nil else { + replay(deferred) + return + } let edge = dockEdge(on: screen) let layout = layout(for: model.phase, edge: edge, on: screen) let origin = origin(for: model.phase, panelSize: layout.panelSize, edge: edge, on: screen) @@ -467,6 +549,41 @@ public final class DictationPillController: DictationPillPresenting { transition(to: CGRect(origin: origin, size: layout.panelSize), phase: model.phase, capsule: layout.capsuleSize, flat: layout.flat) } + /// A drag whose mouse-up never reached the gesture (its `onEnded` did not + /// run) must not hold the pill forever: once the left button is up, it is + /// a drop. Checked wherever a held request arrives. Not in film mode — the + /// sandbox's presses are synthesized, the real button is up throughout. + @discardableResult + private func endDragIfReleased() -> Bool { + guard isDragging, !ignoresRealPointer, NSEvent.pressedMouseButtons & 1 == 0 else { return false } + commitDraggedPlacement() + return true + } + + /// What a drag held back, now: a phase springs from the drop spot to its + /// place at the new anchor; a hide fades the pill out there. + private func replay(_ request: DeferredRequest?) { + switch request { + case .show(let state): show(state) + case .hide: hideCompletely() + case nil: break + } + } + + /// After a drop, the hover the drag kept still: an exit delivered + /// mid-drag only updated `hovered`, so the peek decision is made here. + private func reconsiderPointerAfterDrag() { + let inside: Bool + if ignoresRealPointer { + inside = model.hovered + } else if let panel, panel.isVisible { + inside = (pendingFrame ?? panel.frame).contains(NSEvent.mouseLocation) + } else { + inside = false + } + handleHover(inside) + } + /// The pill's action button. The handler lives in `DictationController`; /// note for whoever writes it: the rest of the app opens Settings through /// SwiftUI's `@Environment(\.openSettings)` (MenuView.swift), but the pill is @@ -522,9 +639,13 @@ public final class DictationPillController: DictationPillPresenting { guard let self, !self.ignoresRealPointer else { return } self.handleHover(hovering) } - hosting.onPointerMove = { [weak self] point in + hosting.onPointerMove = { [weak self, weak hosting] point in guard let self, !self.ignoresRealPointer else { return } self.model.apply(pointer: point) + // Re-asserted on every move, not only when the answer changes: + // anything else that set the cursor meanwhile (the menu's rows, + // another app as the pointer crossed it) is put right at once. + if point != nil, let hosting { self.model.cursor(rootSize: hosting.bounds.size).set() } } hosting.onSecondaryClick = { [weak self] in self?.openMenu() } // The hosting view is a SUBVIEW of a plain container — never the @@ -598,6 +719,19 @@ public final class DictationPillController: DictationPillPresenting { /// wandering over the pill while the film runs (it tucked a timed film). public var ignoresRealPointer = false + /// Film mode: where the scripted pointer is, in screen coordinates — what + /// a drag reads instead of `NSEvent.mouseLocation`, so the sandbox can + /// film a real drag (synthesized mouse events through the pill's own + /// window) without moving the user's cursor. Ignored unless + /// `ignoresRealPointer` is set. + public var filmPointerLocation: CGPoint? + + /// The pointer a drag follows: the real one, or the film's. + private var dragPointerLocation: CGPoint { + if ignoresRealPointer, let filmPointerLocation { return filmPointerLocation } + return NSEvent.mouseLocation + } + /// What the tracking area reports, exposed for the sandbox's film mode /// (there is no pointer to move in a headless run). public func setHovered(_ hovering: Bool) { @@ -614,6 +748,9 @@ public final class DictationPillController: DictationPillPresenting { private func handleHover(_ hovering: Bool) { + // A drag that ended unseen is dropped first; the drop reads the + // pointer again itself. + if endDragIfReleased() { return } peekTask?.cancel() peekTask = nil model.apply(hovered: hovering) @@ -622,7 +759,11 @@ public final class DictationPillController: DictationPillPresenting { // for `fadeOutDuration`, and `closeMenu()`'s pointer re-check lands // inside that window — peeking it would `show(.idle)` and cancel the // hide. A pill on its way out is never pulled back by the pointer. - guard model.phase == .idle, !menuOpen, backdrop == .idle else { return } + // `upcomingPhase`, not `model.phase`: for a turn after a `show` the + // model still says `.idle`, and a hover then peeked the dock right + // over a dictation's `.listening` — superseding it. A drag keeps the + // peek as it is; the drop decides (`reconsiderPointerAfterDrag`). + guard upcomingPhase == .idle, !menuOpen, backdrop == .idle, !isDragging else { return } if hovering { setPeeking(true) } else { @@ -641,7 +782,7 @@ public final class DictationPillController: DictationPillPresenting { // The dock's surface comes out with it — and leaves only once the // collapse has settled (`releaseDockSurface`), never here. if on { model.apply(dockHeld: true) } - if model.phase == .idle, panel?.isVisible == true { show(.idle) } + if upcomingPhase == .idle, panel?.isVisible == true { show(.idle) } } /// Re-reads the REAL pointer once a phase has switched to `.idle` under it. @@ -651,7 +792,7 @@ public final class DictationPillController: DictationPillPresenting { /// dismissed fault — got no `mouseEntered` and stayed tucked until the /// pointer left and came back. Now it peeks straight out of the collapse. private func reconsiderPointer() { - guard !ignoresRealPointer, !peeking, !menuOpen, model.phase == .idle, + guard !ignoresRealPointer, !peeking, !menuOpen, !isDragging, model.phase == .idle, upcomingPhase == .idle, let panel, panel.isVisible else { return } let frame = pendingFrame ?? panel.frame if frame.contains(NSEvent.mouseLocation) { handleHover(true) } @@ -710,7 +851,7 @@ public final class DictationPillController: DictationPillPresenting { model.apply(menuOpen: true) peekTask?.cancel() peekTask = nil - if model.phase == .idle { setPeeking(true) } + if upcomingPhase == .idle { setPeeking(true) } let menuPanel = ensureMenuPanel() layoutMenu() @@ -959,7 +1100,7 @@ public final class DictationPillController: DictationPillPresenting { if let data = try? JSONEncoder().encode(placement) { defaults.set(data, forKey: Self.placementDefaultsKey) } - guard let panel, panel.isVisible else { return } + guard !isDragging, let panel, panel.isVisible else { return } currentDisplayId = screen.kleothDisplayId let layout = layout(for: model.phase, edge: edge, on: screen) let origin = origin(for: model.phase, panelSize: layout.panelSize, edge: edge, on: screen) @@ -1048,7 +1189,7 @@ public final class DictationPillController: DictationPillPresenting { peekTask = nil peeking = false model.apply(peeking: false) - pendingPhase = nil + pendingShape = nil // A backdrop that rose while the panel faded out was only stored (the // old phase was still up); it takes over now, so a capture that // started in the fade never ends up with no bar (hot-mic rule). @@ -1069,14 +1210,23 @@ public final class DictationPillController: DictationPillPresenting { /// Moves the capsule to `target` (a panel rect: capsule + shadow margin) /// and switches it to `phase`, on one spring. See the type comment for the /// stage technique. Under Reduce Motion the panel simply jumps. - private func transition(to target: CGRect, phase: DictationPillState, capsule: CGSize, flat: Bool) { + /// + /// `fromDock`: the change replaces the peeked dock (a start from the + /// pill's own fields or menu). That capsule is already out of the edge, so + /// it MORPHS — never the `.rise`, whose content stays hidden until the + /// capsule "has left the edge": from the dock that was a dock-sized empty + /// capsule, held for as long as the main thread was busy starting the + /// capture (the user's "empty pill when starting from the pill"). + private func transition( + to target: CGRect, phase: DictationPillState, capsule: CGSize, flat: Bool, fromDock: Bool = false + ) { guard let panel else { return } transitionGeneration += 1 let generation = transitionGeneration guard !Self.reduceMotion else { pendingFrame = nil - pendingPhase = nil + pendingShape = nil var still = Transaction() still.disablesAnimations = true withTransaction(still) { @@ -1123,7 +1273,7 @@ public final class DictationPillController: DictationPillPresenting { } let destination = Self.offset(ofCenter: CGPoint(x: target.midX, y: target.midY), in: stage) - pendingPhase = phase + pendingShape = PendingShape(phase: phase, capsule: capsule, flat: flat) // The animated changes go out on the NEXT main-queue callout so // SwiftUI has committed the re-expressed start position first; a // change in the same turn would spring from the previous graph value, @@ -1131,7 +1281,7 @@ public final class DictationPillController: DictationPillPresenting { Task { @MainActor [weak self] in guard let self, self.transitionGeneration == generation else { return } // Applied below in this same callout (or already the model's). - self.pendingPhase = nil + self.pendingShape = nil let moves = self.model.offset != destination let reshapes = self.model.phase != phase || self.model.capsuleSize != capsule || self.model.flat != flat @@ -1142,7 +1292,7 @@ public final class DictationPillController: DictationPillPresenting { // The choreography cue for the view (squash-and-stretch along the // travel axis + content reveal) — fired before the springs so its // keyframes start on the same frame. - let wasResting = self.model.phase == .idle + let wasResting = self.model.phase == .idle && !fromDock let cue: MotionBeat.Kind = moves ? (phase == .idle ? (wasResting ? .peek : .sink) : (wasResting ? (phase == .armed ? .peek : .rise) : .morph)) @@ -1151,19 +1301,12 @@ public final class DictationPillController: DictationPillPresenting { // Sinking to rest: shape first, then move (it shrinks, then // slips into the edge). Rising: move and shape start together — // the size is explicit now, so the capsule visibly grows out of - // the edge instead of popping. The completion rides whichever - // beat ends last — and only a beat that actually changes state, - // since a no-op body completes immediately and would settle - // mid-flight. + // the edge instead of popping. let shapeFirst = phase == .idle + let staggeredMove = shapeFirst && cue != .peek let moveAnimation = cue == .peek ? Self.peekSpring - : (shapeFirst ? Self.moveSpring.delay(Self.stagger) : Self.moveSpring) + : (staggeredMove ? Self.moveSpring.delay(Self.stagger) : Self.moveSpring) let shapeAnimation = Self.shapeSpring - let completeOnMove = moves && (shapeFirst || !reshapes || (Self.moveDuration >= Self.shapeDuration)) - let settleWhenDone: () -> Void = { [weak self] in - guard let self, self.transitionGeneration == generation else { return } - self.settle() - } let reshape = { self.model.apply(phase: phase) self.model.apply(capsuleSize: capsule) @@ -1173,22 +1316,23 @@ public final class DictationPillController: DictationPillPresenting { self.model.apply(flat: flat) } if moves { - if completeOnMove { - withAnimation(moveAnimation, completionCriteria: .logicallyComplete) { - self.model.apply(offset: destination) - } completion: { settleWhenDone() } - } else { - withAnimation(moveAnimation) { self.model.apply(offset: destination) } - } + withAnimation(moveAnimation) { self.model.apply(offset: destination) } } if reshapes { - if completeOnMove { - withAnimation(shapeAnimation) { reshape() } - } else { - withAnimation(shapeAnimation, completionCriteria: .logicallyComplete) { - reshape() - } completion: { settleWhenDone() } - } + withAnimation(shapeAnimation) { reshape() } + } + // Settle once the beat that ends last has ended (`PillMotion`). + // Not `withAnimation(_:completionCriteria:_:completion:)`: with a + // move and a reshape started in this same turn — every rise and + // sink — that completion never fired, so the panel stayed a stage + // (and the dock's surface stayed held) until the next morph. + let delay = PillMotion.settleDelay( + moves: moves, reshapes: reshapes, staggeredMove: staggeredMove, peek: cue == .peek + ) + Task { @MainActor [weak self] in + try? await Task.sleep(for: .seconds(delay)) + guard let self, self.transitionGeneration == generation else { return } + self.settle() } // The phase is `.idle` from here on; a pointer parked on the pill // turns the sink into a peek (a re-targeted transition). @@ -1199,13 +1343,25 @@ public final class DictationPillController: DictationPillPresenting { /// Ends any in-flight transition NOW: panel = destination rect, offset = /// zero, both without animation and in the same turn, so the screen does /// not change. Safe to call when already settled. + /// + /// A transition still waiting for its turn is cancelled by the bump — but + /// its SHAPE lands here, at once: dropping it (as this did) left the panel + /// sized for the new phase around the old one (a drag starting the turn + /// after a `show(.listening)` kept a capsule-less dock in a listening- + /// sized panel while the dictation ran — an empty pill). private func settle() { transitionGeneration += 1 - // A transition still waiting for its turn is cancelled by the bump. - pendingPhase = nil - releaseDockSurface() var still = Transaction() still.disablesAnimations = true + if let shape = pendingShape { + pendingShape = nil + withTransaction(still) { + model.apply(phase: shape.phase) + model.apply(capsuleSize: shape.capsule) + model.apply(flat: shape.flat) + } + } + releaseDockSurface() guard let panel, let target = pendingFrame else { if model.offset != .zero { withTransaction(still) { model.apply(offset: .zero) } @@ -1228,7 +1384,9 @@ public final class DictationPillController: DictationPillPresenting { /// Displays changed: put the pill back on its anchor (tucked if resting) /// on whatever screen still exists, without animation. private func reanchorAfterScreenChange() { - guard let panel, panel.isVisible else { return } + // Mid-drag the drag itself follows the pointer onto whatever screen + // exists, and the drop re-anchors. + guard !isDragging, let panel, panel.isVisible else { return } guard let screen = panelScreen() ?? anchorScreen() else { return } settle() currentDisplayId = screen.kleothDisplayId @@ -1296,28 +1454,12 @@ public final class DictationPillController: DictationPillPresenting { capsuleHeight(for: .warning("")) + 2 * shadowPadding } - /// The anchor's center: the reference panel docked on `edge` at the saved - /// along-axis fraction (if the saved placement is for this screen), else - /// centered on the edge. The screen is the caller's choice — for a fresh - /// show that is the placement's display, else the one **under the mouse** - /// (for an `.accessory` app with no key window `NSScreen.main` is whatever - /// screen last had one — unreliable — while the mouse is where the user - /// is looking). - private func anchorCenter( - edge: PillGeometry.Edge, panelSize: CGSize, flat: Bool, on screen: NSScreen - ) -> CGPoint { - let bounds = Self.bounds(of: screen) - return PillGeometry.dockedCenter( - edge: edge, - along: savedAlong(edge: edge, on: screen, in: bounds), - panelSize: Self.dockReferenceSize(panelSize: panelSize, edge: edge, flat: flat, dock: model.dock, on: screen), - shadowPadding: Self.shadowPadding, - in: bounds - ) - } - /// Where along its edge the pill is parked: the saved fraction if the saved - /// placement is for this screen, else the middle of the edge. + /// placement is for this screen, else the middle of the edge. The screen + /// is the caller's choice — for a fresh show that is the placement's + /// display, else the one **under the mouse** (for an `.accessory` app with + /// no key window `NSScreen.main` is whatever screen last had one — + /// unreliable — while the mouse is where the user is looking). private func savedAlong(edge: PillGeometry.Edge, on screen: NSScreen, in bounds: CGRect) -> CGFloat { if let placement = savedPlacement(), self.screen(for: placement)?.kleothDisplayId == screen.kleothDisplayId { @@ -1326,21 +1468,30 @@ public final class DictationPillController: DictationPillPresenting { return edge.isVertical ? bounds.midY : bounds.midX } - /// A phase's active origin: its panel centered on the anchor. Clamping is - /// a no-op for anything no larger than the reference size; only an - /// over-long text pill can still be nudged. + /// A phase's active origin: its panel centered on the anchor — the + /// reference panel docked at `along` (the saved placement's, or a + /// drag's) — then clamped (`PillGeometry.dockedOrigin`). Clamping is a + /// no-op for anything no larger than the reference size; it lifts the + /// thicker peek dock off the edge, and only an over-long text pill can + /// still be nudged along it. /// /// A flat bar on a side edge is placed differently on purpose: it is not /// centered on the upright anchor but pinned by its NEAR END, so a 250 pt /// toolbar lies along the edge and grows inward instead of hanging half /// off the display. private func activeOrigin( - panelSize size: CGSize, edge: PillGeometry.Edge, flat: Bool, on screen: NSScreen? + panelSize size: CGSize, edge: PillGeometry.Edge, flat: Bool, on screen: NSScreen?, along: CGFloat? = nil ) -> CGPoint { guard let screen else { return .zero } - let center = anchorCenter(edge: edge, panelSize: size, flat: flat, on: screen) - let origin = CGPoint(x: center.x - size.width / 2, y: center.y - size.height / 2) - return PillGeometry.clamp(origin, panelSize: size, in: Self.bounds(of: screen)) + let bounds = Self.bounds(of: screen) + return PillGeometry.dockedOrigin( + edge: edge, + along: along ?? savedAlong(edge: edge, on: screen, in: bounds), + panelSize: size, + referenceSize: Self.dockReferenceSize(panelSize: size, edge: edge, flat: flat, dock: model.dock, on: screen), + shadowPadding: Self.shadowPadding, + in: bounds + ) } /// The edge the pill is docked on for `screen`: the saved one, or — for a diff --git a/app/Sources/KleothPillUI/DictationPillModel.swift b/app/Sources/KleothPillUI/DictationPillModel.swift index 05d4a14..6939bb4 100644 --- a/app/Sources/KleothPillUI/DictationPillModel.swift +++ b/app/Sources/KleothPillUI/DictationPillModel.swift @@ -1,3 +1,4 @@ +import AppKit import Foundation import SwiftUI import KleothCore @@ -82,6 +83,9 @@ final class DictationPillModel: ObservableObject { @Published private(set) var dockHeld = false /// The peek dock's geometry (`PillDockMetrics`); the sandbox rescales it live. @Published private(set) var dock = PillDockMetrics() + /// The pill is being dragged (`DictationPillController.isDragging`): the + /// closed-hand cursor, and no click lands at the drop. + @Published private(set) var dragging = false // MARK: Controller-facing mutation @@ -143,6 +147,10 @@ final class DictationPillModel: ObservableObject { if dockHeld != on { dockHeld = on } } + func apply(dragging on: Bool) { + if dragging != on { dragging = on } + } + func apply(dock metrics: PillDockMetrics) { if dock != metrics { dock = metrics } } @@ -172,6 +180,91 @@ final class DictationPillModel: ObservableObject { guard value.isFinite else { return 0 } return min(max(value, 0), 1) } + + // MARK: Derived (view + controller) + + /// The peek dock is out (or held out by its menu). + var dockOut: Bool { phase == .idle && (peeking || menuOpen) } + + /// Horizontal on the bottom and top edges; on a side edge the pill stands + /// up in every phase, turned so text reads the way a spine label does — + /// bottom-to-top on the left, top-to-bottom on the right. A flat phase + /// (the recording toolbar, and every dictation phase over a live + /// recording) never stands up — see `flat`. + var edgeRotation: Angle { + guard !flat else { return .zero } + switch edge { + case .bottom, .top: return .zero + case .left: return .degrees(-90) + case .right: return .degrees(90) + } + } + + /// The pointer relative to the capsule's centre, in the capsule's OWN + /// (un-rotated) space, or nil when it is off the panel. The capsule is + /// centred on the root (`rootSize` = the panel's content), displaced by + /// `offset` and turned by `edgeRotation`; undoing those here is what lets + /// the peek dock's fields light up — and the cursor change — correctly on + /// a side edge too, without trusting what `GeometryReader` reports through + /// a `rotationEffect`. + func capsulePointer(rootSize: CGSize) -> CGPoint? { + guard let pointer else { return nil } + let dx = pointer.x - (rootSize.width / 2 + offset.width) + let dy = pointer.y - (rootSize.height / 2 + offset.height) + let angle = -edgeRotation.radians + return CGPoint(x: dx * cos(angle) - dy * sin(angle), y: dx * sin(angle) + dy * cos(angle)) + } + + /// What the pointer should look like over the pill: a closed hand while it + /// is dragged; over the capsule, a pointing hand where a click acts (a + /// dock field, a bar's Stop, a prompt's buttons, a capsule that is a + /// button) and an OPEN hand on the body that only drags — the drag's + /// affordance (the user, 2026-11-18: "no intuitive place to click to + /// start dragging"). Every point of the capsule drags past + /// `PillGeometry.dragThreshold` except a real button (Stop, a prompt's + /// words, ✕, a fault's action). + func cursor(rootSize: CGSize) -> PillCursor { + if dragging { return .closedHand } + guard isPresented, let point = capsulePointer(rootSize: rootSize) else { return .arrow } + let size = capsuleSize + guard abs(point.x) <= size.width / 2, abs(point.y) <= size.height / 2 else { return .arrow } + switch phase { + case .hidden: + return .arrow + case .idle, .armed: + return dockOut ? .pointingHand : .arrow + case .listening, .saved, .meetingSaved, .failed: + // The capsule itself is the button (stop / hands-free / reveal / + // open / dismiss) — and still drags. + return .pointingHand + case .recording, .meeting: + let stop = size.width / 2 - PillStyle.compactPadding - PillStyle.stopButtonSize / 2 + let overStop = hypot(point.x - stop, point.y) <= PillStyle.stopButtonSize / 2 + 4 + return overStop ? .pointingHand : .openHand + case .prompt: + // symbol · words | buttons: the words drag, the buttons click. + let wordsEnd = -size.width / 2 + PillStyle.spacingM + 20 + PillStyle.spacingS + (labelWidth ?? 0) + return point.x > wordsEnd ? .pointingHand : .openHand + case .transcribing, .polishing, .done, .warning, .saving: + return .openHand + } + } +} + +/// The pointer shapes the pill sets (`DictationPillModel.cursor`). Set by +/// hand on every pointer move: Kleoth is never the active app, so AppKit's +/// cursor rects (and SwiftUI's pointer styles) do not run for the panel. +enum PillCursor: Equatable { + case arrow, pointingHand, openHand, closedHand + + func set() { + switch self { + case .arrow: NSCursor.arrow.set() + case .pointingHand: NSCursor.pointingHand.set() + case .openHand: NSCursor.openHand.set() + case .closedHand: NSCursor.closedHand.set() + } + } } /// What kind of move the capsule is making, for the view's choreography. diff --git a/app/Sources/KleothPillUI/DictationPillView.swift b/app/Sources/KleothPillUI/DictationPillView.swift index 5d79e52..4d3d14e 100644 --- a/app/Sources/KleothPillUI/DictationPillView.swift +++ b/app/Sources/KleothPillUI/DictationPillView.swift @@ -64,8 +64,12 @@ struct DictationPillView: View { @EnvironmentObject private var model: DictationPillModel @Environment(\.accessibilityReduceMotion) private var reduceMotion - /// True between the first `onChanged` and `onEnded` of a drag. - @State private var dragging = false + /// A press on the capsule is down (between the gesture's first + /// `onChanged` and its `onEnded`), not yet a drag. + @State private var pressing = false + /// The dock field under that press — drawn pressed until it lets go or + /// turns into a drag. + @State private var pressedField: Int? init(controller: DictationPillController) { self.controller = controller @@ -99,29 +103,12 @@ struct DictationPillView: View { .accessibilityLabel(Text(model.phase.pillText)) } - /// The pointer relative to the capsule's centre, in the capsule's OWN - /// (un-rotated) space, or nil when it is off the panel. The capsule is - /// centred on the root, displaced by `offset` and turned by - /// `edgeRotation`; undoing those here is what lets the peek dock's glyphs - /// light up correctly on a side edge too, without trusting what - /// `GeometryReader` reports through a `rotationEffect`. - private func dockPointer(rootSize: CGSize) -> CGPoint? { - guard let pointer = model.pointer else { return nil } - let center = CGPoint( - x: rootSize.width / 2 + model.offset.width, - y: rootSize.height / 2 + model.offset.height - ) - let dx = pointer.x - center.x - let dy = pointer.y - center.y - let angle = -edgeRotation.radians - return CGPoint(x: dx * cos(angle) - dy * sin(angle), y: dx * sin(angle) + dy * cos(angle)) - } - private func pill(rootSize: CGSize) -> some View { // The hit shape is the CAPSULE, not the panel rect: the transparent // shadow margin around it must stay non-interactive, or a `.statusBar`- // level panel would swallow clicks aimed at the app underneath. - capsule(dockPointer: dockPointer(rootSize: rootSize)) + // The pointer in the capsule's own space: `DictationPillModel.capsulePointer`. + capsule(dockPointer: model.capsulePointer(rootSize: rootSize)) // The capsule sizes itself to its content whatever the panel's // size: while a transition is in flight the panel is a stage // larger than the capsule, and on a side edge it is narrower than @@ -129,40 +116,12 @@ struct DictationPillView: View { // the label carries an explicit, screen-capped width. .fixedSize() .contentShape(Capsule(style: .continuous)) - .gesture(dragGesture) - // Click routing (screen-recording design §6.4). The capsule IS the - // control in the recording phases, so a tap on it is the action — - // never a dismissal. - .onTapGesture { - switch model.phase { - case .failed: - controller.dismissFromUser() - case .idle where model.peeking: - // The body of the peek dock opens the menu; the glyphs on - // it are the direct actions. Nothing starts from a stray - // click on the capsule any more. - controller.openMenu() - case .listening(handsFree: true): - controller.perform(.stopHandsFreeDictation) - case .listening(handsFree: false): - controller.perform(.switchToHandsFree) - case .saved: - controller.perform(.revealLastRecording) - case .meetingSaved: - controller.perform(.meeting(.openLast)) - case .prompt: - // Only its buttons answer a prompt: a stray click on the - // capsule must never decline (or accept) a call offer. - break - default: - // `.recording` and `.meeting` deliberately do NOT stop - // here: each bar is a toolbar with its own Stop button, - // and everything else on it is the drag handle. A - // whole-capsule stop was one slipped click away from - // ending a recording the user was only trying to move. - break - } - } + // ONE gesture for the whole capsule: a press that travels past + // `PillGeometry.dragThreshold` drags the pill, one that does not is + // a click on whatever is under it (`click(at:dockField:)`). A real + // Button inside (Stop, a prompt's words, ✕, a fault's action) takes + // its own press first and never drags. + .gesture(pressGesture) .help(model.phase.pillText) // A pill on a side edge stands up in every phase. Applied AFTER // the hit shape and gestures so they rotate with it — a horizontal @@ -173,6 +132,10 @@ struct DictationPillView: View { .scaleEffect(model.isPresented ? 1 : 0.9, anchor: .center) .opacity(model.isPresented ? 1 : 0) .padding(DictationPillController.shadowPadding) + // The cursor follows the phase and the pointer (an open hand on the + // body that drags, closed while dragged); the hosting view also + // re-asserts it on every move (`DictationPillController.ensurePanel`). + .onChange(of: model.cursor(rootSize: rootSize)) { _, cursor in cursor.set() } } private func capsule(dockPointer: CGPoint?) -> some View { @@ -272,11 +235,8 @@ struct DictationPillView: View { var across: CGFloat = 1 } - /// Horizontal on the bottom and top edges; on a side edge the pill stands - /// up in every phase, turned so text reads the way a spine label does — - /// bottom-to-top on the left, top-to-bottom on the right. /// The peek dock is out (or held out by its menu). - private var dockOut: Bool { model.phase == .idle && (model.peeking || model.menuOpen) } + private var dockOut: Bool { model.dockOut } /// Which dock surface is on the capsule: one of the two dock looks /// (`PillDockStyle`; Liquid Glass needs macOS 26 and falls back to ink) @@ -372,16 +332,8 @@ struct DictationPillView: View { .animation(.easeOut(duration: 0.2), value: dockOut) } - private var edgeRotation: Angle { - // A flat phase (the recording toolbar, and every dictation phase over a - // live recording) never stands up — see `DictationPillModel.flat`. - guard !model.flat else { return .zero } - switch model.edge { - case .bottom, .top: return .zero - case .left: return .degrees(-90) - case .right: return .degrees(90) - } - } + /// See `DictationPillModel.edgeRotation`. + private var edgeRotation: Angle { model.edgeRotation } @ViewBuilder private func content(dockPointer: CGPoint?) -> some View { @@ -414,13 +366,13 @@ struct DictationPillView: View { // (filmed). It leaves at settle, already invisible. PeekDock( metrics: model.dock, - pointer: dockPointer, + // No hover lift under a drag: the dock is being + // carried, not pointed at. + pointer: model.dragging ? nil : dockPointer, + pressed: pressedField, rotation: edgeRotation, menuOpen: model.menuOpen, - onDictate: { controller.perform(.startHandsFreeDictation) }, - onMeeting: { controller.perform(.meeting(.start)) }, - onRecord: { controller.perform(.startScreenRecording) }, - onMenu: { controller.openMenu() } + onField: { dockField($0) } ) .opacity(dockOut ? 1 : 0) .allowsHitTesting(dockOut) @@ -460,12 +412,6 @@ struct DictationPillView: View { .frame(width: 10, height: 10) .animation(.spring(duration: 0.18, bounce: 0.35), value: model.hovered) .animation(.spring(duration: 0.18, bounce: 0.35), value: handsFree) - .onChange(of: model.hovered) { _, on in - (on ? NSCursor.pointingHand : NSCursor.arrow).set() - } - // The dictation ended under the pointer: the capsule is no longer - // a button, so the pointing hand must not outlive the slot. - .onDisappear { if model.hovered { NSCursor.arrow.set() } } .transition(.opacity) .accessibilityHidden(true) Waveform(mode: .live(level: model.level), reduceMotion: reduceMotion) @@ -635,30 +581,104 @@ struct DictationPillView: View { .frame(width: model.labelWidth, alignment: .leading) } + /// Press → click or drag (the user, 2026-11-18: the drag was "very + /// glitchy… no intuitive place to click to start dragging" — every point + /// of the peek dock was a Button, and a press on a Button never reached + /// the capsule's drag). Now every point of the capsule but a real button + /// drags once the press has travelled `PillGeometry.dragThreshold`, the + /// dock's fields included; a press that never travels is a click + /// (`click(at:dockField:)`), so a hand that drifts a few points while + /// pressing still clicks. + /// /// The pill slides along its edge (and re-docks when dragged clearly /// toward another one); the controller reads `NSEvent.mouseLocation` /// itself — `value.translation` is measured against a coordinate space /// that moves with the window, so it double-counts and the pill runs away - /// from the cursor. `minimumDistance: 6` leaves the buttons clickable and - /// is forgiving of a jittery click: raised from 3 once the capsule became a - /// start/stop CONTROL (§6.4) — a hand that drifts two points while pressing - /// "stop" must stop the recording, not re-dock the pill. - private var dragGesture: some Gesture { - DragGesture(minimumDistance: 6) - .onChanged { _ in - if !dragging { - dragging = true - controller.beginDrag() + /// from the cursor. It is read here only BEFORE the drag starts, while the + /// window is still, for the threshold. + private var pressGesture: some Gesture { + DragGesture(minimumDistance: 0) + .onChanged { value in + if model.dragging { + controller.dragMoved() + return + } + if !pressing { + pressing = true + pressedField = dockOut ? dockFieldIndex(at: value.startLocation) : nil } + guard PillGeometry.isDrag(translation: value.translation) else { return } + pressedField = nil + controller.beginDrag() controller.dragMoved() } - .onEnded { _ in - guard dragging else { return } - controller.dragMoved() - dragging = false - controller.commitDraggedPlacement() + .onEnded { value in + pressing = false + let field = pressedField + pressedField = nil + if model.dragging { + controller.dragMoved() + controller.commitDraggedPlacement() + return + } + // A press that travelled is never a click — also when its + // drag was already dropped without it (`endDragIfReleased`). + guard !PillGeometry.isDrag(translation: value.translation) else { return } + click(at: value.startLocation, dockField: field) } } + + /// A press that did not travel: click routing (screen-recording design + /// §6.4). The capsule IS the control in the recording phases, so a tap on + /// it is the action — never a dismissal. + private func click(at location: CGPoint, dockField field: Int?) { + switch model.phase { + case .failed: + controller.dismissFromUser() + case .idle where dockOut: + // The dock's fields are its buttons: the one pressed acts. + dockField(field ?? dockFieldIndex(at: location)) + case .listening(handsFree: true): + controller.perform(.stopHandsFreeDictation) + case .listening(handsFree: false): + controller.perform(.switchToHandsFree) + case .saved: + controller.perform(.revealLastRecording) + case .meetingSaved: + controller.perform(.meeting(.openLast)) + case .prompt: + // Only its buttons answer a prompt: a stray click on the + // capsule must never decline (or accept) a call offer. + break + default: + // `.recording` and `.meeting` deliberately do NOT stop here: each + // bar is a toolbar with its own Stop button, and everything else + // on it is the drag handle. A whole-capsule stop was one slipped + // click away from ending a recording the user was only trying to + // move. + break + } + } + + /// The dock field under a point in the capsule's own space (origin at its + /// top-left, un-rotated — the gesture is attached before the rotation), + /// through the hover lift's mapping (`PillGeometry.dockFieldIndex`). + private func dockFieldIndex(at location: CGPoint) -> Int { + PillGeometry.dockFieldIndex( + along: location.x - model.capsuleSize.width / 2, + pitch: model.dock.pitch, count: PillDockMetrics.fieldCount + ) + } + + /// Dictate · Meeting · Screen · More. + private func dockField(_ index: Int) { + switch index { + case 0: controller.perform(.startHandsFreeDictation) + case 1: controller.perform(.meeting(.start)) + case 2: controller.perform(.startScreenRecording) + default: controller.openMenu() + } + } } // MARK: - Peek dock @@ -685,18 +705,25 @@ struct DictationPillView: View { /// belongs to a field (the gaps and insets are folded into the hit areas), so /// hovering anywhere lights something. Hover is computed from the panel-wide /// pointer (relative to the capsule's centre, un-rotated — -/// `DictationPillView.dockPointer`), never from `.onHover`, which is dead +/// `DictationPillModel.capsulePointer`), never from `.onHover`, which is dead /// while another app is active. Geometry comes from `PillDockMetrics`, which /// the sandbox scales and restyles live. +/// +/// The fields are NOT Buttons (2026-11-18): a press on a Button never reached +/// the capsule's drag, and the dock is nothing but fields — so the resting +/// pill could not be dragged at all. The capsule's one press gesture +/// (`DictationPillView.pressGesture`) clicks the field under a press that +/// does not travel and drags the dock otherwise; a field is still a button to +/// VoiceOver. private struct PeekDock: View { let metrics: PillDockMetrics let pointer: CGPoint? + /// The field under a press that is still down (drawn pressed). + let pressed: Int? let rotation: Angle let menuOpen: Bool - let onDictate: () -> Void - let onMeeting: () -> Void - let onRecord: () -> Void - let onMenu: () -> Void + /// Dictate · Meeting · Screen · More, by index — VoiceOver's press. + let onField: (Int) -> Void /// Which field the pointer is over. The capsule is divided into /// `PillDockMetrics.fieldCount` fields along its length, so a pointer @@ -716,28 +743,25 @@ private struct PeekDock: View { HStack(spacing: 0) { PillDockTile( metrics: metrics, look: look, symbol: "mic.fill", caption: "Dictate", label: "Start a dictation", - glyph: nil, - hot: hot == 0, lit: false, divider: !(hot == 0 || hot == 1), rotation: rotation, action: onDictate + glyph: nil, hot: hot == 0, lit: false, pressed: pressed == 0, + divider: !(hot == 0 || hot == 1), rotation: rotation, action: { onField(0) } ) PillDockTile( metrics: metrics, look: look, symbol: "person.2.wave.2.fill", caption: "Meeting", label: "Record a meeting", - glyph: nil, - hot: hot == 1, lit: false, divider: !(hot == 1 || hot == 2), rotation: rotation, action: onMeeting + glyph: nil, hot: hot == 1, lit: false, pressed: pressed == 1, + divider: !(hot == 1 || hot == 2), rotation: rotation, action: { onField(1) } ) PillDockTile( metrics: metrics, look: look, symbol: "record.circle.fill", caption: "Screen", label: "Record the screen", - glyph: PillStyle.recordTint, - hot: hot == 2, lit: false, divider: !(hot == 2 || hot == 3), rotation: rotation, action: onRecord + glyph: PillStyle.recordTint, hot: hot == 2, lit: false, pressed: pressed == 2, + divider: !(hot == 2 || hot == 3), rotation: rotation, action: { onField(2) } ) PillDockTile( metrics: metrics, look: look, symbol: "ellipsis", caption: "More", label: "More", - glyph: nil, - hot: hot == 3, lit: menuOpen, divider: false, rotation: rotation, action: onMenu + glyph: nil, hot: hot == 3, lit: menuOpen, pressed: pressed == 3, + divider: false, rotation: rotation, action: { onField(3) } ) } - .onChange(of: hot) { _, index in - (index == nil ? NSCursor.arrow : NSCursor.pointingHand).set() - } .accessibilityElement(children: .contain) } } @@ -756,9 +780,13 @@ private struct PillDockTile: View { let glyph: Color? let hot: Bool let lit: Bool + /// A press is down on this field: a quick squash (the old ButtonStyle's + /// look), released when the press lets go or becomes a drag. + let pressed: Bool /// A hairline on the trailing edge, separating this field from the next. let divider: Bool let rotation: Angle + /// VoiceOver's press; the pointer's click comes through the capsule. let action: () -> Void private var active: Bool { hot || lit } @@ -775,63 +803,57 @@ private struct PillDockTile: View { } var body: some View { - Button(action: action) { - ZStack { - if active { - RoundedRectangle(cornerRadius: metrics.tileRadius, style: .continuous) - .fill(Color.white.opacity(PillDockStyle.hoverPlate(look))) - .transition(.opacity) - } - VStack(spacing: metrics.captionGap) { - Image(systemName: symbol) - .font(.system(size: metrics.iconSize, weight: .semibold)) - .foregroundStyle(iconStyle) - // A fixed slot: the glyphs differ in height (the ⋯ is - // a quarter of the mic's), so without it the More - // caption sat higher than its neighbours and the dots - // lower than the other glyphs' centres (user, 2026-09-09). - .frame(height: metrics.iconSlotHeight) - if metrics.showsCaptions { - Text(caption) - .font(.system(size: metrics.captionSize, weight: .medium)) - .foregroundStyle(captionStyle) - .lineLimit(1) - .fixedSize() - } - } - // Upright on every edge (the capsule itself is rotated there). - .rotationEffect(-rotation) + ZStack { + if active { + RoundedRectangle(cornerRadius: metrics.tileRadius, style: .continuous) + .fill(Color.white.opacity(PillDockStyle.hoverPlate(look))) + .transition(.opacity) } - .frame(width: metrics.tile.width, height: metrics.tile.height) - .scaleEffect(hot ? 1.03 : 1) - .padding(.horizontal, metrics.gap / 2) - .padding(.vertical, metrics.verticalInset) - .contentShape(Rectangle()) - .overlay(alignment: .trailing) { - if divider { - Rectangle() - .fill(PillDockStyle.divider(look)) - .frame(width: 1, height: metrics.tile.height * 0.5) - .transition(.opacity) + VStack(spacing: metrics.captionGap) { + Image(systemName: symbol) + .font(.system(size: metrics.iconSize, weight: .semibold)) + .foregroundStyle(iconStyle) + // A fixed slot: the glyphs differ in height (the ⋯ is + // a quarter of the mic's), so without it the More + // caption sat higher than its neighbours and the dots + // lower than the other glyphs' centres (user, 2026-09-09). + .frame(height: metrics.iconSlotHeight) + if metrics.showsCaptions { + Text(caption) + .font(.system(size: metrics.captionSize, weight: .medium)) + .foregroundStyle(captionStyle) + .lineLimit(1) + .fixedSize() } } + // Upright on every edge (the capsule itself is rotated there). + .rotationEffect(-rotation) + } + .frame(width: metrics.tile.width, height: metrics.tile.height) + .scaleEffect(hot ? 1.03 : 1) + .padding(.horizontal, metrics.gap / 2) + .padding(.vertical, metrics.verticalInset) + .contentShape(Rectangle()) + .overlay(alignment: .trailing) { + if divider { + Rectangle() + .fill(PillDockStyle.divider(look)) + .frame(width: 1, height: metrics.tile.height * 0.5) + .transition(.opacity) + } } - .buttonStyle(PillDockTileStyle()) + // Pressed = a quick squash; works for an inactive app (the press comes + // from the capsule's own gesture, not from hover tracking). + .scaleEffect(pressed ? 0.95 : 1) + .opacity(pressed ? 0.85 : 1) + .animation(.spring(duration: 0.14, bounce: 0.3), value: pressed) .animation(.spring(duration: 0.22, bounce: 0.25), value: active) .animation(.easeOut(duration: 0.15), value: divider) .help(label) + .accessibilityElement(children: .ignore) .accessibilityLabel(label) - } -} - -/// Pressed = a quick squash; works for an inactive app (`isPressed` comes -/// from the button's own gesture, not from hover tracking). -private struct PillDockTileStyle: ButtonStyle { - func makeBody(configuration: Configuration) -> some View { - configuration.label - .scaleEffect(configuration.isPressed ? 0.95 : 1) - .opacity(configuration.isPressed ? 0.85 : 1) - .animation(.spring(duration: 0.14, bounce: 0.3), value: configuration.isPressed) + .accessibilityAddTraits(.isButton) + .accessibilityAction { action() } } } From 5bb2c29b0c78162d5e0833e4867f42e5b375bdb9 Mon Sep 17 00:00:00 2001 From: "a.sakhatskii" Date: Wed, 18 Nov 2026 11:05:24 +0400 Subject: [PATCH 3/8] pillsandbox: film real drags and the app's start timing press: / move:: / release drive a drag through the pill's own window (synthesized events, a film pointer instead of the cursor, the tracking area's mid-drag exit emulated); --start-delay / --start-block give a start from the pill the app's async meeting start and blocking dictation capture; fired actions print in film mode; the film pointer is drawn on the frames. --- app/Sources/pillsandbox/main.swift | 114 +++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 8 deletions(-) diff --git a/app/Sources/pillsandbox/main.swift b/app/Sources/pillsandbox/main.swift index 6bdbe73..b064749 100644 --- a/app/Sources/pillsandbox/main.swift +++ b/app/Sources/pillsandbox/main.swift @@ -25,7 +25,8 @@ import KleothPillUI /// peek / unpeek = pointer enters / leaves the resting pill, hover: / click: /// with spots mic|meet|rec|menu|center|stop|label (label = over a text phase's words, /// clear of its buttons), perform:startMeeting|stopMeeting|acceptOffer|neverOffer| -/// acceptStop|startScreenRecording|…; +/// acceptStop|startScreenRecording|…; press: / move:: / release = a real +/// drag through the pill's own window (move animates over its hold, screen points, y up); /// any item may end in `@` to hold it that long /// instead of `--hold`, e.g. `listening@3.2`) /// `--demo dictation|screen` also composes every frame onto an 680×425 pt @@ -88,6 +89,13 @@ struct Arguments { var slidesDirectory: URL? var slidesLength: TimeInterval = 20 var slidesMarks: [TimeInterval] = [] + /// The app's timing for a start from the pill (0 = the old, synchronous + /// sandbox): `--start-delay` — a meeting or screen recording's bar comes + /// up that long after the click (`RecordingController.start()` awaits its + /// captures); `--start-block` — a hands-free dictation blocks the main + /// thread that long before `.listening` (`DictationCapture.start()`). + var startDelay: TimeInterval = 0 + var startBlock: TimeInterval = 0 enum LevelPattern: String { case off, speech, steady @@ -111,6 +119,8 @@ struct Arguments { case "--demo": out.demo = value().flatMap(DemoKind.init(rawValue:)); i += 1 case "--slides": out.slidesDirectory = value().map { URL(fileURLWithPath: $0, isDirectory: true) }; i += 1 case "--length": out.slidesLength = value().flatMap(Double.init) ?? 20; i += 1 + case "--start-delay": out.startDelay = value().flatMap(Double.init) ?? 0; i += 1 + case "--start-block": out.startBlock = value().flatMap(Double.init) ?? 0; i += 1 case "--marks": out.slidesMarks = value()?.split(separator: ",").compactMap { Double($0) } ?? []; i += 1 case "--backdrop": out.backdrop = value().flatMap(backdrop(named:)) ?? .idle; i += 1 case "--sequence": out.sequence = value()?.split(separator: ",").map(String.init) ?? out.sequence; i += 1 @@ -332,6 +342,7 @@ final class SandboxDriver: ObservableObject { } private func log(_ line: String) { + if arguments.filmDirectory != nil { print("action: \(line)") } actionLog.insert(line, at: 0) if actionLog.count > 8 { actionLog.removeLast() } } @@ -343,6 +354,7 @@ final class SandboxDriver: ObservableObject { switch action { case .startHandsFreeDictation: log("Start dictation (hands-free)") + if arguments.startBlock > 0 { Thread.sleep(forTimeInterval: arguments.startBlock) } handsFreeTask?.cancel() controller.show(.listening(handsFree: true)) phase = "hands-free" @@ -372,9 +384,11 @@ final class SandboxDriver: ObservableObject { } case .startScreenRecording: log("Record screen… (would open the region picker)") - SandboxClock.recordingStart = Date() - backdropRecording = true - simulateRecordingLevels = true + afterStartDelay { [weak self] in + SandboxClock.recordingStart = Date() + self?.backdropRecording = true + self?.simulateRecordingLevels = true + } case .stopScreenRecording: log("Stop recording → saving → saved") simulateRecordingLevels = false @@ -429,9 +443,11 @@ final class SandboxDriver: ObservableObject { switch action { case .start: log("Record meeting (would call RecordingController.start())") - SandboxClock.meetingStart = Date() - backdropMeeting = true - simulateRecordingLevels = true + afterStartDelay { [weak self] in + SandboxClock.meetingStart = Date() + self?.backdropMeeting = true + self?.simulateRecordingLevels = true + } case .acceptOffer: log("Record (offer accepted — would call RecordingController.start())") // The bridge's `.started` order: the meeting backdrop FIRST — @@ -467,6 +483,15 @@ final class SandboxDriver: ObservableObject { } } + /// Runs a capture's "it started" now, or `--start-delay` later. + private func afterStartDelay(_ body: @escaping @MainActor () -> Void) { + guard arguments.startDelay > 0 else { return body() } + Task { @MainActor in + try? await Task.sleep(for: .seconds(arguments.startDelay)) + body() + } + } + func show(_ state: DictationPillState) { cycleTask?.cancel() controller.show(state) @@ -696,6 +721,12 @@ func film(_ args: Arguments, controller: DictationPillController, exitWhenDone: let start = Date() var speechClock = 0.0 var pointerSpot: String? + // A scripted drag (`press:` / `move:` / `release`): where the held + // pointer is in screen coordinates, the move in flight, and whether the + // emulated tracking area last said "inside". + var heldPointer: CGPoint? + var dragMove: (from: CGPoint, by: CGVector, duration: TimeInterval, begun: Date)? + var heldInside = true for item in args.sequence { let parts = item.split(separator: "@", maxSplits: 1).map(String.init) let name = parts[0] @@ -724,6 +755,30 @@ func film(_ args: Arguments, controller: DictationPillController, exitWhenDone: // may send events to its own windows. pointerSpot = String(name.dropFirst(6)) filmClick(String(name.dropFirst(6)), edge: args.edge, controller: controller) + } else if name.hasPrefix("press:") { + // "press:" — mouse DOWN on the pill at a named spot (see + // `filmPointer`), delivered to the pill window like `click:`; the + // drag reads the film pointer instead of the real cursor. + let spot = String(name.dropFirst(6)) + if let point = filmScreenPoint(spot, edge: args.edge, controller: controller) { + heldPointer = point + heldInside = true + controller.filmPointerLocation = point + filmMouse(.leftMouseDown, at: point) + } + pointerSpot = nil + } else if name.hasPrefix("move:") { + // "move::" — drags the held pointer by (dx, dy) screen + // points (y up) over this step's hold, one leftMouseDragged per frame. + let parts = name.dropFirst(5).split(separator: ":").compactMap { Double($0) } + if let from = heldPointer, parts.count == 2 { + dragMove = (from: from, by: CGVector(dx: parts[0], dy: parts[1]), duration: max(holdFor * 0.8, 0.05), begun: Date()) + } + } else if name == "release" { + // Mouse UP where the held pointer is; the pointer stays there. + if let point = heldPointer { filmMouse(.leftMouseUp, at: point) } + heldPointer = nil + dragMove = nil } else if name.hasPrefix("perform:") { // "perform:startScreenRecording|stopScreenRecording|startMeeting|stopMeeting|startHandsFreeDictation|…" // — fires a pill action through the controller, so the SANDBOX @@ -775,6 +830,21 @@ func film(_ args: Arguments, controller: DictationPillController, exitWhenDone: let phaseStart = Date() while Date().timeIntervalSince(phaseStart) < holdFor { speechClock += interval + if let move = dragMove { + let t = min(1, Date().timeIntervalSince(move.begun) / move.duration) + let point = CGPoint(x: move.from.x + move.by.dx * t, y: move.from.y + move.by.dy * t) + heldPointer = point + controller.filmPointerLocation = point + filmMouse(.leftMouseDragged, at: point) + // The tracking area: an exit is delivered mid-drag too (an + // entered one only after the mouse-up) — AppKit's rule. + let inside = controller.panelFrame?.contains(point) ?? false + if inside != heldInside { + heldInside = inside + if !inside { controller.setHovered(false) } + } + if t >= 1 { dragMove = nil } + } if case .listening = state { let syllable = max(0, sin(speechClock * 9.0)) * (0.5 + 0.5 * sin(speechClock * 1.3)) controller.setLevel(min(1, 0.15 + 0.85 * syllable)) @@ -784,7 +854,7 @@ func film(_ args: Arguments, controller: DictationPillController, exitWhenDone: controller.setRecordingLevels(syntheticLevels(args.levels, at: speechClock)) } if let frame = controller.captureFrame() { - var pointer: CGPoint? + var pointer: CGPoint? = heldPointer if let spot = pointerSpot, let p = filmPointer(spot, edge: args.edge, controller: controller) { // Root space is y-down from the panel's top-left. pointer = CGPoint(x: frame.panelFrame.minX + p.x, y: frame.panelFrame.maxY - p.y) @@ -883,6 +953,12 @@ func writeFilm(_ frames: [CapturedFrame], to dir: URL, edge: PillGeometry.Edge) imageRect = CGRect(origin: imageRect.origin, size: imageSize) } ctx.draw(image, in: imageRect) + // The film's pointer (a drag's held pointer, a hover spot): a ring. + if let pointer = captured.pointer { + ctx.setStrokeColor(CGColor(red: 0.1, green: 0.5, blue: 1, alpha: 0.9)) + ctx.setLineWidth(1.5) + ctx.strokeEllipse(in: CGRect(x: pointer.x - 4, y: pointer.y - 4, width: 8, height: 8)) + } guard let image = ctx.makeImage() else { continue } pngs[index] = image try writePNG(image, to: dir.appendingPathComponent(String(format: "frame-%04d.png", index))) @@ -1011,6 +1087,28 @@ func filmClick(_ spot: String, edge: PillGeometry.Edge, controller: DictationPil print("click:\(spot) at \(location) in window \(window.windowNumber)") } +/// A named spot on the pill in SCREEN coordinates (y up). +@MainActor +func filmScreenPoint(_ spot: String, edge: PillGeometry.Edge, controller: DictationPillController) -> CGPoint? { + guard let point = filmPointer(spot, edge: edge, controller: controller), let frame = controller.panelFrame else { return nil } + return CGPoint(x: frame.minX + point.x, y: frame.maxY - point.y) +} + +/// One synthesized mouse event at a SCREEN point, sent to the pill's own +/// window (located in that window's coordinates as they are NOW — the panel +/// moves under a drag). +@MainActor +func filmMouse(_ kind: NSEvent.EventType, at screenPoint: CGPoint) { + guard let window = NSApp.windows.first(where: { String(describing: type(of: $0)) == "DictationPanel" }) else { return } + let location = CGPoint(x: screenPoint.x - window.frame.minX, y: screenPoint.y - window.frame.minY) + guard let event = NSEvent.mouseEvent( + with: kind, location: location, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, + windowNumber: window.windowNumber, context: nil, eventNumber: 0, clickCount: 1, + pressure: kind == .leftMouseUp ? 0 : 1 + ) else { return } + window.sendEvent(event) +} + /// The film's stage: a plain light or dark window behind the pill, for grabs /// of a Liquid Glass surface that do not depend on the user's windows. enum StageTone: String { case none, light, dark } From 71ed586839d122f59d1de2fb3ae72cc732c53d9e Mon Sep 17 00:00:00 2001 From: "a.sakhatskii" Date: Wed, 18 Nov 2026 11:05:56 +0400 Subject: [PATCH 4/8] Changelog and CLAUDE.md: the pill's drag and empty-pill fixes --- CHANGELOG.md | 18 ++++++++++++++++++ CLAUDE.md | 13 +++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b87a3d..adcdacc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,24 @@ All notable changes to Kleoth are documented here. The format is based on ## [Unreleased] +### Fixed + +- **The pill could not be dragged from its dock.** The four fields (Dictate · Meeting · Screen · + More) took every press, so a drag never started and the pointer slid off the pill. Now the whole + pill drags once the pointer has moved a few points, fields and bars alike; a click that does not + move still clicks. Only a real button (Stop, a question's answers, ✕) never drags. The pointer + shows an open hand where the pill only drags and a closed hand while you drag it. +- **Dragging the pill collapsed it and threw it back.** When the pointer left the dock during a + drag, the dock folded back into the edge at its old place halfway through the drag. A dragged + pill now keeps its shape until you let go, and a dictation that finishes, or a meeting or a + recording that starts, while you drag shows up where you drop it. The pill also no longer drops + a few points as the drag begins. +- **The pill could go empty while it was busy.** A dictation, a meeting or a screen recording + started from the pill showed a pill-shaped blank before its controls appeared, for as long as + the start kept the Mac busy. And a pill that changed while you dragged it was drawn off its own + window, as an empty or half-cut capsule. The controls now show from the first frame, and the + pill always settles back to its own size after it moves. + ## [0.5.6] — 2026-11-17 ### Fixed diff --git a/CLAUDE.md b/CLAUDE.md index 2fdcd76..768e517 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -13,7 +13,7 @@ Liquid Glass gated behind `if #available(macOS 26, *)`. ## Commands ```bash -swift build && swift test # core + CLI (785 tests); CI: .github/workflows/tests.yml (+ app smoke, marketing) +swift build && swift test # core + CLI (790 tests); CI: .github/workflows/tests.yml (+ app smoke, marketing) swift build --package-path app # app package swift test --package-path app # KleothCaptureTests: file/format logic on synthetic buffers (no mic) bash app/setup-signing.sh # once: "Kleoth Self-Signed" cert (Accessibility/TCC trust binds to it) @@ -39,6 +39,7 @@ swift build --package-path app --product micwatch && caffeinate -s -i app/.build swift build --package-path app --product micopen && caffeinate -s -i app/.build/debug/micopen 13 # holds the mic from another process, samples discarded; exit 3 without an existing mic grant (never prompts) swift run --package-path app pillsandbox # pill playground app/.build/debug/pillsandbox --film --edge right --sequence idle,armed,listening,done,idle # filmstrip PNGs (step@secs; --demo dictation|screen composes README frames) +app/.build/debug/pillsandbox --film [--start-delay s] [--start-block s] --sequence peek,press:center,move:240:80@1,release # a real drag; app-like start timing bash app/branding-src/demo/make-demos.sh # README demo GIFs from the real pill → docs/assets/demo-*.gif bash app/branding-src/demo/make-app-demos.sh [data-dir] # meeting + viewer GIFs, screenshot-detail.png: a KleothDemo.app copy (-KleothDemo) films its own window bun app/branding-src/demo/make-demo-data.ts [--provider local] # fictional meetings/recording, real on-device transcripts + summaries (default: Claude Code) @@ -338,9 +339,17 @@ Design docs (binding contracts, error matrices, manual checklists): `docs/plans/ main-queue turn late, so a capture start sets its backdrop FIRST, then dismisses its own leftover phase (`MeetingPillBridge` `.started`); `clearCapturePhaseBlocking` withdraws the other capture's. The other order leaves a hot mic with no bar. A rising bar clears ANY sticky `.failed` (a dictation's is terminal), and both - `setBackdrop` and the coordinator read the phase still to land (`pendingPhase` / `upcomingState`). + `setBackdrop` and the coordinator read the phase still to land (`pendingPhase` / `upcomingState`) — or the + one a drag holds for the drop: while `isDragging`, every `show`/hide is deferred to the drop (latest wins). - `referenceSize` is resolved against the four-field dock (275 pt, every edge) and, on bottom/top, the meeting bar (240 pt): a new longest shape must be folded in there, or a pill parked near a corner creeps. +- Pill motion: never write the panel's frame mid-transition without zeroing `model.offset` (the capsule is drawn + off its own window — an empty pill), and settle on `PillMotion.settleDelay`, never `withAnimation(completion:)`: + with a move and a reshape started in the same turn (every rise and sink) the completion never fired. +- A SwiftUI `Button` inside the capsule takes the press, so the parent's drag never starts from it: what must + drag (the dock's fields) is not a Button — the capsule's one `DragGesture(minimumDistance: 0)` clicks below + `PillGeometry.dragThreshold` and drags past it. Film clicks (`window.sendEvent`) can't fire a `.borderless` + Button (AppKit's tracking loop): a prompt's answers or a fault's action go through `perform:`. - Core Audio: per-process `IsRunningInput` listeners register and never fire (listen to `IsRunning`/`Devices`, then re-read), and `AudioObjectRemovePropertyListenerBlock` from Swift silently removes nothing (listeners stacked per start): use `AudioObjectAdd/RemovePropertyListener` with one `@convention(c)` proc. From 1fc58dbc5638cd5e37a9ca64276ca37a4e1be62b Mon Sep 17 00:00:00 2001 From: "a.sakhatskii" Date: Wed, 18 Nov 2026 11:38:32 +0400 Subject: [PATCH 5/8] Pill core: screen-space press, drag hold and transition ledger, tested PillPress decides click or drag from the pointer's travel ON SCREEN and never calls a press that became a drag a click. PillDragHold holds requests during a drag (latest wins) except a capture going hot. PillTransitionLedger keeps the transition generation and its pending shape, which a settle lands instead of dropping. alongCapsule maps a screen point onto the capsule's own axis. --- .../KleothCore/Dictation/PillGeometry.swift | 135 ++++++++++++++++ Tests/KleothCoreTests/PillPressTests.swift | 152 ++++++++++++++++++ 2 files changed, 287 insertions(+) create mode 100644 Tests/KleothCoreTests/PillPressTests.swift diff --git a/Sources/KleothCore/Dictation/PillGeometry.swift b/Sources/KleothCore/Dictation/PillGeometry.swift index 95bd8b2..43e411b 100644 --- a/Sources/KleothCore/Dictation/PillGeometry.swift +++ b/Sources/KleothCore/Dictation/PillGeometry.swift @@ -227,6 +227,21 @@ public enum PillGeometry { (edge.isVertical ? pointer.y : pointer.x) - grabOffset } + /// A screen pointer's offset from a capsule's centre along the capsule's + /// OWN axis (the one the dock's fields run along, negative toward its + /// start). On a side edge an upright capsule is turned — +90° on the + /// right (its start at the top), −90° on the left (its start at the + /// bottom); a `flat` one never is. The hover lift's mapping + /// (`DictationPillModel.capsulePointer`), from screen coordinates. + public static func alongCapsule(pointer: CGPoint, capsuleCenter: CGPoint, edge: Edge, flat: Bool) -> CGFloat { + guard !flat else { return pointer.x - capsuleCenter.x } + switch edge { + case .bottom, .top: return pointer.x - capsuleCenter.x + case .right: return capsuleCenter.y - pointer.y + case .left: return pointer.y - capsuleCenter.y + } + } + /// The along-axis coordinate a saved placement encodes for `edge`. public static func along(for placement: PillPlacement, edge: Edge, in bounds: CGRect) -> CGFloat { edge.isVertical @@ -435,6 +450,126 @@ public enum PillGeometry { } } +/// One press on the pill, measured on SCREEN: a click until the pointer has +/// travelled `PillGeometry.dragThreshold` from where it went down, then a +/// drag for good. Not the gesture's own translation, which is measured in +/// the capsule's space: it moves with the window during a drag and grows +/// under a still pointer while the capsule animates (a press on the opening +/// dock was no click). +public struct PillPress: Equatable, Sendable { + public let start: CGPoint + /// The press became a drag (and stays one, however its drop came about). + public private(set) var dragged = false + + public init(at start: CGPoint) { + self.start = start + } + + /// Feeds the pointer's screen position; true exactly once — when this + /// press becomes a drag. + public mutating func move(to point: CGPoint) -> Bool { + guard !dragged else { return false } + let travel = CGSize(width: point.x - start.x, height: point.y - start.y) + guard PillGeometry.isDrag(translation: travel) else { return false } + dragged = true + return true + } + + /// Whether the release is a click: never once the press became a drag. + public var isClick: Bool { !dragged } +} + +/// What a drag does with the requests that reach the pill while it is +/// carried (`DictationPillController.isDragging`): it HOLDS them for the drop, +/// the latest winning as it would with no drag — except a capture going hot, +/// which applies at once (a live microphone always has its indicator) and, +/// being the newest request, replaces anything held before it. +public struct PillDragHold: Equatable { + public enum Request: Equatable { + case show(Phase) + case hide + } + + public enum Decision: Equatable { + case hold + case applyNow + } + + public private(set) var held: Request? + + public init() {} + + public mutating func receive(_ request: Request, hot: Bool) -> Decision { + if hot { + held = nil + return .applyNow + } + held = request + return .hold + } + + /// The held request, once, at the drop. + public mutating func drop() -> Request? { + defer { held = nil } + return held + } + + /// The phase the drop will bring (`hidden` for a held hide), or nil. + public func heldPhase(hidden: Phase) -> Phase? { + switch held { + case .show(let phase): return phase + case .hide: return hidden + case nil: return nil + } + } +} + +/// The pill's transitions in flight: each one is a generation, and its shape +/// (phase, size, flatness) is PENDING from the moment it is asked for until +/// its own callout applies it — or until a settle lands it at once. A later +/// transition, a settle or a hide make the earlier generation stale, so its +/// callout and its settle timer do nothing. A settle never drops the pending +/// shape (it did: a drag starting in that turn left the new phase unapplied). +public struct PillTransitionLedger { + public private(set) var generation = 0 + public private(set) var pending: Shape? + + public init() {} + + /// A new transition: supersedes any in flight. Returns its generation. + public mutating func begin(_ shape: Shape) -> Int { + generation += 1 + pending = shape + return generation + } + + /// Whether `generation` is still the transition in flight. + public func isCurrent(_ generation: Int) -> Bool { generation == self.generation } + + /// The transition's own callout: true when it is still current and its + /// shape still pending — the caller applies it now (it is no longer pending). + public mutating func take(_ generation: Int) -> Bool { + guard isCurrent(generation), pending != nil else { return false } + pending = nil + return true + } + + /// Ends whatever is in flight NOW: a new generation (stale callouts and + /// timers), and the shape still pending, which the caller applies at once. + public mutating func settle() -> Shape? { + generation += 1 + defer { pending = nil } + return pending + } + + /// Ends whatever is in flight and forgets its shape (a hide; a jump that + /// applies its own shape directly). + public mutating func cancel() { + generation += 1 + pending = nil + } +} + /// The pill's motion timing (`DictationPillController`): a transition moves /// the capsule on one spring and reshapes it on another, and the panel /// SETTLES (shrinks back to the capsule, offset zero) once the last of them diff --git a/Tests/KleothCoreTests/PillPressTests.swift b/Tests/KleothCoreTests/PillPressTests.swift new file mode 100644 index 0000000..19a62ba --- /dev/null +++ b/Tests/KleothCoreTests/PillPressTests.swift @@ -0,0 +1,152 @@ +import Testing +import Foundation +import CoreGraphics +@testable import KleothCore + +/// The pill's press, drag hold and transition bookkeeping (review of PR #32): +/// the decisions the controller makes about a press and about what a drag +/// holds, pure. Cocoa screen coordinates, y up. +@Suite struct PillPressTests { + + // MARK: Click or drag, in SCREEN space + + /// A press is a click until the pointer has travelled the threshold ON + /// SCREEN — a capsule growing under a still pointer (the dock opening) + /// moves nothing, so the release still clicks. + @Test func aStillPointerStaysAClickWhateverTheCapsuleDoes() { + var press = PillPress(at: CGPoint(x: 500, y: 40)) + let r1 = press.move(to: CGPoint(x: 501, y: 40)) + #expect(!r1) // 1 pt of jitter + let r2 = press.move(to: CGPoint(x: 503, y: 44)) + #expect(!r2) // 5 pt + #expect(press.isClick) + #expect(!press.dragged) + } + + /// Past the threshold the press becomes a drag exactly once, and a press + /// that became a drag is never a click again — not even when its drop was + /// made elsewhere and the release comes back to where it started. + @Test func aDragIsNeverAClick() { + var press = PillPress(at: CGPoint(x: 500, y: 40)) + let r3 = press.move(to: CGPoint(x: 507, y: 40)) + #expect(r3) // becomes a drag: once + let r4 = press.move(to: CGPoint(x: 700, y: 40)) + #expect(!r4) // already one + let r5 = press.move(to: CGPoint(x: 500, y: 40)) + #expect(!r5) // back home + #expect(press.dragged) + #expect(!press.isClick) + } + + @Test func aBadPointerNeverStartsADrag() { + var press = PillPress(at: CGPoint(x: 500, y: 40)) + let r6 = press.move(to: CGPoint(x: CGFloat.nan, y: 40)) + #expect(!r6) + #expect(press.isClick) + } + + // MARK: Which field: the settled capsule, along its own axis + + /// The pointer's offset along the capsule from its centre, in the + /// capsule's own (un-rotated) axis — the dock's fields run along it. The + /// same mapping as the hover lift: on the right edge the capsule is turned + /// +90° (its start at the top), on the left −90° (its start at the bottom). + @Test func alongTheCapsuleOnEveryEdge() { + let center = CGPoint(x: 400, y: 300) + let pointer = CGPoint(x: 430, y: 250) + #expect(PillGeometry.alongCapsule(pointer: pointer, capsuleCenter: center, edge: .bottom, flat: false) == 30) + #expect(PillGeometry.alongCapsule(pointer: pointer, capsuleCenter: center, edge: .top, flat: false) == 30) + #expect(PillGeometry.alongCapsule(pointer: pointer, capsuleCenter: center, edge: .right, flat: false) == 50) + #expect(PillGeometry.alongCapsule(pointer: pointer, capsuleCenter: center, edge: .left, flat: false) == -50) + // A flat bar is never turned. + #expect(PillGeometry.alongCapsule(pointer: pointer, capsuleCenter: center, edge: .right, flat: true) == 30) + // Through the dock's mapping: the first field is the one nearest the start. + let pitch: CGFloat = 65 + let top = CGPoint(x: 400, y: 300 + 1.5 * pitch) // on the right edge the start is at the top + #expect(PillGeometry.dockFieldIndex( + along: PillGeometry.alongCapsule(pointer: top, capsuleCenter: center, edge: .right, flat: false), + pitch: pitch, count: 4 + ) == 0) + } + + // MARK: What a drag holds + + /// While the pill is dragged a request is held for the drop and the latest + /// one wins, as it would have with no drag. + @Test func theLatestHeldRequestWins() { + var hold = PillDragHold() + let r7 = hold.receive(.show("saved"), hot: false) + #expect(r7 == .hold) + let r8 = hold.receive(.hide, hot: false) + #expect(r8 == .hold) + #expect(hold.heldPhase(hidden: "hidden") == "hidden") + let r9 = hold.receive(.show("done"), hot: false) + #expect(r9 == .hold) + #expect(hold.heldPhase(hidden: "hidden") == "done") + let r10 = hold.drop() + #expect(r10 == .show("done")) + let r11 = hold.drop() + #expect(r11 == nil) // once + #expect(hold.heldPhase(hidden: "hidden") == nil) + } + + /// A capture going hot is never held (a microphone that is live has its + /// indicator): it applies at once, and replaces anything older that was + /// held — the hot request is the newer one. + @Test func aHotCaptureIsNeverHeld() { + var hold = PillDragHold() + let r12 = hold.receive(.show("saved"), hot: false) + #expect(r12 == .hold) + let r13 = hold.receive(.show("meeting"), hot: true) + #expect(r13 == .applyNow) + let r14 = hold.drop() + #expect(r14 == nil) + // A held request after it is held again. + let r15 = hold.receive(.show("meetingSaved"), hot: false) + #expect(r15 == .hold) + let r16 = hold.drop() + #expect(r16 == .show("meetingSaved")) + } + + // MARK: Transitions: a pending shape lands, never vanishes + + /// A settle in the turn between a transition and its own callout lands + /// the transition's shape at once (it used to drop it: the panel was sized + /// for the new phase around the old one — an empty pill). + @Test func aSettleLandsThePendingShape() { + var ledger = PillTransitionLedger() + let first = ledger.begin("listening") + #expect(ledger.pending == "listening") + let r17 = ledger.settle() + #expect(r17 == "listening") + #expect(ledger.pending == nil) + // The transition's own turn then finds it superseded: nothing to apply. + let r18 = ledger.take(first) + #expect(!r18) + // Nothing pending: a settle lands nothing. + let r19 = ledger.settle() + #expect(r19 == nil) + } + + /// A later transition supersedes an earlier one; only the latest lands, + /// once; a hide forgets the pending shape. + @Test func onlyTheLatestTransitionLands() { + var ledger = PillTransitionLedger() + let first = ledger.begin("prompt") + let second = ledger.begin("idle") + #expect(!ledger.isCurrent(first)) + #expect(ledger.isCurrent(second)) + let r20 = ledger.take(first) + #expect(!r20) + let r21 = ledger.take(second) + #expect(r21) + #expect(ledger.pending == nil) + let r22 = ledger.take(second) + #expect(!r22) // once + #expect(ledger.isCurrent(second)) // its settle timer still counts + let third = ledger.begin("meeting") + ledger.cancel() + #expect(ledger.pending == nil) + #expect(!ledger.isCurrent(third)) + } +} From adf3b54a2f54d7959df7e34ccde922d245d2e3ce Mon Sep 17 00:00:00 2001 From: "a.sakhatskii" Date: Wed, 18 Nov 2026 11:38:33 +0400 Subject: [PATCH 6/8] Pill: review fixes for the drag (PR #32) - The controller owns the press, measured on screen: a drag dropped without the gesture can no longer end as a click, and a press while the dock opens is a click on the field it aims at (resolved on the settled dock). - show() drops a released drag before cancelling the auto-hide, and hideTask is never reassigned without a cancel: a held phase's replayed timer no longer dismisses the phase that came after it. - A 0.25 s button watch ends a drag whose mouse-up was lost; a capture going hot is never held by a drag: its bar shows at once where the pill is dragged. - VoiceOver gets the capsule's click as named actions. --- .../DictationPillController.swift | 252 ++++++++++++++---- .../KleothPillUI/DictationPillModel.swift | 15 ++ .../KleothPillUI/DictationPillView.swift | 105 ++++---- 3 files changed, 264 insertions(+), 108 deletions(-) diff --git a/app/Sources/KleothPillUI/DictationPillController.swift b/app/Sources/KleothPillUI/DictationPillController.swift index e91925f..4955b86 100644 --- a/app/Sources/KleothPillUI/DictationPillController.swift +++ b/app/Sources/KleothPillUI/DictationPillController.swift @@ -113,24 +113,22 @@ public final class DictationPillController: DictationPillPresenting { /// The rect the panel settles to when the in-flight transition completes; /// nil when the panel is settled (frame == capsule rect, offset == zero). private var pendingFrame: CGRect? - /// The shape an in-flight transition has yet to apply: `transition` puts - /// `model.phase` (with its capsule size and flatness) on the NEXT - /// main-queue turn, so for that turn the model still shows the old phase. - /// nil once applied — by the transition's turn, or by a `settle()` that - /// came first (a drag starting in that turn: the phase lands at once - /// instead of never) — or when a hide superseded it. - private var pendingShape: PendingShape? + /// The transitions in flight (`PillTransitionLedger`, KleothCore): each is + /// a generation, bumped by every transition, settle and hide so a stale + /// callout or settle timer can never shrink the panel to a rect that is no + /// longer the destination. Its PENDING shape is what a transition has yet + /// to apply: `transition` puts `model.phase` (with its capsule size and + /// flatness) on the NEXT main-queue turn, so for that turn the model still + /// shows the old phase. A `settle()` in that turn (a drag starting) lands + /// it at once instead of dropping it; a hide forgets it. + private var ledger = PillTransitionLedger() private struct PendingShape { var phase: DictationPillState var capsule: CGSize var flat: Bool } - /// The phase `pendingShape` carries. - private var pendingPhase: DictationPillState? { pendingShape?.phase } - /// Bumped by every transition, settle, and hide so a stale spring - /// completion can never shrink the panel to a rect that is no longer the - /// destination. - private var transitionGeneration = 0 + /// The phase the pending shape carries. + private var pendingPhase: DictationPillState? { ledger.pending?.phase } /// Where along the edge the pointer grabbed the pill, relative to the /// anchor center, so a drag does not snap the pill's center to the cursor. /// Reset to zero when a drag re-docks to an edge with the other axis. @@ -143,26 +141,27 @@ public final class DictationPillController: DictationPillPresenting { /// the spring's settle then threw the panel back to where the drag began. /// And a hover exit no longer collapses the dock out from under the drag. private(set) var isDragging = false - /// What the pill was asked to do while dragged — the LATEST request wins - /// (as it would have without the drag) and lands at the drop. - private var deferredDuringDrag: DeferredRequest? - private enum DeferredRequest { - case show(DictationPillState) - case hide - } + /// What the pill was asked to do while dragged (`PillDragHold`, + /// KleothCore): held for the drop, the LATEST request winning as it would + /// without the drag — except a capture going hot, which shows at once + /// where the pill is being dragged (`isHotCapture`). + private var dragHold = PillDragHold() + /// While a drag is in progress: watches the left button, so a drag whose + /// mouse-up never arrives (sleep, a lock, Mission Control, a system + /// prompt) is dropped within `dragWatchInterval` instead of holding the + /// pill — and every capture bar asked for meanwhile — until the next event. + private var dragWatchTask: Task? + static let dragWatchInterval: Duration = .milliseconds(250) + /// The press in progress on the capsule (`pressBegan` … `pressEnded`), + /// measured on screen (`PillPress`). + private var press: PillPress? /// The phase the pill is heading for: one asked for during a drag, else /// the one a transition has yet to apply, else what it shows. Every "is /// the pill resting?" decision reads this — `model.phase` alone answered /// for a phase that was already being replaced. private var upcomingPhase: DictationPillState { deferredPhase ?? pendingPhase ?? model.phase } /// The phase a drag is holding for the drop, if any. - private var deferredPhase: DictationPillState? { - switch deferredDuringDrag { - case .show(let state): return state - case .hide: return .hidden - case nil: return nil - } - } + private var deferredPhase: DictationPillState? { dragHold.heldPhase(hidden: .hidden) } public init(defaults: UserDefaults = .standard) { self.defaults = defaults @@ -180,13 +179,21 @@ public final class DictationPillController: DictationPillPresenting { // MARK: DictationPillPresenting public func show(_ state: DictationPillState) { + // A drag whose mouse-up never arrived is dropped FIRST: its drop may + // replay a held phase with its own auto-hide, and this newer request + // must cancel that timer, not run before it (a held `.saved`'s 4 s + // timer dismissed the `.listening` that dropped it). + endDragIfReleased() hideTask?.cancel() hideTask = nil - endDragIfReleased() if isDragging { - // Held for the drop (see `isDragging`). A `.hidden` still goes - // through `dismiss()`, whose backdrop decides — and is held too. - if state == .hidden { dismiss() } else { deferredDuringDrag = .show(state) } + // See `isDragging`. A `.hidden` still goes through `dismiss()`, + // whose backdrop decides — and is held too. + if state == .hidden { + dismiss() + } else if dragHold.receive(.show(state), hot: Self.isHotCapture(state)) == .applyNow { + applyWhileDragged(state) + } return } // The dock is out under the pointer: whatever replaces it starts from @@ -398,7 +405,7 @@ public final class DictationPillController: DictationPillPresenting { private func hideCompletely() { endDragIfReleased() if isDragging { - deferredDuringDrag = .hide + _ = dragHold.receive(.hide, hot: false) return } guard let panel, panel.isVisible else { @@ -415,6 +422,9 @@ public final class DictationPillController: DictationPillPresenting { withAnimation(.easeOut(duration: Self.fadeOutDuration)) { model.isPresented = false } + // Never reassigned without a cancel: a replaced timer would still + // fire (a replayed phase's auto-hide dismissed whatever came after). + hideTask?.cancel() hideTask = Task { [weak self] in try? await Task.sleep(for: .milliseconds(Int(Self.fadeOutDuration * 1000))) guard !Task.isCancelled, let self, let panel = self.panel else { return } @@ -456,8 +466,17 @@ public final class DictationPillController: DictationPillPresenting { closeMenu() settle() isDragging = true - deferredDuringDrag = nil + dragHold = PillDragHold() model.apply(dragging: true) + model.apply(pressedField: nil) + dragWatchTask?.cancel() + dragWatchTask = Task { @MainActor [weak self] in + while !Task.isCancelled { + try? await Task.sleep(for: Self.dragWatchInterval) + guard !Task.isCancelled, let self, self.isDragging else { return } + self.endDragIfReleased() + } + } // A collapse already counting down (the pointer left the dock just // before the press moved) must not fire under the drag. peekTask?.cancel() @@ -508,16 +527,18 @@ public final class DictationPillController: DictationPillPresenting { /// The drop: persists where the user left the pill — the edge it is /// docked on and its fraction along it (see `PillPlacement`) — then lets /// the pill go on from the new spot: to whatever it was asked to show - /// meanwhile (`deferredDuringDrag`), else to its own phase's spot, tucked + /// meanwhile (`dragHold`), else to its own phase's spot, tucked /// into the edge if resting. The pointer is read again last: a dock /// dropped with the pointer off it collapses (after `peekLinger`) at its /// NEW place, not halfway through the drag. func commitDraggedPlacement() { guard isDragging else { return } isDragging = false + dragWatchTask?.cancel() + dragWatchTask = nil model.apply(dragging: false) - let deferred = deferredDuringDrag - deferredDuringDrag = nil + model.apply(pressedField: nil) + let deferred = dragHold.drop() defer { reconsiderPointerAfterDrag() } guard let panel, let screen = panelScreen() ?? defaultScreen() else { replay(deferred) @@ -549,20 +570,132 @@ public final class DictationPillController: DictationPillPresenting { transition(to: CGRect(origin: origin, size: layout.panelSize), phase: model.phase, capsule: layout.capsuleSize, flat: layout.flat) } + // MARK: The press (DictationPillView.pressGesture) + + /// A press went down on the capsule (the gesture's first update): where, + /// ON SCREEN (`PillPress`). The gesture's own translation is measured in + /// the capsule's space, which moves with the window during a drag and + /// grows under a still pointer while the dock opens. + func pressBegan() { + press = PillPress(at: dragPointerLocation) + } + + /// The pointer moved with the button down: past the threshold the press + /// becomes a drag (once), and from then on the drag follows the pointer. + func pressMoved() { + if isDragging { + dragMoved() + return + } + if press == nil { pressBegan() } + guard var current = press else { return } + let becameDrag = current.move(to: dragPointerLocation) + press = current + if becameDrag { + beginDrag() + dragMoved() + } else if current.isClick { + // The dock field under the press, drawn pressed — resolved on the + // SETTLED dock, so a press while it opens names the field the + // user aimed at. + model.apply(pressedField: model.dockOut ? settledDockField(at: current.start) : nil) + } + } + + /// The button went up: a drop if the press became a drag — also when that + /// drag was already dropped without it (`endDragIfReleased`), which used + /// to fall through to a click — else a click, on the dock field under the + /// pointer (again on the settled dock). + func pressEnded() -> PillPressEnd { + defer { + press = nil + model.apply(pressedField: nil) + } + if isDragging { + dragMoved() + commitDraggedPlacement() + return .none + } + guard var current = press else { return .none } + _ = current.move(to: dragPointerLocation) + guard current.isClick else { return .none } + return .click(dockField: model.dockOut ? settledDockField(at: dragPointerLocation) : nil) + } + + /// The dock field under a screen point, on the dock as it will SETTLE + /// (`settledPanelFrame`): mid-peek the capsule is still growing, and the + /// field a user aims at is the one of the dock that is coming out. + private func settledDockField(at point: CGPoint) -> Int { + let frame = settledPanelFrame ?? .zero + let along = PillGeometry.alongCapsule( + pointer: point, capsuleCenter: CGPoint(x: frame.midX, y: frame.midY), + edge: model.edge, flat: ledger.pending?.flat ?? model.flat + ) + return PillGeometry.dockFieldIndex(along: along, pitch: model.dock.pitch, count: PillDockMetrics.fieldCount) + } + + /// The phases that mean a capture just went (or still is) hot — a + /// microphone live, a recording rolling. A drag never holds them: their + /// indicator shows at once (the hot-mic rule), where the pill is. + private static func isHotCapture(_ state: DictationPillState) -> Bool { + switch state { + case .armed, .listening, .recording, .meeting: + return true + case .hidden, .idle, .transcribing, .polishing, .done, .warning, .failed, .saving, .saved, + .meetingSaved, .prompt: + return false + } + } + + /// A hot capture's phase during a drag: applied NOW, without a spring + /// (nothing animates under a drag — a spring's stage is what the drag + /// used to overwrite), at the dragged position, and the drag goes on with + /// it. The grab is kept inside the new capsule, which may be shorter. + private func applyWhileDragged(_ state: DictationPillState) { + guard let panel, panel.isVisible else { return } + let layout = layout(for: state, edge: model.edge, on: panelScreen() ?? anchorScreen()) + guard model.phase != state || model.capsuleSize != layout.capsuleSize || model.flat != layout.flat else { return } + if state != .idle, peeking { + peekTask?.cancel() + peekTask = nil + peeking = false + model.apply(peeking: false) + } + var still = Transaction() + still.disablesAnimations = true + withTransaction(still) { + model.apply(labelWidth: layout.labelWidth) + model.apply(offset: .zero) + model.apply(phase: state) + model.apply(capsuleSize: layout.capsuleSize) + model.apply(flat: layout.flat) + } + // In place, content shown: a `.rise`'s reveal must never hide a live bar. + model.apply(beat: .morph) + releaseDockSurface() + let alongExtent = model.edge.isVertical ? layout.panelSize.height : layout.panelSize.width + let half = max(0, alongExtent / 2 - Self.shadowPadding) + dragGrabOffset = min(max(dragGrabOffset, -half), half) + dragMoved() + announce(state) + scheduleAutoHide(for: state) + } + /// A drag whose mouse-up never reached the gesture (its `onEnded` did not /// run) must not hold the pill forever: once the left button is up, it is - /// a drop. Checked wherever a held request arrives. Not in film mode — the - /// sandbox's presses are synthesized, the real button is up throughout. + /// a drop. Checked wherever a held request arrives, and every + /// `dragWatchInterval` while the drag lasts (`dragWatchTask`). The film + /// reads its own button (`filmLeftButtonDown`). @discardableResult private func endDragIfReleased() -> Bool { - guard isDragging, !ignoresRealPointer, NSEvent.pressedMouseButtons & 1 == 0 else { return false } + guard isDragging, !leftButtonDown else { return false } commitDraggedPlacement() return true } /// What a drag held back, now: a phase springs from the drop spot to its /// place at the new anchor; a hide fades the pill out there. - private func replay(_ request: DeferredRequest?) { + private func replay(_ request: PillDragHold.Request?) { switch request { case .show(let state): show(state) case .hide: hideCompletely() @@ -726,6 +859,17 @@ public final class DictationPillController: DictationPillPresenting { /// `ignoresRealPointer` is set. public var filmPointerLocation: CGPoint? + /// Film mode: whether the film's left button is down — what a drag checks + /// instead of `NSEvent.pressedMouseButtons` (the real button is up for + /// the whole film). The sandbox's `lose-mouseup` lifts it without sending + /// the mouse-up, the way a mouse-up lost to sleep or a system prompt does. + public var filmLeftButtonDown = false + + /// The left button, real or the film's. + private var leftButtonDown: Bool { + ignoresRealPointer ? filmLeftButtonDown : NSEvent.pressedMouseButtons & 1 != 0 + } + /// The pointer a drag follows: the real one, or the film's. private var dragPointerLocation: CGPoint { if ignoresRealPointer, let filmPointerLocation { return filmPointerLocation } @@ -1151,6 +1295,9 @@ public final class DictationPillController: DictationPillPresenting { /// The panel's current frame in screen coordinates (sandbox / tests). public var panelFrame: CGRect? { panel?.frame } + /// Where the panel is heading: the in-flight transition's destination, + /// else its frame — what a user aims a click at while the pill moves. + public var settledPanelFrame: CGRect? { panel.map { pendingFrame ?? $0.frame } } /// The capsule itself, un-rotated (width = its length along the edge) — /// for the sandbox's named pointer spots on a text phase (`label`). public var capsuleSize: CGSize { model.capsuleSize } @@ -1171,8 +1318,9 @@ public final class DictationPillController: DictationPillPresenting { panel.orderOut(nil) model.isPresented = false model.apply(phase: .hidden) - // A hidden panel is settled by definition; forget any in-flight move. - transitionGeneration += 1 + // A hidden panel is settled by definition; forget any in-flight move + // (its pending shape included — further down). + ledger.cancel() pendingFrame = nil model.apply(offset: .zero) model.apply(dockHeld: false) @@ -1189,7 +1337,6 @@ public final class DictationPillController: DictationPillPresenting { peekTask = nil peeking = false model.apply(peeking: false) - pendingShape = nil // A backdrop that rose while the panel faded out was only stored (the // old phase was still up); it takes over now, so a capture that // started in the fade never ends up with no bar (hot-mic rule). @@ -1198,6 +1345,7 @@ public final class DictationPillController: DictationPillPresenting { private func scheduleAutoHide(for state: DictationPillState) { guard let delay = state.autoHideAfter else { return } + hideTask?.cancel() hideTask = Task { [weak self] in try? await Task.sleep(for: delay) guard !Task.isCancelled else { return } @@ -1221,12 +1369,10 @@ public final class DictationPillController: DictationPillPresenting { to target: CGRect, phase: DictationPillState, capsule: CGSize, flat: Bool, fromDock: Bool = false ) { guard let panel else { return } - transitionGeneration += 1 - let generation = transitionGeneration guard !Self.reduceMotion else { + ledger.cancel() pendingFrame = nil - pendingShape = nil var still = Transaction() still.disablesAnimations = true withTransaction(still) { @@ -1273,15 +1419,15 @@ public final class DictationPillController: DictationPillPresenting { } let destination = Self.offset(ofCenter: CGPoint(x: target.midX, y: target.midY), in: stage) - pendingShape = PendingShape(phase: phase, capsule: capsule, flat: flat) + let generation = ledger.begin(PendingShape(phase: phase, capsule: capsule, flat: flat)) // The animated changes go out on the NEXT main-queue callout so // SwiftUI has committed the re-expressed start position first; a // change in the same turn would spring from the previous graph value, // which is a different point on the new stage. Task { @MainActor [weak self] in - guard let self, self.transitionGeneration == generation else { return } - // Applied below in this same callout (or already the model's). - self.pendingShape = nil + // Applied below in this same callout (or already the model's) — + // unless a later transition, a settle or a hide got there first. + guard let self, self.ledger.take(generation) else { return } let moves = self.model.offset != destination let reshapes = self.model.phase != phase || self.model.capsuleSize != capsule || self.model.flat != flat @@ -1331,7 +1477,7 @@ public final class DictationPillController: DictationPillPresenting { ) Task { @MainActor [weak self] in try? await Task.sleep(for: .seconds(delay)) - guard let self, self.transitionGeneration == generation else { return } + guard let self, self.ledger.isCurrent(generation) else { return } self.settle() } // The phase is `.idle` from here on; a pointer parked on the pill @@ -1350,11 +1496,9 @@ public final class DictationPillController: DictationPillPresenting { /// after a `show(.listening)` kept a capsule-less dock in a listening- /// sized panel while the dictation ran — an empty pill). private func settle() { - transitionGeneration += 1 var still = Transaction() still.disablesAnimations = true - if let shape = pendingShape { - pendingShape = nil + if let shape = ledger.settle() { withTransaction(still) { model.apply(phase: shape.phase) model.apply(capsuleSize: shape.capsule) diff --git a/app/Sources/KleothPillUI/DictationPillModel.swift b/app/Sources/KleothPillUI/DictationPillModel.swift index 6939bb4..f4e8d65 100644 --- a/app/Sources/KleothPillUI/DictationPillModel.swift +++ b/app/Sources/KleothPillUI/DictationPillModel.swift @@ -86,6 +86,9 @@ final class DictationPillModel: ObservableObject { /// The pill is being dragged (`DictationPillController.isDragging`): the /// closed-hand cursor, and no click lands at the drop. @Published private(set) var dragging = false + /// The dock field under a press that is still down and has not become a + /// drag — drawn pressed (`DictationPillController.pressMoved`). + @Published private(set) var pressedField: Int? // MARK: Controller-facing mutation @@ -151,6 +154,10 @@ final class DictationPillModel: ObservableObject { if dragging != on { dragging = on } } + func apply(pressedField field: Int?) { + if pressedField != field { pressedField = field } + } + func apply(dock metrics: PillDockMetrics) { if dock != metrics { dock = metrics } } @@ -251,6 +258,14 @@ final class DictationPillModel: ObservableObject { } } +/// How a press on the capsule ended (`DictationPillController.pressEnded`). +enum PillPressEnd: Equatable { + /// A drag (dropped now or already), or nothing to act on. + case none + /// A click; on the peek dock, the field under the pointer. + case click(dockField: Int?) +} + /// The pointer shapes the pill sets (`DictationPillModel.cursor`). Set by /// hand on every pointer move: Kleoth is never the active app, so AppKit's /// cursor rects (and SwiftUI's pointer styles) do not run for the panel. diff --git a/app/Sources/KleothPillUI/DictationPillView.swift b/app/Sources/KleothPillUI/DictationPillView.swift index 4d3d14e..a0caf98 100644 --- a/app/Sources/KleothPillUI/DictationPillView.swift +++ b/app/Sources/KleothPillUI/DictationPillView.swift @@ -64,12 +64,9 @@ struct DictationPillView: View { @EnvironmentObject private var model: DictationPillModel @Environment(\.accessibilityReduceMotion) private var reduceMotion - /// A press on the capsule is down (between the gesture's first - /// `onChanged` and its `onEnded`), not yet a drag. - @State private var pressing = false - /// The dock field under that press — drawn pressed until it lets go or - /// turns into a drag. - @State private var pressedField: Int? + /// A press on the capsule is down: marks the gesture's first update + /// (`pressGesture`). Resets by itself when the gesture ends or is cancelled. + @GestureState private var pressDown = false init(controller: DictationPillController) { self.controller = controller @@ -123,6 +120,9 @@ struct DictationPillView: View { // its own press first and never drags. .gesture(pressGesture) .help(model.phase.pillText) + .accessibilityElement(children: .contain) + .accessibilityLabel(Text(model.phase.pillText)) + .accessibilityActions { capsuleActions } // A pill on a side edge stands up in every phase. Applied AFTER // the hit shape and gestures so they rotate with it — a horizontal // hit capsule under a vertical bar would leave only its middle @@ -369,7 +369,7 @@ struct DictationPillView: View { // No hover lift under a drag: the dock is being // carried, not pointed at. pointer: model.dragging ? nil : dockPointer, - pressed: pressedField, + pressed: model.dragging ? nil : model.pressedField, rotation: edgeRotation, menuOpen: model.menuOpen, onField: { dockField($0) } @@ -585,59 +585,43 @@ struct DictationPillView: View { /// glitchy… no intuitive place to click to start dragging" — every point /// of the peek dock was a Button, and a press on a Button never reached /// the capsule's drag). Now every point of the capsule but a real button - /// drags once the press has travelled `PillGeometry.dragThreshold`, the - /// dock's fields included; a press that never travels is a click - /// (`click(at:dockField:)`), so a hand that drifts a few points while + /// drags once the press has travelled `PillGeometry.dragThreshold` ON + /// SCREEN, the dock's fields included; a press that never travels is a + /// click (`click(dockField:)`), so a hand that drifts a few points while /// pressing still clicks. /// - /// The pill slides along its edge (and re-docks when dragged clearly - /// toward another one); the controller reads `NSEvent.mouseLocation` - /// itself — `value.translation` is measured against a coordinate space - /// that moves with the window, so it double-counts and the pill runs away - /// from the cursor. It is read here only BEFORE the drag starts, while the - /// window is still, for the threshold. + /// The controller owns the press (`pressBegan` / `pressMoved` / + /// `pressEnded`, `PillPress`) and reads the pointer itself: + /// `value.translation` is measured in the capsule's space, which moves + /// with the window during a drag (it double-counts, and after a drop made + /// without this gesture it could read as a click) and grows under a still + /// pointer while the dock opens (a press there was no click). The + /// `GestureState` only marks a press's first update — and resets on its + /// own if the gesture is cancelled. private var pressGesture: some Gesture { DragGesture(minimumDistance: 0) - .onChanged { value in - if model.dragging { - controller.dragMoved() - return - } - if !pressing { - pressing = true - pressedField = dockOut ? dockFieldIndex(at: value.startLocation) : nil - } - guard PillGeometry.isDrag(translation: value.translation) else { return } - pressedField = nil - controller.beginDrag() - controller.dragMoved() + .updating($pressDown) { _, down, _ in + guard !down else { return } + down = true + controller.pressBegan() } - .onEnded { value in - pressing = false - let field = pressedField - pressedField = nil - if model.dragging { - controller.dragMoved() - controller.commitDraggedPlacement() - return - } - // A press that travelled is never a click — also when its - // drag was already dropped without it (`endDragIfReleased`). - guard !PillGeometry.isDrag(translation: value.translation) else { return } - click(at: value.startLocation, dockField: field) + .onChanged { _ in controller.pressMoved() } + .onEnded { _ in + if case .click(let field) = controller.pressEnded() { click(dockField: field) } } } /// A press that did not travel: click routing (screen-recording design /// §6.4). The capsule IS the control in the recording phases, so a tap on - /// it is the action — never a dismissal. - private func click(at location: CGPoint, dockField field: Int?) { + /// it is the action — never a dismissal. VoiceOver reaches the same + /// actions by name (`capsuleActions`). + private func click(dockField field: Int?) { switch model.phase { case .failed: controller.dismissFromUser() case .idle where dockOut: - // The dock's fields are its buttons: the one pressed acts. - dockField(field ?? dockFieldIndex(at: location)) + // The dock's fields are its buttons: the one under the pointer acts. + if let field { dockField(field) } case .listening(handsFree: true): controller.perform(.stopHandsFreeDictation) case .listening(handsFree: false): @@ -660,14 +644,27 @@ struct DictationPillView: View { } } - /// The dock field under a point in the capsule's own space (origin at its - /// top-left, un-rotated — the gesture is attached before the rotation), - /// through the hover lift's mapping (`PillGeometry.dockFieldIndex`). - private func dockFieldIndex(at location: CGPoint) -> Int { - PillGeometry.dockFieldIndex( - along: location.x - model.capsuleSize.width / 2, - pitch: model.dock.pitch, count: PillDockMetrics.fieldCount - ) + /// The capsule's own click, for VoiceOver: it was an `.onTapGesture`, + /// which VoiceOver could press; the press gesture that replaced it is not + /// one it knows. Named after what the click does; nothing where it does + /// nothing (the dock's fields are buttons of their own). + @ViewBuilder + private var capsuleActions: some View { + switch model.phase { + case .failed: + Button("Dismiss") { controller.dismissFromUser() } + case .listening(handsFree: true): + Button("Stop dictation") { controller.perform(.stopHandsFreeDictation) } + case .listening(handsFree: false): + Button("Keep listening hands-free") { controller.perform(.switchToHandsFree) } + case .saved: + Button("Show in Finder") { controller.perform(.revealLastRecording) } + case .meetingSaved: + Button("Open in History") { controller.perform(.meeting(.openLast)) } + case .hidden, .idle, .armed, .transcribing, .polishing, .done, .warning, .recording, .saving, + .meeting, .prompt: + EmptyView() + } } /// Dictate · Meeting · Screen · More. From 404ea0d1518c0f2d42a23dd1d94b0511ac73a68f Mon Sep 17 00:00:00 2001 From: "a.sakhatskii" Date: Wed, 18 Nov 2026 11:38:33 +0400 Subject: [PATCH 7/8] pillsandbox: lose-mouseup, film button state, clicks on the settled pill --- app/Sources/pillsandbox/main.swift | 44 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/app/Sources/pillsandbox/main.swift b/app/Sources/pillsandbox/main.swift index b064749..9c55afe 100644 --- a/app/Sources/pillsandbox/main.swift +++ b/app/Sources/pillsandbox/main.swift @@ -26,7 +26,8 @@ import KleothPillUI /// with spots mic|meet|rec|menu|center|stop|label (label = over a text phase's words, /// clear of its buttons), perform:startMeeting|stopMeeting|acceptOffer|neverOffer| /// acceptStop|startScreenRecording|…; press: / move:: / release = a real -/// drag through the pill's own window (move animates over its hold, screen points, y up); +/// drag through the pill's own window (move animates over its hold, screen points, y up; +/// lose-mouseup = the button goes up but the mouse-up is never delivered); /// any item may end in `@` to hold it that long /// instead of `--hold`, e.g. `listening@3.2`) /// `--demo dictation|screen` also composes every frame onto an 680×425 pt @@ -764,6 +765,7 @@ func film(_ args: Arguments, controller: DictationPillController, exitWhenDone: heldPointer = point heldInside = true controller.filmPointerLocation = point + controller.filmLeftButtonDown = true filmMouse(.leftMouseDown, at: point) } pointerSpot = nil @@ -776,9 +778,16 @@ func film(_ args: Arguments, controller: DictationPillController, exitWhenDone: } } else if name == "release" { // Mouse UP where the held pointer is; the pointer stays there. + controller.filmLeftButtonDown = false if let point = heldPointer { filmMouse(.leftMouseUp, at: point) } heldPointer = nil dragMove = nil + } else if name == "lose-mouseup" { + // The button goes up but the mouse-up never reaches the pill (sleep, + // a lock, a system prompt): only the button state changes. A later + // `release` delivers the late mouse-up. + controller.filmLeftButtonDown = false + dragMove = nil } else if name.hasPrefix("perform:") { // "perform:startScreenRecording|stopScreenRecording|startMeeting|stopMeeting|startHandsFreeDictation|…" // — fires a pill action through the controller, so the SANDBOX @@ -1023,8 +1032,10 @@ func writePNG(_ image: CGImage, to url: URL) throws { /// "along" is the panel's y there — the same mapping the view undoes in /// `dockPointer`. @MainActor -func filmPointer(_ spot: String, edge: PillGeometry.Edge, controller: DictationPillController) -> CGPoint? { - guard let frame = controller.panelFrame else { return nil } +func filmPointer( + _ spot: String, edge: PillGeometry.Edge, controller: DictationPillController, in panelFrame: CGRect? = nil +) -> CGPoint? { + guard let frame = panelFrame ?? controller.panelFrame else { return nil } let center = CGPoint(x: frame.width / 2, y: frame.height / 2) // The dock's fields through the SAME mapping `PeekDock`'s hit test uses // (`PillGeometry.dockField*`), so a film can never light the wrong field. @@ -1070,27 +1081,24 @@ func filmPointer(_ spot: String, edge: PillGeometry.Edge, controller: DictationP /// own event handling — the one thing the timer-driven `perform:` cannot do. @MainActor func filmClick(_ spot: String, edge: PillGeometry.Edge, controller: DictationPillController) { - guard let point = filmPointer(spot, edge: edge, controller: controller), - let frame = controller.panelFrame, - let window = NSApp.windows.first(where: { String(describing: type(of: $0)) == "DictationPanel" }) - else { print("click:\(spot): no pill window"); return } - // Root space is y-down from the top-left; NSEvent wants y-up from the bottom-left. - let location = CGPoint(x: point.x, y: frame.height - point.y) - let now = ProcessInfo.processInfo.systemUptime - for (type, pressure) in [(NSEvent.EventType.leftMouseDown, Float(1)), (.leftMouseUp, 0)] { - guard let event = NSEvent.mouseEvent( - with: type, location: location, modifierFlags: [], timestamp: now, - windowNumber: window.windowNumber, context: nil, eventNumber: 0, clickCount: 1, pressure: pressure - ) else { continue } - window.sendEvent(event) + guard let point = filmScreenPoint(spot, edge: edge, controller: controller) else { + print("click:\(spot): no pill window"); return } - print("click:\(spot) at \(location) in window \(window.windowNumber)") + controller.filmPointerLocation = point + controller.filmLeftButtonDown = true + filmMouse(.leftMouseDown, at: point) + controller.filmLeftButtonDown = false + filmMouse(.leftMouseUp, at: point) + print("click:\(spot) at \(point) (screen)") } /// A named spot on the pill in SCREEN coordinates (y up). @MainActor +/// Measured on the SETTLED panel (`settledPanelFrame`): mid-transition the +/// panel is a larger stage, and a user aims at where the pill is going. func filmScreenPoint(_ spot: String, edge: PillGeometry.Edge, controller: DictationPillController) -> CGPoint? { - guard let point = filmPointer(spot, edge: edge, controller: controller), let frame = controller.panelFrame else { return nil } + guard let frame = controller.settledPanelFrame, + let point = filmPointer(spot, edge: edge, controller: controller, in: frame) else { return nil } return CGPoint(x: frame.minX + point.x, y: frame.maxY - point.y) } From e0e4e9dfa9b6cb4b47eb9b16ea9d57708d4ed343 Mon Sep 17 00:00:00 2001 From: "a.sakhatskii" Date: Wed, 18 Nov 2026 11:38:33 +0400 Subject: [PATCH 8/8] Changelog and CLAUDE.md: hot captures during a drag, the screen-space press --- CHANGELOG.md | 7 ++++--- CLAUDE.md | 11 +++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adcdacc..4f33a77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,9 +15,10 @@ All notable changes to Kleoth are documented here. The format is based on shows an open hand where the pill only drags and a closed hand while you drag it. - **Dragging the pill collapsed it and threw it back.** When the pointer left the dock during a drag, the dock folded back into the edge at its old place halfway through the drag. A dragged - pill now keeps its shape until you let go, and a dictation that finishes, or a meeting or a - recording that starts, while you drag shows up where you drop it. The pill also no longer drops - a few points as the drag begins. + pill now keeps its shape until you let go. A dictation, a meeting or a screen recording that + starts while you drag shows its bar at once, right under the pointer; anything else that changes + (a dictation finishing, a recording saved) shows where you drop it. The pill also no longer drops + a few points as the drag begins, and a press on the dock while it is still opening is a click. - **The pill could go empty while it was busy.** A dictation, a meeting or a screen recording started from the pill showed a pill-shaped blank before its controls appeared, for as long as the start kept the Mac busy. And a pill that changed while you dragged it was drawn off its own diff --git a/CLAUDE.md b/CLAUDE.md index 768e517..e36227f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -13,7 +13,7 @@ Liquid Glass gated behind `if #available(macOS 26, *)`. ## Commands ```bash -swift build && swift test # core + CLI (790 tests); CI: .github/workflows/tests.yml (+ app smoke, marketing) +swift build && swift test # core + CLI (798 tests); CI: .github/workflows/tests.yml (+ app smoke, marketing) swift build --package-path app # app package swift test --package-path app # KleothCaptureTests: file/format logic on synthetic buffers (no mic) bash app/setup-signing.sh # once: "Kleoth Self-Signed" cert (Accessibility/TCC trust binds to it) @@ -39,7 +39,7 @@ swift build --package-path app --product micwatch && caffeinate -s -i app/.build swift build --package-path app --product micopen && caffeinate -s -i app/.build/debug/micopen 13 # holds the mic from another process, samples discarded; exit 3 without an existing mic grant (never prompts) swift run --package-path app pillsandbox # pill playground app/.build/debug/pillsandbox --film --edge right --sequence idle,armed,listening,done,idle # filmstrip PNGs (step@secs; --demo dictation|screen composes README frames) -app/.build/debug/pillsandbox --film [--start-delay s] [--start-block s] --sequence peek,press:center,move:240:80@1,release # a real drag; app-like start timing +app/.build/debug/pillsandbox --film [--start-delay s] [--start-block s] --sequence peek,press:center,move:240:80@1,release # a real drag (lose-mouseup = a lost mouse-up); app-like start timing bash app/branding-src/demo/make-demos.sh # README demo GIFs from the real pill → docs/assets/demo-*.gif bash app/branding-src/demo/make-app-demos.sh [data-dir] # meeting + viewer GIFs, screenshot-detail.png: a KleothDemo.app copy (-KleothDemo) films its own window bun app/branding-src/demo/make-demo-data.ts [--provider local] # fictional meetings/recording, real on-device transcripts + summaries (default: Claude Code) @@ -340,7 +340,9 @@ Design docs (binding contracts, error matrices, manual checklists): `docs/plans/ (`MeetingPillBridge` `.started`); `clearCapturePhaseBlocking` withdraws the other capture's. The other order leaves a hot mic with no bar. A rising bar clears ANY sticky `.failed` (a dictation's is terminal), and both `setBackdrop` and the coordinator read the phase still to land (`pendingPhase` / `upcomingState`) — or the - one a drag holds for the drop: while `isDragging`, every `show`/hide is deferred to the drop (latest wins). + one a drag holds for the drop: while `isDragging`, a `show`/hide waits for the drop (latest wins, `PillDragHold`) + except a capture going hot (`.armed`/`.listening`/`.recording`/`.meeting`), shown at once where it is dragged; a + lost mouse-up is caught by a 0.25 s button watch. Never reassign `hideTask` without cancelling it. - `referenceSize` is resolved against the four-field dock (275 pt, every edge) and, on bottom/top, the meeting bar (240 pt): a new longest shape must be folded in there, or a pill parked near a corner creeps. - Pill motion: never write the panel's frame mid-transition without zeroing `model.offset` (the capsule is drawn @@ -348,7 +350,8 @@ Design docs (binding contracts, error matrices, manual checklists): `docs/plans/ with a move and a reshape started in the same turn (every rise and sink) the completion never fired. - A SwiftUI `Button` inside the capsule takes the press, so the parent's drag never starts from it: what must drag (the dock's fields) is not a Button — the capsule's one `DragGesture(minimumDistance: 0)` clicks below - `PillGeometry.dragThreshold` and drags past it. Film clicks (`window.sendEvent`) can't fire a `.borderless` + `PillGeometry.dragThreshold` and drags past it, measured ON SCREEN (`PillPress`): its `translation` is + capsule-local, moves with the window and grows while the dock opens. Film clicks (`window.sendEvent`) can't fire a `.borderless` Button (AppKit's tracking loop): a prompt's answers or a fault's action go through `perform:`. - Core Audio: per-process `IsRunningInput` listeners register and never fire (listen to `IsRunning`/`Devices`, then re-read), and `AudioObjectRemovePropertyListenerBlock` from Swift silently removes nothing (listeners