Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit 74093f6

Browse files
committed
Add single-selection mode to tags dropdown (mac)
1 parent 5c36f99 commit 74093f6

File tree

4 files changed

+80
-21
lines changed

4 files changed

+80
-21
lines changed

src/ui/osx/TogglDesktop/Features/AutoComplete/GenericAutoComplete/DataSource/TagDataSource.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ final class TagDataSource: AutoCompleteViewDataSource {
3535
}
3636
}
3737

38+
enum Mode {
39+
case multiSelection
40+
case singleSelection
41+
}
42+
43+
var mode: Mode = .multiSelection {
44+
didSet {
45+
tableView.reloadData()
46+
}
47+
}
48+
3849
// MARK: Override
3950

4051
override func render(with items: [Any]) {
@@ -113,7 +124,8 @@ final class TagDataSource: AutoCompleteViewDataSource {
113124
let view = tableView.makeView(withIdentifier: Constants.CellID, owner: self) as! TagCellView
114125
let isSelected = selectedTags.contains(where: { $0.name == item.name })
115126
view.delegate = self
116-
view.render(item, isSelected: isSelected)
127+
let cellStyle: TagCellView.Style = mode == .multiSelection ? .checkbox : .label
128+
view.render(item, isSelected: isSelected, style: cellStyle)
117129
return view
118130
}
119131

src/ui/osx/TogglDesktop/Features/AutoComplete/GenericAutoComplete/Views/TagCell/TagCellView.swift

+28-13
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,30 @@ final class TagCellView: NSTableCellView {
1717

1818
static let cellHeight: CGFloat = 34.0
1919

20+
enum Style {
21+
case checkbox
22+
case label
23+
}
24+
2025
// MARK: Variables
2126

2227
weak var delegate: TagCellViewDelegate?
2328
private var tagItem: Tag?
2429
private var isSelected = false
30+
private var style: Style = .checkbox
31+
2532
private lazy var backgroundColor: NSColor = {
2633
if #available(OSX 10.13, *) {
2734
return NSColor(named: NSColor.Name("tag-selection-background-color"))!
2835
} else {
2936
return ConvertHexColor.hexCode(toNSColor: "#e5f9e8")
3037
}
3138
}()
32-
private lazy var attributeDict: [NSAttributedString.Key: Any] = {
33-
let font = checkButton.font ?? NSFont.systemFont(ofSize: 14.0)
34-
let color = NSColor.togglGreen
35-
return [NSAttributedString.Key.foregroundColor: color,
36-
NSAttributedString.Key.font: font]
37-
}()
3839

3940
// MARK: OUTLET
4041

4142
@IBOutlet weak var checkButton: NSButton!
43+
@IBOutlet weak var nameLabel: NSTextField!
4244
@IBOutlet weak var backgroundView: NSBox!
4345
@IBOutlet weak var hoverView: NSBox!
4446

@@ -58,17 +60,30 @@ final class TagCellView: NSTableCellView {
5860
hoverView.alphaValue = 0.0
5961
}
6062

61-
func render(_ tag: Tag, isSelected: Bool) {
63+
func render(_ tag: Tag, isSelected: Bool, style: Style = .checkbox) {
6264
self.tagItem = tag
6365
self.isSelected = isSelected
64-
checkButton.state = isSelected ? .on : .off
65-
backgroundView.fillColor = isSelected ? backgroundColor : .clear
66-
if isSelected {
67-
checkButton.attributedTitle = NSAttributedString(string: tag.name, attributes: attributeDict)
68-
} else {
69-
checkButton.title = tag.name
66+
67+
switch style {
68+
case .checkbox:
69+
nameLabel.isHidden = true
70+
checkButton.isHidden = false
71+
case .label:
72+
checkButton.isHidden = true
73+
nameLabel.isHidden = false
7074
}
75+
76+
backgroundView.fillColor = isSelected ? backgroundColor : .clear
77+
checkButton.state = isSelected ? .on : .off
78+
79+
checkButton.title = tag.name
80+
nameLabel.stringValue = tag.name
81+
7182
checkButton.toolTip = tag.name
83+
nameLabel.toolTip = tag.name
84+
85+
checkButton.setTextColor(isSelected ? NSColor.togglGreen : NSColor.textColor)
86+
nameLabel.textColor = isSelected ? NSColor.togglGreen : NSColor.textColor
7287
}
7388

7489
func selectCheckBox() {

src/ui/osx/TogglDesktop/Features/AutoComplete/GenericAutoComplete/Views/TagCell/TagCellView.xib

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16097" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17156" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16097"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17156"/>
66
<capability name="Named colors" minToolsVersion="9.0"/>
77
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
88
</dependencies>
99
<objects>
1010
<customObject id="-2" userLabel="File's Owner"/>
1111
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
1212
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
13-
<tableCellView identifier="TagCellView" id="kdV-QX-CmW" customClass="TagCellView" customModule="TogglDesktop" customModuleProvider="target">
13+
<tableCellView identifier="TagCellView" id="kdV-QX-CmW" customClass="TagCellView" customModule="Toggl_Track" customModuleProvider="target">
1414
<rect key="frame" x="0.0" y="0.0" width="319" height="38"/>
1515
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1616
<subviews>
@@ -30,9 +30,17 @@
3030
</view>
3131
<color key="fillColor" name="auto-complete-cell-hover"/>
3232
</box>
33+
<textField hidden="YES" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="sPh-Yd-c5J">
34+
<rect key="frame" x="14" y="11" width="95" height="16"/>
35+
<textFieldCell key="cell" lineBreakMode="truncatingTail" title="Tag name label" id="tpQ-Jz-xSI">
36+
<font key="font" metaFont="system"/>
37+
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
38+
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
39+
</textFieldCell>
40+
</textField>
3341
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="ZvV-w8-dCM">
34-
<rect key="frame" x="13" y="10" width="64" height="18"/>
35-
<buttonCell key="cell" type="check" title="Check" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="cwN-gd-eAk">
42+
<rect key="frame" x="14" y="10" width="64" height="18"/>
43+
<buttonCell key="cell" type="check" title="Check" bezelStyle="regularSquare" imagePosition="left" lineBreakMode="truncatingTail" state="on" inset="2" id="cwN-gd-eAk">
3644
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
3745
<font key="font" metaFont="menu" size="14"/>
3846
</buttonCell>
@@ -60,24 +68,29 @@
6068
<constraint firstItem="P4q-6R-6o7" firstAttribute="bottom" secondItem="Ws3-BI-x3S" secondAttribute="bottom" id="PKF-6h-dTu"/>
6169
<constraint firstAttribute="trailing" secondItem="Ws3-BI-x3S" secondAttribute="trailing" constant="5" id="Pbh-rC-b5o"/>
6270
<constraint firstAttribute="bottom" secondItem="8iR-5i-Bzq" secondAttribute="bottom" id="Tpr-G9-DAF"/>
71+
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="sPh-Yd-c5J" secondAttribute="trailing" constant="16" id="Vnj-Ay-xhe"/>
6372
<constraint firstItem="P4q-6R-6o7" firstAttribute="leading" secondItem="Ws3-BI-x3S" secondAttribute="leading" id="bvN-Gi-jcg"/>
6473
<constraint firstItem="Ws3-BI-x3S" firstAttribute="top" secondItem="kdV-QX-CmW" secondAttribute="top" constant="2" id="dKj-KO-ZfI"/>
6574
<constraint firstAttribute="bottom" secondItem="Ws3-BI-x3S" secondAttribute="bottom" constant="2" id="fu0-8h-Pbb"/>
75+
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="ZvV-w8-dCM" secondAttribute="trailing" constant="16" id="jmC-iv-2ha"/>
76+
<constraint firstItem="sPh-Yd-c5J" firstAttribute="leading" secondItem="kdV-QX-CmW" secondAttribute="leading" constant="16" id="lAA-kp-MAn"/>
6677
<constraint firstAttribute="trailing" secondItem="8iR-5i-Bzq" secondAttribute="trailing" id="pwT-4j-Zl8"/>
78+
<constraint firstItem="sPh-Yd-c5J" firstAttribute="centerY" secondItem="kdV-QX-CmW" secondAttribute="centerY" id="snq-Zb-h19"/>
6779
<constraint firstItem="Ws3-BI-x3S" firstAttribute="leading" secondItem="kdV-QX-CmW" secondAttribute="leading" constant="5" id="vbw-jh-shF"/>
68-
<constraint firstItem="ZvV-w8-dCM" firstAttribute="leading" secondItem="kdV-QX-CmW" secondAttribute="leading" constant="15" id="zEd-Vw-1OT"/>
80+
<constraint firstItem="ZvV-w8-dCM" firstAttribute="leading" secondItem="kdV-QX-CmW" secondAttribute="leading" constant="16" id="zEd-Vw-1OT"/>
6981
</constraints>
7082
<connections>
7183
<outlet property="backgroundView" destination="Ws3-BI-x3S" id="hSq-eI-9Q4"/>
7284
<outlet property="checkButton" destination="ZvV-w8-dCM" id="Rqp-WW-NUb"/>
7385
<outlet property="hoverView" destination="P4q-6R-6o7" id="FqS-op-lgT"/>
86+
<outlet property="nameLabel" destination="sPh-Yd-c5J" id="Hfq-ji-viJ"/>
7487
</connections>
7588
<point key="canvasLocation" x="244.5" y="-9"/>
7689
</tableCellView>
7790
</objects>
7891
<resources>
7992
<namedColor name="auto-complete-cell-hover">
80-
<color red="0.6940000057220459" green="0.6940000057220459" blue="0.6940000057220459" alpha="0.20000000298023224" colorSpace="custom" customColorSpace="sRGB"/>
93+
<color red="0.69019607843137254" green="0.69019607843137254" blue="0.69019607843137254" alpha="0.20000000298023224" colorSpace="custom" customColorSpace="sRGB"/>
8194
</namedColor>
8295
</resources>
8396
</document>

src/ui/osx/TogglDesktop/Features/Timer/TimerViewController/TimerViewController.swift

+19
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ class TimerViewController: NSViewController {
101101
viewModel.projectDataSource.setup(with: projectAutoCompleteView)
102102
viewModel.tagsDataSource.setup(with: tagsAutoCompleteView)
103103

104+
viewModel.tagsDataSource.mode = .multiSelection
105+
106+
// !!!: uncomment to test simplified single-selection mode for tags dropdown
107+
// viewModel.tagsDataSource.mode = .singleSelection
108+
104109
viewModel.prepareData()
105110

106111
configureHideAutoCompleteWhenLostFocus()
@@ -142,6 +147,10 @@ class TimerViewController: NSViewController {
142147
}
143148

144149
viewModel.onTagSelected = { [unowned self] isSelected in
150+
if case .singleSelection = self.viewModel.tagsDataSource.mode {
151+
self.closeTagsAutoComplete()
152+
}
153+
145154
self.tagsButton.isSelected = isSelected
146155
self.tagsButton.toolTip = isSelected ? nil : Constants.emptyTagsButtonTooltip
147156
}
@@ -248,6 +257,16 @@ class TimerViewController: NSViewController {
248257
let wasNotClosedJustNow = (Date().timeIntervalSince1970 - tagsAutocompleteResignTime) > 0.5
249258

250259
if wasNotClosedJustNow {
260+
261+
// !!!: this is here only for testing. We'll remove this once tags shortcut are implemented
262+
switch viewModel.tagsDataSource.mode {
263+
case .singleSelection:
264+
tagsAutoCompleteView.isSearchFieldHidden = true
265+
case .multiSelection:
266+
tagsAutoCompleteView.isSearchFieldHidden = false
267+
}
268+
269+
tagsAutoCompleteView.filter(with: "")
251270
presentTagsAutoComplete(from: .button(tagsButton))
252271
} else {
253272
// returning state back to normal if click is not handled

0 commit comments

Comments
 (0)