forked from CodeEditApp/CodeEdit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorAreaView.swift
More file actions
165 lines (149 loc) · 6.13 KB
/
EditorAreaView.swift
File metadata and controls
165 lines (149 loc) · 6.13 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
//
// EditorAreaView.swift
// CodeEdit
//
// Created by Wouter Hennen on 16/02/2023.
//
import SwiftUI
import CodeEditTextView
import UniformTypeIdentifiers
struct EditorAreaView: View {
@AppSettings(\.general.showEditorJumpBar)
var showEditorJumpBar
@AppSettings(\.navigation.navigationStyle)
var navigationStyle
@AppSettings(\.general.dimEditorsWithoutFocus)
var dimEditorsWithoutFocus
@ObservedObject var editor: Editor
@FocusState.Binding var focus: Editor?
@EnvironmentObject private var editorManager: EditorManager
@State var codeFile: CodeFileDocument?
@Environment(\.window.value)
private var window: NSWindow?
init(editor: Editor, focus: FocusState<Editor?>.Binding) {
self.editor = editor
self._focus = focus
self.codeFile = editor.selectedTab?.file.fileDocument
}
var body: some View {
var shouldShowTabBar: Bool {
return navigationStyle == .openInTabs
|| editorManager.flattenedEditors.contains { editor in
(editor.temporaryTab == nil && !editor.tabs.isEmpty)
|| (editor.temporaryTab != nil && editor.tabs.count > 1)
}
}
var editorInsetAmount: Double {
let tabBarHeight = shouldShowTabBar ? (EditorTabBarView.height + 1) : 0
let jumpBarHeight = showEditorJumpBar ? (EditorJumpBarView.height + 1) : 0
return tabBarHeight + jumpBarHeight
}
VStack {
if let selected = editor.selectedTab {
if let codeFile = codeFile {
EditorAreaFileView(
codeFile: codeFile,
textViewCoordinators: [selected.rangeTranslator].compactMap({ $0 })
)
.focusedObject(editor)
.transformEnvironment(\.edgeInsets) { insets in
insets.top += editorInsetAmount
}
.opacity(dimEditorsWithoutFocus && editor != editorManager.activeEditor ? 0.5 : 1)
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
_ = handleDrop(providers: providers)
return true
}
} else {
LoadingFileView(selected.file.name)
.onAppear {
if let file = selected.file.fileDocument {
self.codeFile = file
}
}
.onReceive(selected.file.fileDocumentPublisher) { latestValue in
self.codeFile = latestValue
}
}
} else {
CEContentUnavailableView("No Editor")
.padding(.top, editorInsetAmount)
.onTapGesture {
editorManager.activeEditor = editor
}
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
_ = handleDrop(providers: providers)
return true
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea(.all)
.safeAreaInset(edge: .top, spacing: 0) {
GeometryReader { geometry in
let topSafeArea = geometry.safeAreaInsets.top
VStack(spacing: 0) {
if topSafeArea > 0 {
Rectangle()
.fill(.clear)
.frame(height: 1)
.background(.clear)
}
if shouldShowTabBar {
EditorTabBarView(hasTopInsets: topSafeArea > 0)
.id("TabBarView" + editor.id.uuidString)
.environmentObject(editor)
Divider()
}
if showEditorJumpBar {
EditorJumpBarView(
file: editor.selectedTab?.file,
shouldShowTabBar: shouldShowTabBar
) { [weak editor] newFile in
if let file = editor?.selectedTab, let index = editor?.tabs.firstIndex(of: file) {
editor?.openTab(file: newFile, at: index)
}
}
.environmentObject(editor)
.padding(.top, shouldShowTabBar ? -1 : 0)
Divider()
}
}
.environment(\.isActiveEditor, editor == editorManager.activeEditor)
.background(EffectView(.headerView))
}
}
.focused($focus, equals: editor)
// Fixing this is causing a malloc exception when a file is edited & closed. See #1886
// .onReceive(NotificationCenter.default.publisher(for: TextView.textDidChangeNotification)) { _ in
// if navigationStyle == .openInTabs {
// editor.temporaryTab = nil
// }
// }
.onChange(of: navigationStyle) { newValue in
if newValue == .openInPlace && editor.tabs.count == 1 {
editor.temporaryTab = editor.tabs[0]
}
}
.onChange(of: editor.selectedTab) { newValue in
codeFile = newValue?.file.fileDocument
}
}
private func handleDrop(providers: [NSItemProvider]) -> Bool {
for provider in providers {
provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { item, _ in
guard let data = item as? Data,
let url = URL(dataRepresentation: data, relativeTo: nil) else {
return
}
DispatchQueue.main.async {
let file = CEWorkspaceFile(url: url)
editor.openTab(file: file)
editorManager.activeEditor = editor
focus = editor
}
}
}
return true
}
}