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.

Shift + wheel, or a sideways wheel, over the sidebar switches spaces like Arc (#93) (#123)

Victor Kildahle875b92f4840
diff --git a/Packages/CraCore/Sources/CraCore/WheelSpaceSwitch.swift b/Packages/CraCore/Sources/CraCore/WheelSpaceSwitch.swift
new file mode 100644
index 0000000..35146a2
--- /dev/null
+++ b/Packages/CraCore/Sources/CraCore/WheelSpaceSwitch.swift
@@ -0,0 +1,58 @@
+import CoreGraphics
+import Foundation
+
+/// Switching spaces with a mouse wheel over the sidebar (issue #93). macOS turns Shift + wheel
+/// into a horizontal wheel before an app sees it, so this reads a wheel's horizontal deltas, the
+/// same as a mouse's own sideways wheel; Arc 1.171 switches on both. Arc pages this way through
+/// `PageSwipeController`'s `NonGesturalScrollState {cumulativeDistance, lastEventTime,
+/// lastTriggerTime}`: quick wheel events add up, and past one line the next or previous space
+/// comes in, at most once per `cooldown` (docs/arc-reverse-engineering.md, "Shift + wheel").
+public struct WheelSpaceSwitch: Sendable {
+ /// More than this many lines (AppKit's `scrollingDeltaX` for a wheel) switch: one slow
+ /// notch does nothing, a flick does (Arc: 1 line never, 2 lines always).
+ public static let distance: CGFloat = 1
+ /// A longer gap between wheel events forgets what had added up (Arc: two notches 35 ms
+ /// apart switch, 45 ms apart half the time, 65 ms apart never).
+ public static let pause: TimeInterval = 0.045
+ /// After a switch the wheel does nothing for this long (Arc: one switch for a 0.245 s burst,
+ /// two for 0.254 s).
+ public static let cooldown: TimeInterval = 0.25
+
+ public private(set) var cumulativeDistance: CGFloat = 0
+ public private(set) var lastEventTime: TimeInterval = -.infinity
+ public private(set) var lastTriggerTime: TimeInterval = -.infinity
+
+ public init() {}
+
+ /// Feeds one wheel event of `dx` lines at `time`. Returns the page step it makes: +1 toward
+ /// the next space, −1 toward the previous one (a positive `dx`, as two fingers moving right
+ /// go back), 0 none.
+ public mutating func step(dx: CGFloat, at time: TimeInterval) -> Int {
+ defer { lastEventTime = time }
+ guard dx != 0, time - lastTriggerTime >= Self.cooldown else { return 0 }
+ if time - lastEventTime > Self.pause || (dx > 0) != (cumulativeDistance > 0) { cumulativeDistance = 0 }
+ cumulativeDistance += dx
+ guard abs(cumulativeDistance) > Self.distance else { return 0 }
+ let step = cumulativeDistance > 0 ? -1 : 1
+ cumulativeDistance = 0
+ lastTriggerTime = time
+ return step
+ }
+
+ /// Where a wheel step goes, the Library being the page before the first space as in a swipe.
+ public enum Target: Equatable, Sendable {
+ case space(Int)
+ case openLibrary
+ case closeLibrary
+ }
+
+ /// The page `step` leads to from space `index` of `count`, or from the Library when
+ /// `inLibrary`; nil at either end (Arc doesn't wrap). `libraryAllowed` is false where a swipe
+ /// can't bring the Library in either (a hidden or floating sidebar).
+ public static func target(step: Int, index: Int, count: Int, inLibrary: Bool, libraryAllowed: Bool) -> Target? {
+ if inLibrary { return step > 0 ? .closeLibrary : nil }
+ let next = index + step
+ if next < 0 { return libraryAllowed && step < 0 ? .openLibrary : nil }
+ return next < count && step != 0 ? .space(next) : nil
+ }
+}
diff --git a/Packages/CraCore/Tests/CraCoreTests/WheelSpaceSwitchTests.swift b/Packages/CraCore/Tests/CraCoreTests/WheelSpaceSwitchTests.swift
new file mode 100644
index 0000000..48b716e
--- /dev/null
+++ b/Packages/CraCore/Tests/CraCoreTests/WheelSpaceSwitchTests.swift
@@ -0,0 +1,70 @@
+import CoreGraphics
+import CraCore
+import Testing
+
+/// Shift + wheel (or a sideways wheel) over the sidebar switches spaces like Arc 1.171 (issue #93).
+struct WheelSpaceSwitchTests {
+ /// Feeds `(dx, time)` events and returns the steps they make.
+ private func steps(_ events: [(CGFloat, Double)]) -> [Int] {
+ var wheel = WheelSpaceSwitch()
+ return events.map { wheel.step(dx: $0.0, at: $0.1) }
+ }
+
+ @Test func oneSlowNotchDoesNothing() {
+ #expect(steps([(-1, 10)]) == [0])
+ // Notches further apart than the pause never add up.
+ #expect(steps([(-1, 10), (-1, 10.065), (-1, 10.13), (-1, 10.2)]) == [0, 0, 0, 0])
+ }
+
+ @Test func aFlickSwitches() {
+ #expect(steps([(-2, 10)]) == [1])
+ #expect(steps([(-1, 10), (-1, 10.035)]) == [0, 1])
+ #expect(steps([(-3, 10)]) == [1])
+ }
+
+ /// A positive delta (Shift + wheel one way, fingers right on a trackpad) goes back.
+ @Test func directionFollowsTheSwipe() {
+ #expect(steps([(2, 10)]) == [-1])
+ #expect(steps([(1, 10), (1, 10.02)]) == [0, -1])
+ }
+
+ @Test func aTurnStartsTheCountAfresh() {
+ #expect(steps([(-1, 10), (1, 10.02), (-1, 10.04)]) == [0, 0, 0])
+ }
+
+ /// One switch per quarter second, however fast the wheel spins (Arc: a 0.245 s burst of
+ /// 3-line events switched once, a 0.254 s burst twice).
+ @Test func coolsDownAfterASwitch() {
+ let burst = (0..<11).map { (CGFloat(-3), 10 + Double($0) * 0.0245) }
+ #expect(steps(burst).filter { $0 != 0 } == [1])
+ let longer = (0..<12).map { (CGFloat(-3), 10 + Double($0) * 0.023) }
+ #expect(steps(longer).filter { $0 != 0 } == [1, 1])
+ }
+
+ /// What adds up during the cooldown is dropped, so the next switch needs a flick of its own.
+ @Test func theCooldownDropsWhatItSees() {
+ #expect(steps([(-2, 10), (-1, 10.03), (-1, 10.06), (-1, 10.26)]) == [1, 0, 0, 0])
+ #expect(steps([(-2, 10), (-1, 10.26), (-1, 10.28)]) == [1, 0, 1])
+ }
+
+ @Test func sidewaysDeltaOfZeroIsIgnored() {
+ #expect(steps([(-1, 10), (0, 10.02), (-1, 10.04)]) == [0, 0, 1])
+ }
+
+ @Test(arguments: [
+ // (step, index, count, inLibrary, libraryAllowed, target)
+ (1, 0, 3, false, true, WheelSpaceSwitch.Target.space(1)),
+ (-1, 2, 3, false, true, .space(1)),
+ (1, 2, 3, false, true, nil),
+ (-1, 0, 3, false, true, .openLibrary),
+ (-1, 0, 3, false, false, nil),
+ (-1, 0, 1, false, true, .openLibrary),
+ (1, 0, 1, false, true, nil),
+ (1, 0, 3, true, true, .closeLibrary),
+ (-1, 0, 3, true, true, nil),
+ (0, 1, 3, false, true, nil),
+ ] as [(Int, Int, Int, Bool, Bool, WheelSpaceSwitch.Target?)])
+ func pagesLikeASwipeWithoutWrapping(step: Int, index: Int, count: Int, inLibrary: Bool, libraryAllowed: Bool, target: WheelSpaceSwitch.Target?) {
+ #expect(WheelSpaceSwitch.target(step: step, index: index, count: count, inLibrary: inLibrary, libraryAllowed: libraryAllowed) == target)
+ }
+}
diff --git a/Sources/Cra/UI/Windows/MainWindowController.swift b/Sources/Cra/UI/Windows/MainWindowController.swift
index 7124392..3df6808 100644
--- a/Sources/Cra/UI/Windows/MainWindowController.swift
+++ b/Sources/Cra/UI/Windows/MainWindowController.swift
@@ -31,6 +31,8 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM
private var swipeTravel: CGFloat = 0
private var swipeSamples: [(time: TimeInterval, dx: CGFloat)] = []
private var swipeDetent = 0
+ /// Shift + wheel over the sidebar adding up toward a space change.
+ private var wheelSpaceSwitch = WheelSpaceSwitch()
/// The Library's reveal a swipe started from: 0 on the first space, 1 in the Library; nil
/// when the swipe can only page between spaces.
private var librarySwipeFrom: CGFloat?
@@ -1540,6 +1542,11 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM
if !event.hasPreciseScrollingDeltas { self.showListKnobs() }
// The momentum tail of a space swipe belongs to the swipe, not the tab list.
if !event.momentumPhase.isEmpty { return self.swipeAxis == .horizontal ? nil : event }
+ // A wheel turned sideways, or with Shift (which macOS turns sideways), pages
+ // between spaces over the sidebar like Arc (issue #93).
+ if !event.hasPreciseScrollingDeltas, abs(event.scrollingDeltaX) > abs(event.scrollingDeltaY) {
+ return self.wheelSwitchesSpace(event) ? nil : event
+ }
// The floating sidebar is in its own panel (FloatingSidebarController): its swipes
// come from that window.
let host = self.shell.sidebarHost
@@ -1747,15 +1754,46 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM
/// The Spaces page's cards under the pointer are scrolled to their end (or there are none).
private func spacesStripAtEnd(_ event: NSEvent) -> Bool {
- guard let root = shell.sidebarHost.window?.contentView?.superview else { return false }
- var v = root.hitTest(event.locationInWindow)
- while let view = v, !(view is NSScrollView && (view as? NSScrollView)?.hasHorizontalScroller == true) { v = view.superview }
// Arc ignores a swipe over the Library's rail.
- guard let strip = v as? NSScrollView else { return false }
+ guard let strip = spacesStrip(at: event) else { return false }
let clip = strip.contentView
return clip.bounds.minX >= (strip.documentView?.frame.width ?? 0) - clip.bounds.width - 1
}
+ /// The Spaces page's row of cards under the pointer.
+ private func spacesStrip(at event: NSEvent) -> NSScrollView? {
+ guard let root = shell.sidebarHost.window?.contentView?.superview else { return nil }
+ var v = root.hitTest(event.locationInWindow)
+ while let view = v, !(view is NSScrollView && (view as? NSScrollView)?.hasHorizontalScroller == true) { v = view.superview }
+ return v as? NSScrollView
+ }
+
+ /// Shift + wheel, or a sideways wheel, over the sidebar: past a flick the next or previous
+ /// space comes in on the space change's spring, and before the first space the Library
+ /// slides in, as a swipe would bring it (Arc 1.171, WheelSpaceSwitch). Returns false for a
+ /// wheel the sidebar doesn't take.
+ private func wheelSwitchesSpace(_ event: NSEvent) -> Bool {
+ let host = shell.sidebarHost
+ guard shell.sidebarVisible || shell.floatingSidebar, event.window != nil, event.window === host.window,
+ host.bounds.contains(host.convert(event.locationInWindow, from: nil)), !isIncognito else { return false }
+ let dx = event.scrollingDeltaX
+ // On the Spaces page the cards scroll first; at their end the wheel leaves the Library, and
+ // over the rail it leaves at once (Arc).
+ if sidebarUI.libraryPage == .spaces, spacesStrip(at: event) != nil, dx > 0 || !spacesStripAtEnd(event) { return false }
+ let step = wheelSpaceSwitch.step(dx: dx, at: event.timestamp)
+ guard step != 0 else { return true }
+ let order = store.spaceOrder
+ // Where the Library is headed, if it is sliding.
+ let inLibrary = librarySettleSpring.map { $0.target >= 1 } ?? (sidebarUI.libraryPage != nil)
+ switch WheelSpaceSwitch.target(step: step, index: order.firstIndex(of: focusedSpaceID) ?? 0, count: order.count,
+ inLibrary: inLibrary, libraryAllowed: !shell.floatingSidebar && shell.sidebarVisible) {
+ case .space(let i): store.focusSpace(order[i], window: windowID)
+ case .openLibrary, .closeLibrary: toggleLibrary()
+ case nil: break
+ }
+ return true
+ }
+
/// A space swipe of `steps` trackpad samples `dx` apart, 8 ms apart (cra://debug?swipe=).
func debugSpaceSwipe(dx: CGFloat, steps: Int) {
beginSpaceSwipe()
diff --git a/docs/arc-reverse-engineering.md b/docs/arc-reverse-engineering.md
index 89b00e6..e5e3e60 100644
--- a/docs/arc-reverse-engineering.md
+++ b/docs/arc-reverse-engineering.md
@@ -2247,3 +2247,28 @@ was frontmost, 1x region captures and 100 fps recordings; test tabs archived aft
what is in effect at launch and after each change (`secure dns: automatic, no provider (system resolver)`, `secure
via dns.google`, `off`), and Export Diagnostics' summary has a `Secure DNS:` line (`SecureDNS` in CraCore, hosts
only).
+
+## Shift + wheel switches spaces (Arc 1.171, 2 Nov 2026, issue #93, live Arc, HID wheel events)
+
+Victor's Arc (2 spaces, Victor and JYSK), the pointer over the sidebar's list, line-unit wheel events posted at the HID
+tap with a real Shift key held around them; the space read from each window's `sidebarSpaceName` AX frames.
+- [O probe] macOS turns Shift + vertical wheel into a horizontal wheel before the app sees it: `scrollingDeltaX` =
+ the posted lines (1.0 per line, point delta 8), `scrollingDeltaY` 0, not precise, no phases, Shift in the flags.
+ A sideways wheel without Shift arrives the same way (point delta 10 per line) without the flag.
+- [O static] `PageSwipeController.nonGesturalScrollState {cumulativeDistance, lastEventTime, lastTriggerTime}`.
+- [O] Over the sidebar both Shift + wheel and a sideways wheel page between spaces. Negative `scrollingDeltaX` = the
+ next space, positive = the previous one (as two fingers moving right go back). No wrap at the last space; before
+ the first space the Library comes in (its Spaces page here), and the opposite flick takes it out again, over the
+ rail as over the cards (cards that fit have nothing to scroll). Over the page: nothing. Vertical wheel: nothing.
+- [O] Distance: one 1-line event never switches; one 2-, 3- or 6-line event always does. Two 1-line events switch
+ 35 ms apart (4/4), 45 ms apart half the time (1/2 + borderline runs at 50 ms), 65, 80, 150 ms apart never
+ (0/6); eight notches 100 ms apart never. So quick events add up and a gap of ~45 ms forgets them.
+- [O] Cooldown: 3-line events every ~22 ms switched once for bursts up to 0.245 s long, twice for 0.254–0.306 s:
+ one switch per 0.25 s. Ten 1-line events 30 ms apart (first switch at the 2nd event) switched once, so what
+ arrives during the cooldown is dropped.
+- [O 120 fps] The switch animates like a space change: the header slides in and the tint follows, ~0.15–0.2 s.
+- Cra: `WheelSpaceSwitch` (CraCore) with distance > 1 line, pause 45 ms, cooldown 0.25 s; the window controller's
+ scroll monitor feeds it non-precise horizontal wheels over the sidebar (docked or floating, not incognito). A
+ step focuses the space on Cra's usual space-change spring (Dia's 0.4 s, slower than Arc's here), or slides the
+ Library in or out like its button. On the Spaces page the cards take the wheel first (10 pt per line, as in Arc)
+ and the wheel leaves the Library only at their end.