Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

PiP follows two fingers smoothly and keeps the pointer on it, as in Arc

Victorbb9e7261541b
diff --git a/Packages/CraCore/Sources/CraCore/PictureInPictureThrow.swift b/Packages/CraCore/Sources/CraCore/PictureInPictureThrow.swift
index ee8d7b5..8596590 100644
--- a/Packages/CraCore/Sources/CraCore/PictureInPictureThrow.swift
+++ b/Packages/CraCore/Sources/CraCore/PictureInPictureThrow.swift
@@ -17,16 +17,22 @@ public struct PictureInPictureThrow: Sendable {
/// millisecond) comes to rest after v · 0.998 / (1 − 0.998) / 1000 = v · 0.499 s.
public static let projection: CGFloat = 0.998 / (1 - 0.998) / 1000
+ /// Where the fingers have the window: after the latest move, ahead of what is on screen.
public private(set) var frame: CGRect
/// False while the fingers only rest on the window: lifting them then throws nothing.
public private(set) var hasMoved = false
private let start: Double
private var samples: [(time: Double, dx: CGFloat, dy: CGFloat)] = []
+ /// The window's origin after each move, for `origin(at:)`.
+ private var path: [(time: Double, origin: CGPoint)]
+ /// The time between the last few moves (a trackpad sends about 120 a second, unevenly).
+ private var gaps: [Double] = []
/// The fingers touched down on a window at `frame` at `time` (seconds, any clock).
public init(frame: CGRect, at time: Double) {
self.frame = frame
start = time
+ path = [(time, frame.origin)]
}
/// The fingers moved the window by `dx, dy` at `time`; returns where it is now.
@@ -35,9 +41,41 @@ public struct PictureInPictureThrow: Sendable {
if dx != 0 || dy != 0 { hasMoved = true }
samples.append((time, dx, dy))
samples.removeAll { $0.time < time - 2 * Self.sampleWindow }
+
+ let last = path[path.count - 1]
+ let gap = time - last.time
+ if gap > 0.05 {
+ // The fingers rested before this move: they were still where they were until just
+ // before it, rather than creeping there since the last one.
+ path.append((time - (gaps.last ?? 1.0 / 120), last.origin))
+ } else if gap > 0 {
+ gaps.append(gap)
+ if gaps.count > 8 { gaps.removeFirst() }
+ }
+ path.append((max(time, last.time), frame.origin))
+ if path.count > 64 { path.removeFirst(path.count - 64) }
return frame
}
+ /// How far the window on screen runs behind the fingers: the longest of the last few gaps
+ /// between moves, so a frame almost always falls between two moves that have both come in.
+ public var lag: Double { min(max((gaps.max() ?? 1.0 / 120) + 0.001, 1.0 / 240), 1.0 / 30) }
+
+ /// Where to show the window for a display frame at `time` (the same clock as the moves):
+ /// where the fingers had it `lag` earlier, interpolated between the two moves around then.
+ /// Moving the window on each move instead puts it wherever the last move left it when the
+ /// screen refreshes, so a frame that caught two moves jumps twice as far as one that caught
+ /// one, and the window shudders even though the fingers glide. Past the latest move the
+ /// window waits for the next one rather than guessing ahead and stepping back.
+ public func origin(at time: Double) -> CGPoint {
+ let t = time - lag
+ guard let after = path.firstIndex(where: { $0.time > t }) else { return path[path.count - 1].origin }
+ guard after > 0 else { return path[0].origin }
+ let a = path[after - 1], b = path[after]
+ let f = CGFloat((t - a.time) / (b.time - a.time))
+ return CGPoint(x: a.origin.x + (b.origin.x - a.origin.x) * f, y: a.origin.y + (b.origin.y - a.origin.y) * f)
+ }
+
/// The window's speed when the fingers lift at `time`: how far it went in the moves of the
/// last `sampleWindow`, over the time since the move before them. Fingers that rested before
/// lifting throw nothing.
diff --git a/Packages/CraCore/Tests/CraCoreTests/PictureInPictureThrowTests.swift b/Packages/CraCore/Tests/CraCoreTests/PictureInPictureThrowTests.swift
index db44ce9..1035e67 100644
--- a/Packages/CraCore/Tests/CraCoreTests/PictureInPictureThrowTests.swift
+++ b/Packages/CraCore/Tests/CraCoreTests/PictureInPictureThrowTests.swift
@@ -24,6 +24,47 @@ struct PictureInPictureThrowTests {
#expect(s.frame == window.offsetBy(dx: -40, dy: 20))
}
+ /// Fingers gliding at 600 pt/s whose moves come unevenly (4 ms, then 12 ms apart, the way a
+ /// trackpad's land against a 120 Hz screen): the window on screen still steps the same
+ /// distance every frame, where showing the latest move steps 0, 5 or 10 points. Moves and
+ /// frames are replayed in time order, so a frame only sees the moves that came before it.
+ @Test func eachFrameStepsEvenly() {
+ var s = PictureInPictureThrow(frame: window, at: 0)
+ var shown: [CGFloat] = [], latest: [CGFloat] = []
+ var t = 0.0, i = 0
+ var frame = 1.0 / 120
+ while t < 0.5 {
+ let next = t + (i % 2 == 0 ? 0.004 : 0.012)
+ while frame < next {
+ if frame > 0.1 {
+ shown.append(s.origin(at: frame).x)
+ latest.append(s.frame.minX)
+ }
+ frame += 1.0 / 120
+ }
+ _ = s.move(dx: CGFloat((next - t) * 600), dy: 0, at: next)
+ t = next
+ i += 1
+ }
+ let steps = zip(shown.dropFirst(), shown).map { $0 - $1 }
+ let latestSteps = zip(latest.dropFirst(), latest).map { $0 - $1 }
+ #expect(steps.allSatisfy { abs($0 - 5) < 0.01 })
+ #expect(latestSteps.contains { abs($0 - 5) > 2 })
+ // About a move behind the fingers, no more.
+ #expect(s.lag < 0.015)
+ }
+
+ /// Fingers that stop leave the window where they stopped, not past it.
+ @Test func theWindowStopsWithTheFingers() {
+ var s = PictureInPictureThrow(frame: window, at: 0)
+ for i in 1...10 { _ = s.move(dx: 8, dy: 0, at: Double(i) / 120) }
+ #expect(s.origin(at: 1).x == window.minX + 80)
+ // Resting, then moving again: the window doesn't creep there over the rest.
+ _ = s.move(dx: 8, dy: 0, at: 1)
+ #expect(s.origin(at: 0.9).x == window.minX + 80)
+ #expect(s.origin(at: 1.05).x == window.minX + 88)
+ }
+
@Test func speedComesFromTheLastMoves() {
let (s, t) = swipe(-10, 0, frames: 30)
let v = s.velocity(at: t)
diff --git a/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift b/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift
index cfb812c..2a3ef8f 100644
--- a/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift
+++ b/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift
@@ -211,9 +211,10 @@ final class PictureInPictureOverlayView: NSView {
override func viewWillMove(toWindow newWindow: NSWindow?) {
if newWindow == nil {
BackgroundCursor.allow(false)
- flightLink?.invalidate()
- flightLink = nil
+ stopLink()
+ swipe = nil
flight = nil
+ releasePointer()
}
}
@@ -241,6 +242,9 @@ final class PictureInPictureOverlayView: NSView {
return
}
land()
+ swipe = nil
+ releasePointer()
+ stopLink()
gesture = PictureInPictureGesture(mouse: Self.screenLocation(of: event), frame: target.frame)
}
@@ -313,13 +317,18 @@ final class PictureInPictureOverlayView: NSView {
private var swipe: PictureInPictureThrow?
private var flight: PictureInPictureFlight?
- private var flightLink: CADisplayLink?
- private var flightTime: CFTimeInterval = 0
+ /// Runs while the fingers are on the window and while it flies: the window moves once per
+ /// screen refresh, never straight from a scroll event.
+ private var link: CADisplayLink?
+ private var lastTick: CFTimeInterval = 0
+ /// Where the pointer sits in the window while it rides along with the fingers.
+ private var carried: CGVector?
/// Two fingers on the trackpad (or a Magic Mouse) move the window with them, and when they
/// lift it flies on into the corner of the screen it was heading for, as Arc's does. A wheel
/// without phases (a plain mouse) moves nothing, and AppKit's own momentum after the lift is
- /// left out: the flight is the momentum.
+ /// left out: the flight is the momentum. Scroll events only record where the fingers are;
+ /// `tick` shows the window there, a frame at a time (see `PictureInPictureThrow.origin(at:)`).
override func scrollWheel(with event: NSEvent) {
guard gesture == nil, let target, event.hasPreciseScrollingDeltas, event.momentumPhase.isEmpty else { return }
let t = event.timestamp
@@ -327,45 +336,87 @@ final class PictureInPictureOverlayView: NSView {
case .mayBegin, .began:
land()
swipe = PictureInPictureThrow(frame: target.frame, at: t)
+ startLink()
case .changed:
if swipe == nil {
land()
swipe = PictureInPictureThrow(frame: target.frame, at: t)
+ startLink()
}
// The window goes where the fingers go: with natural scrolling the deltas already
// follow them, without it they're the other way round.
let toFingers: CGFloat = event.isDirectionInvertedFromDevice ? 1 : -1
- guard let frame = swipe?.move(dx: toFingers * event.scrollingDeltaX, dy: -toFingers * event.scrollingDeltaY, at: t) else { return }
- target.setFrameOrigin(frame.origin)
+ _ = swipe?.move(dx: toFingers * event.scrollingDeltaX, dy: -toFingers * event.scrollingDeltaY, at: t)
case .ended, .cancelled:
guard let s = swipe else { return }
swipe = nil
- guard s.hasMoved else { return }
- throwWindow(target, from: s.frame, velocity: event.phase == .ended ? s.velocity(at: t) : .zero)
+ releasePointer()
+ guard s.hasMoved else { return stopLink() }
+ throwWindow(target, aim: s.frame, velocity: event.phase == .ended ? s.velocity(at: t) : .zero)
default:
break
}
}
- private func throwWindow(_ target: NSWindow, from frame: NSRect, velocity: CGVector) {
- let rest = PictureInPictureThrow.restingCentre(of: frame, velocity: velocity)
+ /// The fingers lifted with the window shown at its own frame and aimed at `aim` (where the
+ /// fingers had it): it flies on from where it is shown, at their speed, so the hand-off is seamless.
+ private func throwWindow(_ target: NSWindow, aim: NSRect, velocity: CGVector) {
+ let rest = PictureInPictureThrow.restingCentre(of: aim, velocity: velocity)
let screen = NSScreen.screens.first { $0.frame.contains(rest) } ?? target.screen ?? NSScreen.main
- guard let visible = screen?.visibleFrame else { return }
- let corner = PictureInPictureThrow.corner(for: frame, velocity: velocity, in: visible)
- let end = PictureInPictureThrow.frame(in: corner, size: frame.size, visible: visible, margin: PictureInPictureChrome.edgeMargin)
+ guard let visible = screen?.visibleFrame else { return stopLink() }
+ let corner = PictureInPictureThrow.corner(for: aim, velocity: velocity, in: visible)
+ let end = PictureInPictureThrow.frame(in: corner, size: aim.size, visible: visible, margin: PictureInPictureChrome.edgeMargin)
CraLog.note("pip: swipe to \(corner.rawValue) at \(Int(hypot(velocity.dx, velocity.dy))) pt/s")
- flight = PictureInPictureFlight(from: frame.origin, velocity: velocity, to: end.origin)
- flightLink?.invalidate()
- let link = displayLink(target: self, selector: #selector(flightStep(_:)))
- link.add(to: .main, forMode: .common)
- flightLink = link
- flightTime = 0
+ flight = PictureInPictureFlight(from: target.frame.origin, velocity: velocity, to: end.origin)
+ startLink()
+ }
+
+ /// Scroll events go to the window under the pointer, so once the window slid out from under
+ /// it the fingers moved whatever was behind instead. As in Arc, the pointer hides when the
+ /// window starts to move and rides along at its spot in the window until the fingers lift.
+ private func carryPointer(from old: CGPoint, to origin: CGPoint) {
+ if carried == nil {
+ let m = NSEvent.mouseLocation
+ carried = CGVector(dx: m.x - old.x, dy: m.y - old.y)
+ NSCursor.hide()
+ }
+ guard let c = carried, let primary = NSScreen.screens.first else { return }
+ CGWarpMouseCursorPosition(CGPoint(x: origin.x + c.dx, y: primary.frame.maxY - (origin.y + c.dy)))
+ // A warp holds the pointer's own moves back for a quarter of a second; this lifts the hold.
+ CGAssociateMouseAndMouseCursorPosition(1)
+ }
+
+ private func releasePointer() {
+ guard carried != nil else { return }
+ carried = nil
+ NSCursor.unhide()
}
- @objc private func flightStep(_ link: CADisplayLink) {
- guard let target, var f = flight else { return land() }
- let dt = flightTime == 0 ? link.targetTimestamp - link.timestamp : link.timestamp - flightTime
- flightTime = link.timestamp
+ private func startLink() {
+ guard link == nil else { return }
+ let l = displayLink(target: self, selector: #selector(tick(_:)))
+ l.add(to: .main, forMode: .common)
+ link = l
+ lastTick = 0
+ }
+
+ private func stopLink() {
+ link?.invalidate()
+ link = nil
+ }
+
+ @objc private func tick(_ link: CADisplayLink) {
+ let dt = lastTick == 0 ? link.targetTimestamp - link.timestamp : link.timestamp - lastTick
+ lastTick = link.timestamp
+ guard let target else { return stopLink() }
+ if let s = swipe {
+ let origin = s.origin(at: link.timestamp)
+ guard origin != target.frame.origin else { return }
+ carryPointer(from: target.frame.origin, to: origin)
+ target.setFrameOrigin(origin)
+ return
+ }
+ guard var f = flight else { return stopLink() }
target.setFrameOrigin(f.step(max(dt, 1.0 / 240)))
flight = f
if f.isSettled { land() }
@@ -374,10 +425,9 @@ final class PictureInPictureOverlayView: NSView {
/// Ends a flight where the window is (a new swipe or press catches it mid-air) and keeps the
/// spot for the next video.
private func land() {
- guard let link = flightLink else { return }
- link.invalidate()
- flightLink = nil
+ guard flight != nil else { return }
flight = nil
+ if swipe == nil { stopLink() }
if let target { PictureInPictureChrome.shared.remember(target.frame) }
}
}