-
Notifications
You must be signed in to change notification settings - Fork 873
/
Copy pathAuth_Signup.swift
375 lines (288 loc) · 12.7 KB
/
Auth_Signup.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// Auth_SignupController.swift
// Telegram
//
// Created by Mike Renoir on 18.02.2022.
// Copyright © 2022 Telegram. All rights reserved.
//
import Foundation
import Foundation
import TGUIKit
import AppKit
import TelegramCore
import SwiftSignalKit
final class Auth_SignupHeader : View {
private let logo:ImageButton = ImageButton(frame: Auth_Insets.logoSize.bounds)
private let header: TextView = TextView()
private let desc: TextView = TextView()
private(set) var selectedPath: String? {
didSet {
updateLocalizationAndTheme(theme: theme)
}
}
private var descAttr: NSAttributedString?
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(logo)
addSubview(header)
addSubview(desc)
header.userInteractionEnabled = false
header.isSelectable = false
logo.scaleOnClick = true
logo.autohighlight = false
desc.userInteractionEnabled = false
desc.isSelectable = false
logo.contextMenu = { [weak self] in
let menu = ContextMenu()
menu.addItem(ContextMenuItem(strings().loginNewRegisterSelect, handler: { [weak self] in
guard let window = self?._window else {
return
}
filePanel(with: photoExts, allowMultiple: false, canChooseDirectories: false, for: window, completion: { paths in
if let path = paths?.first {
self?.selectedPath = path
}
})
}, itemImage: MenuAnimation.menu_shared_media.value))
if self?.selectedPath != nil {
menu.addItem(ContextSeparatorItem())
menu.addItem(ContextMenuItem(strings().loginNewRegisterRemove, handler: { [weak self] in
self?.selectedPath = nil
}, itemImage: MenuAnimation.menu_delete.value))
}
return menu
}
updateLocalizationAndTheme(theme: theme)
}
override func updateLocalizationAndTheme(theme: PresentationTheme) {
super.updateLocalizationAndTheme(theme: theme)
let theme = theme as! TelegramPresentationTheme
if let path = selectedPath, let image = NSImage(contentsOf: URL(fileURLWithPath: path)) {
self.logo.set(image: generateImage(Auth_Insets.logoSize, contextGenerator: { size, ctx in
ctx.clear(size.bounds)
ctx.setFillColor(theme.colors.accent.cgColor)
ctx.fillEllipse(in: size.bounds)
let image = image._cgImage!
let imgSize = image.systemSize.aspectFilled(size)
ctx.round(size, size.height / 2)
ctx.draw(image, in: size.bounds.focus(imgSize))
})!, for: .Normal)
} else {
self.logo.set(image: generateImage(Auth_Insets.logoSize, contextGenerator: { size, ctx in
ctx.clear(size.bounds)
ctx.setFillColor(theme.colors.accent.cgColor)
ctx.fillEllipse(in: size.bounds)
let image = NSImage(named: "Icon_Register_AddPhoto")!.precomposed(theme.colors.underSelectedColor)
ctx.draw(image, in: size.bounds.focus(image.backingSize))
})!, for: .Normal)
}
let layout = TextViewLayout(.initialize(string: strings().loginNewRegisterHeader, color: theme.colors.text, font: Auth_Insets.headerFont))
layout.measure(width: frame.width)
self.header.update(layout)
let descAttr: NSAttributedString = .initialize(string: strings().loginNewRegisterInfo, color: theme.colors.grayText, font: Auth_Insets.infoFont)
let descLayout = TextViewLayout(descAttr, alignment: .center)
descLayout.measure(width: frame.width)
self.desc.update(descLayout)
self.layout()
}
func update(desc: NSAttributedString) {
self.descAttr = desc
updateLocalizationAndTheme(theme: theme)
}
override func layout() {
super.layout()
self.logo.centerX(y: 0)
self.header.centerX(y: self.logo.frame.maxY + 20)
self.desc.centerX(y: self.header.frame.maxY + 10)
}
var height: CGFloat {
return self.desc.frame.maxY
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private final class Auth_SignPhoneInput: View, NSTextFieldDelegate {
private let separator = View()
private let firstText:NSTextField = NSTextField()
private let lastText:NSTextField = NSTextField()
var next:(()->Void)?
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(separator)
layer?.cornerRadius = 10
firstText.font = .normal(.title)
lastText.font = .normal(.title)
firstText.isBordered = false
firstText.isBezeled = false
firstText.focusRingType = .none
firstText.drawsBackground = false
lastText.isBordered = false
lastText.isBezeled = false
lastText.focusRingType = .none
lastText.drawsBackground = false
lastText.delegate = self
lastText.nextResponder = firstText
lastText.nextKeyView = firstText
firstText.delegate = self
firstText.nextResponder = lastText
firstText.nextKeyView = lastText
addSubview(lastText)
addSubview(firstText)
updateLocalizationAndTheme(theme: theme)
}
override func updateLocalizationAndTheme(theme: PresentationTheme) {
super.updateLocalizationAndTheme(theme: theme)
self.backgroundColor = theme.colors.grayBackground
self.separator.background = theme.colors.border
lastText.backgroundColor = .clear
firstText.backgroundColor = .clear
firstText.textColor = theme.colors.text
lastText.textColor = theme.colors.text
lastText.placeholderAttributedString = .initialize(string: "Last Name", color: theme.colors.grayText, font: .normal(.header))
firstText.placeholderAttributedString = .initialize(string: "First Name", color: theme.colors.grayText, font: .normal(.header))
needsLayout = true
}
override func layout() {
super.layout()
firstText.setFrameSize(frame.width - 20, 18)
lastText.setFrameSize(frame.width - 20, 18)
self.separator.frame = focus(NSMakeSize(frame.width - 20, .borderSize))
firstText.setFrameOrigin(10, floor((frame.height / 2 - firstText.frame.height/2) / 2))
lastText.setFrameOrigin(10, frame.height - floor(frame.height / 2 - lastText.frame.height/2))
}
private var hasChanges: Bool = false
func controlTextDidChange(_ obj: Notification) {
needsLayout = true
}
func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
return false
}
func set(firstName: String, lastName: String) {
self.firstText.stringValue = firstName
self.lastText.stringValue = lastName
}
var readyValue: (String, String) {
return (self.firstText.stringValue, self.lastText.stringValue)
}
func shakeFirst() {
self.firstText.shake()
}
var firstResponder: NSResponder? {
if window?.firstResponder != firstText.textView || window?.firstResponder != lastText.textView {
if self.firstText.stringValue.isEmpty {
return self.firstText
}
return self.lastText
}
return window?.firstResponder
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
final class Auth_SignupView : View {
private let header:Auth_SignupHeader
private let container: View = View()
private let input = Auth_SignPhoneInput(frame: .zero)
private let nextView = Auth_NextView()
private let error: LoginErrorStateView = LoginErrorStateView()
private let footer = TextView()
private var takeToken:(()->Void)?
private var takeNext:((String, String, String?)->Void)?
private var takeTerms:(()->Void)?
required init(frame frameRect: NSRect) {
header = Auth_SignupHeader(frame: frameRect.size.bounds)
super.init(frame: frameRect)
container.addSubview(header)
container.addSubview(input)
container.addSubview(nextView)
container.addSubview(error)
// container.addSubview(footer)
nextView.scaleOnClick = true
nextView.set(handler: { [weak self] _ in
self?.invoke()
}, for: .Click)
addSubview(container)
updateLocalizationAndTheme(theme: theme)
}
private func invoke() {
if !self.input.readyValue.0.isEmpty {
self.takeNext?(self.input.readyValue.0, self.input.readyValue.1, self.header.selectedPath)
} else {
self.input.shakeFirst()
}
}
override func updateLocalizationAndTheme(theme: PresentationTheme) {
super.updateLocalizationAndTheme(theme: theme)
let theme = theme as! TelegramPresentationTheme
self.header.setFrameSize(NSMakeSize(frame.width, self.header.height))
let attr = parseMarkdownIntoAttributedString(strings().loginNewRegisterFooter, attributes: MarkdownAttributes(body: MarkdownAttributeSet(font: Auth_Insets.infoFont, textColor: theme.colors.grayText), bold: MarkdownAttributeSet(font: Auth_Insets.infoFontBold, textColor: theme.colors.grayText), link: MarkdownAttributeSet(font: Auth_Insets.infoFont, textColor: theme.colors.link), linkAttribute: { contents in
return (NSAttributedString.Key.link.rawValue, inAppLink.callback(contents, { [weak self] _ in
self?.takeTerms?()
}))
}))
let layout = TextViewLayout(attr)
layout.measure(width: frame.width)
layout.interactions = globalLinkExecutor
footer.update(layout)
nextView.updateLocalizationAndTheme(theme: theme)
needsLayout = true
}
override func layout() {
super.layout()
self.input.setFrameSize(NSMakeSize(280, 80))
self.container.setFrameSize(NSMakeSize(frame.width, self.header.height + Auth_Insets.betweenHeader + input.frame.height + Auth_Insets.betweenNextView + Auth_Insets.nextHeight))
self.header.centerX(y: 0)
self.input.centerX(y: self.header.frame.maxY + Auth_Insets.betweenHeader)
self.error.centerX(y: input.frame.maxY + Auth_Insets.betweenError)
self.nextView.centerX(y: self.input.frame.maxY + Auth_Insets.betweenNextView)
self.footer.centerX(y: self.nextView.frame.maxY + Auth_Insets.betweenHeader)
self.container.center()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(_ locked: Bool, error: SignUpError?, takeNext: @escaping(String, String, String?)->Void, takeTerms:@escaping()->Void) {
nextView.updateLocked(locked, string: strings().loginNewRegisterNext)
self.takeNext = takeNext
self.takeTerms = takeTerms
if let error = error {
let text: String
switch error {
case .limitExceeded:
text = strings().loginFloodWait
case .codeExpired:
text = strings().phoneCodeExpired
case .invalidFirstName:
text = strings().loginInvalidFirstNameError
case .invalidLastName:
text = strings().loginInvalidLastNameError
case .generic:
text = strings().unknownError
}
self.error.state.set(.error(text))
} else {
self.error.state.set(.normal)
}
needsLayout = true
}
var firstResponder: NSResponder? {
return input.firstResponder
}
}
final class Auth_SignupController: GenericViewController<Auth_SignupView> {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
}
override func viewDidLoad() {
super.viewDidLoad()
readyOnce()
}
func update(_ locked: Bool, error: SignUpError?, takeNext: @escaping(String, String, String?)->Void, takeTerms:@escaping()->Void) {
self.genericView.update(locked, error: error, takeNext: takeNext, takeTerms: takeTerms)
}
override func firstResponder() -> NSResponder? {
return genericView.firstResponder
}
}