.../Source/App/SearchAndPreview/SearchViewControllerTableViewDelegate.swift
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // SearchViewControllerTableViewDelegate.swift |
3 | | // CutBox |
4 | | // |
5 | | // Created by Jason Milkins on 31/3/18. |
6 | | // Copyright © 2018-2023 ocodo. All rights reserved. |
7 | | // |
8 | | |
9 | | import Cocoa |
10 | | |
11 | | extension SearchViewController: NSTableViewDataSource { |
12 | 141 | func numberOfRows(in tableView: NSTableView) -> Int { |
13 | 141 | return self.historyService.count |
14 | 141 | } |
15 | | } |
16 | | |
17 | | extension SearchViewController: NSTableViewDelegate { |
18 | 77 | func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { |
19 | 77 | return 30 |
20 | 77 | } |
21 | | |
22 | 1 | func tableViewSelectionDidChange(_ notification: Notification) { |
23 | 1 | self.updateSearchItemPreview() |
24 | 1 | } |
25 | | |
26 | 73 | func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { |
27 | 73 | let rowView = ClipItemTableRowContainerView() |
28 | 73 | rowView.searchView = self.searchView |
29 | 73 | return rowView |
30 | 73 | } |
31 | | |
32 | | func tableView(_ tableView: NSTableView, |
33 | 147 | viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { |
34 | 147 | guard let record = self.historyService.dict[safe: row], |
35 | 147 | let column = tableColumn else { |
36 | 0 | return nil |
37 | 147 | } |
38 | 147 | |
39 | 147 | let theme = CutBoxPreferencesService.shared.currentTheme |
40 | 147 | let rowView: ClipItemTableRowContainerView? = tableView |
41 | 147 | .rowView(atRow: row, makeIfNecessary: false) |
42 | 147 | as? ClipItemTableRowContainerView |
43 | 147 | |
44 | 147 | switch column.identifier.rawValue { |
45 | 147 | case "icon": |
46 | 73 | let rowImageView = tableView.getRowView() as ClipItemTableRowImageView |
47 | 73 | rowImageView.data = record |
48 | 73 | rowImageView.color = theme.clip.textColor |
49 | 73 | rowView?.imageView = rowImageView |
50 | 73 | |
51 | 73 | return rowImageView |
52 | 147 | |
53 | 147 | case "string": |
54 | 73 | let rowTextView = tableView.getRowView() as ClipItemTableRowTextView |
55 | 73 | rowTextView.data = record |
56 | 73 | rowTextView.color = theme.clip.textColor |
57 | 73 | rowView?.textView = rowTextView |
58 | 73 | |
59 | 73 | return rowTextView |
60 | 147 | |
61 | 147 | default: |
62 | 1 | return nil |
63 | 147 | } |
64 | 147 | } |
65 | | |
66 | | func tableView(_ tableView: NSTableView, |
67 | 2 | selectionIndexesForProposedSelection proposedSelectionIndexes: IndexSet) -> IndexSet { |
68 | 2 | let proposed = proposedSelectionIndexes |
69 | 2 | |
70 | 2 | guard !proposed.isEmpty else { |
71 | 0 | return proposedSelectionIndexes |
72 | 2 | } |
73 | 2 | |
74 | 2 | let selected = self.searchView.itemsList.selectedRowIndexes |
75 | 2 | let removed: IndexSet = selected.subtracting(proposed) |
76 | 2 | let theme = CutBoxPreferencesService.shared.currentTheme |
77 | 2 | let added: IndexSet = proposed.subtracting(selected) |
78 | 2 | |
79 | 2 | removed |
80 | 2 | .map { |
81 | 0 | tableView.rowView(atRow: $0, |
82 | 0 | makeIfNecessary: true) |
83 | 0 | as? ItemTableRowContainerView |
84 | 0 | } |
85 | 2 | .forEach { |
86 | 0 | $0?.imageView?.color = theme.clip.textColor |
87 | 0 | $0?.textView?.color = theme.clip.textColor |
88 | 0 | } |
89 | 2 | |
90 | 2 | added.forEach { self.orderedSelection.add($0) } |
91 | 2 | removed.forEach { self.orderedSelection.remove($0) } |
92 | 2 | |
93 | 2 | var cruft: [Int] = [] |
94 | 2 | |
95 | 2 | self.orderedSelection.all().forEach { |
96 | 2 | if !proposed.contains($0) { |
97 | 0 | cruft.append($0) |
98 | 2 | } |
99 | 2 | } |
100 | 2 | |
101 | 2 | cruft.forEach { self.orderedSelection.remove($0) } |
102 | 2 | |
103 | 2 | return proposedSelectionIndexes |
104 | 2 | } |
105 | | } |