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.
Folder emblems follow the front panel's perspective (#234)
Grigoriy11ff4c99b5cf
diff --git a/Sources/Cra/UI/Sidebar/List/FolderGlyphView.swift b/Sources/Cra/UI/Sidebar/List/FolderGlyphView.swiftnew file mode 100644index 0000000..5cc1378--- /dev/null+++ b/Sources/Cra/UI/Sidebar/List/FolderGlyphView.swift@@ -0,0 +1,235 @@+import AppKit++/// An image printed on the folder's front panel, independent of its source or folder type.+struct FolderEmblem: Equatable {+ enum Coloring { case template, original }++ var image: NSImage+ var coloring: Coloring = .original+ /// Fraction of the front panel's shorter side (7 pt on the current 16 × 12 pt panel).+ var relativeSize: CGFloat = 7.0 / 12.0++ fileprivate func rasterized(size: CGSize, scale: CGFloat, tint: NSColor) -> CGImage? {+ guard image.size.width > 0, image.size.height > 0,+ let context = CGContext(data: nil, width: Int(ceil(size.width * scale)),+ height: Int(ceil(size.height * scale)), bitsPerComponent: 8,+ bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(),+ bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil }+ context.scaleBy(x: scale, y: scale)+ NSGraphicsContext.saveGraphicsState()+ defer { NSGraphicsContext.restoreGraphicsState() }+ NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)+ let fit = min(size.width / image.size.width, size.height / image.size.height)+ let fitted = CGSize(width: image.size.width * fit, height: image.size.height * fit)+ let rect = CGRect(x: (size.width - fitted.width) / 2, y: (size.height - fitted.height) / 2,+ width: fitted.width, height: fitted.height)+ image.draw(in: rect, from: .zero, operation: .sourceOver, fraction: 1,+ respectFlipped: false, hints: [.interpolation: NSImageInterpolation.high.rawValue])+ if coloring == .template {+ context.setBlendMode(.sourceIn)+ context.setFillColor(tint.cgColor)+ context.fill(CGRect(origin: .zero, size: size))+ }+ return context.makeImage()+ }+}++/// Arc's folder glyph (FolderIconView, read from Arc 1.171's binary): a back panel with the tab+/// and a front panel, both hinged at the bottom-left corner. Opening turns them toward you in+/// perspective, the front tipping forward and the back leaning away, each shaded darker as it+/// turns, with Core Animation's implicit 0.25 s transition. An optional emblem turns with the front.+final class FolderGlyphView: NSView {+ /// Arc fixes the glyph at 16 × 16 pt.+ static let side: CGFloat = 16+ private let icon = CALayer()+ private let back = CAShapeLayer()+ private let backShade = CAGradientLayer()+ private let backShadeMask = CAShapeLayer()+ private let front = CALayer()+ private let frontPanel = CAShapeLayer()+ private let frontShade = CAGradientLayer()+ private let frontShadeMask = CAShapeLayer()+ /// The back panel shows only outside the front one. Theme folders are opaque (FolderTones), so+ /// the front hides the back's outline anyway; the White and Black icon styles fill with+ /// translucent tones, and without this an open folder showed the back's edges through the+ /// front. The mask is the front panel's shape cut out of a larger square, turned with the+ /// front's own transform so it moves with it.+ private let backHolder = CALayer()+ private let frontHole = CAShapeLayer()+ private let mark = CALayer()+ private var emblem: FolderEmblem?+ private var emblemTint = NSColor.labelColor+ private struct EmblemRendering: Equatable {+ var emblem: FolderEmblem+ var tint: NSColor+ var size: CGSize+ var scale: CGFloat+ }+ private var renderedEmblem: EmblemRendering?+ /// Arc's peeked folder (FolderPeek): three 2 pt dots on a 4 pt pitch in the closed body.+ private let dots = CAShapeLayer()+ private var expanded = false++ override init(frame: NSRect) {+ super.init(frame: frame)+ wantsLayer = true+ // Arc: black from clear to 0.36 across the back, to 0.15 across the front.+ backShade.colors = [NSColor.black.withAlphaComponent(0).cgColor, NSColor.black.withAlphaComponent(0.36).cgColor]+ backShade.startPoint = CGPoint(x: 0, y: 0)+ backShade.endPoint = CGPoint(x: 0.6, y: 1)+ backShade.mask = backShadeMask+ frontShade.colors = [NSColor.black.withAlphaComponent(0).cgColor, NSColor.black.withAlphaComponent(0.15).cgColor]+ frontShade.startPoint = CGPoint(x: 0.1, y: 0)+ frontShade.endPoint = CGPoint(x: 0.9, y: 1)+ frontShade.mask = frontShadeMask+ back.addSublayer(backShade)+ front.addSublayer(frontPanel)+ // The mark is in the panel's local coordinates, so its perspective, hinge and animation+ // come from the panel. Shading covers the printed mark as well as the folder's surface.+ front.addSublayer(mark)+ front.addSublayer(frontShade)+ for l in [back, front, frontHole] as [CALayer] {+ // Hinged on the bottom-left corner.+ l.anchorPoint = CGPoint(x: 0, y: 1)+ }+ frontHole.fillRule = .evenOdd+ frontHole.fillColor = NSColor.black.cgColor+ backHolder.addSublayer(back)+ backHolder.mask = frontHole+ icon.addSublayer(backHolder)+ icon.addSublayer(front)+ layer?.addSublayer(icon)+ layer?.addSublayer(dots)+ mark.contentsGravity = .resizeAspect+ apply(animated: false)+ }++ required init?(coder: NSCoder) { fatalError() }++ override var isFlipped: Bool { true }+ override func hitTest(_ point: NSPoint) -> NSView? { nil }++ func show(expanded: Bool, peeked: Bool = false, emblem: FolderEmblem? = nil, tint: NSColor,+ fillColor: NSColor, backColor: NSColor? = nil, animated: Bool = false) {+ CATransaction.begin()+ CATransaction.setDisableActions(true)+ back.fillColor = (backColor ?? fillColor).cgColor+ frontPanel.fillColor = fillColor.cgColor+ for l in [back, frontPanel] { l.strokeColor = tint.cgColor }+ self.emblem = emblem+ emblemTint = tint+ mark.isHidden = emblem == nil+ dots.isHidden = !peeked || expanded || emblem != nil+ dots.fillColor = tint.cgColor+ CATransaction.commit()+ needsLayout = true+ guard expanded != self.expanded else { return }+ self.expanded = expanded+ apply(animated: animated)+ }++ /// Arc's per-state transforms: perspective m34 −0.00291, 3 pt to the right, 32° about the+ /// vertical axis, then the back 28° and the front −30° about the horizontal one.+ private func apply(animated: Bool) {+ CATransaction.begin()+ CATransaction.setDisableActions(!animated)+ let open = expanded+ var t = CATransform3DIdentity+ t.m34 = -0.0029129464+ t = CATransform3DTranslate(t, open ? 3 : 0, 0, 0)+ t = CATransform3DRotate(t, open ? 32 * .pi / 180 : 0, 0, 1, 0)+ back.transform = CATransform3DRotate(t, open ? 28 * .pi / 180 : 0, 1, 0, 0)+ front.transform = CATransform3DRotate(t, open ? -30 * .pi / 180 : 0, 1, 0, 0)+ frontHole.transform = front.transform+ frontShade.opacity = open ? 1 : 0+ backShade.opacity = open ? 1 : 0.2+ CATransaction.commit()+ }++ override func viewDidChangeBackingProperties() {+ super.viewDidChangeBackingProperties()+ needsLayout = true+ }++ override func layout() {+ super.layout()+ let scale = window?.backingScaleFactor ?? 2+ func px(_ v: CGFloat) -> CGFloat { (v * scale).rounded() / scale }+ let w = Self.side, h = Self.side+ CATransaction.begin()+ CATransaction.setDisableActions(true)+ icon.frame = CGRect(x: px((bounds.width - w) / 2), y: px((bounds.height - h) / 2), width: w, height: h)+ let r = (w / 7).rounded()+ let tabH = px((h * 0.25).rounded())+ let t = (tabH / 1.5).rounded(.down)+ let line = px(1.25)+ // The back panel with its tab (the view is flipped: y runs down).+ let path = CGMutablePath()+ path.move(to: CGPoint(x: r, y: 0))+ path.addLine(to: CGPoint(x: px(0.41 * w), y: 0))+ path.addLine(to: CGPoint(x: px(0.58 * w), y: t))+ path.addLine(to: CGPoint(x: w - r, y: t))+ path.addArc(center: CGPoint(x: w - r, y: t + r), radius: r, startAngle: -.pi / 2, endAngle: 0, clockwise: false)+ path.addLine(to: CGPoint(x: w, y: h - r))+ path.addArc(center: CGPoint(x: w - r, y: h - r), radius: r, startAngle: 0, endAngle: .pi / 2, clockwise: false)+ path.addLine(to: CGPoint(x: r, y: h))+ path.addArc(center: CGPoint(x: r, y: h - r), radius: r, startAngle: .pi / 2, endAngle: .pi, clockwise: false)+ path.addLine(to: CGPoint(x: 0, y: r))+ path.addArc(center: CGPoint(x: r, y: r), radius: r, startAngle: .pi, endAngle: 1.5 * .pi, clockwise: false)+ path.closeSubpath()+ let backFrame = CGRect(x: 0, y: 0, width: w, height: h)+ back.bounds = CGRect(origin: .zero, size: backFrame.size)+ back.position = CGPoint(x: backFrame.minX, y: backFrame.maxY)+ back.path = path+ back.lineWidth = line+ backShade.frame = back.bounds+ backShadeMask.frame = back.bounds+ backShadeMask.path = path+ let frontFrame = CGRect(x: 0, y: tabH, width: w, height: h - tabH)+ front.bounds = CGRect(origin: .zero, size: frontFrame.size)+ front.position = CGPoint(x: frontFrame.minX, y: frontFrame.maxY)+ let panel = CGPath(roundedRect: front.bounds, cornerWidth: r, cornerHeight: r, transform: nil)+ frontPanel.frame = front.bounds+ frontPanel.path = panel+ frontPanel.lineWidth = line+ frontShade.frame = front.bounds+ frontShadeMask.frame = front.bounds+ frontShadeMask.path = panel+ layoutEmblem(scale: scale)+ // In the holder's space (the icon's), everything but the front panel; the margin keeps the+ // turned square over the whole glyph.+ backHolder.frame = CGRect(x: 0, y: 0, width: w, height: h)+ frontHole.bounds = front.bounds+ frontHole.position = front.position+ let hole = CGMutablePath()+ hole.addRect(front.bounds.insetBy(dx: -w, dy: -h))+ hole.addPath(panel)+ frontHole.path = hole+ // In the front panel, on whole points so each dot is two pixels square at 1x.+ dots.frame = bounds+ let dotPath = CGMutablePath()+ let y = (icon.frame.minY + frontFrame.midY).rounded(.down) - 1+ for i in -1...1 { dotPath.addEllipse(in: CGRect(x: (bounds.width / 2).rounded(.down) + CGFloat(i) * 4 - 1, y: y, width: 2, height: 2)) }+ dots.path = dotPath+ CATransaction.commit()+ }++ /// Rasterize only when the image, ink, size or backing scale changes, never on animation frames.+ private func layoutEmblem(scale: CGFloat) {+ guard let emblem else {+ mark.contents = nil+ renderedEmblem = nil+ return+ }+ let side = min(front.bounds.width, front.bounds.height) * min(1, max(0, emblem.relativeSize))+ guard side > 0 else { mark.contents = nil; renderedEmblem = nil; return }+ let size = CGSize(width: side, height: side)+ mark.bounds = CGRect(origin: .zero, size: size)+ mark.position = CGPoint(x: front.bounds.midX, y: front.bounds.midY)+ mark.contentsScale = scale+ let rendering = EmblemRendering(emblem: emblem, tint: emblemTint, size: size, scale: scale)+ guard rendering != renderedEmblem else { return }+ mark.contents = emblem.rasterized(size: size, scale: scale, tint: emblemTint)+ renderedEmblem = rendering+ }+}diff --git a/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift b/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swiftindex 76276d4..fd41ddf 100644--- a/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift+++ b/Sources/Cra/UI/Sidebar/List/SidebarRowViews.swift@@ -396,176 +396,6 @@ final class SidebarFaviconView: NSImageView {}}-/// Arc's folder glyph (FolderIconView, read from Arc 1.171's binary): a back panel with the tab-/// and a front panel, both hinged at the bottom-left corner. Opening turns them toward you in-/// perspective, the front tipping forward and the back leaning away, each shaded darker as it-/// turns, with Core Animation's implicit 0.25 s transition. GitHub's mark on a live folder.-final class FolderGlyphView: NSView {- /// Arc fixes the glyph at 16 × 16 pt.- static let side: CGFloat = 16- private let icon = CALayer()- private let back = CAShapeLayer()- private let backShade = CAGradientLayer()- private let backShadeMask = CAShapeLayer()- private let front = CALayer()- private let frontPanel = CAShapeLayer()- private let frontShade = CAGradientLayer()- private let frontShadeMask = CAShapeLayer()- /// The back panel shows only outside the front one. Theme folders are opaque (FolderTones), so- /// the front hides the back's outline anyway; the White and Black icon styles fill with- /// translucent tones, and without this an open folder showed the back's edges through the- /// front. The mask is the front panel's shape cut out of a larger square, turned with the- /// front's own transform so it moves with it.- private let backHolder = CALayer()- private let frontHole = CAShapeLayer()- private let mark = NSImageView()- /// Arc's peeked folder (FolderPeek): three 2 pt dots on a 4 pt pitch in the closed body.- private let dots = CAShapeLayer()- private var expanded = false-- override init(frame: NSRect) {- super.init(frame: frame)- wantsLayer = true- // Arc: black from clear to 0.36 across the back, to 0.15 across the front.- backShade.colors = [NSColor.black.withAlphaComponent(0).cgColor, NSColor.black.withAlphaComponent(0.36).cgColor]- backShade.startPoint = CGPoint(x: 0, y: 0)- backShade.endPoint = CGPoint(x: 0.6, y: 1)- backShade.mask = backShadeMask- frontShade.colors = [NSColor.black.withAlphaComponent(0).cgColor, NSColor.black.withAlphaComponent(0.15).cgColor]- frontShade.startPoint = CGPoint(x: 0.1, y: 0)- frontShade.endPoint = CGPoint(x: 0.9, y: 1)- frontShade.mask = frontShadeMask- back.addSublayer(backShade)- front.addSublayer(frontPanel)- front.addSublayer(frontShade)- for l in [back, front, frontHole] as [CALayer] {- // Hinged on the bottom-left corner.- l.anchorPoint = CGPoint(x: 0, y: 1)- }- frontHole.fillRule = .evenOdd- frontHole.fillColor = NSColor.black.cgColor- backHolder.addSublayer(back)- backHolder.mask = frontHole- icon.addSublayer(backHolder)- icon.addSublayer(front)- layer?.addSublayer(icon)- layer?.addSublayer(dots)- mark.image = GitHubMark.image- mark.imageScaling = .scaleProportionallyUpOrDown- addSubview(mark)- apply(animated: false)- }-- required init?(coder: NSCoder) { fatalError() }-- override var isFlipped: Bool { true }- override func hitTest(_ point: NSPoint) -> NSView? { nil }-- func show(expanded: Bool, peeked: Bool = false, live: Bool, tint: NSColor, fillColor: NSColor, backColor: NSColor? = nil, animated: Bool = false,- source: LiveFolderSource = .github) {- CATransaction.begin()- CATransaction.setDisableActions(true)- back.fillColor = (backColor ?? fillColor).cgColor- frontPanel.fillColor = fillColor.cgColor- for l in [back, frontPanel] { l.strokeColor = tint.cgColor }- mark.isHidden = !live- mark.image = source == .gitlab ? GitLabMark.image : GitHubMark.image- mark.contentTintColor = tint- dots.isHidden = !peeked || expanded || live- dots.fillColor = tint.cgColor- CATransaction.commit()- guard expanded != self.expanded else { return }- self.expanded = expanded- apply(animated: animated)- }-- /// Arc's per-state transforms: perspective m34 −0.00291, 3 pt to the right, 32° about the- /// vertical axis, then the back 28° and the front −30° about the horizontal one.- private func apply(animated: Bool) {- CATransaction.begin()- CATransaction.setDisableActions(!animated)- let open = expanded- var t = CATransform3DIdentity- t.m34 = -0.0029129464- t = CATransform3DTranslate(t, open ? 3 : 0, 0, 0)- t = CATransform3DRotate(t, open ? 32 * .pi / 180 : 0, 0, 1, 0)- back.transform = CATransform3DRotate(t, open ? 28 * .pi / 180 : 0, 1, 0, 0)- front.transform = CATransform3DRotate(t, open ? -30 * .pi / 180 : 0, 1, 0, 0)- frontHole.transform = front.transform- frontShade.opacity = open ? 1 : 0- backShade.opacity = open ? 1 : 0.2- CATransaction.commit()- }-- override func viewDidChangeBackingProperties() {- super.viewDidChangeBackingProperties()- needsLayout = true- }-- override func layout() {- super.layout()- let scale = window?.backingScaleFactor ?? 2- func px(_ v: CGFloat) -> CGFloat { (v * scale).rounded() / scale }- let w = Self.side, h = Self.side- CATransaction.begin()- CATransaction.setDisableActions(true)- icon.frame = CGRect(x: px((bounds.width - w) / 2), y: px((bounds.height - h) / 2), width: w, height: h)- let r = (w / 7).rounded()- let tabH = px((h * 0.25).rounded())- let t = (tabH / 1.5).rounded(.down)- let line = px(1.25)- // The back panel with its tab (the view is flipped: y runs down).- let path = CGMutablePath()- path.move(to: CGPoint(x: r, y: 0))- path.addLine(to: CGPoint(x: px(0.41 * w), y: 0))- path.addLine(to: CGPoint(x: px(0.58 * w), y: t))- path.addLine(to: CGPoint(x: w - r, y: t))- path.addArc(center: CGPoint(x: w - r, y: t + r), radius: r, startAngle: -.pi / 2, endAngle: 0, clockwise: false)- path.addLine(to: CGPoint(x: w, y: h - r))- path.addArc(center: CGPoint(x: w - r, y: h - r), radius: r, startAngle: 0, endAngle: .pi / 2, clockwise: false)- path.addLine(to: CGPoint(x: r, y: h))- path.addArc(center: CGPoint(x: r, y: h - r), radius: r, startAngle: .pi / 2, endAngle: .pi, clockwise: false)- path.addLine(to: CGPoint(x: 0, y: r))- path.addArc(center: CGPoint(x: r, y: r), radius: r, startAngle: .pi, endAngle: 1.5 * .pi, clockwise: false)- path.closeSubpath()- let backFrame = CGRect(x: 0, y: 0, width: w, height: h)- back.bounds = CGRect(origin: .zero, size: backFrame.size)- back.position = CGPoint(x: backFrame.minX, y: backFrame.maxY)- back.path = path- back.lineWidth = line- backShade.frame = back.bounds- backShadeMask.frame = back.bounds- backShadeMask.path = path- let frontFrame = CGRect(x: 0, y: tabH, width: w, height: h - tabH)- front.bounds = CGRect(origin: .zero, size: frontFrame.size)- front.position = CGPoint(x: frontFrame.minX, y: frontFrame.maxY)- let panel = CGPath(roundedRect: front.bounds, cornerWidth: r, cornerHeight: r, transform: nil)- frontPanel.frame = front.bounds- frontPanel.path = panel- frontPanel.lineWidth = line- frontShade.frame = front.bounds- frontShadeMask.frame = front.bounds- frontShadeMask.path = panel- // In the holder's space (the icon's), everything but the front panel; the margin keeps the- // turned square over the whole glyph.- backHolder.frame = CGRect(x: 0, y: 0, width: w, height: h)- frontHole.bounds = front.bounds- frontHole.position = front.position- let hole = CGMutablePath()- hole.addRect(front.bounds.insetBy(dx: -w, dy: -h))- hole.addPath(panel)- frontHole.path = hole- // In the front panel, on whole points so each dot is two pixels square at 1x.- dots.frame = bounds- let dotPath = CGMutablePath()- let y = (icon.frame.minY + frontFrame.midY).rounded(.down) - 1- for i in -1...1 { dotPath.addEllipse(in: CGRect(x: (bounds.width / 2).rounded(.down) + CGFloat(i) * 4 - 1, y: y, width: 2, height: 2)) }- dots.path = dotPath- CATransaction.commit()- mark.frame = NSRect(x: icon.frame.minX + (w - 7) / 2, y: icon.frame.minY + frontFrame.midY - 3.5, width: 7, height: 7)- }-}-/// Arc's keyboard-focus ring: 2 pt systemYellow with black 6/6 dashes, inside the edge.final class FocusRingLayer: CALayer {private let yellow = CAShapeLayer()@@ -1553,8 +1383,11 @@ final class FolderRowView: SidebarRowView, NSTextFieldDelegate {// The same folder opening or closing turns its glyph; a row taking another folder doesn't.let toggled = model.map { $0.id == m.id && $0.expanded != m.expanded } ?? falsemodel = m- glyph.show(expanded: m.expanded, peeked: m.peeked, live: m.live,- tint: m.tint, fillColor: m.fill, backColor: m.back, animated: toggled && window != nil, source: m.liveSource ?? .github)+ let emblem: FolderEmblem? = m.live+ ? FolderEmblem(image: m.liveSource == .gitlab ? GitLabMark.image : GitHubMark.image, coloring: .template)+ : nil+ glyph.show(expanded: m.expanded, peeked: m.peeked, emblem: emblem,+ tint: m.tint, fillColor: m.fill, backColor: m.back, animated: toggled && window != nil)titleLabel.stringValue = m.titletitleLabel.textColor = colors.textPrimarysetAccessibilityLabel(m.title)diff --git a/Tests/FolderGlyphTests.swift b/Tests/FolderGlyphTests.swiftnew file mode 100644index 0000000..d569c83--- /dev/null+++ b/Tests/FolderGlyphTests.swift@@ -0,0 +1,160 @@+import AppKit++/// Component contract: the real layer tree and image pixels, without CEF or an on-screen window.+@main+enum FolderGlyphTests {+ private static var checks = 0++ @MainActor+ static func main() {+ _ = NSApplication.shared+ NSApp.setActivationPolicy(.prohibited)+ followsFrontPanel()+ imageRendering()+ backingScaleAndRemoval()+ print("Folder glyph: \(checks) checks passed")+ }++ @MainActor+ private static func followsFrontPanel() {+ let glyph = makeGlyph()+ let emblem = FolderEmblem(image: image(), coloring: .template)+ show(glyph, emblem: emblem)+ let root = glyph.layer!+ let mark = imageLayer(in: root)!+ let front = root.sublayers!.first!.sublayers!.last!+ let corners = [CGPoint.zero, CGPoint(x: mark.bounds.width, y: 0),+ CGPoint(x: 0, y: mark.bounds.height),+ CGPoint(x: mark.bounds.width, y: mark.bounds.height)]+ let closed = corners.map { mark.convert($0, to: root) }+ let cachedImage = mark.contents as! CGImage+ for open in [true, false, true, false] {+ show(glyph, open: open, emblem: emblem, animated: true)+ for (i, corner) in corners.enumerated() {+ let actual = mark.convert(corner, to: root)+ let onPanel = CGPoint(x: (front.bounds.width - mark.bounds.width) / 2 + corner.x,+ y: (front.bounds.height - mark.bounds.height) / 2 + corner.y)+ let expected = front.convert(onPanel, to: root)+ expect(distance(actual, expected) < 0.001,+ "every emblem corner must follow the front panel's perspective when toggled")+ expect(open ? distance(actual, closed[i]) > 0.5 : distance(actual, closed[i]) < 0.001,+ "opening moves the emblem; closing returns it without drift")+ }+ expect((mark.contents as! CGImage) === cachedImage, "a toggle must reuse the image bitmap")+ }+ expect(glyph.hitTest(.zero) == nil, "the emblem must not intercept folder clicks")+ }++ @MainActor+ private static func imageRendering() {+ let glyph = makeGlyph()+ let wide = image(size: CGSize(width: 20, height: 10))+ show(glyph, emblem: FolderEmblem(image: wide))+ let mark = imageLayer(in: glyph.layer!)!+ let original = bitmap(mark)+ expect(isRed(pixel(original)), "original artwork keeps its color despite the blue folder tint")+ expect(pixel(original, y: 0).alphaComponent < 0.01, "wide artwork is letterboxed, not stretched")+ let originalImage = mark.contents as! CGImage+ show(glyph, emblem: FolderEmblem(image: wide))+ expect((mark.contents as! CGImage) === originalImage, "unchanged artwork is not rasterized again")++ show(glyph, emblem: FolderEmblem(image: wide, coloring: .template))+ expect(isBlue(pixel(bitmap(mark))), "template artwork takes the folder's blue ink")+ show(glyph, emblem: FolderEmblem(image: wide, coloring: .template), tint: .red)+ expect(isRed(pixel(bitmap(mark))), "changing the theme updates template ink")++ let tall = image(size: CGSize(width: 10, height: 20), color: .green)+ show(glyph, emblem: FolderEmblem(image: tall))+ let replaced = bitmap(mark)+ expect(pixel(replaced).greenComponent > 0.9, "replacing the emblem replaces its pixels")+ expect(pixel(replaced, x: 0).alphaComponent < 0.01, "tall artwork keeps its aspect ratio")++ show(glyph, emblem: FolderEmblem(image: wide, relativeSize: 0.5))+ expect(mark.bounds.size == CGSize(width: 6, height: 6), "size is relative to the front panel")+ show(glyph, emblem: FolderEmblem(image: wide, relativeSize: 2))+ expect(mark.bounds.size == CGSize(width: 12, height: 12), "oversized artwork fits inside the panel")+ show(glyph, emblem: FolderEmblem(image: wide, relativeSize: 0))+ expect(mark.contents == nil, "zero-size artwork has no stale pixels")+ show(glyph, emblem: FolderEmblem(image: NSImage(size: .zero)))+ expect(mark.contents == nil, "an empty image has no stale pixels")+ }++ @MainActor+ private static func backingScaleAndRemoval() {+ let window = EmblemTestWindow(contentRect: NSRect(x: -10000, y: -10000, width: 100, height: 100),+ styleMask: .borderless, backing: .buffered, defer: false)+ window.isReleasedWhenClosed = false+ defer { window.close() }+ let glyph = makeGlyph()+ window.contentView!.addSubview(glyph)+ let emblem = FolderEmblem(image: image())+ for scale: CGFloat in [1, 2, 1] {+ window.testScale = scale+ glyph.viewDidChangeBackingProperties()+ show(glyph, emblem: emblem)+ let mark = imageLayer(in: glyph.layer!)!+ let pixels = bitmap(mark)+ expect(pixels.pixelsWide == Int(7 * scale) && pixels.pixelsHigh == Int(7 * scale),+ "moving between 1x and Retina regenerates artwork at the correct resolution")+ expect(mark.contentsScale == scale, "the bitmap is displayed at its backing scale")+ }+ let mark = imageLayer(in: glyph.layer!)!+ show(glyph, emblem: nil, peeked: true)+ expect(mark.isHidden && mark.contents == nil, "reusing a row without an emblem removes the old image")+ let dots = glyph.layer!.sublayers!.last!+ expect(!dots.isHidden, "a plain peeked folder still shows its dots")+ show(glyph, open: true, emblem: nil, peeked: true)+ expect(dots.isHidden, "an open folder hides peek dots")+ show(glyph, emblem: emblem, peeked: true)+ expect(!mark.isHidden && mark.contents != nil, "an emblem reappears after removing it")+ expect(dots.isHidden, "an emblem takes precedence over peek dots")+ }++ @MainActor+ private static func makeGlyph() -> FolderGlyphView {+ FolderGlyphView(frame: NSRect(x: 0, y: 0, width: 20, height: 20))+ }++ @MainActor+ private static func show(_ glyph: FolderGlyphView, open: Bool = false, emblem: FolderEmblem?,+ tint: NSColor = .blue, peeked: Bool = false, animated: Bool = false) {+ glyph.show(expanded: open, peeked: peeked, emblem: emblem, tint: tint,+ fillColor: .white, animated: animated)+ glyph.layoutSubtreeIfNeeded()+ }++ private static func image(size: CGSize = CGSize(width: 10, height: 10), color: NSColor = .red) -> NSImage {+ NSImage(size: size, flipped: false) { rect in+ color.setFill()+ rect.fill()+ return true+ }+ }++ private static func imageLayer(in layer: CALayer) -> CALayer? {+ if layer.contents != nil { return layer }+ return layer.sublayers?.lazy.compactMap { imageLayer(in: $0) }.first+ }++ private static func bitmap(_ layer: CALayer) -> NSBitmapImageRep {+ NSBitmapImageRep(cgImage: layer.contents as! CGImage)+ }++ private static func pixel(_ bitmap: NSBitmapImageRep, x: Int? = nil, y: Int? = nil) -> NSColor {+ bitmap.colorAt(x: x ?? bitmap.pixelsWide / 2, y: y ?? bitmap.pixelsHigh / 2)!.usingColorSpace(.deviceRGB)!+ }++ private static func isRed(_ color: NSColor) -> Bool { color.redComponent > 0.9 && color.blueComponent < 0.1 }+ private static func isBlue(_ color: NSColor) -> Bool { color.blueComponent > 0.9 && color.redComponent < 0.1 }+ private static func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat { hypot(a.x - b.x, a.y - b.y) }++ private static func expect(_ condition: @autoclosure () -> Bool, _ message: String) {+ guard condition() else { print("FAIL: \(message)"); exit(1) }+ checks += 1+ }+}++private final class EmblemTestWindow: NSWindow {+ var testScale: CGFloat = 2+ override var backingScaleFactor: CGFloat { testScale }+}diff --git a/docs/development.md b/docs/development.mdindex 5591967..588bd62 100644--- a/docs/development.md+++ b/docs/development.md@@ -67,6 +67,7 @@ the save panel.scripts/test.sh # everything below except the smoke test, in secondsscripts/test.sh core # only Packages/CraCore+ scripts/test-folder-glyph.sh # folder emblems: layer projection, colors and Retina resolutionTests/smoke-launch.sh # launches build/Cra.app three times, about 30 sscripts/scenario.sh all # user journeys in build/Cra.app, about 6 minutesscripts/scenario.sh sleep-tabs # one of them (Tests/Scenarios/<name>.scenario)@@ -97,6 +98,9 @@ to Markdown or `docs/` alone runs nothing.`ManualScheduler`, against the made-up Arc and Chromium profiles in `Tests/Fixtures`.- `scripts/test-policies.sh`: the decisions pulled out of the window controllers(`ExtensionLaunch`, `DevToolsToggle`, `FeatureFlags`).+- `scripts/test-folder-glyph.sh`: the standalone folder component's real AppKit layers and image+ pixels: emblems follow the front panel's perspective, keep their colors and proportions, and+ update at 1x/Retina. No CEF or visible window; coverage uses the same harness as `scripts/test.sh`.- `scripts/test-perf.sh`: the performance recorder on a real main run loop: injected stalls,hang files with a `sample` stack, the session marker, ownership, the recording switch([performance.md](performance.md)).diff --git a/scripts/test-folder-glyph.sh b/scripts/test-folder-glyph.shnew file mode 100755index 0000000..f14466d--- /dev/null+++ b/scripts/test-folder-glyph.sh@@ -0,0 +1,11 @@+#!/bin/bash+# Folder emblems: real AppKit layer projection and bitmap pixels, without CEF or visible windows.+set -euo pipefail+cd "$(dirname "$0")/.."+TEST_DIR="$(mktemp -d "${TMPDIR:-/tmp}/cra-folder-glyph.XXXXXX")"+trap 'rm -rf "$TEST_DIR"' EXIT+xcrun swiftc -swift-version 5 -parse-as-library ${CRA_TEST_SWIFTC_FLAGS:-} \+ Sources/Cra/UI/Sidebar/List/FolderGlyphView.swift Tests/FolderGlyphTests.swift \+ -o "$TEST_DIR/folder-glyph-tests"+"$TEST_DIR/folder-glyph-tests"+[ -z "${CRA_TEST_KEEP:-}" ] || cp "$TEST_DIR/folder-glyph-tests" "$CRA_TEST_KEEP/"diff --git a/scripts/test.sh b/scripts/test.shindex 10b1943..6497888 100755--- a/scripts/test.sh+++ b/scripts/test.sh@@ -28,6 +28,7 @@ if [ "${1:-}" != core ]; thensuite "Store and importers" scripts/test-store.shsuite "Policies" scripts/test-policies.shsuite "Command bar glyph" scripts/test-command-bar-glyph.sh+ suite "Folder glyph" scripts/test-folder-glyph.shsuite "Perf recorder" scripts/test-perf.shsuite "Lint" scripts/lint.pyfi