Skip to content

Commit d16a159

Browse files
committed
Fix theme lag
1 parent 6da8a56 commit d16a159

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

StikJIT/Utilities/AppTheme.swift

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ struct ThemedBackground: View {
130130
@Environment(\.accessibilityReduceMotion) private var reduceMotion
131131
@Environment(\.colorScheme) private var colorScheme
132132

133+
private var identity: String {
134+
style.identityKey
135+
}
136+
133137
var body: some View {
134138
Group {
135139
switch style {
@@ -166,6 +170,7 @@ struct ThemedBackground: View {
166170
.ignoresSafeArea()
167171
}
168172
}
173+
.id(identity)
169174
}
170175
}
171176

@@ -329,8 +334,15 @@ private struct ParticleFieldBackground: View {
329334
}
330335
}
331336
.task {
332-
while true {
333-
try? await Task.sleep(nanoseconds: 16_000_000)
337+
while !Task.isCancelled {
338+
do {
339+
try await Task.sleep(nanoseconds: 16_000_000)
340+
} catch is CancellationError {
341+
break
342+
} catch {
343+
break
344+
}
345+
guard !Task.isCancelled else { break }
334346
var next = particles
335347
for i in next.indices {
336348
var p = next[i]
@@ -359,4 +371,37 @@ private extension Array where Element == Color {
359371
if count == 1 { return [self[0], self[0].opacity(0.6)] }
360372
return self
361373
}
374+
375+
var identityKey: String {
376+
map { $0.identityKey }.joined(separator: ",")
377+
}
378+
}
379+
380+
private extension BackgroundStyle {
381+
var identityKey: String {
382+
switch self {
383+
case .staticGradient(let colors):
384+
return "static:\(colors.identityKey)"
385+
case .animatedGradient(let colors, let speed):
386+
return "animated:\(String(format: "%.4f", speed)):\(colors.identityKey)"
387+
case .blobs(let colors, let background):
388+
return "blobs:\(colors.identityKey)|bg:\(background.identityKey)"
389+
case .particles(let particle, let background):
390+
return "particles:\(particle.identityKey)|bg:\(background.identityKey)"
391+
case .customGradient(let colors):
392+
return "custom:\(colors.identityKey)"
393+
case .adaptiveGradient(let light, let dark):
394+
return "adaptive:l=\(light.identityKey)|d=\(dark.identityKey)"
395+
}
396+
}
397+
}
398+
399+
private extension Color {
400+
var identityKey: String {
401+
if let hex = toHex() {
402+
return hex
403+
}
404+
// Fallback to descriptive string when hex can't be produced (e.g. dynamic colors)
405+
return String(describing: self)
406+
}
362407
}

StikJIT/Views/DisplayView.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,17 @@ struct DisplayView: View {
487487
// Grid of theme previews
488488
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 12) {
489489
ForEach(AppTheme.allCases, id: \.self) { theme in
490+
let isSelected = selectedBuiltInTheme == theme && selectedCustomTheme == nil
490491
ThemePreviewCard(style: theme.backgroundStyle,
491492
title: theme.displayName,
492-
selected: selectedBuiltInTheme == theme && selectedCustomTheme == nil,
493+
selected: isSelected,
493494
action: {
494495
guard hasThemeExpansion else { return }
495496
appThemeRaw = theme.rawValue
496497
applyThemePreferences()
497498
showSavedToast()
498499
},
499-
staticPreview: false)
500+
staticPreview: !isSelected)
500501
}
501502
}
502503
}
@@ -582,15 +583,16 @@ struct DisplayView: View {
582583
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 12) {
583584
ForEach(manager.customThemes, id: \.id) { theme in
584585
let identifier = manager.customThemeIdentifier(for: theme)
586+
let isSelected = selectedCustomTheme?.id == theme.id
585587
ThemePreviewCard(style: manager.backgroundStyle(for: identifier),
586588
title: theme.name,
587-
selected: selectedCustomTheme?.id == theme.id,
589+
selected: isSelected,
588590
action: {
589591
appThemeRaw = identifier
590592
applyThemePreferences()
591593
showSavedToast()
592594
},
593-
staticPreview: false)
595+
staticPreview: !isSelected)
594596
.contextMenu {
595597
Button("Edit") { editingCustomTheme = theme }
596598
Button("Delete", role: .destructive) {
@@ -955,4 +957,3 @@ private struct CustomThemeEditorView: View {
955957
}
956958
}
957959
}
958-

0 commit comments

Comments
 (0)