Skip to content

Commit e85a8ef

Browse files
committed
Update Theme While Editing, Warnings
1 parent 225521a commit e85a8ef

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

CodeEdit/Features/Editor/Views/CodeFileView.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct CodeFileView: View {
4646

4747
@EnvironmentObject private var editorManager: EditorManager
4848

49-
@StateObject private var themeModel: ThemeModel = .shared
49+
@ObservedObject private var themeModel: ThemeModel = .shared
5050

5151
private var cancellables = Set<AnyCancellable>()
5252

@@ -77,7 +77,9 @@ struct CodeFileView: View {
7777
codeFile.undoManager = self.undoManager.manager
7878
}
7979

80-
@State private var selectedTheme = ThemeModel.shared.selectedTheme ?? ThemeModel.shared.themes.first!
80+
private var currentTheme: Theme {
81+
themeModel.selectedTheme ?? themeModel.themes.first!
82+
}
8183

8284
@State private var font: NSFont = Settings[\.textEditing].font.current
8385

@@ -107,7 +109,7 @@ struct CodeFileView: View {
107109
CodeEditSourceEditor(
108110
codeFile.content ?? NSTextStorage(),
109111
language: getLanguage(),
110-
theme: selectedTheme.editor.editorTheme,
112+
theme: currentTheme.editor.editorTheme,
111113
font: font,
112114
tabWidth: codeFile.defaultTabWidth ?? defaultTabWidth,
113115
indentOption: (codeFile.indentOption ?? indentOption).textViewOption(),
@@ -131,17 +133,9 @@ struct CodeFileView: View {
131133
EffectView(.contentBackground)
132134
}
133135
}
134-
.colorScheme(
135-
selectedTheme.appearance == .dark
136-
? .dark
137-
: .light
138-
)
136+
.colorScheme(currentTheme.appearance == .dark ? .dark : .light)
139137
// minHeight zero fixes a bug where the app would freeze if the contents of the file are empty.
140138
.frame(minHeight: .zero, maxHeight: .infinity)
141-
.onChange(of: themeModel.selectedTheme) { newValue in
142-
guard let theme = newValue else { return }
143-
self.selectedTheme = theme
144-
}
145139
.onChange(of: settingsFont) { newFontSetting in
146140
font = newFontSetting.current
147141
}
@@ -162,10 +156,12 @@ struct CodeFileView: View {
162156
}
163157

164158
private func getBracketPairHighlight() -> BracketPairHighlight? {
165-
let theme = ThemeModel.shared.selectedTheme ?? ThemeModel.shared.themes.first!
166-
let color = Settings[\.textEditing].bracketHighlight.useCustomColor
167-
? Settings[\.textEditing].bracketHighlight.color.nsColor
168-
: theme.editor.text.nsColor.withAlphaComponent(0.8)
159+
let color = if Settings[\.textEditing].bracketHighlight.useCustomColor {
160+
Settings[\.textEditing].bracketHighlight.color.nsColor
161+
} else {
162+
currentTheme.editor.text.nsColor.withAlphaComponent(0.8)
163+
}
164+
169165
switch Settings[\.textEditing].bracketHighlight.highlightType {
170166
case .disabled:
171167
return nil

CodeEdit/Features/Settings/Pages/ThemeSettings/Models/ThemeModel+CRUD.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ extension ThemeModel {
199199

200200
try self.loadThemes()
201201

202-
if var index = self.themes.firstIndex(where: { $0.fileURL == destinationFileURL }) {
202+
if let index = self.themes.firstIndex(where: { $0.fileURL == destinationFileURL }) {
203203
self.themes[index].displayName = newFileName
204204
self.themes[index].name = newFileName.lowercased().replacingOccurrences(of: " ", with: "-")
205205

CodeEdit/Features/Settings/Pages/ThemeSettings/Models/ThemeModel.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,17 @@ final class ThemeModel: ObservableObject {
126126
}
127127
}
128128

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

131134
enum ThemeSettingsAppearances: String, CaseIterable {
132135
case light = "Light Appearance"
133136
case dark = "Dark Appearance"
134137
}
135138

136-
func getThemeActive (_ theme: Theme) -> Bool {
139+
func getThemeActive(_ theme: Theme) -> Bool {
137140
if settings.matchAppearance {
138141
return selectedAppearance == .dark
139142
? selectedDarkTheme == theme
@@ -144,7 +147,10 @@ final class ThemeModel: ObservableObject {
144147
return selectedTheme == theme
145148
}
146149

147-
func activateTheme (_ theme: Theme) {
150+
/// Activates the current theme, setting ``selectedTheme`` and ``selectedLightTheme``/``selectedDarkTheme`` as
151+
/// necessary.
152+
/// - Parameter theme: The theme to activate.
153+
func activateTheme(_ theme: Theme) {
148154
if settings.matchAppearance {
149155
if selectedAppearance == .dark {
150156
selectedDarkTheme = theme

CodeEdit/Features/Settings/Pages/ThemeSettings/ThemeSettingsView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ struct ThemeSettingsView: View {
8888
set: { newValue in
8989
themeModel.themes[index] = newValue
9090
themeModel.save(newValue)
91+
if settings.selectedTheme == theme.name {
92+
themeModel.activateTheme(newValue)
93+
}
9194
}
9295
))
9396
}

0 commit comments

Comments
 (0)