From 8fbdbe638b89071f469b0c831b1437e9ec5c9d6f Mon Sep 17 00:00:00 2001 From: AriesZhou Date: Wed, 25 Nov 2026 12:01:01 +0800 Subject: [PATCH] fix: clip burning snapshot to paper and fade shadow --- Sources/Howler/HowlerView.swift | 36 +++++++------- Sources/Howler/PanelController.swift | 48 ++++++++++++++++--- .../HowlerAppTests/LetterLifecycleTests.swift | 45 +++++++++++++++++ 3 files changed, 106 insertions(+), 23 deletions(-) diff --git a/Sources/Howler/HowlerView.swift b/Sources/Howler/HowlerView.swift index 6a3837d..e1b2159 100644 --- a/Sources/Howler/HowlerView.swift +++ b/Sources/Howler/HowlerView.swift @@ -31,7 +31,7 @@ struct HowlerView: View { var body: some View { let parchment = Parchment.forScheme(viewModel.candlelit ? .dark : .light) ZStack(alignment: .top) { - shadow + SheetShadow() ZStack(alignment: .top) { sheet(parchment) if settings.isConfigured { @@ -93,22 +93,6 @@ struct HowlerView: View { // MARK: Sheet - /// Cast separately from the sheet so it can fade while the paper burns. The sheet's - /// own area is cut out, so holes burned in the paper show the desktop, not a backing. - private var shadow: some View { - ZStack { - DeckledSheet() - .fill(Color.black) - .shadow(color: .black.opacity(0.30), radius: 20, x: 0, y: 12) - .shadow(color: .black.opacity(0.14), radius: 1.5, x: 0, y: 1) - DeckledSheet() - .fill(Color.black) - .blendMode(.destinationOut) - } - .compositingGroup() - .allowsHitTesting(false) - } - private func sheet(_ parchment: Parchment) -> some View { DeckledSheet() .fill(parchment.paper) @@ -432,3 +416,21 @@ private struct SetupPage: View { .padding(24) } } + +/// Cast separately from the sheet so it can fade while the paper burns. The sheet's +/// own area is cut out, so holes burned in the paper show the desktop, not a backing. +struct SheetShadow: View { + var body: some View { + ZStack { + DeckledSheet() + .fill(Color.black) + .shadow(color: .black.opacity(0.30), radius: 20, x: 0, y: 12) + .shadow(color: .black.opacity(0.14), radius: 1.5, x: 0, y: 1) + DeckledSheet() + .fill(Color.black) + .blendMode(.destinationOut) + } + .compositingGroup() + .allowsHitTesting(false) + } +} diff --git a/Sources/Howler/PanelController.swift b/Sources/Howler/PanelController.swift index f2f5c06..972b154 100644 --- a/Sources/Howler/PanelController.swift +++ b/Sources/Howler/PanelController.swift @@ -167,7 +167,13 @@ final class PanelController { viewModel.burn() // invalidate requests and clear content before the animation viewModel.setVisible(false) guard let snapshot else { finishHiding(); return } - panel.contentView = NSHostingView(rootView: BurningSnapshot(image: snapshot, start: Date())) + let margins = EdgeInsets( + top: Self.margins.top, + leading: Self.margins.left, + bottom: Self.margins.bottom, + trailing: Self.margins.right + ) + panel.contentView = NSHostingView(rootView: BurningSnapshot(image: snapshot, margins: margins, start: Date())) let completion = DispatchWorkItem { [weak self] in self?.finishHiding() } burnCompletion = completion DispatchQueue.main.asyncAfter(deadline: .now() + BurnField.duration, execute: completion) @@ -398,16 +404,46 @@ final class ClosureMenuItem: NSMenuItem { } /// A short-lived rendered image, never a live conversation model or a file on disk. -private struct BurningSnapshot: View { +struct BurningSnapshot: View { let image: NSImage + let margins: EdgeInsets let start: Date + private let field: BurnField + private let paperSize: CGSize + + init(image: NSImage, margins: EdgeInsets, start: Date) { + self.image = image + self.margins = margins + self.start = start + let paperSize = CGSize( + width: max(1, image.size.width - margins.leading - margins.trailing), + height: max(1, image.size.height - margins.top - margins.bottom) + ) + self.paperSize = paperSize + self.field = BurnField(size: paperSize) + } + var body: some View { TimelineView(.animation) { timeline in - Image(nsImage: image) - .resizable() - .modifier(BurningLetter(field: BurnField(size: image.size), - progress: BurnField.progress(elapsed: timeline.date.timeIntervalSince(start)))) + let progress = BurnField.progress(elapsed: timeline.date.timeIntervalSince(start)) + ZStack(alignment: .top) { + // Fade the shadow out as the first flames catch so no outline lingers in the margins. + SheetShadow() + .opacity(1 - min(max(progress * 3, 0), 1)) + Color.clear + .frame(width: paperSize.width, height: paperSize.height) + .overlay(alignment: .topLeading) { + Image(nsImage: image) + .resizable() + .frame(width: image.size.width, height: image.size.height) + .offset(x: -margins.leading, y: -margins.top) + } + .clipShape(DeckledSheet()) + .modifier(BurningLetter(field: field, progress: progress)) + } + .frame(width: paperSize.width, height: paperSize.height) } + .padding(margins) .frame(width: image.size.width, height: image.size.height) .accessibilityHidden(true) } diff --git a/Tests/HowlerAppTests/LetterLifecycleTests.swift b/Tests/HowlerAppTests/LetterLifecycleTests.swift index c2c4716..2e13c46 100644 --- a/Tests/HowlerAppTests/LetterLifecycleTests.swift +++ b/Tests/HowlerAppTests/LetterLifecycleTests.swift @@ -1,4 +1,5 @@ import AppKit +import SwiftUI import XCTest import HowlerCore @testable import Howler @@ -159,4 +160,48 @@ final class LetterLifecycleTests: XCTestCase { settings.resetScreenshotHotKey() XCTAssertEqual(settings.screenshotHotKey, .screenshot) } + + func testBurningSnapshotDoesNotLeaveShadowOrScorchInMargins() throws { + _ = NSApplication.shared + let margins = EdgeInsets( + top: PanelController.margins.top, + leading: PanelController.margins.left, + bottom: PanelController.margins.bottom, + trailing: PanelController.margins.right + ) + let size = NSSize(width: 240, height: 220) + // Paint the entire snapshot opaque (simulating paper + baked-in shadow in the margins). + let image = NSImage(size: size, flipped: false) { rect in + NSColor.black.withAlphaComponent(0.5).setFill() + rect.fill() + return true + } + let view = NSHostingView(rootView: BurningSnapshot( + image: image, + margins: margins, + start: Date().addingTimeInterval(-0.35) + )) + view.frame = NSRect(origin: .zero, size: size) + view.layoutSubtreeIfNeeded() + let bitmap = try XCTUnwrap(view.bitmapImageRepForCachingDisplay(in: view.bounds)) + view.cacheDisplay(in: view.bounds, to: bitmap) + + let scale = CGFloat(bitmap.pixelsWide) / size.width + + // Outer margins (top-left and bottom-right) must be clear of baked-in shadow and scorch. + let topLeft = try XCTUnwrap(bitmap.colorAt(x: Int(8 * scale), y: Int(8 * scale))) + XCTAssertLessThan(topLeft.alphaComponent, 0.05, "Top-left margin should be clear during burn") + let bottomRight = try XCTUnwrap(bitmap.colorAt( + x: bitmap.pixelsWide - Int(8 * scale), + y: bitmap.pixelsHigh - Int(8 * scale) + )) + XCTAssertLessThan(bottomRight.alphaComponent, 0.05, "Bottom-right margin should be clear during burn") + + // Unburned top center of the paper remains visible. + let paperTopCenter = try XCTUnwrap(bitmap.colorAt( + x: bitmap.pixelsWide / 2, + y: Int((margins.top + 20) * scale) + )) + XCTAssertGreaterThan(paperTopCenter.alphaComponent, 0.4, "Unburned top of paper should remain visible") + } }