Skip to content

Commit dbbfc35

Browse files
GetToSetigorkulman
authored andcommitted
Allow opening folder dragged to dock
1 parent 1312985 commit dbbfc35

4 files changed

Lines changed: 62 additions & 11 deletions

File tree

sources/LocalizationEditor/AppDelegate.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {
2626
return true
2727
}
2828

29+
func application(_ sender: NSApplication, openFile filename: String) -> Bool {
30+
var isDirectory: ObjCBool = false
31+
if FileManager.default.fileExists(atPath: filename, isDirectory: &isDirectory), isDirectory.boolValue == true {
32+
showEditorWindow()
33+
let windowController = (Self.editorWindow?.windowController) as! WindowController
34+
windowController.openFolder(withPath: filename)
35+
return true
36+
}
37+
return false
38+
}
39+
2940
private func showEditorWindow() {
3041
if let editorWindow = Self.editorWindow {
3142
editorWindow.makeKeyAndOrderFront(nil)

sources/LocalizationEditor/Info.plist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44
<dict>
55
<key>CFBundleDevelopmentRegion</key>
66
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleDocumentTypes</key>
8+
<array>
9+
<dict>
10+
<key>CFBundleTypeName</key>
11+
<string>Localization Folder</string>
12+
<key>CFBundleTypeRole</key>
13+
<string>Editor</string>
14+
<key>LSHandlerRank</key>
15+
<string>Default</string>
16+
<key>LSItemContentTypes</key>
17+
<array>
18+
<string>public.folder</string>
19+
</array>
20+
</dict>
21+
</array>
722
<key>CFBundleExecutable</key>
823
<string>$(EXECUTABLE_NAME)</string>
924
<key>CFBundleIconFile</key>

sources/LocalizationEditor/UI/ViewController.swift

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,8 @@ final class ViewController: NSViewController {
120120
tableView.reloadData()
121121
}
122122

123-
private func openFolder() {
124-
let openPanel = NSOpenPanel()
125-
openPanel.allowsMultipleSelection = false
126-
openPanel.canChooseDirectories = true
127-
openPanel.canCreateDirectories = true
128-
openPanel.canChooseFiles = false
129-
openPanel.begin { [unowned self] result -> Void in
130-
guard result.rawValue == NSApplication.ModalResponse.OK.rawValue, let url = openPanel.url else {
131-
return
132-
}
133-
123+
private func openFolder(forPath path: String? = nil) {
124+
let handleOpenFolder: (URL) -> Void = { url in
134125
self.progressIndicator.startAnimation(self)
135126
self.dataSource.load(folder: url) { [unowned self] languages, title, localizationFiles in
136127
self.reloadData(with: languages, title: title)
@@ -142,6 +133,22 @@ final class ViewController: NSViewController {
142133
}
143134
}
144135
}
136+
137+
if let path = path {
138+
handleOpenFolder(URL(fileURLWithPath: path))
139+
} else {
140+
let openPanel = NSOpenPanel()
141+
openPanel.allowsMultipleSelection = false
142+
openPanel.canChooseDirectories = true
143+
openPanel.canCreateDirectories = true
144+
openPanel.canChooseFiles = false
145+
openPanel.begin { result -> Void in
146+
guard result.rawValue == NSApplication.ModalResponse.OK.rawValue, let url = openPanel.url else {
147+
return
148+
}
149+
handleOpenFolder(url)
150+
}
151+
}
145152
}
146153
}
147154

@@ -253,6 +260,13 @@ extension ViewController: WindowControllerToolbarDelegate {
253260
func userDidRequestFolderOpen() {
254261
openFolder()
255262
}
263+
264+
/**
265+
Invoked when user requests opening a folder for specific path
266+
*/
267+
func userDidRequestFolderOpen(withPath path: String) {
268+
openFolder(forPath: path)
269+
}
256270
}
257271

258272
// MARK: - AddViewControllerDelegate

sources/LocalizationEditor/UI/WindowController.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ protocol WindowControllerToolbarDelegate: AnyObject {
1717
*/
1818
func userDidRequestFolderOpen()
1919

20+
/**
21+
Invoked when user requests opening a folder for a specific path
22+
*/
23+
func userDidRequestFolderOpen(withPath: String)
24+
2025
/**
2126
Invoked when user requests filter change
2227

@@ -68,6 +73,12 @@ final class WindowController: NSWindowController {
6873
setupDelegates()
6974
}
7075

76+
// MAKR: - Interfaces
77+
78+
func openFolder(withPath path: String) {
79+
delegate?.userDidRequestFolderOpen(withPath: path)
80+
}
81+
7182
// MARK: - Setup
7283

7384
private func setupDelegates() {

0 commit comments

Comments
 (0)