Skip to content

Commit

Permalink
Fixed theme details flicker when duplicating and then canceling.
Browse files Browse the repository at this point in the history
  • Loading branch information
austincondiff committed Oct 25, 2024
1 parent 51669a3 commit 77efa39
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,12 @@ extension ThemeModel {
self.save(self.themes[index])
}

self.previousTheme = self.selectedTheme

activateTheme(self.themes[index])

self.detailsTheme = self.themes[index]
self.detailsIsPresented = true
}
} catch {
print("Error adding theme: \(error.localizedDescription)")
Expand Down Expand Up @@ -238,6 +241,8 @@ extension ThemeModel {
iterator += 1
}

let isActive = self.getThemeActive(theme)

try filemanager.moveItem(at: oldURL, to: finalURL)

try self.loadThemes()
Expand All @@ -246,6 +251,9 @@ extension ThemeModel {
themes[index].displayName = finalName
themes[index].fileURL = finalURL
themes[index].name = finalName.lowercased().replacingOccurrences(of: " ", with: "-")
if isActive {
self.activateTheme(themes[index])
}
}

} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class ThemeModel: ObservableObject {
}
}

@Published var presentingDetails: Bool = false
@Published var detailsIsPresented: Bool = false

@Published var isAdding: Bool = false

Expand All @@ -87,10 +87,11 @@ final class ThemeModel: ObservableObject {
DispatchQueue.main.async {
Settings[\.theme].selectedTheme = self.selectedTheme?.name
}
updateAppearanceTheme()
}
}

@Published var previousTheme: Theme?

/// Only themes where ``Theme/appearance`` == ``Theme/ThemeType/dark``
var darkThemes: [Theme] {
themes.filter { $0.appearance == .dark }
Expand Down Expand Up @@ -127,48 +128,29 @@ final class ThemeModel: ObservableObject {
}

/// Initialize to the app's current appearance.
@Published var selectedAppearance: ThemeSettingsAppearances = {
var selectedAppearance: ThemeSettingsAppearances {
NSApp.effectiveAppearance.name == .darkAqua ? .dark : .light
}()
}

enum ThemeSettingsAppearances: String, CaseIterable {
case light = "Light Appearance"
case dark = "Dark Appearance"
}

func getThemeActive(_ theme: Theme) -> Bool {
if settings.matchAppearance {
return selectedAppearance == .dark
? selectedDarkTheme == theme
: selectedAppearance == .light
? selectedLightTheme == theme
: selectedTheme == theme
}
return selectedTheme == theme
}

/// Activates the current theme, setting ``selectedTheme`` and ``selectedLightTheme``/``selectedDarkTheme`` as
/// necessary.
/// - Parameter theme: The theme to activate.
func activateTheme(_ theme: Theme) {
if settings.matchAppearance {
if selectedAppearance == .dark {
selectedDarkTheme = theme
} else if selectedAppearance == .light {
selectedLightTheme = theme
}
if (selectedAppearance == .dark && colorScheme == .dark)
|| (selectedAppearance == .light && colorScheme == .light) {
selectedTheme = theme
}
} else {
selectedTheme = theme
if colorScheme == .light {
selectedLightTheme = theme
}
if colorScheme == .dark {
selectedDarkTheme = theme
}
selectedTheme = theme
if colorScheme == .light {
selectedLightTheme = theme
}
if colorScheme == .dark {
selectedDarkTheme = theme
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ struct ThemeSettingsThemeRow: View {

@ObservedObject private var themeModel: ThemeModel = .shared

@State private var presentingDetails: Bool = false

@State private var isHovering = false

var body: some View {
Expand Down Expand Up @@ -42,6 +40,7 @@ struct ThemeSettingsThemeRow: View {
Menu {
Button("Details...") {
themeModel.detailsTheme = theme
themeModel.detailsIsPresented = true
}
Button("Duplicate") {
if let fileURL = theme.fileURL {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ struct ThemeSettingsThemeDetails: View {

@StateObject private var themeModel: ThemeModel = .shared

@State private var duplicatingTheme: Theme?

var isActive: Bool {
themeModel.getThemeActive(theme)
}

init(theme: Binding<Theme>) {
_theme = theme
originalTheme = theme.wrappedValue
Expand All @@ -29,6 +35,7 @@ struct ThemeSettingsThemeDetails: View {
VStack(spacing: 0) {
Form {
Group {
Text("Theme is\(isActive ? "" : " not") active")
Section {
TextField("Name", text: $theme.displayName)
TextField("Author", text: $theme.author)
Expand Down Expand Up @@ -177,6 +184,7 @@ struct ThemeSettingsThemeDetails: View {
}
Button {
if let fileURL = theme.fileURL {
duplicatingTheme = theme
themeModel.duplicate(fileURL)
}
} label: {
Expand All @@ -188,6 +196,7 @@ struct ThemeSettingsThemeDetails: View {
if !themeModel.isAdding && theme.isBundled {
Button {
if let fileURL = theme.fileURL {
duplicatingTheme = theme
themeModel.duplicate(fileURL)
}
} label: {
Expand All @@ -197,12 +206,28 @@ struct ThemeSettingsThemeDetails: View {
} else {
Button {
if themeModel.isAdding {
themeModel.delete(theme)
if let previousTheme = themeModel.previousTheme {
themeModel.activateTheme(previousTheme)
}
if let duplicatingWithinDetails = duplicatingTheme {
let duplicateTheme = theme
themeModel.detailsTheme = duplicatingWithinDetails
themeModel.delete(duplicateTheme)
} else {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
themeModel.delete(theme)
}
}
} else {
themeModel.cancelDetails(theme)
}

dismiss()
if duplicatingTheme == nil {
dismiss()
} else {
duplicatingTheme = nil
themeModel.isAdding = false
}
} label: {
Text("Cancel")
.frame(minWidth: 56)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,37 @@ struct ThemeSettingsView: View {
}
.padding(.top, 10)
}
.sheet(item: $themeModel.detailsTheme) {
themeModel.isAdding = false
} content: { theme in
if let index = themeModel.themes.firstIndex(where: {
.sheet(isPresented: $themeModel.detailsIsPresented, onDismiss: {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
themeModel.isAdding = false
}
}, content: {
if let theme = themeModel.detailsTheme, let index = themeModel.themes.firstIndex(where: {
$0.fileURL?.absoluteString == theme.fileURL?.absoluteString
}) {
ThemeSettingsThemeDetails(theme: Binding(
get: { themeModel.themes[index] },
set: { newValue in
themeModel.themes[index] = newValue
themeModel.save(newValue)
if settings.selectedTheme == theme.name {
themeModel.activateTheme(newValue)
if themeModel.detailsIsPresented {
themeModel.themes[index] = newValue
themeModel.save(newValue)
if settings.selectedTheme == theme.name {
themeModel.activateTheme(newValue)
}
}
}
))
}
}
})
.onAppear {
updateFilteredThemes()
}
.onChange(of: themeSearchQuery) { _ in
updateFilteredThemes()
}
.onChange(of: themeModel.themes) { _ in
updateFilteredThemes()
}
.onChange(of: colorScheme) { newColorScheme in
updateFilteredThemes(overrideColorScheme: newColorScheme)
}
Expand Down

0 comments on commit 77efa39

Please sign in to comment.