diff --git a/src/SearchViewController.swift b/src/SearchViewController.swift index d094a46..e2f1e35 100644 --- a/src/SearchViewController.swift +++ b/src/SearchViewController.swift @@ -11,13 +11,14 @@ struct ProgramWeighted { let weight: Int } -class SearchViewController: NSViewController, NSTextFieldDelegate, - NSPopoverDelegate, NSTableViewDataSource, NSTableViewDelegate -{ +fileprivate let maxItems = 10 + +class SearchViewController: NSViewController, NSTextFieldDelegate, NSPopoverDelegate, NSTableViewDataSource, NSTableViewDelegate { private var keyboardEvents: EventMonitor? - private var programsList: [Program] = Array(repeating: Program(), count: 10) private var listIndex = 0 + private var programsList: [Program] = Array(repeating: Program(), count: maxItems) + private var programsListCells: [ProgramsTableViewCell] = [] private var programsTableViewSelection = 0 @@ -109,8 +110,7 @@ class SearchViewController: NSViewController, NSTextFieldDelegate, table.allowsColumnReordering = false table.allowsColumnResizing = false table.allowsColumnSelection = false - table.addTableColumn(NSTableColumn( - identifier: NSUserInterfaceItemIdentifier("Program"))) + table.addTableColumn(NSTableColumn(identifier: NSUserInterfaceItemIdentifier("Program"))) table.doubleAction = #selector(tableDoubleClick) @@ -168,6 +168,11 @@ class SearchViewController: NSViewController, NSTextFieldDelegate, override func viewDidLoad() { super.viewDidLoad() + // Initialize an array of reusable cells. + for _ in 0..= 10 { break outerloop } + if listIndex >= maxItems { break outerloop } + let prog = path.value[i] if prog.name.lowercased().contains(searchInput.stringValue.lowercased()) { programsList[listIndex].path = prog.path @@ -325,8 +328,7 @@ class SearchViewController: NSViewController, NSTextFieldDelegate, } else if commandSelector == #selector(NSResponder.insertTab(_:)) { return true } else if commandSelector == #selector(NSResponder.moveUp(_:)) || commandSelector == #selector(NSResponder.moveDown(_:)) { - // Ignore arrows keys up or down because we use those to - // navigate the programs list. + // Ignore arrows up and down because we use those to navigate the programs list. return true } @@ -351,17 +353,10 @@ class SearchViewController: NSViewController, NSTextFieldDelegate, return ProgramsTableRowView() } - func tableView(_ tableView: NSTableView, - viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? - { - let cell = ProgramsTableViewCell() + func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { + let cell = programsListCells[row] let program = programsList[row] - // PERF: This is very slow, even with 10 items on the list! It has - // to be the image of concern. UIKit has reusable cells, - // is that possible? Or is fetching an image is slow? - // searchInput.stringValue - let app = program.name + program.ext let rangeToHighlight = (app.lowercased() as NSString).range(of: searchInput.stringValue.lowercased()) let attributedString = NSMutableAttributedString(string: app)