-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathSwitchView.swift
186 lines (144 loc) · 5.87 KB
/
SwitchView.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
//
// SwitchView.swift
// TGUIKit
//
// Created by keepcoder on 12/10/2016.
// Copyright © 2016 Telegram. All rights reserved.
//
import Cocoa
import SwiftSignalKit
import AppKit
public struct SwitchViewAppearance : Equatable {
let backgroundColor: NSColor
let disabledColor: NSColor
let stateOnColor: NSColor
let stateOffColor: NSColor
let borderColor: NSColor
public init(backgroundColor: NSColor, stateOnColor: NSColor, stateOffColor: NSColor, disabledColor: NSColor, borderColor: NSColor) {
self.backgroundColor = backgroundColor
self.stateOnColor = stateOnColor
self.stateOffColor = stateOffColor
self.disabledColor = disabledColor
self.borderColor = borderColor
}
public init(theme presentation: PresentationTheme) {
self.init(backgroundColor: presentation.colors.background, stateOnColor: presentation.colors.accent, stateOffColor: presentation.colors.grayForeground, disabledColor: presentation.colors.grayTransparent, borderColor: presentation.colors.border)
}
}
public var switchViewAppearance: SwitchViewAppearance {
return SwitchViewAppearance(backgroundColor: presentation.colors.background, stateOnColor: presentation.colors.accent, stateOffColor: presentation.colors.grayForeground, disabledColor: presentation.colors.grayTransparent, borderColor: presentation.colors.border)
}
public class SwitchView: Control {
private let disposable = MetaDisposable()
public var autoswitch: Bool = true
public var presentation: SwitchViewAppearance = switchViewAppearance {
didSet {
let animates = self.animates
self.animates = false
afterChanged()
resize()
self.animates = animates
}
}
public func setIsOn(_ isOn:Bool, animated:Bool = true) {
self.animates = animated
self.isOn = isOn
}
public private(set) var isOn:Bool = false {
didSet {
if isOn != oldValue {
afterChanged()
}
}
}
public var stateChanged:(()->Void)?
private var buble:CALayer = CALayer()
private var backBuble:CALayer = CALayer()
private var backgroundLayer:CALayer = CALayer()
override convenience init() {
self.init(frame:NSMakeRect(0, 0, 30, 20))
}
public required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
layer?.disableActions()
// layer?.isOpaque = true
resize();
backgroundLayer.backgroundColor = NSColor.white.cgColor
buble.backgroundColor = NSColor.white.cgColor
backgroundLayer.disableActions()
buble.disableActions()
backBuble.disableActions()
layer?.addSublayer(backgroundLayer)
layer?.addSublayer(backBuble)
layer?.addSublayer(buble)
self.set(handler: { [weak self] control in
if let strongSelf = self {
strongSelf.disposable.set((Signal<Void, NoError>.single(Void()) |> delay(0.15, queue: Queue.mainQueue())).start(next: { [weak strongSelf] in
if let strongSelf = strongSelf, let stateChanged = strongSelf.stateChanged, strongSelf.isEnabled {
stateChanged()
}
}))
let control = control as! SwitchView
let animates = control.animates
control.animates = true
if strongSelf.autoswitch {
control.isOn = !control.isOn
}
control.animates = animates
}
}, for: .Click)
let animates = self.animates
self.animates = false
afterChanged()
self.animates = animates
}
public override func apply(state: ControlState) {
super.apply(state: state)
afterChanged()
}
func afterChanged() -> Void {
CATransaction.begin()
if animates {
buble.animateFrame(from: buble.frame, to: bubleRect, duration: 0.2, timingFunction: CAMediaTimingFunctionName.spring)
}
backgroundLayer.backgroundColor = isEnabled ? ( isOn ? presentation.stateOnColor.cgColor : presentation.stateOffColor.cgColor ) : presentation.disabledColor.cgColor
backgroundLayer.borderWidth = isOn ? 0.0 : 0.0
buble.borderWidth = isOn ? 0.0 : 0.0
buble.backgroundColor = presentation.backgroundColor.cgColor
if animates {
backgroundLayer.animateBackground()
backgroundLayer.animateBorder()
buble.animateBackground()
buble.animateBorder()
}
backgroundLayer.setNeedsDisplay()
buble.setNeedsDisplay()
self.buble.frame = bubleRect
CATransaction.commit()
needsDisplay = true
}
var bubleRect:NSRect {
let w = frame.height - (isOn ? 2.0 : 2.0)
return NSMakeRect(isOn ? frame.width - w - (isOn ? 1 : 1) : 1, isOn ? 1.0 : 1.0, w, w)
}
func resize() -> Void {
// standart 36:20
backgroundLayer.frame = NSMakeRect(0, 0, frame.width, frame.height)
backgroundLayer.cornerRadius = frame.height / 2.0
backgroundLayer.borderWidth = isOn ? 0.0 : 0.0
backgroundLayer.borderColor = presentation.borderColor.cgColor
buble.frame = bubleRect
buble.cornerRadius = bubleRect.height/2.0
buble.borderWidth = isOn ? 0.0 : 0.0
buble.borderColor = presentation.borderColor.cgColor
}
deinit {
disposable.dispose()
}
public override func setFrameSize(_ newSize: NSSize) {
super.setFrameSize(newSize)
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}