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.
Make hidden sidebar reveal reliable during rapid re-entry (#19)
Quinn Battersby54c189fddd98
diff --git a/Sources/Cra/UI/ShellView.swift b/Sources/Cra/UI/ShellView.swiftindex cb14f8d..f231e51 100644--- a/Sources/Cra/UI/ShellView.swift+++ b/Sources/Cra/UI/ShellView.swift@@ -422,9 +422,10 @@ final class ShellView: NSView {super.mouseMoved(with: event)let p = convert(event.locationInWindow, from: nil)guard !sidebarVisible, !contentFullscreen else { return }- if !floatingSidebar, p.x <= 2 {+ if !floatingSidebar, FloatingSidebarGeometry.shouldReveal(at: p, in: bounds) {onEdgeHover?(true)- } else if floatingSidebar, p.x > sidebarWidth + 24 {+ } else if floatingSidebar, FloatingSidebarGeometry.shouldHide(at: p, in: bounds, sidebarWidth: sidebarWidth,+ pressedMouseButtons: NSEvent.pressedMouseButtons) {onEdgeHover?(false)}}@@ -465,7 +466,7 @@ final class ShellView: NSView {}if floating {// The floating sidebar hovers over the page; the card does not move (Arc).- let m: CGFloat = 8+ let m = FloatingSidebarGeometry.marginlet inset = CraMetrics.contentInsetreturn Frames(sidebar: NSRect(x: m, y: m, width: width, height: b.height - 2 * m),handle: .zero, content: b.insetBy(dx: inset, dy: inset), cornerRadius: CraMetrics.contentCornerRadius, sidebarCorner: 12)diff --git a/Sources/Cra/UI/Windows/FloatingSidebar.swift b/Sources/Cra/UI/Windows/FloatingSidebar.swiftindex 5d88979..83e2962 100644--- a/Sources/Cra/UI/Windows/FloatingSidebar.swift+++ b/Sources/Cra/UI/Windows/FloatingSidebar.swift@@ -19,6 +19,8 @@ final class FloatingSidebarController {var didAttachPanel: (() -> Void)?private(set) var isShown = false+ /// A completed hide must not take the panel away from a newer reveal or anchor.+ private var transitionGeneration: UInt64 = 0private var panel: FloatingSidebarPanel?/// The sidebar's rounded body; it slides inside the panel (see `panelFrame`).private let slider = NSView()@@ -28,10 +30,7 @@ final class FloatingSidebarController {private var trafficLights: FloatingTrafficLights?enum Metrics {- static let edge: CGFloat = 2- static let leaveSlack: CGFloat = 24static let cornerRadius: CGFloat = 12- static let margin: CGFloat = 8static let duration: TimeInterval = 0.085}@@ -62,12 +61,12 @@ final class FloatingSidebarController {guard NSApp.isActive, !shell.contentFullscreen, window.isVisible else { return }guard isShown || sidebarHidden() else { return }let screenPoint = NSEvent.mouseLocation- let inWindow = window.frame.contains(screenPoint)- let p = window.convertPoint(fromScreen: screenPoint)+ let p = shell.convert(window.convertPoint(fromScreen: screenPoint), from: nil)if !isShown {- guard sidebarHidden(), inWindow, p.x <= Metrics.edge else { return }+ guard sidebarHidden(), FloatingSidebarGeometry.shouldReveal(at: p, in: shell.bounds) else { return }show()- } else if !inWindow || p.x > shell.sidebarWidth + Metrics.margin + Metrics.leaveSlack {+ } else if FloatingSidebarGeometry.shouldHide(at: p, in: shell.bounds, sidebarWidth: shell.sidebarWidth,+ pressedMouseButtons: NSEvent.pressedMouseButtons) {hide(animated: true)}}@@ -142,6 +141,8 @@ final class FloatingSidebarController {func show() {guard !isShown, sidebarHidden() else { return }isShown = true+ transitionGeneration &+= 1+ let generation = transitionGenerationlet p = panel ?? makePanel()panel = p// The window shadow is traced from the panel's pixels once; it comes on when the@@ -172,18 +173,24 @@ final class FloatingSidebarController {ctx.timingFunction = CAMediaTimingFunction(controlPoints: 0.15, 0.7, 0.3, 1)slider.animator().frame = slideFrame(shown: true)} completionHandler: { [weak self] in- guard let self, self.isShown else { return }- p.hasShadow = true- p.invalidateShadow()+ MainActor.assumeIsolated {+ guard let self, self.transitionGeneration == generation, self.isShown else { return }+ p.hasShadow = true+ p.invalidateShadow()+ }}}func hide(animated: Bool) {- guard isShown, let p = panel else { return }+ // A synchronous hide also finishes a hide already in flight (for example,+ // before a traffic-light action). Repeated animated exits do not restart it.+ guard isShown || (!animated && shell.sidebarDetached), let p = panel else { return }isShown = false+ transitionGeneration &+= 1+ let generation = transitionGenerationp.hasShadow = falselet finish = { [weak self] in- guard let self else { return }+ guard let self, self.transitionGeneration == generation, !self.isShown else { return }self.window.removeChildWindow(p)p.orderOut(nil)self.reattach(floating: false)@@ -199,7 +206,10 @@ final class FloatingSidebarController {/// The sidebar is about to be anchored (⌘S while floating): put it back in the/// shell at its floating frame so the anchoring animation starts from there.func prepareForAnchoring() {- guard isShown, let p = panel else { return }+ transitionGeneration &+= 1+ // During the hide animation isShown is already false, but the view still+ // belongs to the panel and must come home before the anchoring animation.+ guard shell.sidebarDetached, let p = panel else { return }isShown = falsewindow.removeChildWindow(p)p.orderOut(nil)diff --git a/Sources/Cra/UI/Windows/FloatingSidebarGeometry.swift b/Sources/Cra/UI/Windows/FloatingSidebarGeometry.swiftnew file mode 100644index 0000000..9b92c25--- /dev/null+++ b/Sources/Cra/UI/Windows/FloatingSidebarGeometry.swift@@ -0,0 +1,26 @@+import Foundation++/// Both the shell's tracking area and the floating panel's pointer poll use the+/// same window-local boundaries.+enum FloatingSidebarGeometry {+ /// Arc 1.170, measured with the pointer on a window away from the screen edge (29 Oct):+ /// the hidden sidebar comes out within 5 pt of the window's left edge (not at 6 or 9 pt)+ /// and from just outside it (2–4 pt).+ static let revealInside: CGFloat = 5+ static let revealOutside: CGFloat = 4+ static let margin: CGFloat = 8+ static let leaveSlack: CGFloat = 24++ static func shouldReveal(at point: CGPoint, in bounds: CGRect) -> Bool {+ point.y >= bounds.minY && point.y <= bounds.maxY+ && point.x >= bounds.minX - revealOutside && point.x <= bounds.minX + revealInside+ }++ static func shouldHide(at point: CGPoint, in bounds: CGRect, sidebarWidth: CGFloat,+ pressedMouseButtons: Int) -> Bool {+ // Window movement can put the pointer outside the panel briefly. Preserve+ // the active drag/context gesture; the next released-pointer check dismisses.+ guard pressedMouseButtons == 0, !shouldReveal(at: point, in: bounds) else { return false }+ return !bounds.contains(point) || point.x > bounds.minX + sidebarWidth + margin + leaveSlack+ }+}diff --git a/Tests/FloatingSidebarTests.swift b/Tests/FloatingSidebarTests.swiftnew file mode 100644index 0000000..af5f35e--- /dev/null+++ b/Tests/FloatingSidebarTests.swift@@ -0,0 +1,152 @@+import AppKit++@main+enum FloatingSidebarTests {+ private static var checks = 0++ @MainActor+ static func main() {+ geometry()+ _ = NSApplication.shared+ NSApp.setActivationPolicy(.prohibited)+ let window = NSWindow(contentRect: NSRect(x: -10000, y: -10000, width: 1400, height: 880),+ styleMask: [.borderless], backing: .buffered, defer: false)+ window.isReleasedWhenClosed = false+ let shell = ShellView(frame: NSRect(origin: .zero, size: window.frame.size))+ window.contentView = shell+ let sidebar = FloatingSidebarController(shell: shell, window: window, palette: { SpacePalette() })+ sidebar.sidebarHidden = { true }+ // Do not install the pointer timer or post input events. The real controller+ // and AppKit animation completions run with an offscreen disposable panel.+ sidebar.show()+ expect(sidebar.isShown && shell.sidebarDetached, "show attaches the sidebar to its panel")+ let panel = window.childWindows!.first!++ sidebar.hide(animated: true)+ expect(!sidebar.isShown && shell.sidebarDetached, "animated hide remains in flight before completion")+ sidebar.show()+ drainAnimations()+ expect(sidebar.isShown && shell.sidebarDetached && panel.parent === window && panel.isVisible,+ "an old hide completion must not remove a newly revealed panel")+ expect(shell.sidebarHost.window === panel, "the revealed sidebar retains its native panel owner")++ // Multiple animation completions can be queued; only the latest show owns it.+ sidebar.hide(animated: true)+ sidebar.show()+ sidebar.hide(animated: true)+ sidebar.show()+ drainAnimations()+ expect(sidebar.isShown && shell.sidebarDetached && panel.parent === window && panel.isVisible,+ "rapid edge re-entry leaves the latest reveal visible")++ sidebar.hide(animated: true)+ sidebar.prepareForAnchoring()+ expect(!sidebar.isShown && !shell.sidebarDetached && shell.sidebarHost.superview === shell,+ "anchoring during hide immediately brings the sidebar home")+ expect(shell.floatingSidebar && panel.parent == nil && !panel.isVisible,+ "anchoring preserves the floating start frame while removing its panel")+ drainAnimations()+ expect(shell.floatingSidebar && shell.sidebarHost.superview === shell,+ "a stale hide completion must not overwrite the anchoring start state")++ sidebar.show()+ sidebar.hide(animated: true)+ sidebar.hide(animated: false)+ expect(!shell.sidebarDetached && panel.parent == nil && !panel.isVisible,+ "a synchronous hide finishes an in-flight animated hide immediately")+ drainAnimations()+ expect(!sidebar.isShown && !shell.sidebarDetached && !panel.hasShadow,+ "late show and hide completions cannot revive a hidden panel or shadow")++ sidebar.show()+ sidebar.hide(animated: true)+ drainAnimations()+ expect(!sidebar.isShown && !shell.sidebarDetached && !shell.floatingSidebar,+ "an uninterrupted hide still reattaches the sidebar normally")+ window.close()+ print("Floating sidebar: \(checks) checks passed")+ }++ private static func geometry() {+ // Nonzero bounds also check that both paths use shell-local geometry.+ let bounds = CGRect(x: 20, y: 10, width: 1400, height: 880)+ expect(FloatingSidebarGeometry.shouldReveal(at: CGPoint(x: 20, y: 100), in: bounds), "left edge reveals")+ expect(FloatingSidebarGeometry.shouldReveal(at: CGPoint(x: 25, y: 100), in: bounds), "5 pt in reveals, as in Arc")+ expect(!FloatingSidebarGeometry.shouldReveal(at: CGPoint(x: 26, y: 100), in: bounds), "6 pt in does not reveal, as in Arc")+ expect(FloatingSidebarGeometry.shouldReveal(at: CGPoint(x: 16, y: 100), in: bounds), "just outside the window reveals, as in Arc")+ expect(!FloatingSidebarGeometry.shouldReveal(at: CGPoint(x: 15.9, y: 100), in: bounds), "further outside does not reveal")+ expect(!FloatingSidebarGeometry.shouldReveal(at: CGPoint(x: 22, y: 9.9), in: bounds), "above the window does not reveal")+ expect(!FloatingSidebarGeometry.shouldReveal(at: CGPoint(x: 22, y: 890.1), in: bounds), "below the window does not reveal")+ expect(!FloatingSidebarGeometry.shouldHide(at: CGPoint(x: 17, y: 100), in: bounds, sidebarWidth: 328, pressedMouseButtons: 0),+ "the outside reveal strip does not hide what it revealed")+ expect(!FloatingSidebarGeometry.shouldHide(at: CGPoint(x: 380, y: 100), in: bounds, sidebarWidth: 328, pressedMouseButtons: 0),+ "panel margin and leave slack are included in the retention boundary")+ expect(FloatingSidebarGeometry.shouldHide(at: CGPoint(x: 380.1, y: 100), in: bounds, sidebarWidth: 328, pressedMouseButtons: 0),+ "leaving the shared retention boundary hides after release")+ expect(FloatingSidebarGeometry.shouldHide(at: CGPoint(x: 15.9, y: 100), in: bounds, sidebarWidth: 328, pressedMouseButtons: 0),+ "leaving the window hides after release")+ for button in [1, 2, 4] {+ expect(!FloatingSidebarGeometry.shouldHide(at: CGPoint(x: 380.1, y: 100), in: bounds, sidebarWidth: 328, pressedMouseButtons: button),+ "button mask \(button) preserves the sidebar beyond the leave slack during a gesture")+ expect(!FloatingSidebarGeometry.shouldHide(at: CGPoint(x: 15.9, y: 100), in: bounds, sidebarWidth: 328, pressedMouseButtons: button),+ "button mask \(button) preserves the sidebar outside the moving window during a gesture")+ }+ }++ private static func drainAnimations() {+ RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.3))+ }++ private static func expect(_ condition: @autoclosure () -> Bool, _ message: String) {+ guard condition() else { fatalError(message) }+ checks += 1+ }+}++// Lightweight dependencies for compiling the production controller without CEF.+// They provide layout/theme data only; lifecycle and animation code is not copied.+struct SpacePalette {+ var vibrant = false+ var isDark = false+}++final class PaletteBackgroundView: NSView {+ var palette: SpacePalette+ init(palette: SpacePalette) { self.palette = palette; super.init(frame: .zero) }+ required init?(coder: NSCoder) { fatalError() }+}++enum CraMetrics {+ static let contentInset: CGFloat = 10+ static let titleRowCenter: CGFloat = 24+}++final class CraTuning {+ static let shared = CraTuning()+ let material: NSVisualEffectView.Material = .sidebar+ let darkBlur = false+ let liftActive: CGFloat = 0.1+}++final class ShellView: NSView {+ let sidebarHost = NSView()+ let resizeHandle = NSView()+ var sidebarWidth: CGFloat = 328+ var contentFullscreen = false+ var sidebarDetached = false+ var floatingSidebar = false+ struct Frames { let sidebar: NSRect }++ override init(frame: NSRect) {+ super.init(frame: frame)+ sidebarHost.wantsLayer = true+ addSubview(sidebarHost)+ addSubview(resizeHandle)+ }+ required init?(coder: NSCoder) { fatalError() }++ func frames(sidebarVisible: Bool, floating: Bool, fullscreen: Bool, width: CGFloat) -> Frames {+ let margin = FloatingSidebarGeometry.margin+ return Frames(sidebar: NSRect(x: margin, y: margin, width: width, height: bounds.height - 2 * margin))+ }+}diff --git a/docs/floating-sidebar-testing.md b/docs/floating-sidebar-testing.mdnew file mode 100644index 0000000..598abea--- /dev/null+++ b/docs/floating-sidebar-testing.md@@ -0,0 +1,27 @@+# Hidden sidebar regression checks++Run `bash scripts/test-floating-sidebar.sh` on macOS. The test compiles the production+floating-sidebar controller with lightweight theme/layout stubs. It uses an offscreen+disposable AppKit panel, does not install the pointer poll, and does not post input events.++Checks cover the shared reveal strip (Arc's: 5 pt inside the window's left edge, 4 pt outside), window bounds, matching leave slack,+held-button retention outside those bounds and dismissal after release,+re-entry during a hide animation, repeated reversals, anchoring while a hide is in+flight, and immediate dismissal while an animated hide is pending. The tests wait+for real AppKit animation completions; they do not establish physical hover behavior.++In an isolated CraDev profile, verify:++- Hide the sidebar with Command-S and move to within 5 pt of the window's left edge,+ or just outside it. It should reveal there, and not 6 pt or more into the window.+- Move beyond the floating sidebar's right edge and quickly return to the gutter,+ repeatedly. The latest reveal should stay visible and interactive.+- Press Command-S while the floating sidebar is entering or leaving. It should+ anchor correctly, without disappearing after the old animation completes.+- The sidebar should stay open while the pointer crosses its right margin and+ should dismiss after leaving the shared 24pt slack or the browser window.+- While holding a mouse button for a window drag, tab reorder or context gesture,+ move beyond those bounds. Automatic hover dismissal must wait until release;+ explicit hide, toggle and window actions must still work.+- Verify both compact and full toolbars. Page clicks outside the gutter and+ full-screen page behavior should remain unchanged.diff --git a/scripts/test-floating-sidebar.sh b/scripts/test-floating-sidebar.shnew file mode 100755index 0000000..aa67cf9--- /dev/null+++ b/scripts/test-floating-sidebar.sh@@ -0,0 +1,10 @@+#!/bin/bash+# Production sidebar lifecycle and shared hit geometry; no CEF/profile/input events.+set -euo pipefail+cd "$(dirname "$0")/.."+TEST_DIR="$(mktemp -d /tmp/cra-sidebar-tests.XXXXXX)"+trap 'rm -rf "$TEST_DIR"' EXIT+xcrun swiftc -parse-as-library Sources/Cra/UI/Windows/FloatingSidebarGeometry.swift \+ Sources/Cra/UI/Windows/FloatingSidebar.swift Tests/FloatingSidebarTests.swift \+ -o "$TEST_DIR/sidebar-tests"+"$TEST_DIR/sidebar-tests"