Skip to content

Commit d62a0c1

Browse files
committed
Add dark theme to example app
1 parent fd16af7 commit d62a0c1

File tree

7 files changed

+83
-91
lines changed

7 files changed

+83
-91
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/CodeEditSourceEditorExampleApp.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ struct CodeEditSourceEditorExampleApp: App {
1212
var body: some Scene {
1313
DocumentGroup(newDocument: CodeEditSourceEditorExampleDocument()) { file in
1414
ContentView(document: file.$document, fileURL: file.fileURL)
15-
.preferredColorScheme(.light)
1615
}
1716
}
1817
}

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Extensions/EditorTheme+Default.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import AppKit
1010
import CodeEditSourceEditor
1111

1212
extension EditorTheme {
13-
static var standard: EditorTheme {
13+
static var light: EditorTheme {
1414
EditorTheme(
1515
text: Attribute(color: NSColor(hex: "000000")),
1616
insertionPoint: NSColor(hex: "000000"),
@@ -25,9 +25,29 @@ extension EditorTheme {
2525
variables: Attribute(color: NSColor(hex: "0F68A0")),
2626
values: Attribute(color: NSColor(hex: "6C36A9")),
2727
numbers: Attribute(color: NSColor(hex: "1C00CF")),
28-
strings: Attribute(color: NSColor(hex: "C41A16"), bold: true, italic: true),
28+
strings: Attribute(color: NSColor(hex: "C41A16")),
2929
characters: Attribute(color: NSColor(hex: "1C00CF")),
30-
comments: Attribute(color: NSColor(hex: "267507"), italic: true)
30+
comments: Attribute(color: NSColor(hex: "267507"))
31+
)
32+
}
33+
static var dark: EditorTheme {
34+
EditorTheme(
35+
text: Attribute(color: NSColor(hex: "FFFFFF")),
36+
insertionPoint: NSColor(hex: "007AFF"),
37+
invisibles: Attribute(color: NSColor(hex: "53606E")),
38+
background: NSColor(hex: "292A30"),
39+
lineHighlight: NSColor(hex: "2F3239"),
40+
selection: NSColor(hex: "646F83"),
41+
keywords: Attribute(color: NSColor(hex: "FF7AB2"), bold: true),
42+
commands: Attribute(color: NSColor(hex: "78C2B3")),
43+
types: Attribute(color: NSColor(hex: "6BDFFF")),
44+
attributes: Attribute(color: NSColor(hex: "CC9768")),
45+
variables: Attribute(color: NSColor(hex: "4EB0CC")),
46+
values: Attribute(color: NSColor(hex: "B281EB")),
47+
numbers: Attribute(color: NSColor(hex: "D9C97C")),
48+
strings: Attribute(color: NSColor(hex: "FF8170")),
49+
characters: Attribute(color: NSColor(hex: "D9C97C")),
50+
comments: Attribute(color: NSColor(hex: "7F8C98"))
3151
)
3252
}
3353
}

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/ContentView.swift

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ import CodeEditLanguages
1111
import CodeEditTextView
1212

1313
struct ContentView: View {
14+
@Environment(\.colorScheme)
15+
var colorScheme
16+
1417
@Binding var document: CodeEditSourceEditorExampleDocument
1518
let fileURL: URL?
1619

1720
@State private var language: CodeLanguage = .default
18-
@State private var theme: EditorTheme = .standard
21+
@State private var theme: EditorTheme = .light
1922
@State private var font: NSFont = NSFont.monospacedSystemFont(ofSize: 12, weight: .regular)
2023
@AppStorage("wrapLines") private var wrapLines: Bool = true
2124
@State private var cursorPositions: [CursorPosition] = []
2225
@AppStorage("systemCursor") private var useSystemCursor: Bool = false
2326
@State private var isInLongParse = false
27+
@State private var treeSitterClient = TreeSitterClient()
2428

2529
init(document: Binding<CodeEditSourceEditorExampleDocument>, fileURL: URL?) {
2630
self._document = document
@@ -29,68 +33,65 @@ struct ContentView: View {
2933

3034
var body: some View {
3135
VStack {
32-
ZStack {
33-
if isInLongParse {
34-
VStack {
35-
HStack {
36-
Spacer()
37-
Text("Parsing document...")
38-
Spacer()
39-
}
40-
.padding(4)
41-
.background(Color(NSColor.windowBackgroundColor))
42-
Spacer()
43-
}
44-
.zIndex(2)
45-
.transition(.opacity)
46-
}
47-
CodeEditSourceEditor(
48-
$document.text,
49-
language: language,
50-
theme: theme,
51-
font: font,
52-
tabWidth: 4,
53-
lineHeight: 1.2,
54-
wrapLines: wrapLines,
55-
cursorPositions: $cursorPositions,
56-
useSystemCursor: useSystemCursor
57-
)
58-
.safeAreaInset(edge: .bottom, spacing: 0) {
59-
VStack(spacing: 0) {
60-
Divider()
61-
HStack {
62-
Toggle("Wrap Lines", isOn: $wrapLines)
36+
CodeEditSourceEditor(
37+
$document.text,
38+
language: language,
39+
theme: theme,
40+
font: font,
41+
tabWidth: 4,
42+
lineHeight: 1.2,
43+
wrapLines: wrapLines,
44+
cursorPositions: $cursorPositions,
45+
useThemeBackground: true,
46+
highlightProviders: [treeSitterClient],
47+
useSystemCursor: useSystemCursor
48+
)
49+
.safeAreaInset(edge: .bottom, spacing: 0) {
50+
VStack(spacing: 0) {
51+
Divider()
52+
HStack {
53+
Toggle("Wrap Lines", isOn: $wrapLines)
54+
.toggleStyle(.button)
55+
.buttonStyle(.accessoryBar)
56+
if #available(macOS 14, *) {
57+
Toggle("Use System Cursor", isOn: $useSystemCursor)
58+
.toggleStyle(.button)
59+
.buttonStyle(.accessoryBar)
60+
} else {
61+
Toggle("Use System Cursor", isOn: $useSystemCursor)
62+
.disabled(true)
63+
.help("macOS 14 required")
6364
.toggleStyle(.button)
6465
.buttonStyle(.accessoryBar)
65-
if #available(macOS 14, *) {
66-
Toggle("Use System Cursor", isOn: $useSystemCursor)
67-
.toggleStyle(.button)
68-
.buttonStyle(.accessoryBar)
69-
} else {
70-
Toggle("Use System Cursor", isOn: $useSystemCursor)
71-
.disabled(true)
72-
.help("macOS 14 required")
73-
.toggleStyle(.button)
74-
.buttonStyle(.accessoryBar)
66+
}
67+
68+
Spacer()
69+
if isInLongParse {
70+
HStack(spacing: 5) {
71+
ProgressView()
72+
.controlSize(.small)
73+
Text("Parsing Document")
7574
}
76-
Spacer()
75+
} else {
7776
Text(getLabel(cursorPositions))
78-
Divider()
79-
.frame(height: 12)
80-
LanguagePicker(language: $language)
81-
.buttonStyle(.borderless)
8277
}
83-
.padding(.horizontal, 8)
84-
.frame(height: 28)
78+
Divider()
79+
.frame(height: 12)
80+
LanguagePicker(language: $language)
81+
.buttonStyle(.borderless)
8582
}
86-
.background(.bar)
87-
.zIndex(2)
83+
.padding(.horizontal, 8)
84+
.frame(height: 28)
8885
}
86+
.background(.bar)
87+
.zIndex(2)
8988
}
9089
.onAppear {
9190
self.language = detectLanguage(fileURL: fileURL) ?? .default
91+
self.theme = colorScheme == .dark ? .dark : .light
9292
}
9393
}
94+
.frame(maxWidth: .infinity, maxHeight: .infinity)
9495
.onReceive(NotificationCenter.default.publisher(for: TreeSitterClient.Constants.longParse)) { _ in
9596
withAnimation(.easeIn(duration: 0.1)) {
9697
isInLongParse = true
@@ -101,6 +102,13 @@ struct ContentView: View {
101102
isInLongParse = false
102103
}
103104
}
105+
.onChange(of: colorScheme) { _, newValue in
106+
if newValue == .dark {
107+
theme = .dark
108+
} else {
109+
theme = .light
110+
}
111+
}
104112
}
105113

106114
private func detectLanguage(fileURL: URL?) -> CodeLanguage? {

Sources/CodeEditSourceEditor/CodeEditUI/PanelStyles.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ struct PanelButtonStyle: ButtonStyle {
6868
.clipped()
6969
.contentShape(Rectangle())
7070
}
71-
}
71+
}

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -157,41 +157,6 @@ extension TextViewController {
157157
}
158158
}
159159

160-
/// Handles the tab key event.
161-
/// If the Shift key is pressed, it handles unindenting. If no modifier key is pressed, it checks if multiple lines
162-
/// are highlighted and handles indenting accordingly.
163-
///
164-
/// - Returns: The original event if it should be passed on, or `nil` to indicate handling within the method.
165-
func handleTab(event: NSEvent, modifierFalgs: UInt) -> NSEvent? {
166-
let shiftKey = NSEvent.ModifierFlags.shift.rawValue
167-
168-
if modifierFalgs == shiftKey {
169-
handleIndent(inwards: true)
170-
} else {
171-
// Only allow tab to work if multiple lines are selected
172-
guard multipleLinesHighlighted() else { return event }
173-
handleIndent()
174-
}
175-
return nil
176-
}
177-
func handleCommand(event: NSEvent, modifierFlags: UInt) -> NSEvent? {
178-
let commandKey = NSEvent.ModifierFlags.command.rawValue
179-
180-
switch (modifierFlags, event.charactersIgnoringModifiers) {
181-
case (commandKey, "/"):
182-
handleCommandSlash()
183-
return nil
184-
case (commandKey, "["):
185-
handleIndent(inwards: true)
186-
return nil
187-
case (commandKey, "]"):
188-
handleIndent()
189-
return nil
190-
case (_, _):
191-
return event
192-
}
193-
}
194-
195160
/// Handles the tab key event.
196161
/// If the Shift key is pressed, it handles unindenting. If no modifier key is pressed, it checks if multiple lines
197162
/// are highlighted and handles indenting accordingly.

Sources/CodeEditSourceEditor/Highlighting/HighlighProviding/HighlightProviderState.swift renamed to Sources/CodeEditSourceEditor/Highlighting/HighlightProviding/HighlightProviderState.swift

File renamed without changes.

Sources/CodeEditSourceEditor/Highlighting/HighlighProviding/HighlightProviding.swift renamed to Sources/CodeEditSourceEditor/Highlighting/HighlightProviding/HighlightProviding.swift

File renamed without changes.

0 commit comments

Comments
 (0)