-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathStatusBar.swift
More file actions
216 lines (197 loc) · 7.6 KB
/
StatusBar.swift
File metadata and controls
216 lines (197 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// StatusBar.swift
// CodeEditSourceEditorExample
//
// Created by Khan Winter on 4/17/25.
//
import SwiftUI
import CodeEditSourceEditor
import CodeEditLanguages
struct StatusBar: View {
let fileURL: URL?
@Environment(\.colorScheme)
var colorScheme
@Binding var document: CodeEditSourceEditorExampleDocument
@Binding var wrapLines: Bool
@Binding var useSystemCursor: Bool
@Binding var state: SourceEditorState
@Binding var isInLongParse: Bool
@Binding var language: CodeLanguage
@Binding var theme: EditorTheme
@Binding var showGutter: Bool
@Binding var showMinimap: Bool
@Binding var indentOption: IndentOption
@Binding var reformatAtColumn: Int
@Binding var showReformattingGuide: Bool
@Binding var showFoldingRibbon: Bool
@Binding var invisibles: InvisibleCharactersConfiguration
@Binding var warningCharacters: Set<UInt16>
var body: some View {
HStack {
Menu {
IndentPicker(indentOption: $indentOption, enabled: document.text.length == 0)
.buttonStyle(.borderless)
Toggle("Wrap Lines", isOn: $wrapLines)
Toggle("Show Gutter", isOn: $showGutter)
Toggle("Show Minimap", isOn: $showMinimap)
Toggle("Show Reformatting Guide", isOn: $showReformattingGuide)
Picker("Reformat column at column", selection: $reformatAtColumn) {
ForEach([40, 60, 80, 100, 120, 140, 160, 180, 200], id: \.self) { column in
Text("\(column)").tag(column)
}
}
.onChange(of: reformatAtColumn) { _, newValue in
reformatAtColumn = max(1, min(200, newValue))
}
Toggle("Show Folding Ribbon", isOn: $showFoldingRibbon)
if #available(macOS 14, *) {
Toggle("Use System Cursor", isOn: $useSystemCursor)
} else {
Toggle("Use System Cursor", isOn: $useSystemCursor)
.disabled(true)
.help("macOS 14 required")
}
Menu {
Toggle("Spaces", isOn: $invisibles.showSpaces)
Toggle("Tabs", isOn: $invisibles.showTabs)
Toggle("Line Endings", isOn: $invisibles.showLineEndings)
Divider()
Toggle(
"Warning Characters",
isOn: Binding(
get: {
!warningCharacters.isEmpty
},
set: { newValue in
// In this example app, we only add one character
// For real apps, consider providing a table where users can add UTF16
// char codes to warn about, as well as a set of good defaults.
if newValue {
warningCharacters.insert(0x200B) // zero-width space
} else {
warningCharacters.removeAll()
}
}
)
)
} label: {
Text("Invisibles")
}
} label: {}
.background {
Image(systemName: "switch.2")
.foregroundStyle(.secondary)
.font(.system(size: 13.5, weight: .regular))
}
.menuStyle(.borderlessButton)
.menuIndicator(.hidden)
.frame(maxWidth: 18, alignment: .center)
Spacer()
Group {
if isInLongParse {
HStack(spacing: 5) {
ProgressView()
.controlSize(.small)
Text("Parsing Document")
}
}
scrollPosition
Text(getLabel(state.cursorPositions ?? []))
}
.foregroundStyle(.secondary)
Divider()
.frame(height: 12)
Text(state.findText ?? "")
.frame(maxWidth: 30)
.lineLimit(1)
.truncationMode(.head)
.foregroundStyle(.secondary)
Button {
state.findPanelVisible?.toggle()
} label: {
Text(state.findPanelVisible ?? false ? "Hide" : "Show") + Text(" Find")
}
.buttonStyle(.borderless)
.foregroundStyle(.secondary)
Divider()
.frame(height: 12)
LanguagePicker(language: $language)
.buttonStyle(.borderless)
}
.font(.subheadline)
.fontWeight(.medium)
.controlSize(.small)
.padding(.horizontal, 8)
.frame(height: 28)
.background(.bar)
.overlay(alignment: .top) {
VStack {
Divider()
.overlay {
if colorScheme == .dark {
Color.black
}
}
}
}
.zIndex(2)
.onAppear {
self.language = detectLanguage(fileURL: fileURL) ?? .default
self.theme = colorScheme == .dark ? .dark : .light
}
}
var formatter: NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 0
formatter.allowsFloats = true
return formatter
}
@ViewBuilder private var scrollPosition: some View {
HStack(spacing: 0) {
Text("{")
TextField(
"",
value: Binding(get: { Double(state.scrollPosition?.x ?? 0.0) }, set: { state.scrollPosition?.x = $0 }),
formatter: formatter
)
.textFieldStyle(.plain)
.labelsHidden()
.fixedSize()
Text(",")
TextField(
"",
value: Binding(get: { Double(state.scrollPosition?.y ?? 0.0) }, set: { state.scrollPosition?.y = $0 }),
formatter: formatter
)
.textFieldStyle(.plain)
.labelsHidden()
.fixedSize()
Text("}")
}
}
private func detectLanguage(fileURL: URL?) -> CodeLanguage? {
guard let fileURL else { return nil }
return CodeLanguage.detectLanguageFrom(
url: fileURL,
prefixBuffer: document.text.string.getFirstLines(5),
suffixBuffer: document.text.string.getLastLines(5)
)
}
/// Create a label string for cursor positions.
/// - Parameter cursorPositions: The cursor positions to create the label for.
/// - Returns: A string describing the user's location in a document.
func getLabel(_ cursorPositions: [CursorPosition]?) -> String {
guard let cursorPositions else { return "No cursor" }
if cursorPositions.isEmpty {
return "No cursor"
}
// More than one selection, display the number of selections.
if cursorPositions.count > 1 {
return "\(cursorPositions.count) selected ranges"
}
// When there's a single cursor, display the line and column.
return "Line: \(cursorPositions[0].line) Col: \(cursorPositions[0].column) Range: \(cursorPositions[0].range)"
}
}