Skip to content

Commit 3c672b0

Browse files
committed
Fix bugs on color schema (import, file name with space, background color).
1 parent 12bf625 commit 3c672b0

File tree

8 files changed

+80
-18
lines changed

8 files changed

+80
-18
lines changed

Application/HighlightWrapper.swift

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ class HighlightWrapper {
385385
continue
386386
}
387387
try theme.save()
388+
NotificationCenter.default.post(name: .ThemeNeedRefresh, object: theme)
388389
}
389390
}
390391

Application/SCSHWrapper.swift

+26
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ public class SCSHWrapper: NSObject {
9090
}
9191

9292
NotificationCenter.default.addObserver(self, selector: #selector(afterThemeDeleted(_:)), name: .CustomThemeRemoved, object: nil)
93+
NotificationCenter.default.addObserver(self, selector: #selector(afterThemeChanged(_:)), name: .ThemeNeedRefresh, object: nil)
9394
}
9495

9596
deinit {
9697
NotificationCenter.default.removeObserver(self, name: .CustomThemeRemoved, object: nil)
98+
NotificationCenter.default.removeObserver(self, name: .ThemeNeedRefresh, object: nil)
9799
}
98100

99101
fileprivate (set) var isSaving = false
@@ -218,6 +220,30 @@ public class SCSHWrapper: NSObject {
218220
})
219221
}
220222

