Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
161 changes: 118 additions & 43 deletions Sources/ContentView.swift

Large diffs are not rendered by default.

117 changes: 69 additions & 48 deletions Sources/Panels/BrowserPanelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,6 @@ private struct OmnibarAddressButtonStyleBody: View {
}
}

private extension Image {
func cmuxFlatSymbolColorRendering() -> Image {
// `symbolColorRenderingMode(.flat)` is not available in the current SDK
// used by CI/local builds. Keep this modifier as a compatibility no-op.
self
}
}

func resolvedBrowserChromeBackgroundColor(
for colorScheme: ColorScheme,
themeBackgroundColor: NSColor,
Expand Down Expand Up @@ -1329,8 +1321,11 @@ struct BrowserPanelView: View {
#endif
panel.goBack()
}) {
Image(systemName: "chevron.left")
.cmuxSymbolRasterSize(chromeMetrics.navigationIconFontSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: "chevron.left",
pointSize: chromeMetrics.navigationIconFontSize,
weight: .medium
)
.frame(width: addressBarButtonHitSize, height: addressBarButtonHitSize, alignment: .center)
.contentShape(Rectangle())
}
Expand All @@ -1345,8 +1340,11 @@ struct BrowserPanelView: View {
#endif
panel.goForward()
}) {
Image(systemName: "chevron.right")
.cmuxSymbolRasterSize(chromeMetrics.navigationIconFontSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: "chevron.right",
pointSize: chromeMetrics.navigationIconFontSize,
weight: .medium
)
.frame(width: addressBarButtonHitSize, height: addressBarButtonHitSize, alignment: .center)
.contentShape(Rectangle())
}
Expand All @@ -1356,8 +1354,11 @@ struct BrowserPanelView: View {
.safeHelp(String(localized: "browser.goForward", defaultValue: "Go Forward"))

Button(action: handleReloadOrStopButtonAction) {
Image(systemName: panel.isLoading ? "xmark" : "arrow.clockwise")
.cmuxSymbolRasterSize(chromeMetrics.navigationIconFontSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: panel.isLoading ? "xmark" : "arrow.clockwise",
pointSize: chromeMetrics.navigationIconFontSize,
weight: .medium
)
.frame(width: addressBarButtonHitSize, height: addressBarButtonHitSize, alignment: .center)
.contentShape(Rectangle())
}
Expand Down Expand Up @@ -1388,10 +1389,11 @@ struct BrowserPanelView: View {

private var screenshotPageButton: some View {
Button(action: handleScreenshotPageButtonAction) {
Image(systemName: screenshotPageCopied ? "checkmark" : "camera")
.symbolRenderingMode(.monochrome)
.cmuxFlatSymbolColorRendering()
.cmuxSymbolRasterSize(devToolsButtonIconSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: screenshotPageCopied ? "checkmark" : "camera",
pointSize: devToolsButtonIconSize,
weight: .medium
)
.foregroundStyle(screenshotPageButtonColor)
.frame(width: addressBarButtonSize, height: addressBarButtonSize, alignment: .center)
}
Expand Down Expand Up @@ -1444,8 +1446,11 @@ struct BrowserPanelView: View {
private var browserFocusModeButton: some View {
Button(action: handleBrowserFocusModeButtonAction) {
HStack(spacing: 5) {
Image(systemName: "keyboard")
.cmuxSymbolRasterSize(devToolsButtonIconSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: "keyboard",
pointSize: devToolsButtonIconSize,
weight: .medium
)
.scaleEffect(panel.isBrowserFocusModeActive ? 1.08 : 1.0)
.animation(.spring(response: 0.18, dampingFraction: 0.82), value: panel.isBrowserFocusModeActive)
if panel.isBrowserFocusModeActive {
Expand Down Expand Up @@ -1490,10 +1495,11 @@ struct BrowserPanelView: View {
panel.clearReactGrabRoundTrip(reason: "toolbarButton.manualStart")
Task { await panel.toggleOrInjectReactGrab() }
}) {
Image(systemName: "cursorarrow.click.2")
.symbolRenderingMode(.monochrome)
.cmuxFlatSymbolColorRendering()
.cmuxSymbolRasterSize(devToolsButtonIconSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: "cursorarrow.click.2",
pointSize: devToolsButtonIconSize,
weight: .medium
)
.foregroundStyle(panel.isReactGrabActive ? Color.accentColor : Color.secondary)
.frame(width: addressBarButtonSize, height: addressBarButtonSize, alignment: .center)
}
Expand All @@ -1507,10 +1513,11 @@ struct BrowserPanelView: View {
Button(action: {
openDevTools()
}) {
Image(systemName: devToolsIconOption.rawValue)
.symbolRenderingMode(.monochrome)
.cmuxFlatSymbolColorRendering()
.cmuxSymbolRasterSize(devToolsButtonIconSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: devToolsIconOption.rawValue,
pointSize: devToolsButtonIconSize,
weight: .medium
)
.foregroundStyle(devToolsColorOption.color)
.frame(width: addressBarButtonSize, height: addressBarButtonSize, alignment: .center)
}
Expand All @@ -1524,10 +1531,11 @@ struct BrowserPanelView: View {
Button(action: {
isBrowserProfileMenuPresented.toggle()
}) {
Image(systemName: "person.crop.circle")
.symbolRenderingMode(.monochrome)
.cmuxFlatSymbolColorRendering()
.cmuxSymbolRasterSize(devToolsButtonIconSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: "person.crop.circle",
pointSize: devToolsButtonIconSize,
weight: .medium
)
.foregroundStyle(devToolsColorOption.color)
.frame(width: addressBarButtonSize, height: addressBarButtonSize, alignment: .center)
}
Expand Down Expand Up @@ -1585,10 +1593,11 @@ struct BrowserPanelView: View {
Label(developerToolsButtonHelp, systemImage: devToolsIconOption.rawValue)
}
} label: {
Image(systemName: "ellipsis")
.symbolRenderingMode(.monochrome)
.cmuxFlatSymbolColorRendering()
.cmuxSymbolRasterSize(devToolsButtonIconSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: "ellipsis",
pointSize: devToolsButtonIconSize,
weight: .medium
)
.foregroundStyle(devToolsColorOption.color)
.frame(width: addressBarButtonSize, height: addressBarButtonSize, alignment: .center)
}
Expand All @@ -1603,10 +1612,11 @@ struct BrowserPanelView: View {
Button(action: {
isBrowserThemeMenuPresented.toggle()
}) {
Image(systemName: browserThemeMode.iconName)
.symbolRenderingMode(.monochrome)
.cmuxFlatSymbolColorRendering()
.cmuxSymbolRasterSize(devToolsButtonIconSize, weight: .medium)
CmuxSystemSymbolImage(
systemName: browserThemeMode.iconName,
pointSize: devToolsButtonIconSize,
weight: .medium
)
.foregroundStyle(browserThemeModeIconColor)
.frame(width: addressBarButtonSize, height: addressBarButtonSize, alignment: .center)
}
Expand All @@ -1632,8 +1642,11 @@ struct BrowserPanelView: View {
isBrowserImportHintPopoverPresented.toggle()
}) {
HStack(spacing: 4) {
Image(systemName: "square.and.arrow.down.on.square")
.cmuxSymbolRasterSize(10, weight: .medium)
CmuxSystemSymbolImage(
systemName: "square.and.arrow.down.on.square",
pointSize: 10,
weight: .medium
)
Text(String(localized: "browser.import.hint.toolbar", defaultValue: "Import"))
.cmuxFont(size: 11, weight: .medium)
.lineLimit(1)
Expand Down Expand Up @@ -1662,8 +1675,11 @@ struct BrowserPanelView: View {
applyBrowserProfileSelection(profile.id)
} label: {
HStack(spacing: 8) {
Image(systemName: profile.id == panel.profileID ? "checkmark" : "circle")
.cmuxSymbolRasterSize(10, weight: .semibold)
CmuxSystemSymbolImage(
systemName: profile.id == panel.profileID ? "checkmark" : "circle",
pointSize: 10,
weight: .semibold
)
.opacity(profile.id == panel.profileID ? 1.0 : 0.0)
.frame(width: 12, alignment: .center)
Text(profile.displayName)
Expand Down Expand Up @@ -1725,8 +1741,11 @@ struct BrowserPanelView: View {
isBrowserThemeMenuPresented = false
} label: {
HStack(spacing: 8) {
Image(systemName: mode == browserThemeMode ? "checkmark" : "circle")
.cmuxSymbolRasterSize(10, weight: .semibold)
CmuxSystemSymbolImage(
systemName: mode == browserThemeMode ? "checkmark" : "circle",
pointSize: 10,
weight: .semibold
)
.opacity(mode == browserThemeMode ? 1.0 : 0.0)
.frame(width: 12, alignment: .center)
Text(mode.displayName)
Expand Down Expand Up @@ -1758,8 +1777,10 @@ struct BrowserPanelView: View {

return HStack(spacing: 4) {
if showSecureBadge {
Image(systemName: "lock.fill")
.cmuxSymbolRasterSize(chromeMetrics.secureBadgeFontSize)
CmuxSystemSymbolImage(
systemName: "lock.fill",
pointSize: chromeMetrics.secureBadgeFontSize
)
.foregroundColor(.secondary)
}

Expand Down
5 changes: 2 additions & 3 deletions Sources/Panels/PanelContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct PanelFilePathHeader<TrailingContent: View>: View {

var body: some View {
HStack(spacing: 8) {
Image(systemName: iconSystemName)
CmuxSystemSymbolImage(systemName: iconSystemName, pointSize: 16)
.foregroundStyle(.secondary)
.frame(width: 16)
Text(filePath)
Expand Down Expand Up @@ -213,8 +213,7 @@ struct PanelHeaderIconGlyph: View {
let systemName: String

var body: some View {
Image(systemName: systemName)
.cmuxSymbolRasterSize(13)
CmuxSystemSymbolImage(systemName: systemName, pointSize: 13)
.frame(width: 20, height: 20, alignment: .center)
.contentShape(Rectangle())
}
Expand Down
114 changes: 103 additions & 11 deletions Sources/RenderableSystemSymbol.swift
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),
Expand Down Expand Up @@ -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)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 #if DEBUG test seam in production source worsened

resetRenderabilityCacheForTesting() already existed as a #if DEBUG-guarded function named with ForTesting, which matches the pattern the team's production-seam rule explicitly fails. This PR worsens the seam by extending it to also reset appKitImageCache and appKitImageCacheInsertionOrder — two new private caches whose only reset path in tests is now this compile-guarded function. The canonical fix is to widen appKitImageCache and appKitImageCacheInsertionOrder from private to internal, then let the test target reach them directly via @testable import, removing the need for the #if DEBUG function to cover the new state (or removing the function entirely).

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Per-render NSImage allocations on hot sidebar-row path

CmuxSystemSymbolImage.body calls configuredAppKitImage unconditionally on every render, allocating at least three NSImage objects per symbol (NSImage(systemSymbolName:), withSymbolConfiguration, copy()). TabItemView alone hosts up to 8 CmuxSystemSymbolImage instances (pin, audio, mic, camera, xmark, two branch icons, circle.fill), meaning ≥24 AppKit allocations per visible tab per render cycle. The previous cmuxSymbolRasterSize modifier was a zero-allocation font/frame attribute change — this replacement adds unbounded AppKit churn on every view invalidation for every visible row. Add a @MainActor static var imageCache: [CacheKey: NSImage] = [:] on RenderableSystemSymbol (keyed by (systemName, rasterSize, weight)) matching the pattern already used for renderabilityCache, so repeat renders of the same symbol at the same size reuse the existing NSImage instance.

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!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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

}
Loading
Loading