Skip to content

Commit

Permalink
Update Theme While Editing, Warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoolwinter committed Jun 26, 2024
1 parent 225521a commit e85a8ef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
28 changes: 12 additions & 16 deletions CodeEdit/Features/Editor/Views/CodeFileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct CodeFileView: View {

@EnvironmentObject private var editorManager: EditorManager

@StateObject private var themeModel: ThemeModel = .shared
@ObservedObject private var themeModel: ThemeModel = .shared

private var cancellables = Set<AnyCancellable>()

Expand Down Expand Up @@ -77,7 +77,9 @@ struct CodeFileView: View {
codeFile.undoManager = self.undoManager.manager
}

@State private var selectedTheme = ThemeModel.shared.selectedTheme ?? ThemeModel.shared.themes.first!
private var currentTheme: Theme {
themeModel.selectedTheme ?? themeModel.themes.first!
}

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

Expand Down Expand Up @@ -107,7 +109,7 @@ struct CodeFileView: View {
CodeEditSourceEditor(
codeFile.content ?? NSTextStorage(),
language: getLanguage(),
theme: selectedTheme.editor.editorTheme,
theme: currentTheme.editor.editorTheme,
font: font,
tabWidth: codeFile.defaultTabWidth ?? defaultTabWidth,
indentOption: (codeFile.indentOption ?? indentOption).textViewOption(),
Expand All @@ -131,17 +133,9 @@ struct CodeFileView: View {
EffectView(.contentBackground)
}
}
.colorScheme(
selectedTheme.appearance == .dark
? .dark
: .light
)
.colorScheme(currentTheme.appearance == .dark ? .dark : .light)
// minHeight zero fixes a bug where the app would freeze if the contents of the file are empty.
.frame(minHeight: .zero, maxHeight: .infinity)
.onChange(of: themeModel.selectedTheme) { newValue in
guard let theme = newValue else { return }
self.selectedTheme = theme
}
.onChange(of: settingsFont) { newFontSetting in
font = newFontSetting.current
}
Expand All @@ -162,10 +156,12 @@ struct CodeFileView: View {
}

private func getBracketPairHighlight() -> BracketPairHighlight? {
let theme = ThemeModel.shared.selectedTheme ?? ThemeModel.shared.themes.first!
let color = Settings[\.textEditing].bracketHighlight.useCustomColor
? Settings[\.textEditing].bracketHighlight.color.nsColor
: theme.editor.text.nsColor.withAlphaComponent(0.8)
let color = if Settings[\.textEditing].bracketHighlight.useCustomColor {
Settings[\.textEditing].bracketHighlight.color.nsColor
} else {
currentTheme.editor.text.nsColor.withAlphaComponent(0.8)
}

switch Settings[\.textEditing].bracketHighlight.highlightType {
case .disabled:
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ extension ThemeModel {

try self.loadThemes()

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,17 @@ final class ThemeModel: ObservableObject {
}
}

@Published var selectedAppearance: ThemeSettingsAppearances = .dark
/// Initialize to the app's current appearance.
@Published 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 {
func getThemeActive(_ theme: Theme) -> Bool {
if settings.matchAppearance {
return selectedAppearance == .dark
? selectedDarkTheme == theme
Expand All @@ -144,7 +147,10 @@ final class ThemeModel: ObservableObject {
return selectedTheme == theme
}

func activateTheme (_ theme: 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ struct ThemeSettingsView: View {
set: { newValue in
themeModel.themes[index] = newValue
themeModel.save(newValue)
if settings.selectedTheme == theme.name {
themeModel.activateTheme(newValue)
}
}
))
}
Expand Down

0 comments on commit e85a8ef

Please sign in to comment.