223+
@objc internal func afterThemeChanged(_ notification: Notification) {
224+
guard let theme = notification.object as? SCSHTheme else { return }
225+
SCSHWrapper.service?.updateSettingsAfterThemeBGChanged(name: theme.name, background: theme.backgroundColor) { changed in
226+
227+
}
228+
229+
let name = theme.nameForSettings
230+
if self.settings?.lightThemeName == name {
231+
self.settings?.lightBackgroundColor = theme.backgroundColor
232+
}
233+
if self.settings?.darkThemeName == name {
234+
self.settings?.darkBackgroundColor = theme.backgroundColor
235+
}
236+
237+
self.settings?.utiSettings.forEach({
238+
if $0.value.lightThemeName == name {
239+
$0.value.lightBackgroundColor = theme.backgroundColor
240+
}
241+
if $0.value.darkThemeName == name {
242+
$0.value.darkBackgroundColor = theme.backgroundColor
243+
}
244+
})
245+
}
246+
221247
func applicationShouldTerminate(notifyTerminate: Bool = true) -> NSApplication.TerminateReply {
222248
guard !isSaving else {
223249
return .terminateLater

Application/SettingsView.swift

+2
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,12 @@ class SettingsView: NSView, SettingsSplitViewElement {
191191
let themeName = theme.nameForSettings
192192
if settings.lightThemeName == theme.nameForSettings {
193193
settings.lightThemeName = themeName
194+
settings.lightBackgroundColor = theme.backgroundColor
194195
initTheme(name: settings.lightThemeName, label: themeLightLabel, button: themeLightButton)
195196
self.updateAppearanceWarning()
196197
} else if settings.darkThemeName == theme.nameForSettings {
197198
settings.darkThemeName = themeName
199+
settings.darkBackgroundColor = theme.backgroundColor
198200
initTheme(name: settings.darkThemeName, label: themeDarkLabel, button: themeDarkButton)
199201
self.updateAppearanceWarning()
200202
}

Application/resources/colorize.sh

+1-7
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,12 @@ debug "Reader: ${reader}"
133133
debug "Highlight: $cmd"
134134

135135
go4it () {
136-
if [ "x${themeHL}" != "x" ]; then
137-
theme="--style=${themeHL}"
138-
else
139-
theme=""
140-
fi
141-
142136
# Split extraFlagsHL to an array of arguments using '•' as separator.
143137
#
144138
# Do not use zsh {= expansion because it split the string on all space ignoring quotes and causing error.
145139
cmdExtra=("${(@s//)extraFlagsHL}")
146140

147-
export cmdOptsHL=(${plugin} --syntax=${lang} --quiet --include-style ${=theme} --encoding=${textEncoding} ${cmdExtra} --validate-input)
141+
export cmdOptsHL=(${plugin} --syntax=${lang} --quiet --include-style --encoding=${textEncoding} ${cmdExtra} --validate-input)
148142

149143
if [ "x${useLSP}" != "x" ]; then
150144
# LSP require full path.

SyntaxHighlightRenderXPC/SCSHBaseXPCService.swift

+12-10
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,17 @@ class SCSHBaseXPCService: NSObject {
231231
hlArguments.arguments.append("--data-dir=\(dataDir)")
232232
}
233233

234-
if hlArguments.theme.hasPrefix("!") {
235-
// Custom theme.
236-
hlArguments.theme.remove(at: hlArguments.theme.startIndex)
237-
if let theme_url = self.getCustomThemesUrl(createIfMissing: false)?.appendingPathComponent(hlArguments.theme).appendingPathExtension("theme") {
238-
hlArguments.arguments.append("--config-file=\(theme_url.path)")
234+
if custom_settings.themeLua.isEmpty {
235+
if hlArguments.theme.hasPrefix("!") {
236+
// Custom theme.
237+
hlArguments.theme.remove(at: hlArguments.theme.startIndex)
238+
if let theme_url = self.getCustomThemesUrl(createIfMissing: false)?.appendingPathComponent(hlArguments.theme).appendingPathExtension("theme") {
239+
hlArguments.arguments.append("--style=\(theme_url.path)")
240+
}
241+
} else if let dataDir = self.dataDir {
242+
hlArguments.arguments.append("--style=\(dataDir)/themes/\(hlArguments.theme).theme")
243+
} else {
244+
hlArguments.arguments.append("--style=\(hlArguments.theme)")
239245
}
240246
}
241247

@@ -374,21 +380,17 @@ class SCSHBaseXPCService: NSObject {
374380
temporaryThemeFile = URL(fileURLWithPath: directory).appendingPathComponent(NSUUID().uuidString).appendingPathExtension("theme")
375381
do {
376382
try inline_theme.write(to: temporaryThemeFile!, atomically: true, encoding: .utf8)
377-
colorize.arguments.append("--config-file=\(temporaryThemeFile!.path)")
383+
colorize.arguments.append("--style=\(temporaryThemeFile!.path)")
378384
} catch {
379385
temporaryThemeFile = nil
380386
}
381387
}
382388

383-
384389
colorize.env.merge([
385390
// Highlight path
386391
"pathHL": colorize.highlight,
387392
"pathDos2unix": self.bundle.path(forResource: "dos2unix", ofType: nil) ?? "dos2unix",
388393

389-
// Theme to use.
390-
"themeHL": colorize.theme,
391-
392394
"extraFlagsHL": colorize.arguments.joined(separator: ""),
393395
]) { (_, new) in new }
394396

XPCService/SCSHTheme.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ public class SCSHTheme: NSObject, Sequence {
12361236
do {
12371237
try s.write(to: url, atomically: true, encoding: .utf8)
12381238
name = url.deletingPathExtension().lastPathComponent
1239+
self.path = url.path
12391240
isDirty = false
12401241
} catch {
12411242
throw error

XPCService/SCSHXPCService.swift

+36
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,42 @@ class SCSHXPCService: SCSHBaseXPCService, SCSHXPCServiceProtocol {
359359
reply(u)
360360
}
361361

362+
/// Delete a custom theme.
363+
/// Any references of deleted theme in the settings are replaced with a default theme.
364+
/// - parameters:
365+
/// - name: Name of the theme. Is equal to the file name.
366+
/// - reply:
367+
/// - changed: True if the settings are changed.
368+
func updateSettingsAfterThemeBGChanged(name: String, background: String, withReply reply: @escaping (_ changed: Bool) -> Void) {
369+
// Search if any settings use the deleted theme.
370+
let name = "\(name)"
371+
var changed = false
372+
if settings.lightThemeName == name {
373+
settings.lightBackgroundColor = background
374+
changed = true
375+
}
376+
if settings.darkThemeName == name {
377+
settings.darkBackgroundColor = background
378+
changed = true
379+
}
380+
for (_, settings) in self.settings.utiSettings {
381+
if settings.lightThemeName == name {
382+
settings.lightBackgroundColor = background
383+
changed = true
384+
}
385+
if settings.darkThemeName == name {
386+
settings.darkBackgroundColor = background
387+
changed = true
388+
}
389+
}
390+
if changed {
391+
// Save the changed settings.
392+
settings.synchronize(domain: type(of: self).XPCDomain, CSSFolder: type(of: self).getCustomStylesUrl(createIfMissing: true))
393+
}
394+
395+
reply(changed)
396+
}
397+
362398
/// Delete a custom theme.
363399
/// Any references of deleted theme in the settings are replaced with a default theme.
364400
/// - parameters:

XPCService/SCSHXPCServiceProtocol.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ import Foundation
9191
/// - reply:
9292
/// - changed: True if the settings are changed.
9393
func updateSettingsAfterThemeDeleted(name: String, withReply reply: @escaping (_ changed: Bool) -> Void)
94-
94+
func updateSettingsAfterThemeBGChanged(name: String, background: String, withReply reply: @escaping (_ changed: Bool) -> Void)
9595
/// Return the url of the application support folder that contains themes and custom css styles.
9696
func getApplicationSupport(reply: @escaping (URL?)->Void)
9797
}

0 commit comments

Comments
 (0)