-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCommands.swift
67 lines (56 loc) · 2.28 KB
/
Commands.swift
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
//
// Commands.swift
// Returns (macOS)
//
// Created by James Chen on 2021/11/25.
//
import SwiftUI
struct DesktopCommands: Commands {
@AppStorage(NavigationItem.appStorageKeyLastItem) var navigationSelection: String?
var body: some Commands {
SidebarCommands()
CommandGroup(replacing: .newItem, addition: {
Button("New Portfolio") {
NotificationCenter.default.post(name: .willCreatePortfolioNotification, object: nil)
}
.keyboardShortcut("N", modifiers: .command)
Button("New Account") {
NotificationCenter.default.post(name: .willCreateAccountNotification, object: nil)
}
.disabled(!hasSelectedPortfolio)
.keyboardShortcut("N", modifiers: [.command, .shift])
Divider()
Button("New Sample Portfolio") {
NotificationCenter.default.post(name: .willCreateSamplePortfolioNotification, object: nil)
}
Divider()
Button("Import...") {
NotificationCenter.default.post(name: .willImportNotification, object: nil)
}
Button("Export...") {
NotificationCenter.default.post(name: .willExportNotification, object: nil)
}
})
CommandGroup(before: .windowArrangement) {
Button("Main Window") {
if let window = NSApp.keyWindow {
window.orderFront(nil)
} else {
NSWorkspace.shared.open(URL(string: "returnsapp:main")!)
}
}
.keyboardShortcut("0", modifiers: .command)
}
CommandGroup(replacing: .windowList, addition: {})
}
private var hasSelectedPortfolio: Bool {
NavigationItem(tag: navigationSelection ?? "").isPortfolio
}
}
extension Notification.Name {
static let willCreatePortfolioNotification = Notification.Name("willCreatePortfolio")
static let willCreateSamplePortfolioNotification = Notification.Name("willCreateSamplePortfolio")
static let willCreateAccountNotification = Notification.Name("willCreateAccount")
static let willImportNotification = Notification.Name("willImport")
static let willExportNotification = Notification.Name("willExport")
}