Repository navigation
Expand file tree
/
Copy pathBackgroundRenderer.swift
More file actions
247 lines (213 loc) · 8.47 KB
/
Copy pathBackgroundRenderer.swift
File metadata and controls
247 lines (213 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import AppKit
import QuartzCore
enum BackgroundStyle: Hashable, Identifiable {
case checkerboard
case color(red: CGFloat, green: CGFloat, blue: CGFloat)
case texture(name: String, ext: String)
var id: String {
switch self {
case .checkerboard: return "checkerboard"
case .color(let r, let g, let b): return "color-\(r)-\(g)-\(b)"
case .texture(let name, let ext): return "texture-\(name).\(ext)"
}
}
static let allBackgrounds: [BackgroundStyle] = [
.checkerboard,
.color(red: 1, green: 0, blue: 0),
.color(red: 0, green: 1, blue: 0),
.color(red: 0, green: 0, blue: 1),
.texture(name: "white-gravel-128x128", ext: "png"),
.texture(name: "clear-sea-water-128x128", ext: "png"),
.texture(name: "green-grass-128x128", ext: "png"),
.texture(name: "dark-smooth-stone-128x128", ext: "png"),
.texture(name: "dark-wood-parquet-128x128", ext: "png"),
.texture(name: "burning-hot-lava-128x128", ext: "png"),
.texture(name: "dark-lucy-leaves-128x128", ext: "png"),
.texture(name: "brick-wall-128x128", ext: "png"),
]
/// What VoiceOver calls the style's button in the sidebar.
var accessibilityName: String {
switch self {
case .checkerboard:
return "Checkerboard"
case .color(1, 0, 0):
return "Red"
case .color(0, 1, 0):
return "Green"
case .color(0, 0, 1):
return "Blue"
case .color:
return "Solid color"
case .texture(let name, _):
// "white-gravel-128x128" → "White gravel"
let words = name.replacingOccurrences(of: "-128x128", with: "").replacingOccurrences(
of: "-", with: " ")
return words.prefix(1).uppercased() + words.dropFirst()
}
}
/// Read from the bundle once per texture, since the sidebar's thumbnails
/// ask on every redraw. NSCache needs no lock across threads.
/// /documentation/foundation/nscache
private static let textureCache = NSCache<NSString, NSImage>()
/// The texture's image; nil for other styles or a missing file. The
/// subdirectory form, because path(forResource:ofType:) "does not recurse
/// through other subfolders". /documentation/foundation/bundle/path(forresource:oftype:)
var textureImage: NSImage? {
guard case .texture(let name, let ext) = self else { return nil }
let key = "\(name).\(ext)" as NSString
if let cached = Self.textureCache.object(forKey: key) { return cached }
guard let url = Bundle.main.url(forResource: name, withExtension: ext, subdirectory: "textures"),
let image = NSImage(contentsOf: url)
else { return nil }
Self.textureCache.setObject(image, forKey: key)
return image
}
}
protocol BackgroundRendering {
func makeLayer() -> CALayer
var canMove: Bool { get }
func moveBy(_ delta: NSSize)
}
class ColorBackground: BackgroundRendering {
let cgColor: CGColor
init(color: NSColor) {
let srgbColor = color.usingColorSpace(.sRGB) ?? color
self.cgColor = srgbColor.cgColor
}
init(r: CGFloat, g: CGFloat, b: CGFloat) {
self.cgColor = CGColor(srgbRed: r, green: g, blue: b, alpha: 1)
}
var canMove: Bool { false }
func makeLayer() -> CALayer {
let layer = CALayer()
layer.backgroundColor = cgColor
return layer
}
func moveBy(_ delta: NSSize) {}
}
class PatternBackground: BackgroundRendering {
private let cgImage: CGImage?
private var offset: CGPoint = .zero
private weak var currentLayer: CALayer?
init(image: NSImage) {
var rect = NSRect(origin: .zero, size: image.size)
self.cgImage = image.cgImage(forProposedRect: &rect, context: nil, hints: nil)
}
var canMove: Bool { true }
func makeLayer() -> CALayer {
let layer = CALayer()
layer.actions = [
"backgroundColor": NSNull(),
"contents": NSNull(),
"bounds": NSNull(),
]
currentLayer = layer
updateTiling(on: layer)
return layer
}
func moveBy(_ delta: NSSize) {
offset.x += delta.width
offset.y += delta.height
if let layer = currentLayer {
updateTiling(on: layer)
}
}
private func updateTiling(on layer: CALayer) {
guard let img = cgImage else { return }
let width = CGFloat(img.width)
let height = CGFloat(img.height)
// Retain the CGImage for the pattern callback lifetime
let retainedImage = Unmanaged<CGImage>.passRetained(img)
var callbacks = CGPatternCallbacks(
version: 0,
drawPattern: { info, ctx in
guard let info = info else { return }
let image = Unmanaged<CGImage>.fromOpaque(info).takeUnretainedValue()
ctx.draw(
image, in: CGRect(x: 0, y: 0, width: CGFloat(image.width), height: CGFloat(image.height)))
},
releaseInfo: { info in
guard let info = info else { return }
Unmanaged<CGImage>.fromOpaque(info).release()
})
let rawPtr = retainedImage.toOpaque()
guard
let pattern = CGPattern(
info: rawPtr,
bounds: CGRect(x: 0, y: 0, width: width, height: height),
matrix: CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: offset.x, ty: offset.y),
xStep: width,
yStep: height,
tiling: .constantSpacing,
isColored: true,
callbacks: &callbacks
)
else {
retainedImage.release()
return
}
guard let space = CGColorSpace(patternBaseSpace: nil) else { return }
var alpha: CGFloat = 1.0
guard let color = CGColor(patternSpace: space, pattern: pattern, components: &alpha) else { return }
layer.backgroundColor = color
}
}
class CheckerboardBackground: BackgroundRendering {
// Light mode: white (1.0) + light gray (0.86) — classic Photoshop
// Dark mode: dark gray (0.24) + slightly lighter (0.30)
static func checkerLightWhite(isDark: Bool) -> CGFloat { isDark ? 0.30 : 1.0 }
static func checkerDarkWhite(isDark: Bool) -> CGFloat { isDark ? 0.24 : 0.86 }
static func isDark(_ appearance: NSAppearance) -> Bool {
appearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
}
static let checkerLight = NSColor(name: nil) { appearance in
NSColor(white: checkerLightWhite(isDark: isDark(appearance)), alpha: 1)
}
static let checkerDark = NSColor(name: nil) { appearance in
NSColor(white: checkerDarkWhite(isDark: isDark(appearance)), alpha: 1)
}
/// Fixed at creation rather than resolved from dynamic colours while
/// drawing, when the thread's appearance needn't be the view's.
/// /documentation/appkit/nsappearance/current
private let isDark: Bool
init(isDark: Bool) {
self.isDark = isDark
}
var canMove: Bool { false }
func makeLayer() -> CALayer {
guard let checkerImage = createCheckerboardImage() else {
let layer = CALayer()
layer.backgroundColor = CGColor(gray: 0.8, alpha: 1)
return layer
}
let bg = PatternBackground(image: checkerImage)
return bg.makeLayer()
}
func moveBy(_ delta: NSSize) {}
private func createCheckerboardImage() -> NSImage? {
let size = 16
let light = NSColor(white: Self.checkerLightWhite(isDark: isDark), alpha: 1)
let dark = NSColor(white: Self.checkerDarkWhite(isDark: isDark), alpha: 1)
return NSImage(size: NSSize(width: size * 2, height: size * 2), flipped: false) { rect in
dark.setFill()
rect.fill()
light.setFill()
NSRect(x: 0, y: 0, width: size, height: size).fill()
NSRect(x: size, y: size, width: size, height: size).fill()
return true
}
}
}
func makeBackgroundRenderer(for style: BackgroundStyle, isDark: Bool) -> BackgroundRendering {
switch style {
case .checkerboard:
return CheckerboardBackground(isDark: isDark)
case .color(let r, let g, let b):
return ColorBackground(r: r, g: g, b: b)
case .texture:
if let image = style.textureImage {
return PatternBackground(image: image)
}
return ColorBackground(r: 0.5, g: 0.5, b: 0.5)
}
}