-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathProjectNavigatorMenu.swift
More file actions
233 lines (196 loc) Β· 8.64 KB
/
ProjectNavigatorMenu.swift
File metadata and controls
233 lines (196 loc) Β· 8.64 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// OutlineMenu.swift
// CodeEdit
//
// Created by Lukas Pistrol on 07.04.22.
//
import SwiftUI
import UniformTypeIdentifiers
/// A subclass of `NSMenu` implementing the contextual menu for the project navigator
final class ProjectNavigatorMenu: NSMenu {
/// The item to show the contextual menu for
var item: CEWorkspaceFile?
/// The workspace, for opening the item
var workspace: WorkspaceDocument?
/// The `ProjectNavigatorViewController` is being called from.
/// By sending it, we can access it's variables and functions.
var sender: ProjectNavigatorViewController
init(_ sender: ProjectNavigatorViewController) {
self.sender = sender
super.init(title: "Options")
}
@available(*, unavailable)
required init(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// Creates a `NSMenuItem` depending on the given arguments
/// - Parameters:
/// - title: The title of the menu item
/// - action: A `Selector` or `nil` of the action to perform.
/// - key: A `keyEquivalent` of the menu item. Defaults to an empty `String`
/// - Returns: A `NSMenuItem` which has the target `self`
private func menuItem(_ title: String, action: Selector?, key: String = "") -> NSMenuItem {
let mItem = NSMenuItem(title: title, action: action, keyEquivalent: key)
mItem.target = self
return mItem
}
/// Configures the menu based on the current selection in the outline view.
/// - Menu items get added depending on the amount of selected items.
private func setupMenu() { // swiftlint:disable:this function_body_length
guard let item else { return }
let showInFinder = menuItem("Show in Finder", action: #selector(showInFinder))
let openInTab = menuItem("Open in Tab", action: #selector(openInTab))
let openInNewWindow = menuItem("Open in New Window", action: nil)
let openExternalEditor = menuItem("Open with External Editor", action: #selector(openWithExternalEditor))
let openAs = menuItem("Open As", action: nil)
let copyPath = menuItem("Copy Path", action: #selector(copyPath))
let copyRelativePath = menuItem("Copy Relative Path", action: #selector(copyRelativePath))
let showFileInspector = menuItem("Show File Inspector", action: nil)
let newFile = menuItem("New File...", action: #selector(newFile))
let newFolder = menuItem("New Folder", action: #selector(newFolder))
let rename = menuItem("Rename", action: #selector(renameFile))
let trash = menuItem("Move to Trash", action:
item.url != workspace?.workspaceFileManager?.folderUrl
? #selector(trash) : nil)
// trash has to be the previous menu item for delete.isAlternate to work correctly
let delete = menuItem("Delete Immediately...", action:
item.url != workspace?.workspaceFileManager?.folderUrl
? #selector(delete) : nil)
delete.keyEquivalentModifierMask = .option
delete.isAlternate = true
let duplicate = menuItem("Duplicate \(item.isFolder ? "Folder" : "File")", action: #selector(duplicate))
let sortByName = menuItem("Sort by Name", action: nil)
sortByName.isEnabled = item.isFolder
let sortByType = menuItem("Sort by Type", action: nil)
sortByType.isEnabled = item.isFolder
let sourceControl = menuItem("Source Control", action: nil)
items = [
showInFinder,
NSMenuItem.separator(),
openInTab,
openInNewWindow,
openExternalEditor,
openAs,
NSMenuItem.separator(),
copyPath,
copyRelativePath,
NSMenuItem.separator(),
showFileInspector,
NSMenuItem.separator(),
newFile,
newFolder
]
if canCreateFolderFromSelection() {
items.append(menuItem("New Folder from Selection", action: #selector(newFolderFromSelection)))
}
items.append(NSMenuItem.separator())
if selectedItems().count == 1 {
items.append(rename)
}
items.append(
contentsOf: [
trash,
delete,
duplicate,
NSMenuItem.separator(),
sortByName,
sortByType,
NSMenuItem.separator(),
sourceControl,
]
)
setSubmenu(openAsMenu(item: item), for: openAs)
setSubmenu(sourceControlMenu(item: item), for: sourceControl)
}
/// Submenu for **Open As** menu item.
private func openAsMenu(item: CEWorkspaceFile) -> NSMenu {
let openAsMenu = NSMenu(title: "Open As")
func getMenusItems() -> ([NSMenuItem], [NSMenuItem]) {
// Use UTType to distinguish between bundle file and user-browsable directory
// The isDirectory property is not accurate on this.
guard let type = item.contentType else { return ([.none()], []) }
if type.conforms(to: .folder) {
return ([.none()], [])
}
var primaryItems = [NSMenuItem]()
if type.conforms(to: .sourceCode) {
primaryItems.append(.sourceCode())
}
if type.conforms(to: .propertyList) {
primaryItems.append(.propertyList())
}
if type.conforms(to: UTType(filenameExtension: "xcassets")!) {
primaryItems.append(NSMenuItem(title: "Asset Catalog Document", action: nil, keyEquivalent: ""))
}
if type.conforms(to: UTType(filenameExtension: "xib")!) {
primaryItems.append(NSMenuItem(title: "Interface Builder XIB Document", action: nil, keyEquivalent: ""))
}
if type.conforms(to: UTType(filenameExtension: "xcodeproj")!) {
primaryItems.append(NSMenuItem(title: "Xcode Project", action: nil, keyEquivalent: ""))
}
var secondaryItems = [NSMenuItem]()
if type.conforms(to: .text) {
secondaryItems.append(.asciiPropertyList())
secondaryItems.append(.hex())
}
// FIXME: Update the quickLook condition
if type.conforms(to: .data) {
secondaryItems.append(.quickLook())
}
return (primaryItems, secondaryItems)
}
let (primaryItems, secondaryItems) = getMenusItems()
for item in primaryItems {
openAsMenu.addItem(item)
}
if !secondaryItems.isEmpty {
openAsMenu.addItem(.separator())
}
for item in secondaryItems {
openAsMenu.addItem(item)
}
return openAsMenu
}
/// Submenu for **Source Control** menu item.
private func sourceControlMenu(item: CEWorkspaceFile) -> NSMenu {
let sourceControlMenu = NSMenu(title: "Source Control")
sourceControlMenu.addItem(
withTitle: "Commit \"\(String(describing: item.fileName()))\"...",
action: nil,
keyEquivalent: ""
)
sourceControlMenu.addItem(.separator())
sourceControlMenu.addItem(withTitle: "Discard Changes...", action: nil, keyEquivalent: "")
sourceControlMenu.addItem(.separator())
sourceControlMenu.addItem(withTitle: "Add Selected Files", action: nil, keyEquivalent: "")
sourceControlMenu.addItem(withTitle: "Mark Selected Files as Resolved", action: nil, keyEquivalent: "")
return sourceControlMenu
}
/// Updates the menu for the selected item and hides it if no item is provided.
override func update() {
removeAllItems()
setupMenu()
}
}
extension NSMenuItem {
fileprivate static func none() -> NSMenuItem {
let item = NSMenuItem(title: "<None>", action: nil, keyEquivalent: "")
item.isEnabled = false
return item
}
fileprivate static func sourceCode() -> NSMenuItem {
NSMenuItem(title: "Source Code", action: nil, keyEquivalent: "")
}
fileprivate static func propertyList() -> NSMenuItem {
NSMenuItem(title: "Property List", action: nil, keyEquivalent: "")
}
fileprivate static func asciiPropertyList() -> NSMenuItem {
NSMenuItem(title: "ASCII Property List", action: nil, keyEquivalent: "")
}
fileprivate static func hex() -> NSMenuItem {
NSMenuItem(title: "Hex", action: nil, keyEquivalent: "")
}
fileprivate static func quickLook() -> NSMenuItem {
NSMenuItem(title: "Quick Look", action: nil, keyEquivalent: "")
}
}