forked from overtake/TelegramSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallTooltipView.swift
More file actions
93 lines (71 loc) · 2.46 KB
/
Copy pathCallTooltipView.swift
File metadata and controls
93 lines (71 loc) · 2.46 KB
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
//
// CallTooltip.swift
// Telegram
//
// Created by Mikhail Filimonov on 14/08/2020.
// Copyright © 2020 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
enum CallTooltipType : Int32 {
case cameraOff
case microOff
case batteryLow
var icon: CGImage {
switch self {
case .cameraOff:
return theme.icons.call_tooltip_camera_off
case .microOff:
return theme.icons.call_tooltip_micro_off
case .batteryLow:
return theme.icons.call_tooltip_battery_low
}
}
func text(_ title: String) -> String {
switch self {
case .cameraOff:
return strings().callToastCameraOff(title)
case .microOff:
return strings().callToastMicroOff(title)
case .batteryLow:
return strings().callToastLowBattery(title)
}
}
}
final class CallTooltipView : Control {
private let textView: TextView = TextView()
private let icon: ImageView = ImageView()
fileprivate(set) var type: CallTooltipType? = nil
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(textView)
addSubview(icon)
textView.disableBackgroundDrawing = true
textView.isSelectable = false
textView.userInteractionEnabled = false
backgroundColor = NSColor.grayText.withAlphaComponent(0.7)
// wantsLayer = true
// self.material = .light
// self.state = .active
}
func update(type: CallTooltipType, icon: CGImage, text: String, maxWidth: CGFloat) {
self.type = type
self.icon.image = icon
self.icon.sizeToFit()
let attr: NSAttributedString = .initialize(string: text, color: .white, font: .medium(.title))
let layout = TextViewLayout(attr, maximumNumberOfLines: 1)
layout.measure(width: maxWidth - 30 - icon.backingSize.width)
textView.update(layout)
setFrameSize(NSMakeSize(30 + self.icon.frame.width + self.textView.frame.width, 26))
layer?.cornerRadius = frame.height / 2
needsLayout = true
}
override func layout() {
super.layout()
icon.centerY(x: 10)
textView.centerY(x: icon.frame.maxX + 10)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}