-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathNESMenuController.swift
More file actions
230 lines (184 loc) · 7.67 KB
/
NESMenuController.swift
File metadata and controls
230 lines (184 loc) · 7.67 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
import Cocoa
import ComposableArchitecture
import SwiftUI
import HostAppActivator
class NESMenuController: ObservableObject {
private static let defaultParagraphTabStopLocation: CGFloat = 180.0
private static let titleColor: NSColor = NSColor(Color.secondary)
private static let shortcutIconColor: NSColor = NSColor.tertiaryLabelColor
static let baseFontSize: CGFloat = 13
private var menu: NSMenu?
var fontSize: CGFloat {
didSet { menu = nil }
}
var fontSizeScale: Double {
didSet { menu = nil }
}
var store: StoreOf<NESSuggestionPanelFeature>
private var imageSize: NSSize {
NSSize(width: self.fontSize, height: self.fontSize)
}
private var paragraphStyle: NSMutableParagraphStyle {
let style = NSMutableParagraphStyle()
style.tabStops = [
NSTextTab(
textAlignment: .right,
location: Self.defaultParagraphTabStopLocation * fontSizeScale
)
]
return style
}
init(fontSize: CGFloat, fontSizeScale: Double, store: StoreOf<NESSuggestionPanelFeature>) {
self.fontSize = fontSize
self.fontSizeScale = fontSizeScale
self.store = store
}
func createMenu() -> NSMenu {
let menu = NESCustomMenu(title: "")
menu.menuController = self
menu.font = NSFont.systemFont(ofSize: fontSize, weight: .regular)
let titleItem = createTitleItem()
let settingsItem = createSettingItem()
let goToAcceptItem = createGoToAcceptItem()
let rejectItem = createRejectItem()
let moreInfoItem = createGetMoreInfoItem()
menu.addItem(titleItem)
menu.addItem(NSMenuItem.separator())
menu.addItem(settingsItem)
menu.addItem(NSMenuItem.separator())
menu.addItem(goToAcceptItem)
menu.addItem(rejectItem)
// menu.addItem(NSMenuItem.separator())
// menu.addItem(moreInfoItem)
self.menu = menu
return menu
}
private func createImage(_ name: String, description accessibilityDescription: String) -> NSImage? {
guard let image = NSImage(
systemSymbolName: name, accessibilityDescription: accessibilityDescription
) else { return nil }
image.size = self.imageSize
return image
}
private func createParagraphAttributedTitle(_ text: String, helpText: String) -> NSAttributedString {
let attributedTitle = NSMutableAttributedString(string: text)
attributedTitle.append(NSAttributedString(
string: "\t\(helpText)",
attributes: [
.foregroundColor: Self.shortcutIconColor,
.font: NSFont.systemFont(ofSize: fontSize - 1, weight: .regular),
.paragraphStyle: paragraphStyle
]
))
attributedTitle.addAttribute(
.paragraphStyle,
value: paragraphStyle,
range: NSRange(location: 0, length: attributedTitle.length)
)
return attributedTitle
}
private func createParagraphAttributedTitle(_ text: String, systemSymbolName: String) -> NSAttributedString {
let attributedTitle = NSMutableAttributedString(string: text)
attributedTitle.append(NSAttributedString(string: "\t"))
if let image = createImage(systemSymbolName, description: "\(systemSymbolName) key") {
let attachment = NSTextAttachment()
attachment.image = image
let attachmentString = NSMutableAttributedString(attachment: attachment)
attachmentString.addAttributes([
.foregroundColor: Self.shortcutIconColor,
.font: NSFont.systemFont(ofSize: fontSize - 1, weight: .regular),
.paragraphStyle: paragraphStyle
], range: NSRange(location: 0, length: attachmentString.length))
attributedTitle.append(attachmentString)
}
attributedTitle.addAttribute(
.paragraphStyle,
value: paragraphStyle,
range: NSRange(location: 0, length: attributedTitle.length)
)
return attributedTitle
}
@objc func handleSettingsAction() {
try? launchHostAppAdvancedSettings()
}
@objc func handleGoToAcceptAction() {
let state = store.withState { $0 }
state.nesContent?.acceptNESSuggestion()
}
@objc func handleRejectAction() {
let state = store.withState { $0 }
state.nesContent?.rejectNESSuggestion()
}
@objc func handleGetMoreInfoAction() { }
private func createTitleItem() -> NSMenuItem {
let titleItem = NSMenuItem()
titleItem.isEnabled = false
let attributedTitle = NSMutableAttributedString(string: "Copilot Next Edit Suggestion")
attributedTitle.addAttributes([
.foregroundColor: Self.titleColor,
.font: NSFont.systemFont(ofSize: fontSize - 1, weight: .medium)
], range: NSRange(location: 0, length: attributedTitle.length))
titleItem.attributedTitle = attributedTitle
return titleItem
}
private func createSettingItem() -> NSMenuItem {
let settingsItem = NSMenuItem(
title: "Settings",
action: #selector(handleSettingsAction),
keyEquivalent: ""
)
settingsItem.target = self
if let gearImage = NSImage(
systemSymbolName: "gearshape",
accessibilityDescription: "Settings"
) {
gearImage.size = self.imageSize
settingsItem.image = gearImage
}
return settingsItem
}
private func createGoToAcceptItem() -> NSMenuItem {
let goToAcceptItem = NSMenuItem(
title: "Go To / Accept",
action: #selector(handleGoToAcceptAction),
keyEquivalent: ""
)
goToAcceptItem.target = self
let imageSymbolName = "arrow.right.to.line"
if let arrowImage = createImage(imageSymbolName, description: "Go To or Accept") {
goToAcceptItem.image = arrowImage
}
let attributedTitle = createParagraphAttributedTitle("Go To / Accept", systemSymbolName: imageSymbolName)
goToAcceptItem.attributedTitle = attributedTitle
return goToAcceptItem
}
private func createRejectItem() -> NSMenuItem {
let rejectItem = NSMenuItem(
title: "Reject",
action: #selector(handleRejectAction),
keyEquivalent: ""
)
rejectItem.target = self
if let xImage = createImage("xmark", description: "Reject") {
rejectItem.image = xImage
}
let attributedTitle = createParagraphAttributedTitle("Reject", helpText: "Esc")
rejectItem.attributedTitle = attributedTitle
return rejectItem
}
private func createGetMoreInfoItem() -> NSMenuItem {
let moreInfoItem = NSMenuItem(
title: "Get More Info",
action: #selector(handleGetMoreInfoAction),
keyEquivalent: ""
)
moreInfoItem.target = self
let attributedTitle = NSMutableAttributedString(string: "Get More Info")
attributedTitle.addAttributes([
.foregroundColor: NSColor.linkColor,
.font: NSFont.systemFont(ofSize: fontSize, weight: .medium)
], range: NSRange(location: 0, length: attributedTitle.length))
moreInfoItem.attributedTitle = attributedTitle
return moreInfoItem
}
}