Fix JSON editor not updating on appearance change on macOS
This commit is contained in:
@@ -40,8 +40,10 @@ private extension NSColor {
|
||||
}
|
||||
}
|
||||
|
||||
private func makeTheme() -> EditorTheme {
|
||||
EditorTheme(
|
||||
private func makeTheme(for colorScheme: ColorScheme) -> EditorTheme {
|
||||
var theme: EditorTheme!
|
||||
let build = {
|
||||
theme = EditorTheme(
|
||||
text: .init(color: NSColor.labelColor.forEditor),
|
||||
insertionPoint: NSColor.labelColor.forEditor,
|
||||
invisibles: .init(color: NSColor.tertiaryLabelColor.forEditor),
|
||||
@@ -60,11 +62,18 @@ private func makeTheme() -> EditorTheme {
|
||||
comments: .init(color: NSColor.secondaryLabelColor.forEditor)
|
||||
)
|
||||
}
|
||||
if let appearance = NSAppearance(named: colorScheme == .dark ? .darkAqua : .aqua) {
|
||||
appearance.performAsCurrentDrawingAppearance(build)
|
||||
} else {
|
||||
build()
|
||||
}
|
||||
return theme
|
||||
}
|
||||
|
||||
private func makeConfiguration(isEditable: Bool) -> SourceEditorConfiguration {
|
||||
private func makeConfiguration(isEditable: Bool, colorScheme: ColorScheme) -> SourceEditorConfiguration {
|
||||
SourceEditorConfiguration(
|
||||
appearance: .init(
|
||||
theme: makeTheme(),
|
||||
theme: makeTheme(for: colorScheme),
|
||||
font: .monospacedSystemFont(ofSize: 14, weight: .regular),
|
||||
lineHeightMultiple: 1.3,
|
||||
wrapLines: false
|
||||
@@ -85,6 +94,8 @@ struct CodeEditTextView: NSViewRepresentable {
|
||||
let isEditable: Bool
|
||||
let editorController: CodeEditEditorController?
|
||||
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
|
||||
init(text: Binding<String>, isEditable: Bool, editorController: CodeEditEditorController? = nil) {
|
||||
_text = text
|
||||
self.isEditable = isEditable
|
||||
@@ -95,7 +106,7 @@ struct CodeEditTextView: NSViewRepresentable {
|
||||
let controller = TextViewController(
|
||||
string: text,
|
||||
language: .json,
|
||||
configuration: makeConfiguration(isEditable: isEditable),
|
||||
configuration: makeConfiguration(isEditable: isEditable, colorScheme: colorScheme),
|
||||
cursorPositions: []
|
||||
)
|
||||
controller.loadView()
|
||||
@@ -115,6 +126,7 @@ struct CodeEditTextView: NSViewRepresentable {
|
||||
])
|
||||
|
||||
context.coordinator.controller = controller
|
||||
context.coordinator.lastColorScheme = colorScheme
|
||||
context.coordinator.setupObservation()
|
||||
editorController?.controller = controller
|
||||
Task { @MainActor in
|
||||
@@ -133,7 +145,11 @@ struct CodeEditTextView: NSViewRepresentable {
|
||||
controller.language = .json
|
||||
}
|
||||
if controller.configuration.behavior.isEditable != isEditable {
|
||||
controller.configuration = makeConfiguration(isEditable: isEditable)
|
||||
controller.configuration.behavior.isEditable = isEditable
|
||||
}
|
||||
if context.coordinator.lastColorScheme != colorScheme {
|
||||
context.coordinator.lastColorScheme = colorScheme
|
||||
controller.configuration.appearance.theme = makeTheme(for: colorScheme)
|
||||
}
|
||||
editorController?.controller = controller
|
||||
}
|
||||
@@ -144,6 +160,7 @@ struct CodeEditTextView: NSViewRepresentable {
|
||||
|
||||
class Coordinator: NSObject {
|
||||
var controller: TextViewController?
|
||||
var lastColorScheme: ColorScheme?
|
||||
@Binding var text: String
|
||||
private var observation: NSObjectProtocol?
|
||||
private weak var editorController: CodeEditEditorController?
|
||||
|
||||
Reference in New Issue
Block a user