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.

Cut the device sticker's edge around the picture alone

Țugui Dragoșfd41309cce09
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 715b541b..55fbb94e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -112,6 +112,8 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.
- Right after Peel updates itself, its helper no longer shows as not answering, and what only the helper can move
is no longer locked, while the helper of the version before is still leaving: Peel waits for it to go, then asks
again.
+- The picture of your Mac keeps an even edge all around, with no pale smudge below a desktop Mac or a virtual
+ machine's display.
#### Tweaks
diff --git a/Peel/Views/Stickers/StickerStyle.swift b/Peel/Views/Stickers/StickerStyle.swift
index d2049db2..788815d7 100644
--- a/Peel/Views/Stickers/StickerStyle.swift
+++ b/Peel/Views/Stickers/StickerStyle.swift
@@ -74,31 +74,39 @@ struct DieCut: View {
var edge: CGFloat = 4
var body: some View {
- ZStack {
+ Canvas { context, size in
+ // One resolved image for contour and picture: SwiftUI picks an icon's rep by scale, and reps differ.
+ let picture = context.resolve(Image(nsImage: image))
+ let scale = min(size.width / picture.size.width, size.height / picture.size.height)
+ let fitted = CGSize(width: picture.size.width * scale, height: picture.size.height * scale)
+ let frame = CGRect(
+ origin: CGPoint(x: (size.width - fitted.width) / 2, y: (size.height - fitted.height) / 2),
+ size: fitted
+ )
if showsBorders {
- contour(at: edge + 1, in: .primary)
+ contour(of: picture, in: frame, at: edge + 1, color: .primary, on: context)
}
- contour(at: edge, in: Album.stickerEdge)
- Image(nsImage: image)
- .resizable()
- .aspectRatio(contentMode: .fit)
+ contour(of: picture, in: frame, at: edge, color: Album.stickerEdge, on: context)
+ // Drawn only over the contour, so a shadow in the picture ends where the sticker does.
+ context.blendMode = .sourceAtop
+ context.draw(picture, in: frame)
}
- .compositingGroup()
.shadow(color: Album.shadow, radius: 2.5, y: 1.5)
.accessibilityIgnoresInvertColors()
}
- /// An outline `distance` points wide around the image's shape: the shape drawn 20 times in one color, each
- /// copy moved `distance` points in a different direction.
- private func contour(at distance: CGFloat, in style: some ShapeStyle) -> some View {
- ForEach(0..<20, id: \.self) { step in
+ /// An outline `distance` points wide around what is at least half opaque in the picture, so not its soft shadow:
+ /// that part drawn 20 times in one color, each copy moved another way, and once in place, under every thin part.
+ private func contour(
+ of picture: GraphicsContext.ResolvedImage, in frame: CGRect, at distance: CGFloat, color: Color,
+ on context: GraphicsContext
+ ) {
+ var context = context
+ context.addFilter(.alphaThreshold(min: 0.5, color: color))
+ for step in 0...20 {
let angle = Double(step) / 20 * 2 * .pi
- Image(nsImage: image)
- .resizable()
- .renderingMode(.template)
- .aspectRatio(contentMode: .fit)
- .foregroundStyle(style)
- .offset(x: cos(angle) * distance, y: sin(angle) * distance)
+ let moved = step < 20 ? distance : 0
+ context.draw(picture, in: frame.offsetBy(dx: cos(angle) * moved, dy: sin(angle) * moved))
}
}
}
diff --git a/PeelTests/DieCutTests.swift b/PeelTests/DieCutTests.swift
new file mode 100644
index 00000000..4ee570f1
--- /dev/null
+++ b/PeelTests/DieCutTests.swift
@@ -0,0 +1,45 @@
+import AppKit
+@testable import Peel
+import SwiftUI
+import Testing
+import UniformTypeIdentifiers
+
+struct DieCutTests {
+ @Test(arguments: ["Mac13,1", "Mac14,2", "VirtualMac"], [2.0, 1.0])
+ func theContourIsAsWideAllAroundThePictureAndNoWider(model: String, scale: Double) throws {
+ let kind = try #require(
+ UTType(tag: model, tagClass: UTTagClass(rawValue: "com.apple.device-model-code"), conformingTo: nil)
+ )
+ let picture = NSWorkspace.shared.icon(for: kind)
+
+ let sticker = try solidPart(of: DieCut(image: picture), scale: scale)
+ let alone = try solidPart(of: Image(nsImage: picture).resizable().aspectRatio(contentMode: .fit), scale: scale)
+ let reach = [
+ alone.minY - sticker.minY, sticker.maxY - alone.maxY, alone.minX - sticker.minX, sticker.maxX - alone.maxX,
+ ]
+
+ #expect(reach.allSatisfy { (3.5...5.5).contains($0) }, "\(reach)")
+ }
+
+ private func solidPart(of view: some View, scale: Double) throws -> CGRect {
+ let renderer = ImageRenderer(
+ content: view.frame(width: 104, height: 104).padding(8).environment(\.colorScheme, .light)
+ )
+ renderer.scale = scale
+ let image = try #require(renderer.cgImage)
+ let context = try #require(CGContext(
+ data: nil, width: image.width, height: image.height, bitsPerComponent: 8, bytesPerRow: image.width * 4,
+ space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
+ ))
+ context.draw(image, in: CGRect(x: 0, y: 0, width: image.width, height: image.height))
+ let pixels = try #require(context.data).bindMemory(to: UInt8.self, capacity: image.width * image.height * 4)
+ var solid = CGRect.null
+ for y in 0..<image.height {
+ for x in 0..<image.width where pixels[(y * image.width + x) * 4 + 3] >= 128 {
+ let pixel = CGRect(x: Double(x) / scale, y: Double(y) / scale, width: 1 / scale, height: 1 / scale)
+ solid = solid.union(pixel)
+ }
+ }
+ return solid
+ }
+}