Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

The face is surprised when a new version of Peel is out

Țugui Dragoșfd7f938384e5
diff --git a/Packages/PeelCore/Sources/PeelCore/Home/Surprise.swift b/Packages/PeelCore/Sources/PeelCore/Home/Surprise.swift
new file mode 100644
index 00000000..6d60e842
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Home/Surprise.swift
@@ -0,0 +1,69 @@
+import Foundation
+
+/// The face's surprise when a new version of Peel is out. Its eyes open wide and blink twice while it rises a
+/// little, then it looks up to the right, where a red dot pops up like a badge, and it smiles once it has seen it.
+/// Each value below is how far into one part of that the face is, at the time counted so far. Time is counted in the
+/// steps the face takes, never read from the clock, so a face that rested while Peel was in the background goes on
+/// from where it stopped.
+public struct Surprise: Sendable {
+ /// How long the whole surprise takes, in seconds.
+ public static let duration = 2.4
+ /// Where the face looks while the dot shows: up and to the right, where the dot is.
+ public static let aim = (x: 0.71, y: -0.71)
+
+ private var elapsed = 0.0
+
+ public init() {}
+
+ public mutating func advance(by step: Double) {
+ elapsed += max(0, step)
+ }
+
+ public var isOver: Bool {
+ elapsed >= Self.duration
+ }
+
+ /// How much wider the eyes are than usual, as a fraction of their size.
+ public var widen: Double {
+ 0.28 * bump(from: 0, to: 0.84)
+ }
+
+ /// How far the head rises, in the face's units, which are a hundredth of its width.
+ public var rise: Double {
+ 3 * bump(from: 0, to: 1.2)
+ }
+
+ /// How open the eyes are, 1 when fully open, through the two quick blinks.
+ public var openness: Double {
+ 1 - 0.9 * max(bump(from: 0.288, to: 0.48), bump(from: 0.528, to: 0.72))
+ }
+
+ /// How far the look has gone toward the dot, from 0 to 1.
+ public var look: Double {
+ ramp(from: 0.72, to: 1.008) * (1 - ramp(from: 1.92, to: 2.28))
+ }
+
+ /// The dot's size, from 0 to its full size, 1. It grows quickly and then slows as it reaches full size.
+ public var dot: Double {
+ let growing = ramp(from: 0.72, to: 0.96)
+ let slowing = elapsed < 0.96 ? 1.25 - 0.25 * ramp(from: 0.816, to: 0.96) : 1
+ return min(1, growing * slowing) * (1 - ramp(from: 2.16, to: 2.4))
+ }
+
+ /// Whether the face should give its happy pose, which is how the surprise ends: the smile arrives, holds, and
+ /// has time to leave before the surprise is over.
+ public var wantsHappyPose: Bool {
+ elapsed >= 1.49 && elapsed < 2.0
+ }
+
+ /// 0 before `start`, 1 after `end`, and in a straight line between.
+ private func ramp(from start: Double, to end: Double) -> Double {
+ min(1, max(0, (elapsed - start) / (end - start)))
+ }
+
+ /// 0 outside `start` to `end`, rising to 1 at the middle and falling back, as half a sine wave.
+ private func bump(from start: Double, to end: Double) -> Double {
+ let progress = ramp(from: start, to: end)
+ return progress > 0 && progress < 1 ? sin(.pi * progress) : 0
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SurpriseTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SurpriseTests.swift
new file mode 100644
index 00000000..b714014d
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SurpriseTests.swift
@@ -0,0 +1,85 @@
+import Foundation
+@testable import PeelCore
+import Testing
+
+struct SurpriseTests {
+ private let frame = 1.0 / 120
+
+ private func stepped(_ seconds: Double, _ surprise: inout Surprise) {
+ for _ in 0..<Int((seconds / frame).rounded()) {
+ surprise.advance(by: frame)
+ }
+ }
+
+ /// The eyes open wide at once and settle back, while the head rises and comes down.
+ @Test func theEyesOpenWideAndTheHeadRises() {
+ var surprise = Surprise()
+ #expect(surprise.widen == 0)
+ #expect(surprise.rise == 0)
+
+ stepped(0.42, &surprise)
+ #expect(abs(surprise.widen - 0.28) < 0.001, "the eyes are widest at the peak")
+ stepped(0.18, &surprise)
+ #expect(abs(surprise.rise - 3) < 0.001, "the head is highest at the peak")
+
+ stepped(0.66, &surprise)
+ #expect(surprise.widen == 0)
+ #expect(surprise.rise == 0)
+ }
+
+ /// Two quick blinks, then the eyes stay open.
+ @Test func blinksTwice() {
+ var surprise = Surprise()
+ var closings = 0
+ var wasShut = false
+ for _ in 0..<Int((Surprise.duration / frame).rounded()) {
+ surprise.advance(by: frame)
+ let isShut = surprise.openness < 0.5
+ if isShut, !wasShut { closings += 1 }
+ wasShut = isShut
+ }
+ #expect(closings == 2)
+ #expect(surprise.openness == 1)
+ }
+
+ /// The dot pops up at the top right as the look reaches it, stays while the face looks, and is gone at the end.
+ @Test func looksAtTheDotWhileItShows() {
+ var surprise = Surprise()
+ stepped(0.7, &surprise)
+ #expect(surprise.look == 0)
+ #expect(surprise.dot == 0)
+
+ stepped(0.4, &surprise)
+ #expect(surprise.look == 1)
+ #expect(surprise.dot == 1)
+
+ stepped(1.35, &surprise)
+ #expect(surprise.look == 0)
+ #expect(surprise.dot == 0)
+ #expect(surprise.isOver)
+ }
+
+ /// The smile comes once the dot has been seen, and has time to leave before the surprise is over.
+ @Test func smilesAtTheEnd() {
+ var surprise = Surprise()
+ stepped(1.45, &surprise)
+ #expect(!surprise.wantsHappyPose)
+ stepped(0.1, &surprise)
+ #expect(surprise.wantsHappyPose)
+ stepped(0.5, &surprise)
+ #expect(!surprise.wantsHappyPose)
+ #expect(!surprise.isOver, "over before the smile had time to leave")
+ }
+
+ /// Time is counted in the steps the face takes, so a face that rested while Peel was in the background goes
+ /// on from where it stopped.
+ @Test func onlyTheFacesOwnStepsCount() {
+ var surprise = Surprise()
+ stepped(1, &surprise)
+ let look = surprise.look
+ surprise.advance(by: 0)
+ surprise.advance(by: -5)
+ #expect(surprise.look == look)
+ #expect(!surprise.isOver)
+ }
+}
diff --git a/Peel/Views/Stickers/PeelFace.swift b/Peel/Views/Stickers/PeelFace.swift
index 2c696367..4696723c 100644
--- a/Peel/Views/Stickers/PeelFace.swift
+++ b/Peel/Views/Stickers/PeelFace.swift
@@ -4,12 +4,13 @@ import SwiftUI
/// Peel's logo with a face. Its motion is ported from blobatar (MIT, blobatar.dev): breathing, bobbing,
/// glancing, blinking, following the pointer, and a `happy` pose while the pointer is on the face. With the
-/// pointer outside the window, it looks around by itself (`LookAround`), and it smiles once when a removal
-/// finishes (`Cheer`).
+/// pointer outside the window, it looks around by itself (`LookAround`), it smiles once when a removal
+/// finishes (`Cheer`), and it is surprised once when a new version of Peel is out (`Surprise`).
struct PeelFace: View {
var size: CGFloat
@Environment(RemovalHistoryStore.self) private var history
+ @Environment(AppLibrary.self) private var library
@Environment(\.accessibilityReduceMotion) private var reduceMotion
/// Lets the face rest while no window is open. A closed window keeps its views, and no occlusion change
/// reaches them, but the scene phase turns to `.background`, and back to active when the window returns.
@@ -24,6 +25,9 @@ struct PeelFace: View {
.onChange(of: history.justMoved) { _, moved in
if moved != nil { Self.follow.cheer() }
}
+ .task(id: library.newerPeel?.version) {
+ if let version = library.newerPeel?.version { Self.follow.surprise(for: version) }
+ }
}
/// Where the face is looking. It is shared rather than kept in the view because the sidebar is rebuilt
@@ -65,6 +69,8 @@ private final class FaceLayerView: NSView {
private let peel = CAShapeLayer()
private let flap = CAShapeLayer()
private let eyes = [CAShapeLayer(), CAShapeLayer()]
+ /// The red dot of `Surprise`, beside the head rather than on it, so it stays put while the head moves.
+ private let dot = CAShapeLayer()
private var link: CADisplayLink?
private var observers: [any NSObjectProtocol] = []
private var pointerSeen: CGPoint?
@@ -99,6 +105,12 @@ private final class FaceLayerView: NSView {
}
head.sublayers = [peel, flap] + eyes
canvas.addSublayer(head)
+ let dotSize = 2 * Face.dot.radius * size / 100
+ dot.bounds = CGRect(x: 0, y: 0, width: dotSize, height: dotSize)
+ dot.path = CGPath(ellipseIn: dot.bounds, transform: nil)
+ dot.position = CGPoint(x: Face.dot.x * size / 100, y: Face.dot.y * size / 100)
+ dot.isHidden = true
+ canvas.addSublayer(dot)
layer?.addSublayer(canvas)
recolor()
draw(at: CACurrentMediaTime())
@@ -137,7 +149,7 @@ private final class FaceLayerView: NSView {
override func viewDidChangeBackingProperties() {
super.viewDidChangeBackingProperties()
let scale = window?.backingScaleFactor ?? 2
- for layer in [canvas, head, peel, flap] + eyes {
+ for layer in [canvas, head, peel, flap, dot] + eyes {
layer.contentsScale = scale
}
}
@@ -187,7 +199,7 @@ private final class FaceLayerView: NSView {
pointerSeen = nil
}
let look = PeelFace.follow.step(toward: seen, at: time, still: isStill)
- let quick = time - pointerMovedAt < 0.5 || PeelFace.follow.isCheering
+ let quick = time - pointerMovedAt < 0.5 || PeelFace.follow.isReacting
if quick != isQuick {
isQuick = quick
link?.preferredFrameRateRange = quick ? .default : Self.calm
@@ -202,6 +214,8 @@ private final class FaceLayerView: NSView {
eye.transform = placed.transform
eye.position = placed.position
}
+ dot.isHidden = pose.dot <= 0
+ dot.transform = CATransform3DMakeScale(max(pose.dot, 0.001), max(pose.dot, 0.001), 1)
CATransaction.commit()
}
@@ -212,6 +226,7 @@ private final class FaceLayerView: NSView {
peel.fillColor = NSColor(resource: .peelOrange).cgColor
flap.fillColor = NSColor(resource: .peelCream).cgColor
flap.shadowColor = NSColor(resource: .stickerShadow).cgColor
+ dot.fillColor = NSColor.systemRed.cgColor
for eye in eyes {
eye.fillColor = NSColor(resource: .faceInk).cgColor
}
@@ -239,14 +254,17 @@ private struct Pose {
let size: CGFloat
var head: Placed
var eyes: [Placed] = []
+ /// The size of `Surprise`'s dot, from 0 to 1.
+ var dot: Double
init(_ idle: Idle, _ look: Look, size: CGFloat) {
self.size = size
+ dot = look.dot
let unit = size / 100
// The head turns with the eyes, which is what keeps a glance readable at sidebar size.
let turn = CGSize(width: look.aim.width * look.hold * Face.turn.x * unit,
height: look.aim.height * look.hold * Face.turn.y * unit)
- let rise = idle.bob * unit - 1.5 * look.lift * unit
+ let rise = idle.bob * unit - 1.5 * look.lift * unit - look.startle * unit
let lift = 1 + 0.04 * look.lift
head = Placed(transform: CATransform3DMakeScale(idle.breathe.width, idle.breathe.height, 1)
.then(CATransform3DMakeRotation(look.aim.width * look.hold * Face.lean * .pi / 180, 0, 0, 1))
@@ -263,7 +281,8 @@ private struct Pose {
let tilt = idle.wrap.tilt + 4.1 * look.aim.width * look.aim.height * look.hold
for side in [-1.0, 1.0] {
let scale = smile.scale(side)
- eyes.append(Placed(transform: CATransform3DMakeScale(1, idle.blink, 1)
+ eyes.append(Placed(transform: CATransform3DMakeScale(1, min(idle.blink, look.openness), 1)
+ .then(CATransform3DMakeScale(1 + look.widen, 1 + look.widen, 1))
.then(CATransform3DMakeScale(1 + across + idle.wrap.leading * side, 1 + down, 1))
.then(CATransform3DMakeRotation(tilt * side * .pi / 180, 0, 0, 1))
.then(CATransform3DMakeScale(scale.width, scale.height, 1))
@@ -291,6 +310,9 @@ private struct Pose {
points.append(onCanvas(CGPoint(x: eye.position.x + offset.x, y: eye.position.y + offset.y)))
}
}
+ let reach = Face.dot.radius * dot * size / 100
+ let middle = SIMD2(Face.dot.x * size / 100, Face.dot.y * size / 100)
+ points += [middle + [-reach, -reach], middle + [reach, -reach], middle + [reach, reach], middle + [-reach, reach]]
return points
}
}
@@ -302,12 +324,17 @@ private extension CATransform3D {
}
}
-/// Where the face is aimed, and how far into each reaction it is.
+/// Where the face is aimed, and how far into each reaction it is. `widen`, `startle`, `openness`, and `dot` are
+/// `Surprise`'s: how much wider the eyes are, how far the head rises, in units, how open the eyes are, and the dot.
private struct Look {
var aim = CGSize.zero
var hold = 0.0
var smile = 0.0
var lift = 0.0
+ var widen = 0.0
+ var startle = 0.0
+ var openness = 1.0
+ var dot = 0.0
}
/// The face's look, stepped once a frame: blobatar's pursuit of the pointer, and its two reactions (a smile and
@@ -323,6 +350,10 @@ private final class Follow {
private var lifting = 0.0
private var holding = 0.0
private var cheering: Cheer?
+ private var surprising: Surprise?
+ /// The version the face was last surprised by, so a face built again, or a version found again, does not
+ /// surprise twice.
+ private var surprisedBy: String?
/// How long the smile takes to arrive, and to leave.
private static let smileRise = 0.3
private static let smileFall = 0.4
@@ -340,7 +371,15 @@ private final class Follow {
cheering = Cheer(rise: Self.smileRise, hold: Motion.settle.duration, fall: Self.smileFall)
}
- var isCheering: Bool { cheering != nil }
+ /// Starts the surprise, once for each new version of Peel.
+ func surprise(for version: String) {
+ guard version != surprisedBy else { return }
+ surprisedBy = version
+ surprising = Surprise()
+ }
+
+ /// Whether a reaction is under way, which the face follows at the display's full frame rate.
+ var isReacting: Bool { cheering != nil || surprising != nil }
func step(toward seen: Pointer, at time: TimeInterval, still: Bool) -> Look {
let elapsed = last.map { time - $0 } ?? 0
@@ -348,8 +387,9 @@ private final class Follow {
if elapsed > 0, elapsed < 0.1 { frame += (elapsed - frame) / 10 }
let step = max(0, min(elapsed, 2 * frame))
guard !still else {
- // Under Reduce Motion the face holds still, and a smile it could not give is not given later.
+ // Under Reduce Motion the face holds still, and a reaction it could not give is not given later.
cheering = nil
+ surprising = nil
return Look()
}
let pointer: CGPoint?
@@ -359,8 +399,8 @@ private final class Follow {
case .at(let point): pointer = point
}
- let target: CGSize
- let held: Double
+ var target: CGSize
+ var held: Double
if let pointer {
lookingAround = nil
let toward = CGSize(width: pointer.x - Face.center.x, height: pointer.y - Face.center.y)
@@ -377,6 +417,14 @@ private final class Follow {
held = moment.hold
}
+ surprising?.advance(by: step)
+ if surprising?.isOver == true { surprising = nil }
+ // While the surprise looks at its dot, that is where the face looks, wherever the pointer is.
+ if let surprising, surprising.look > 0 {
+ target = CGSize(width: Surprise.aim.x, height: Surprise.aim.y)
+ held = surprising.look
+ }
+
let pursuit = step > 0 ? 1 - exp(-step * 1000 / Face.settle) : 1
let rate = hypot(target.width - look.aim.width, target.height - look.aim.height) > 1.6 ? 1 : pursuit
look.aim = CGSize(width: look.aim.width + (target.width - look.aim.width) * rate,
@@ -385,7 +433,7 @@ private final class Follow {
let onFace = pointer.map { hypot($0.x - 50, $0.y - 50) <= Face.radius * 100 } ?? false
cheering?.advance(by: step)
if cheering?.isOver == true { cheering = nil }
- let happy = onFace || cheering?.wantsHappyPose == true
+ let happy = onFace || cheering?.wantsHappyPose == true || surprising?.wantsHappyPose == true
holding = Self.ramp(holding, to: held, by: step / 0.25)
smiling = Self.ramp(smiling, to: happy ? 1 : 0, by: step / (happy ? Self.smileRise : Self.smileFall))
lifting = Self.ramp(lifting, to: happy ? 1 : 0, by: step / (happy ? 0.22 : 0.16))
@@ -393,6 +441,10 @@ private final class Follow {
look.hold = Curve.easeInOut(holding)
look.smile = happy ? Curve.morph(smiling) : Curve.easeInOut(smiling)
look.lift = Curve.lift(lifting)
+ look.widen = surprising?.widen ?? 0
+ look.startle = surprising?.rise ?? 0
+ look.openness = surprising?.openness ?? 1
+ look.dot = surprising?.dot ?? 0
return look
}
@@ -624,6 +676,8 @@ nonisolated private enum Face {
/// The time constant of the look's pursuit, in milliseconds. blobatar uses 110 for a face that fills a page,
/// but at sidebar size that reads as lag, and what matters here is where the eyes point, not the glide.
static let settle = 60.0
+ /// Where `Surprise`'s dot sits, beyond the top right of the circle where the look goes, and its radius.
+ static let dot = (x: 96.0, y: 4.0, radius: 6.0)
/// How far the eyes go toward `aim`: as far from the circle's middle as looking down takes them, or to
/// `peelClearance` from the cut, whichever comes first. The eyes rest below and left of that middle, so one