/
DSFQuickActionBar+ResultsView.swift
429 lines (356 loc) · 14.6 KB
/
DSFQuickActionBar+ResultsView.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// DSFQuickActionBar+ResultsView.swift
//
// Copyright © 2022 Darren Ford. All rights reserved.
//
// MIT license
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
import AppKit
import DSFAppearanceManager
extension DSFQuickActionBar {
class ResultsView: NSView {
private let scrollView = NSScrollView()
private let tableView = DSFQuickActionBar.ResultsTableView()
private let horizontalView = NSBox()
// The parent window
var quickActionBarWindow: DSFQuickActionBar.Window? {
self.window as? DSFQuickActionBar.Window
}
// The parent
var quickActionBar: DSFQuickActionBar!
// Do we show keyboard shortcuts?
var showKeyboardShortcuts = false
var currentSearchTerm = ""
// The identifiers to be displayed in the results
var identifiers: [AnyHashable] = [] {
didSet {
self.reconfigure()
}
}
// Returns the currently selected row in the list
@inlinable var selectedRow: Int {
return self.tableView.selectedRow
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.translatesAutoresizingMaskIntoConstraints = false
self.setContentHuggingPriority(.defaultLow, for: .horizontal)
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Private
// The font for displaying the keyboard shortcut
private let keyboardShortcutFont = NSFont.monospacedDigitSystemFont(ofSize: 16, weight: .medium)
// keyboard shortcut assigning
private var shortcutKeyboardMap: [AnyHashable: Int] = [:]
}
}
extension DSFQuickActionBar.ResultsView {
func configure() {
self.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
tableView.translatesAutoresizingMaskIntoConstraints = false
scrollView.contentView = DSFFlippedClipView()
horizontalView.boxType = .separator
horizontalView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(horizontalView)
self.addConstraint(NSLayoutConstraint(item: horizontalView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0))
self.addConstraint(NSLayoutConstraint(item: horizontalView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0))
self.addConstraint(NSLayoutConstraint(item: horizontalView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0))
scrollView.addConstraint(NSLayoutConstraint(item: scrollView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.quickActionBar.width))
scrollView.addConstraint(NSLayoutConstraint(item: scrollView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.quickActionBar.height))
// Scrollview
scrollView.backgroundColor = NSColor.clear
scrollView.drawsBackground = false
scrollView.autohidesScrollers = true
scrollView.documentView = tableView
scrollView.hasHorizontalScroller = false
scrollView.hasVerticalScroller = true
// Embedded table view
tableView.dataSource = self
tableView.delegate = self
tableView.parent = self
tableView.headerView = nil
tableView.backgroundColor = NSColor.clear
tableView.intercellSpacing = NSSize(width: 0, height: 5)
if #available(macOS 10.13, *) {
tableView.usesAutomaticRowHeights = true
}
else {
// For macOS 10.11 and 10.12, it doesn't support usesAutomaticRowHeights.
// User has to specify rowHeight in the parent class
tableView.rowHeight = self.quickActionBar.rowHeight
}
// Table column
let c = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("searchresult"))
tableView.addTableColumn(c)
// Callback when a row is clicked by the user
tableView.action = #selector(didClickRow)
tableView.doubleAction = #selector(didDoubleClickRow)
}
}
// MARK: - Table Data
private extension DSFQuickActionBar.ResultsView {
// Rebuild the results table
func reconfigure() {
// If there are no results, hide the results view
self.isHidden = self.identifiers.count == 0
// Build the shortcuts list
self.buildShortcuts()
// Build the first view of the table
self.tableView.reloadData()
// Select the first selectable row (if possible)
if self.identifiers.count > 0 {
_ = self.selectFirstSelectableRow()
}
}
// Map the first 10 selectable identifiers to the return + (1...9) keyboard shortcuts
func buildShortcuts() {
guard let content = contentSource else { fatalError() }
self.shortcutKeyboardMap.removeAll()
guard showKeyboardShortcuts else { return }
var count = 0
for identifier in self.identifiers {
if content.quickActionBar(self.quickActionBar, canSelectItem: identifier) {
// A selectable item!
self.shortcutKeyboardMap[identifier] = count
count += 1
if count > 9 {
break
}
}
}
}
}
extension DSFQuickActionBar.ResultsView: NSTableViewDelegate, NSTableViewDataSource {
@inlinable func reloadData() {
self.tableView.reloadData()
}
@inlinable var contentSource: DSFQuickActionBarContentSource? {
self.quickActionBar.contentSource
}
func numberOfRows(in _: NSTableView) -> Int {
self.identifiers.count
}
func tableView(_ tableView: NSTableView, viewFor _: NSTableColumn?, row: Int) -> NSView? {
let itemIdentifier = self.identifiers[row]
let content = self.contentSource?.quickActionBar(
self.quickActionBar,
viewForItem: itemIdentifier,
searchTerm: currentSearchTerm
) ?? NSView()
guard
showKeyboardShortcuts == true,
let shortcutKey = self.shortcutKeyboardMap[itemIdentifier]
else {
// We don't have keyboard shortcuts turned on, or there's no shortcut for the identifier
return content
}
content.translatesAutoresizingMaskIntoConstraints = false
content.setContentHuggingPriority(.defaultLow, for: .horizontal)
let container = NSView()
container.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(content)
container.addConstraint(NSLayoutConstraint(item: content, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leading, multiplier: 1, constant: 0))
container.addConstraint(NSLayoutConstraint(item: content, attribute: .top, relatedBy: .equal, toItem: container, attribute: .top, multiplier: 1, constant: 0))
container.addConstraint(NSLayoutConstraint(item: content, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottom, multiplier: 1, constant: 0))
let t = shortcutKey == 0 ? NSTextField.newLabel("↩︎") : NSTextField.newLabel("⌘\(shortcutKey)")
t.alignment = .right
t.translatesAutoresizingMaskIntoConstraints = false
t.font = self.keyboardShortcutFont
t.textColor = .secondaryLabelColor
t.setContentHuggingPriority(.init(999), for: .horizontal)
t.setContentHuggingPriority(.required, for: .vertical)
container.addSubview(t)
// A hack for layout on pre-11 systems. Autolayout didn't account for the appearance of scrollbars back then
var inset: CGFloat = -24
if #available(macOS 11, *) {
inset = 0
}
container.addConstraint(NSLayoutConstraint(item: t, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailing, multiplier: 1, constant: inset))
container.addConstraint(NSLayoutConstraint(item: t, attribute: .centerY, relatedBy: .equal, toItem: container, attribute: .centerY, multiplier: 1, constant: 0))
container.addConstraint(NSLayoutConstraint(item: t, attribute: .leading, relatedBy: .equal, toItem: content, attribute: .trailing, multiplier: 1, constant: 4))
return container
}
func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
if row < 0 { return false }
return self.contentSource?.quickActionBar(
self.quickActionBar,
canSelectItem: self.identifiers[row]
) ?? false
}
func tableViewSelectionDidChange(_ notification: Notification) {
assert(self.selectedRow < self.identifiers.count)
// If you click on the tableview but NOT on one of those rows the selected row will be -1. Ignore this.
guard self.selectedRow > 0 else {
return
}
self.contentSource?.quickActionBar(self.quickActionBar, didSelectItem: self.identifiers[self.selectedRow])
}
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
ResultsRowView()
}
}
// MARK: - Table Actions
extension DSFQuickActionBar.ResultsView {
@objc private func didClickRow() {
if quickActionBar.requiredClickCount == .single {
self.rowAction()
}
}
@objc private func didDoubleClickRow() {
self.rowAction()
}
func performShortcutAction(for itemIndex: Int) -> Bool {
// Shortcut key is must be between 0 (the initial row) and 1...9
guard itemIndex >= 0, itemIndex <= 9 else {
return false
}
// Find the identifier for the shortcut
guard let which = self.shortcutKeyboardMap.first(where: { $0.value == itemIndex }) else {
return false
}
// Mark that the user activated an item in the list
self.quickActionBarWindow?.userDidActivateItem = true
// Tell the delegate that the user selected an item
self.quickActionBar.contentSource?.quickActionBar(self.quickActionBar, didActivateItem: which.key)
// Close the bar
self.window?.resignMain()
return true
}
func rowAction() {
let selectedRow = self.tableView.selectedRow
if selectedRow < 0 || selectedRow >= self.identifiers.count {
return
}
// If the user clicked a row, check to see if the row can be selected BEFORE
// performing the action on the row.
if
self.tableView.clickedRow >= 0,
self.tableView(self.tableView, shouldSelectRow: self.tableView.clickedRow) == false
{
return
}
// Mark that the user has activated an item in the list
self.quickActionBarWindow?.userDidActivateItem = true
let itemIdentifier = self.identifiers[selectedRow]
self.quickActionBar.contentSource?.quickActionBar(self.quickActionBar, didActivateItem: itemIdentifier)
// Make the bar lose focus, which will trigger it to close.
self.window?.resignMain()
}
func backAction() {
self.quickActionBar.quickActionBarWindow?.pressedLeftArrowInResultsView()
}
}
// MARK: - Safe external selection
extension DSFQuickActionBar.ResultsView {
/// Selects the first selectable row in the table
///
/// * Skips any row(s) that identify themselves as 'unselectable'
/// * If there are no selectable rows in the results table, no selection is made
func selectFirstSelectableRow() -> Bool {
for index in 0 ..< self.identifiers.count {
if self.tableView(self.tableView, shouldSelectRow: index) {
self.tableView.selectRowIndexes(IndexSet(integer: index), byExtendingSelection: false)
return true
}
}
return false
}
/// Move the selection to the next selectable row
///
/// * Skips any row(s) that identify themselves as 'unselectable'
/// * If there are no following selectable rows in the table, does not change the current selection
func selectNextSelectableRow() -> Bool {
var currentSelection = self.tableView.selectedRow
while currentSelection < (self.identifiers.count - 1) {
currentSelection += 1
if self.tableView(self.tableView, shouldSelectRow: currentSelection) {
self.tableView.selectRowIndexes(IndexSet(integer: currentSelection), byExtendingSelection: false)
self.tableView.scrollRowToVisible(currentSelection)
return true
}
}
return false
}
/// Move the selection to the previous selectable row
///
/// * Skips any row(s) that identify themselves as 'unselectable'
/// * If there are no prior selectable rows in the table, does not change the current selection
func selectPreviousSelectableRow() -> Bool {
var currentSelection = self.tableView.selectedRow
while currentSelection > 0 {
currentSelection -= 1
if self.tableView(self.tableView, shouldSelectRow: currentSelection) {
self.tableView.selectRowIndexes(IndexSet(integer: currentSelection), byExtendingSelection: false)
self.tableView.scrollRowToVisible(currentSelection)
return true
}
}
return false
}
}
// MARK: - Handle key events in the results table
extension DSFQuickActionBar {
class ResultsTableView: NSTableView {
weak var parent: DSFQuickActionBar.ResultsView?
override func keyDown(with event: NSEvent) {
guard let parent = self.parent else { fatalError() }
if event.keyCode == 0x24 { // kVK_Return {
parent.rowAction()
}
else if event.keyCode == 0x7B { // kVK_LeftArrow
parent.backAction()
}
else if event.modifierFlags.contains(.command),
let c = event.characters,
let v = Int(c)
{
if parent.performShortcutAction(for: v) {
return
}
else {
super.keyDown(with: event)
}
}
else {
super.keyDown(with: event)
}
}
}
}
// MARK: - Custom row drawing
private class ResultsRowView: NSTableRowView {
override func drawSelection(in dirtyRect: NSRect) {
if selectionHighlightStyle != .none {
DSFAppearanceManager.AccentColor.setFill()
let pth = NSBezierPath(roundedRect: self.bounds.insetBy(dx: 4, dy: 2), xRadius: 4, yRadius: 4)
pth.fill()
}
}
}