-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathSegmentController.swift
225 lines (182 loc) · 7.38 KB
/
SegmentController.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
//
// SegmentController.swift
// TGUIKit
//
// Created by Mikhail Filimonov on 21/06/2018.
// Copyright © 2018 Telegram. All rights reserved.
//
import Cocoa
public class SegmentedItem {
let title:String
let handler: ()->Void
public init(title:String, handler:@escaping()->Void) {
self.title = title
self.handler = handler
}
}
private enum SegmentItemPosition {
case left
case right
case inner
}
public struct SegmentTheme {
let backgroundColor: NSColor
let foregroundColor: NSColor
let textColor: NSColor
public init(backgroundColor: NSColor, foregroundColor: NSColor, textColor: NSColor) {
self.backgroundColor = backgroundColor
self.foregroundColor = foregroundColor
self.textColor = textColor
}
}
private final class SegmentItemView : Control {
let item: SegmentedItem
let position: SegmentItemPosition
let selected: Bool
init(item: SegmentedItem, selected: Bool, theme: SegmentTheme, position: SegmentItemPosition, select:@escaping()->Void) {
self.item = item
self.theme = theme
self.position = position
self.selected = selected
super.init(frame: NSZeroRect)
set(handler: { _ in
select()
}, for: .SingleClick)
}
var theme: SegmentTheme {
didSet {
needsDisplay = true
}
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required public init(frame frameRect: NSRect) {
fatalError("init(frame:) has not been implemented")
}
override func layout() {
super.layout()
needsDisplay = true
}
override func draw(_ layer: CALayer, in ctx: CGContext) {
super.draw(layer, in: ctx)
switch position {
case .left:
ctx.round(bounds, flags: [.bottom, .top, .left])
ctx.setFillColor(theme.foregroundColor.cgColor)
ctx.fill(bounds)
ctx.round(NSMakeRect(.borderSize , .borderSize, bounds.width - (.borderSize / 2), bounds.height - .borderSize), flags: [.bottom, .top, .left])
ctx.setFillColor(theme.backgroundColor.cgColor)
ctx.fill(bounds)
case .right:
ctx.round(bounds, flags: [.bottom, .top, .right])
ctx.setFillColor(theme.foregroundColor.cgColor)
ctx.fill(bounds)
ctx.round(NSMakeRect(.borderSize / 2, .borderSize, bounds.width - .borderSize , bounds.height - .borderSize ), flags: [.bottom, .top, .right])
ctx.setFillColor(theme.backgroundColor.cgColor)
ctx.fill(bounds)
case .inner:
ctx.setFillColor(theme.foregroundColor.cgColor)
ctx.fill(NSMakeRect(0, 0, .borderSize / 2, frame.height))
ctx.setFillColor(theme.foregroundColor.cgColor)
ctx.fill(NSMakeRect(0, 0, frame.width, .borderSize))
ctx.setFillColor(theme.foregroundColor.cgColor)
ctx.fill(NSMakeRect(frame.width - .borderSize / 2, 0, .borderSize / 2, frame.height))
ctx.setFillColor(theme.foregroundColor.cgColor)
ctx.fill(NSMakeRect(0, frame.height - .borderSize, frame.width, .borderSize))
}
if selected {
ctx.setFillColor(theme.foregroundColor.cgColor)
ctx.fill(bounds)
}
let text = TextNode.layoutText(.initialize(string: item.title, color: selected ? theme.backgroundColor : theme.foregroundColor, font: .normal(12)), selected ? theme.foregroundColor : theme.backgroundColor, 1, .end, NSMakeSize(frame.width - 10, frame.height), nil, false, .center)
let f = focus(text.0.size)
text.1.draw(f, in: ctx, backingScaleFactor: backingScaleFactor, backgroundColor: selected ? theme.foregroundColor : theme.backgroundColor)
}
}
private class SegmentedControlView: View {
public var theme: SegmentTheme = SegmentTheme(backgroundColor: presentation.colors.background, foregroundColor: presentation.colors.accent, textColor: presentation.colors.accent) {
didSet {
for subview in subviews.compactMap({ $0 as? SegmentItemView}) {
subview.theme = theme
}
}
}
func update(items: [SegmentedItem], selected: Int, select: @escaping(Int)->Void) -> Void {
self.removeAllSubviews()
for i in 0 ..< items.count {
let view = SegmentItemView(item: items[i], selected: selected == i, theme: theme, position: i == 0 ? .left : i == items.count - 1 ? .right : .inner, select: { select(i) })
addSubview(view)
}
needsLayout = true
}
override func layout() {
super.layout()
guard !subviews.isEmpty else {return}
var x: CGFloat = 0
let itemWidth = floor(frame.width / CGFloat(subviews.count))
for view in subviews {
view.frame = NSMakeRect(x, 0, itemWidth, frame.height)
x += itemWidth
}
}
}
public class SegmentController: ViewController {
private var items: [SegmentedItem] = []
private var selected: Int = 0
private var genericView: SegmentedControlView {
return self.view as! SegmentedControlView
}
public override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
bar = .init(height: 0)
}
public var theme: SegmentTheme {
get {
return genericView.theme
}
set {
genericView.theme = newValue
}
}
public override func viewDidLoad() {
super.viewDidLoad()
readyOnce()
}
private func select(_ index: Int) {
self.selected = index
items[index].handler()
genericView.update(items: items, selected: selected, select: { [weak self] index in self?.select(index)})
}
public func add(segment: SegmentedItem) -> Void {
items.append(segment)
genericView.update(items: items, selected: selected, select: { [weak self] index in self?.select(index)})
}
public func segment(at index:Int) -> SegmentedItem {
return items[index]
}
public func replace(segment: SegmentedItem, at index:Int) -> Void {
items[index] = segment
genericView.update(items: items, selected: selected, select: { [weak self] index in self?.select(index)})
}
public func insert(segment: SegmentedItem, at index: Int) -> Void {
items.insert(segment, at: index)
genericView.update(items: items, selected: selected, select: { [weak self] index in self?.select(index)})
}
public func remove(at index: Int) -> Void {
items.remove(at: index)
genericView.update(items: items, selected: selected, select: { [weak self] index in self?.select(index)})
}
public func set(selected index: Int) -> Void {
selected = index
genericView.update(items: items, selected: selected, select: { [weak self] index in self?.select(index)})
}
public func removeAll() -> Void {
selected = 0
items.removeAll()
genericView.update(items: items, selected: selected, select: { [weak self] index in self?.select(index)})
}
override public func viewClass() -> AnyClass {
return SegmentedControlView.self
}
}