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
|
/*
* Copyright (c) 2016 Jose Pereira <onaips@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Carbon
import Cocoa
import Fuse
import KeyboardShortcuts
class SearchViewController: NSViewController, NSTextFieldDelegate, NSWindowDelegate {
@IBOutlet fileprivate var searchText: InputField!
@IBOutlet fileprivate var resultsText: ResultsView!
var hotkey: DDHotKey?
var listProvider: ListProvider?
var promptValue = ""
override func viewDidLoad() {
super.viewDidLoad()
searchText.delegate = self
KeyboardShortcuts.onKeyUp(for: .activateSearch) { [self] in
resumeApp()
}
DistributedNotificationCenter.default.addObserver(
self,
selector: #selector(interfaceModeChanged),
name: .AppleInterfaceThemeChangedNotification,
object: nil
)
let stdinStr = ReadStdin.read()
if stdinStr.count > 0 {
listProvider = PipeListProvider(str: stdinStr)
} else {
listProvider = AppListProvider()
}
let options = DmenuMac.parseOrExit()
if options.prompt != nil {
promptValue = options.prompt!
}
clearFields()
resumeApp()
}
@objc func interfaceModeChanged(sender: NSNotification) {
updateColors()
}
func updateColors() {
guard let window = NSApp.windows.first else { return }
window.isOpaque = false
window.backgroundColor = NSColor.windowBackgroundColor.withAlphaComponent(0.6)
searchText.textColor = NSColor.textColor
}
@objc func resumeApp() {
NSApplication.shared.activate(ignoringOtherApps: true)
view.window?.orderFrontRegardless()
if let controller = view.window as? SearchWindow {
controller.updatePosition()
}
updateColors()
}
func controlTextDidChange(_ obj: Notification) {
if searchText.stringValue == "" {
clearFields()
return
}
// Get provider list, filter using fuzzy search, apply
var scoreDict = [Int: Double]()
let fuse = Fuse(threshold: 0.4)
let pattern = fuse.createPattern(from: searchText.stringValue)
let list = listProvider?.get() ?? []
for (idx, item) in list.enumerated() {
guard let result = fuse.search(pattern, in: item.name) else {
continue
}
scoreDict[idx] = result.score
}
let sortedScoreDict = scoreDict.sorted(by: {$0.1 < $1.1}).map({list[$0.0]})
if !sortedScoreDict.isEmpty {
self.resultsText.list = sortedScoreDict
} else {
self.resultsText.clear()
}
self.resultsText.updateWidth()
}
func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
let movingLeft: Bool =
commandSelector == #selector(moveLeft(_:)) ||
commandSelector == #selector(insertBacktab(_:))
let movingRight: Bool =
commandSelector == #selector(moveRight(_:)) ||
commandSelector == #selector(insertTab(_:))
if movingLeft {
resultsText.selectedIndex = resultsText.selectedIndex == 0 ?
resultsText.list.count - 1 : resultsText.selectedIndex - 1
resultsText.updateWidth()
return true
} else if movingRight {
resultsText.selectedIndex = (resultsText.selectedIndex + 1) % resultsText.list.count
resultsText.updateWidth()
return true
} else if commandSelector == #selector(insertNewline(_:)) {
// open current selected app
if let item = resultsText.selectedItem() {
listProvider?.doAction(item: item)
closeApp()
}
return true
} else if commandSelector == #selector(cancelOperation(_:)) {
closeApp()
return true
}
return false
}
func clearFields() {
self.searchText.stringValue = promptValue
self.resultsText.list = listProvider?.get().sorted(by: {$0.name < $1.name}) ?? []
}
func closeApp() {
clearFields()
if promptValue == "" {
NSApplication.shared.hide(nil)
}
}
}
|