Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions Sources/Howler/HowlerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
48 changes: 42 additions & 6 deletions Sources/Howler/PanelController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
45 changes: 45 additions & 0 deletions Tests/HowlerAppTests/LetterLifecycleTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AppKit
import SwiftUI
import XCTest
import HowlerCore
@testable import Howler
Expand Down Expand Up @@ -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")
}
}
Loading