Skip to content

Commit aac7aa1

Browse files
Drag and Drop External Files to Open in Editor (#2000)
1 parent 50ba7d1 commit aac7aa1

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

CodeEdit/Features/Editor/Views/EditorAreaView.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import SwiftUI
99
import CodeEditTextView
10+
import UniformTypeIdentifiers
1011

1112
struct EditorAreaView: View {
1213
@AppSettings(\.general.showEditorJumpBar)
@@ -26,6 +27,9 @@ struct EditorAreaView: View {
2627

2728
@State var codeFile: CodeFileDocument?
2829

30+
@Environment(\.window.value)
31+
private var window: NSWindow?
32+
2933
init(editor: Editor, focus: FocusState<Editor?>.Binding) {
3034
self.editor = editor
3135
self._focus = focus
@@ -59,6 +63,10 @@ struct EditorAreaView: View {
5963
insets.top += editorInsetAmount
6064
}
6165
.opacity(dimEditorsWithoutFocus && editor != editorManager.activeEditor ? 0.5 : 1)
66+
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
67+
_ = handleDrop(providers: providers)
68+
return true
69+
}
6270
} else {
6371
LoadingFileView(selected.file.name)
6472
.onAppear {
@@ -77,6 +85,10 @@ struct EditorAreaView: View {
7785
.onTapGesture {
7886
editorManager.activeEditor = editor
7987
}
88+
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
89+
_ = handleDrop(providers: providers)
90+
return true
91+
}
8092
}
8193
}
8294
.frame(maxWidth: .infinity, maxHeight: .infinity)
@@ -122,4 +134,25 @@ struct EditorAreaView: View {
122134
codeFile = newValue?.file.fileDocument
123135
}
124136
}
137+
138+
private func handleDrop(providers: [NSItemProvider]) -> Bool {
139+
for provider in providers {
140+
provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { item, _ in
141+
guard let data = item as? Data,
142+
let url = URL(dataRepresentation: data, relativeTo: nil) else {
143+
return
144+
}
145+
146+
DispatchQueue.main.async {
147+
let file = CEWorkspaceFile(url: url)
148+
editor.openTab(file: file)
149+
editorManager.activeEditor = editor
150+
focus = editor
151+
NSApp.activate(ignoringOtherApps: true)
152+
window?.makeKeyAndOrderFront(nil)
153+
}
154+
}
155+
}
156+
return true
157+
}
125158
}

CodeEdit/WorkspaceView.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import SwiftUI
9+
import UniformTypeIdentifiers
910

1011
struct WorkspaceView: View {
1112
@Environment(\.window.value)
@@ -167,8 +168,29 @@ struct WorkspaceView: View {
167168
}
168169
.background(EffectView(.contentBackground))
169170
.background(WorkspaceSheets().environmentObject(sourceControlManager))
171+
.onDrop(of: [.fileURL], isTargeted: nil) { providers in
172+
_ = handleDrop(providers: providers)
173+
return true
174+
}
170175
.accessibilityElement(children: .contain)
171176
.accessibilityLabel("workspace area")
172177
}
173178
}
179+
180+
private func handleDrop(providers: [NSItemProvider]) -> Bool {
181+
for provider in providers {
182+
provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { item, _ in
183+
guard let data = item as? Data,
184+
let url = URL(dataRepresentation: data, relativeTo: nil) else {
185+
return
186+
}
187+
188+
DispatchQueue.main.async {
189+
let file = CEWorkspaceFile(url: url)
190+
editorManager.activeEditor.openTab(file: file)
191+
}
192+
}
193+
}
194+
return true
195+
}
174196
}

0 commit comments

Comments
 (0)