Repository navigation
Fix macOS 27 symbol launch crash #6728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
94ed3b2
72665b1
0228823
c3f35cb
0b8cb2c
b19dc3c
a3e7c20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,24 @@ | ||
| import AppKit | ||
| import CmuxFoundation | ||
| import SwiftUI | ||
|
|
||
| enum RenderableSystemSymbol { | ||
| static let defaultWorkspaceGroupIcon = "folder.fill" | ||
| static let defaultSurfaceTabIcon = "doc.text" | ||
| private static let minimumRasterPointSize: CGFloat = 1 | ||
| private static let appKitImageCacheLimit = 256 | ||
| @MainActor | ||
| private static var renderabilityCache: [String: Bool] = [:] | ||
| @MainActor | ||
| private static var appKitImageCache: [AppKitImageCacheKey: NSImage] = [:] | ||
| @MainActor | ||
| private static var appKitImageCacheInsertionOrder: [AppKitImageCacheKey] = [] | ||
|
|
||
| private struct AppKitImageCacheKey: Hashable { | ||
| let systemName: String | ||
| let rasterSize: CGFloat | ||
| let weightRawValue: CGFloat | ||
| } | ||
|
|
||
| static func trimmed(_ raw: String?) -> String? { | ||
| guard let trimmed = raw?.trimmingCharacters(in: .whitespacesAndNewlines), | ||
|
|
@@ -58,24 +70,104 @@ enum RenderableSystemSymbol { | |
| return max(minimumRasterPointSize, pointSize) | ||
| } | ||
|
|
||
| static func resolvedRasterPointSize( | ||
| _ pointSize: CGFloat, | ||
| globalFontPercent: Int, | ||
| appliesGlobalFontMagnification: Bool | ||
| ) -> CGFloat { | ||
| let rasterSize = clampedRasterPointSize(pointSize) | ||
| guard appliesGlobalFontMagnification else { | ||
| return rasterSize | ||
| } | ||
| return GlobalFontMagnification.scaledSize(rasterSize, percent: globalFontPercent) | ||
| } | ||
|
|
||
| @MainActor | ||
| static func configuredAppKitImage( | ||
| systemName: String, | ||
| pointSize: CGFloat, | ||
| weight: Font.Weight? = nil | ||
| ) -> NSImage? { | ||
| let rasterSize = clampedRasterPointSize(pointSize) | ||
| let fontWeight = nsFontWeight(for: weight) | ||
| let cacheKey = AppKitImageCacheKey( | ||
| systemName: systemName, | ||
| rasterSize: rasterSize, | ||
| weightRawValue: fontWeight.rawValue | ||
| ) | ||
| if let cached = appKitImageCache[cacheKey] { | ||
| return cached | ||
| } | ||
| guard let baseImage = NSImage(systemSymbolName: systemName, accessibilityDescription: nil) else { | ||
| return nil | ||
| } | ||
| let configuration = NSImage.SymbolConfiguration( | ||
| pointSize: rasterSize, | ||
| weight: fontWeight | ||
| ) | ||
| let configuredImage = baseImage.withSymbolConfiguration(configuration) ?? baseImage | ||
| let image = (configuredImage.copy() as? NSImage) ?? configuredImage | ||
| image.isTemplate = true | ||
| image.size = NSSize(width: rasterSize, height: rasterSize) | ||
| appKitImageCache[cacheKey] = image | ||
| appKitImageCacheInsertionOrder.append(cacheKey) | ||
| while appKitImageCacheInsertionOrder.count > appKitImageCacheLimit { | ||
| let evictedKey = appKitImageCacheInsertionOrder.removeFirst() | ||
| appKitImageCache.removeValue(forKey: evictedKey) | ||
| } | ||
| return image | ||
| } | ||
|
|
||
| private static func nsFontWeight(for weight: Font.Weight?) -> NSFont.Weight { | ||
| guard let weight else { return .regular } | ||
| if weight == .ultraLight { return .ultraLight } | ||
| if weight == .thin { return .thin } | ||
| if weight == .light { return .light } | ||
| if weight == .medium { return .medium } | ||
| if weight == .semibold { return .semibold } | ||
| if weight == .bold { return .bold } | ||
| if weight == .heavy { return .heavy } | ||
| if weight == .black { return .black } | ||
| return .regular | ||
| } | ||
|
|
||
| #if DEBUG | ||
| @MainActor | ||
| static func resetRenderabilityCacheForTesting() { | ||
| renderabilityCache.removeAll() | ||
| appKitImageCache.removeAll() | ||
| appKitImageCacheInsertionOrder.removeAll() | ||
| } | ||
| #endif | ||
|
Comment on lines
166
to
174
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| } | ||
|
|
||
| extension Image { | ||
| /// Keeps a positive symbol frame while avoiding the blank-prone resizable SF Symbol rasterizer. | ||
| func cmuxSymbolRasterSize( | ||
| _ pointSize: CGFloat, | ||
| weight: Font.Weight? = nil, | ||
| alignment: Alignment = .center | ||
| ) -> some View { | ||
| let rasterSize = RenderableSystemSymbol.clampedRasterPointSize(pointSize) | ||
| let systemFont: Font = weight.map { .system(size: rasterSize, weight: $0) } ?? .system(size: rasterSize) | ||
| return font(systemFont) | ||
| .frame(width: rasterSize, height: rasterSize, alignment: alignment) | ||
| struct CmuxSystemSymbolImage: View { | ||
| @Environment(\.cmuxGlobalFontMagnificationPercent) private var globalFontPercent | ||
|
|
||
| let systemName: String | ||
| let pointSize: CGFloat | ||
| var weight: Font.Weight? | ||
| var alignment: Alignment = .center | ||
| var appliesGlobalFontMagnification = false | ||
|
|
||
| var body: some View { | ||
| let rasterSize = RenderableSystemSymbol.resolvedRasterPointSize( | ||
| pointSize, | ||
| globalFontPercent: globalFontPercent, | ||
| appliesGlobalFontMagnification: appliesGlobalFontMagnification | ||
| ) | ||
| if let image = RenderableSystemSymbol.configuredAppKitImage( | ||
| systemName: systemName, | ||
| pointSize: rasterSize, | ||
| weight: weight | ||
| ) { | ||
| Image(nsImage: image) | ||
| .renderingMode(.template) | ||
| .frame(width: rasterSize, height: rasterSize, alignment: alignment) | ||
| } else { | ||
| Color.clear | ||
| .frame(width: rasterSize, height: rasterSize, alignment: alignment) | ||
| .accessibilityHidden(true) | ||
| } | ||
| } | ||
|
Comment on lines
+215
to
234
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 72665b1 by adding a bounded @mainactor cache keyed by symbol name, clamped raster size, and AppKit font weight. Added a test that repeated configuredAppKitImage calls reuse the same NSImage instance. — Claude Code |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.