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.

The page behind a Peek gently shrinks unless pages fill the window edge to edge (#221)

Grigoriy09cbcb6d243d
diff --git a/Packages/CraCore/Sources/CraCore/PeekLayout.swift b/Packages/CraCore/Sources/CraCore/PeekLayout.swift
index f485c38..da00c6c 100644
--- a/Packages/CraCore/Sources/CraCore/PeekLayout.swift
+++ b/Packages/CraCore/Sources/CraCore/PeekLayout.swift
@@ -27,6 +27,27 @@ public enum PeekLayout {
public static let buttonCount = 3
/// The page under a Peek (Arc: 233/240/254 → 178/172/151 over the window's background).
public static let pageAlpha: CGFloat = 0.28
+ /// The page under a Peek also draws back a little, its margins widening, unless pages fill the
+ /// window edge to edge (no margins to widen) (PR #221).
+ public static let pageScale: CGFloat = 0.985
+
+ public struct Backdrop: Sendable, Equatable {
+ public let alpha: CGFloat
+ public let scale: CGFloat
+ public init(alpha: CGFloat, scale: CGFloat) { (self.alpha, self.scale) = (alpha, scale) }
+ }
+
+ /// How the page behind looks with a Peek over it (`veiled`) or without.
+ public static func backdrop(veiled: Bool, edgeToEdge: Bool) -> Backdrop {
+ .init(alpha: veiled ? pageAlpha : 1, scale: veiled && !edgeToEdge ? pageScale : 1)
+ }
+
+ /// A sublayer transform (applied about the layer's anchor) that scales by `scale` around
+ /// `center`, both in the layer's own coordinates.
+ public static func backdropTransform(scale: CGFloat, around center: CGPoint, anchor: CGPoint) -> CGAffineTransform {
+ CGAffineTransform(a: scale, b: 0, c: 0, d: scale,
+ tx: (center.x - anchor.x) * (1 - scale), ty: (center.y - anchor.y) * (1 - scale))
+ }
/// Arc's open, frame by frame at 60 fps, from the start of the link picture's fade: the picture
/// fades out on an ease-out over 0.075 s (0.40, 0.21, 0.065 a frame apart). The frame's centre
/// and width go from the link's to the card's on an ease-out cubic over 0.205 s from 0.016 s;
diff --git a/Packages/CraCore/Sources/CraCore/PeekVeil.swift b/Packages/CraCore/Sources/CraCore/PeekVeil.swift
new file mode 100644
index 0000000..f47d9b6
--- /dev/null
+++ b/Packages/CraCore/Sources/CraCore/PeekVeil.swift
@@ -0,0 +1,38 @@
+import Foundation
+
+/// Which Peek the page behind is veiled for (faded, and smaller unless edge to edge:
+/// `PeekLayout.backdrop`). Each call says whether the window has to change the page.
+///
+/// A Peek veils the page 30 ms into its open, fades it back as it starts closing, and ends when
+/// its window goes. A Peek that ended before its open veiled the page (Expand, Add To Split or a
+/// close in those 30 ms) never veils it, and a Peek ending after its close already fades the page
+/// back leaves that fade running.
+public struct PeekVeil: Sendable, Equatable {
+ /// The Peek the page is veiled for, nil when it is (or is on its way back to) itself.
+ public private(set) var owner: UUID?
+ private var ended: Set<UUID> = []
+
+ public init() {}
+
+ public var isOn: Bool { owner != nil }
+
+ /// `peek` veils the page: true when the window fades it (animated).
+ public mutating func show(for peek: UUID) -> Bool {
+ guard !ended.contains(peek), owner != peek else { return false }
+ owner = peek
+ return true
+ }
+
+ /// `peek` starts closing: true when the window fades the page back (animated).
+ public mutating func hide(for peek: UUID) -> Bool {
+ guard owner == peek else { return false }
+ owner = nil
+ return true
+ }
+
+ /// `peek`'s window is gone: true when the page is still veiled for it and snaps back.
+ public mutating func end(for peek: UUID) -> Bool {
+ ended.insert(peek)
+ return hide(for: peek)
+ }
+}
diff --git a/Packages/CraCore/Tests/CraCoreTests/PeekVeilTests.swift b/Packages/CraCore/Tests/CraCoreTests/PeekVeilTests.swift
new file mode 100644
index 0000000..51d3d8f
--- /dev/null
+++ b/Packages/CraCore/Tests/CraCoreTests/PeekVeilTests.swift
@@ -0,0 +1,88 @@
+import CoreGraphics
+import CraCore
+import Foundation
+import Testing
+
+/// The page behind a Peek: who it is veiled for, and what it looks like (PR #221).
+struct PeekVeilTests {
+ let a = UUID(), b = UUID()
+
+ @Test func showsOnceAndComesBackAnimated() {
+ var veil = PeekVeil()
+ do { let changed = veil.show(for: a); #expect(changed) }
+ do { let changed = veil.show(for: a); #expect(!changed) }
+ #expect(veil.isOn)
+ do { let changed = veil.hide(for: a); #expect(changed) }
+ #expect(!veil.isOn)
+ }
+
+ /// Expand, Add To Split or a close in the 30 ms before the open's veil starts: the Peek is gone
+ /// first, and the late show used to leave the page faded and shrunk with no Peek over it.
+ @Test func aPeekThatHasEndedNeverVeilsThePage() {
+ var veil = PeekVeil()
+ do { let changed = veil.end(for: a); #expect(!changed) }
+ do { let changed = veil.show(for: a); #expect(!changed) }
+ #expect(!veil.isOn)
+ }
+
+ /// Closing fades the page back over 0.2 s while the card lands in 0.16 s; the Peek ending then
+ /// must leave that fade running instead of snapping it, and the card's close keeps Arc's length.
+ @Test func endingAfterTheCloseLeavesItsFadeAlone() {
+ var veil = PeekVeil()
+ _ = veil.show(for: a)
+ do { let changed = veil.hide(for: a); #expect(changed) }
+ do { let changed = veil.end(for: a); #expect(!changed) }
+ }
+
+ /// Expand and Add To Split end a Peek without its close: the page snaps back.
+ @Test func endingWhileVeiledSnapsBack() {
+ var veil = PeekVeil()
+ _ = veil.show(for: a)
+ do { let changed = veil.end(for: a); #expect(changed) }
+ #expect(!veil.isOn)
+ }
+
+ /// A Peek still closing ends after the next one veiled the page: the new Peek keeps it.
+ @Test func anotherPeekEndingLeavesTheVeil() {
+ var veil = PeekVeil()
+ _ = veil.show(for: a)
+ _ = veil.hide(for: a)
+ do { let changed = veil.show(for: b); #expect(changed) }
+ do { let changed = veil.end(for: a); #expect(!changed) }
+ do { let changed = veil.hide(for: a); #expect(!changed) }
+ #expect(veil.isOn)
+ do { let changed = veil.end(for: b); #expect(changed) }
+ }
+
+ @Test func pageFadesAndShrinksUnlessEdgeToEdge() {
+ #expect(PeekLayout.backdrop(veiled: true, edgeToEdge: false) == .init(alpha: PeekLayout.pageAlpha, scale: PeekLayout.pageScale))
+ #expect(PeekLayout.backdrop(veiled: true, edgeToEdge: true) == .init(alpha: PeekLayout.pageAlpha, scale: 1))
+ #expect(PeekLayout.backdrop(veiled: false, edgeToEdge: false) == .init(alpha: 1, scale: 1))
+ #expect(PeekLayout.pageScale < 1 && PeekLayout.pageScale > 0.95)
+ }
+
+ /// The scale holds `center` still and pulls the corners in around it, whatever the anchor.
+ @Test(arguments: [CGPoint(x: 0.5, y: 0.5), CGPoint(x: 0, y: 0)])
+ func shrinksAroundTheCentre(anchorPoint: CGPoint) {
+ let bounds = CGRect(x: 0, y: 0, width: 1000, height: 600)
+ let anchor = CGPoint(x: bounds.width * anchorPoint.x, y: bounds.height * anchorPoint.y)
+ let center = CGPoint(x: 400, y: 250)
+ let t = PeekLayout.backdropTransform(scale: 0.9, around: center, anchor: anchor)
+ // A sublayer transform applies about the anchor: p' = anchor + T(p - anchor).
+ func apply(_ p: CGPoint) -> CGPoint {
+ let q = CGPoint(x: p.x - anchor.x, y: p.y - anchor.y).applying(t)
+ return CGPoint(x: q.x + anchor.x, y: q.y + anchor.y)
+ }
+ let c = apply(center), o = apply(.zero), e = apply(CGPoint(x: 1000, y: 600))
+ #expect(abs(c.x - 400) < 1e-9 && abs(c.y - 250) < 1e-9)
+ #expect(abs(o.x - 40) < 1e-9 && abs(o.y - 25) < 1e-9)
+ #expect(abs(e.x - 940) < 1e-9 && abs(e.y - 565) < 1e-9)
+ #expect(PeekLayout.backdropTransform(scale: 1, around: center, anchor: anchor).isIdentity)
+ }
+
+ /// Arc's measured close stays its own length; the page's slower fade back runs on its own.
+ @Test func closeKeepsArcsLength() {
+ #expect(PeekLayout.closeDuration() == PeekLayout.closeScaleDuration)
+ #expect(PeekLayout.closeDuration(scale: PeekLayout.closeToAreaDuration) == PeekLayout.closeFadeDelay + PeekLayout.closeFadeDuration)
+ }
+}
diff --git a/Sources/Cra/App/AppDelegate.swift b/Sources/Cra/App/AppDelegate.swift
index 531112c..65cb06d 100644
--- a/Sources/Cra/App/AppDelegate.swift
+++ b/Sources/Cra/App/AppDelegate.swift
@@ -750,6 +750,11 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
}
return
}
+ // peekShown=<label>: the page behind a Peek in each window, faded and drawn back or whole (PR #221).
+ if let label = dict["peekShown"] {
+ for m in mainWindows { NSLog("Cra peek shown %@: %@", label, m.peekBackdropShown()) }
+ return
+ }
if dict["relaunch"] != nil { relaunch(); return }
if dict["quit"] != nil { NSApp.terminate(nil); return }
if let t = dict["selectTab"].flatMap(UUID.init(uuidString:)) { store.selectTab(t, window: main.windowID); return }
diff --git a/Sources/Cra/UI/Windows/LittleWindowController.swift b/Sources/Cra/UI/Windows/LittleWindowController.swift
index f278de4..a5b0254 100644
--- a/Sources/Cra/UI/Windows/LittleWindowController.swift
+++ b/Sources/Cra/UI/Windows/LittleWindowController.swift
@@ -137,6 +137,8 @@ final class LittleWindowController: NSWindowController, NSWindowDelegate, @preco
/// covers; a click on it, Escape or the close button ends the Peek.
let isPeek: Bool
private(set) weak var peekParent: MainWindowController?
+ /// This Peek to its window's veil (MainWindowController.showPeekVeil).
+ private let peekID = UUID()
/// The tab the Peek came from: links that leave the Peek open as tabs placed from it.
private let peekSourceTab: UUID?
private var dim: PeekDimPanel?
@@ -463,8 +465,10 @@ final class LittleWindowController: NSWindowController, NSWindowDelegate, @preco
// Next turn, with the panel on its way to the screen.
DispatchQueue.main.async { [weak self, weak t] in
guard let self, let t, self.transition === t, !self.closingPeek else { return }
- DispatchQueue.main.asyncAfter(deadline: .now() + PeekTransition.lead) {
- parent.setPeekVeil(true, duration: PeekLayout.openVeilDuration, timing: PeekTransition.easeOutCubic)
+ // A Peek gone by then (Expand, Add To Split, a close) never veils the page.
+ let peek = self.peekID
+ DispatchQueue.main.asyncAfter(deadline: .now() + PeekTransition.lead) { [weak parent] in
+ parent?.showPeekVeil(for: peek)
}
self.landPeek(t)
}
@@ -537,7 +541,7 @@ final class LittleWindowController: NSWindowController, NSWindowDelegate, @preco
window.alphaValue = 0
if let host = self.browser?.hostWindow { host.alphaValue = 0 }
self.controls?.alphaValue = 0
- parent.setPeekVeil(false, duration: PeekLayout.closeVeilDuration, timing: PeekTransition.easeOutCubic)
+ parent.hidePeekVeil(for: self.peekID)
}
}
let ready: (Bool) -> Void = { timedOut in
@@ -574,7 +578,7 @@ final class LittleWindowController: NSWindowController, NSWindowDelegate, @preco
self.controls = nil
}
if let window, let p = window.parent { p.removeChildWindow(window) }
- peekParent?.setPeekVeil(false, duration: 0)
+ peekParent?.endPeekVeil(for: peekID)
if !promoted, let pw = peekParent?.window, NSApp.isActive { pw.makeKeyAndOrderFront(nil) }
}
@@ -1411,7 +1415,7 @@ final class PeekDimPanel: NSPanel {
let shade = Shade(frame: NSRect(origin: .zero, size: parent.frame.size))
shade.onClick = onClick
shade.wantsLayer = true
- // The page fades instead (MainWindowController.setPeekVeil); this only catches the click,
+ // The page fades instead (MainWindowController.showPeekVeil); this only catches the click,
// and a window passes clicks through where it is fully clear.
shade.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.01).cgColor
shade.layer?.cornerRadius = LittleWindow.cornerRadius(of: parent)
diff --git a/Sources/Cra/UI/Windows/MainWindowController.swift b/Sources/Cra/UI/Windows/MainWindowController.swift
index 16e305f..4605f6b 100644
--- a/Sources/Cra/UI/Windows/MainWindowController.swift
+++ b/Sources/Cra/UI/Windows/MainWindowController.swift
@@ -1574,15 +1574,62 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM
}
}
- /// Arc fades the page under a Peek to 28 % over the window's background, its sidebar as it is.
- func setPeekVeil(_ on: Bool, duration: TimeInterval, timing: CAMediaTimingFunction? = nil) {
- let alpha: CGFloat = on ? PeekLayout.pageAlpha : 1
+ /// Arc fades the page under a Peek to 28 % over the window's background, its sidebar as it is;
+ /// Cra also draws it back a little unless it fills the window edge to edge (PR #221).
+ /// `PeekVeil` says which Peek it is veiled for, so a Peek that ended early never veils it and
+ /// one whose close already fades it back doesn't snap that fade.
+ private var peekVeil = PeekVeil()
+
+ /// `peek` is opening over this window.
+ func showPeekVeil(for peek: UUID) {
+ guard peekVeil.show(for: peek) else { return }
+ applyPeekVeil(duration: PeekLayout.openVeilDuration, timing: PeekTransition.easeOutCubic)
+ }
+
+ /// `peek` is closing: the page comes back over Arc's 0.2 s while the card lands.
+ func hidePeekVeil(for peek: UUID) {
+ guard peekVeil.hide(for: peek) else { return }
+ applyPeekVeil(duration: PeekLayout.closeVeilDuration, timing: PeekTransition.easeOutCubic)
+ }
+
+ /// `peek`'s window is gone (Expand and Add To Split don't animate): the page snaps back.
+ func endPeekVeil(for peek: UUID) {
+ guard peekVeil.end(for: peek) else { return }
+ applyPeekVeil(duration: 0)
+ }
+
+ /// cra://debug?peekShown: the page behind a Peek as it is set now (the peek-backdrop scenario).
+ func peekBackdropShown() -> String {
+ func scale(_ v: NSView?) -> String {
+ String(format: "%.3f", v?.layer.map { CATransform3DGetAffineTransform($0.sublayerTransform).a } ?? 1)
+ }
+ let content = shell.contentContainer
+ // Pages in their own windows (DevTools docked, or CRA_EMBED_PAGES=0); embedded ones go with the content.
+ let pages = browsers.runtimes.values.compactMap(\.hostWindow).filter { $0.parent === window && !EmbeddedPages.isHost($0) }
+ return String(format: "veiled=%d alpha=%.2f scale=%@ windows=%@", peekVeil.isOn ? 1 : 0, content.alphaValue, scale(content),
+ pages.map { String(format: "%.2f@", $0.alphaValue) + scale($0.contentView) }.joined(separator: ","))
+ }
+
+ private func applyPeekVeil(duration: TimeInterval, timing: CAMediaTimingFunction? = nil) {
+ let look = PeekLayout.backdrop(veiled: peekVeil.isOn, edgeToEdge: shell.edgeToEdge)
+ let content = shell.contentContainer
+ let center = CGPoint(x: content.bounds.midX, y: content.bounds.midY)
+ PeekTransition.scaleBackdrop(content, around: center, to: look.scale, duration: duration, timing: timing)
let pages = browsers.runtimes.values.compactMap(\.hostWindow).filter { $0.parent === window }
+ if let window {
+ // Each page window shrinks around the same point on screen as the page's card.
+ let screenCenter = window.convertPoint(toScreen: content.convert(center, to: nil))
+ for page in pages where !EmbeddedPages.isHost(page) {
+ guard let view = page.contentView else { continue }
+ let pageCenter = view.convert(page.convertPoint(fromScreen: screenCenter), from: nil)
+ PeekTransition.scaleBackdrop(view, around: pageCenter, to: look.scale, duration: duration, timing: timing)
+ }
+ }
NSAnimationContext.runAnimationGroup { ctx in
ctx.duration = duration
ctx.timingFunction = timing
- shell.contentContainer.animator().alphaValue = alpha
- for w in pages { w.animator().alphaValue = alpha }
+ content.animator().alphaValue = look.alpha
+ for w in pages { w.animator().alphaValue = look.alpha }
}
}
@@ -3003,6 +3050,8 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM
// A toast that stays (the protected media one) follows the page's corner, once laid out.
DispatchQueue.main.async { [weak self] in
guard let self, let window = self.window else { return }
+ // The page behind a Peek shrinks around its new centre.
+ if self.peekVeil.isOn { self.applyPeekVeil(duration: 0) }
self.toast.follow(page: self.shell.webContainer.convert(self.shell.webContainer.bounds, to: nil), in: window)
}
saveFrame()
diff --git a/Sources/Cra/UI/Windows/PeekChrome.swift b/Sources/Cra/UI/Windows/PeekChrome.swift
index 76cb58f..600d52a 100644
--- a/Sources/Cra/UI/Windows/PeekChrome.swift
+++ b/Sources/Cra/UI/Windows/PeekChrome.swift
@@ -373,6 +373,31 @@ final class PeekTransition {
/// this far ahead instead, held at their first frame until then.
static let lead: CFTimeInterval = 0.03
+ /// The page behind a Peek drawn back to `scale` around `center` (the view's coordinates),
+ /// animated from wherever it is now.
+ static func scaleBackdrop(_ view: NSView, around center: CGPoint, to scale: CGFloat,
+ duration: TimeInterval, timing: CAMediaTimingFunction?) {
+ guard let layer = view.layer else { return }
+ let anchor = CGPoint(x: layer.bounds.minX + layer.bounds.width * layer.anchorPoint.x,
+ y: layer.bounds.minY + layer.bounds.height * layer.anchorPoint.y)
+ let transform = CATransform3DMakeAffineTransform(PeekLayout.backdropTransform(scale: scale, around: center, anchor: anchor))
+ let from = layer.presentation()?.sublayerTransform ?? layer.sublayerTransform
+ CATransaction.begin()
+ CATransaction.setDisableActions(true)
+ layer.sublayerTransform = transform
+ if duration > 0 {
+ let animation = CABasicAnimation(keyPath: "sublayerTransform")
+ animation.fromValue = NSValue(caTransform3D: from)
+ animation.toValue = NSValue(caTransform3D: transform)
+ animation.duration = duration
+ animation.timingFunction = timing
+ layer.add(animation, forKey: "peekBackdropScale")
+ } else {
+ layer.removeAnimation(forKey: "peekBackdropScale")
+ }
+ CATransaction.commit()
+ }
+
/// Out of the link (or the area) to the card's frame; `done` once it has landed.
func open(done: @escaping () -> Void) {
let d = PeekLayout.openDuration
diff --git a/Tests/Scenarios/peek-backdrop.scenario b/Tests/Scenarios/peek-backdrop.scenario
new file mode 100644
index 0000000..a8710e9
--- /dev/null
+++ b/Tests/Scenarios/peek-backdrop.scenario
@@ -0,0 +1,29 @@
+# The page behind a Peek fades to 28 % and, unless pages fill the window edge to edge, draws back
+# to 98.5 % around its centre; it comes back whole however the Peek goes, closed or expanded (PR #221).
+budget rss=900 stalls50=40
+pace 0.5
+seed settings.json {"hasSeenImportPrompt": true, "toolbarDefaultMigrated": true, "showToolbar": false, "peekInEveryTab": true}
+
+cra://open?url=${PAGE}?n=under
+expect-log loaded 127\.0\.0\.1 \(HTTP 200\)
+wait 1
+# window.open from a click (the cdp script counts as one) peeks from any tab with peekInEveryTab.
+cra://debug?cdp=Runtime.evaluate&params={"expression":"String(!!window.open('${PAGE}?n=peek'))","userGesture":true}
+expect-log peek: from
+wait 1.5
+cra://debug?peekShown=open
+expect-log Cra peek shown open: veiled=1 alpha=0\.28 scale=0\.985
+cra://action/closeTab
+wait 1
+cra://debug?peekShown=closed
+expect-log Cra peek shown closed: veiled=0 alpha=1\.00 scale=1\.000
+
+# Expand ends the Peek without its close: the page snaps back.
+cra://debug?cdp=Runtime.evaluate&params={"expression":"String(!!window.open('${PAGE}?n=expand'))","userGesture":true}
+wait 1.5
+cra://debug?peekShown=reopened
+expect-log Cra peek shown reopened: veiled=1 alpha=0\.28 scale=0\.985
+cra://action/promoteLittleWindow
+wait 1
+cra://debug?peekShown=expanded
+expect-log Cra peek shown expanded: veiled=0 alpha=1\.00 scale=1\.000
diff --git a/docs/release-checklist.md b/docs/release-checklist.md
index d76fad5..1660574 100644
--- a/docs/release-checklist.md
+++ b/docs/release-checklist.md
@@ -61,6 +61,10 @@ built release app with a test profile. Note anything that fails in the release's
### Peek, Little Window, Picture in Picture, split view, Capture
+- **PR #221:** opening a Peek fades the page behind and draws it back a little, its margins widening
+ while the sidebar stays still, and closing brings it back in one smooth motion with no jump at the
+ end. With "Pages fill the window edge to edge" on, the page only fades. (The values are checked
+ by the peek-backdrop scenario; this is the motion.)
- **#195:** in a Peek and a Little Window, click in the page, then Back, Forward, Refresh, the
address and "Open in": each works on the first click. Capture and scroll: the page moves and
the grey picture and the lens follow. With another app full screen, ⌥⌘N opens a Little