-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathunread_badge.cpp
154 lines (141 loc) · 3.8 KB
/
unread_badge.cpp
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
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "ui/unread_badge.h"
#include "data/data_peer.h"
#include "dialogs/ui/dialogs_layout.h"
#include "lang/lang_keys.h"
#include "styles/style_dialogs.h"
namespace Ui {
void UnreadBadge::setText(const QString &text, bool active) {
_text = text;
_active = active;
const auto st = Dialogs::Ui::UnreadBadgeStyle();
resize(
std::max(st.font->width(text) + 2 * st.padding, st.size),
st.size);
update();
}
int UnreadBadge::textBaseline() const {
const auto st = Dialogs::Ui::UnreadBadgeStyle();
return ((st.size - st.font->height) / 2) + st.font->ascent;
}
void UnreadBadge::paintEvent(QPaintEvent *e) {
if (_text.isEmpty()) {
return;
}
Painter p(this);
Dialogs::Ui::UnreadBadgeStyle unreadSt;
unreadSt.muted = !_active;
auto unreadRight = width();
auto unreadTop = 0;
Dialogs::Ui::PaintUnreadBadge(
p,
_text,
unreadRight,
unreadTop,
unreadSt);
}
QSize ScamBadgeSize(bool fake) {
const auto phrase = fake
? tr::lng_fake_badge(tr::now)
: tr::lng_scam_badge(tr::now);
const auto phraseWidth = st::dialogsScamFont->width(phrase);
const auto width = st::dialogsScamPadding.left()
+ phraseWidth
+ st::dialogsScamPadding.right();
const auto height = st::dialogsScamPadding.top()
+ st::dialogsScamFont->height
+ st::dialogsScamPadding.bottom();
return { width, height };
}
void DrawScamFakeBadge(
Painter &p,
QRect rect,
int outerWidth,
const style::color &color,
const QString &phrase,
int phraseWidth) {
PainterHighQualityEnabler hq(p);
auto pen = color->p;
pen.setWidth(st::lineWidth);
p.setPen(pen);
p.setBrush(Qt::NoBrush);
p.drawRoundedRect(rect, st::dialogsScamRadius, st::dialogsScamRadius);
p.setFont(st::dialogsScamFont);
if (rtl()) {
p.drawTextRight(
rect.x() + st::dialogsScamPadding.left(),
rect.y() + st::dialogsScamPadding.top(),
outerWidth,
phrase,
phraseWidth);
} else {
p.drawTextLeft(
rect.x() + st::dialogsScamPadding.left(),
rect.y() + st::dialogsScamPadding.top(),
outerWidth,
phrase,
phraseWidth);
}
}
void DrawScamBadge(
bool fake,
Painter &p,
QRect rect,
int outerWidth,
const style::color &color) {
const auto phrase = fake
? tr::lng_fake_badge(tr::now)
: tr::lng_scam_badge(tr::now);
DrawScamFakeBadge(
p,
rect,
outerWidth,
color,
phrase,
st::dialogsScamFont->width(phrase));
}
int DrawPeerBadgeGetWidth(
not_null<PeerData*> peer,
Painter &p,
QRect rectForName,
int nameWidth,
int outerWidth,
const PeerBadgeStyle &st) {
if (peer->isVerified() && st.verified) {
const auto iconw = st.verified->width();
st.verified->paint(
p,
rectForName.x() + qMin(nameWidth, rectForName.width() - iconw),
rectForName.y(),
outerWidth);
return iconw;
} else if ((peer->isScam() || peer->isFake()) && st.scam) {
const auto phrase = peer->isScam()
? tr::lng_scam_badge(tr::now)
: tr::lng_fake_badge(tr::now);
const auto phraseWidth = st::dialogsScamFont->width(phrase);
const auto width = st::dialogsScamPadding.left()
+ phraseWidth
+ st::dialogsScamPadding.right();
const auto height = st::dialogsScamPadding.top()
+ st::dialogsScamFont->height
+ st::dialogsScamPadding.bottom();
auto x = (rtl()
? rectForName.right() - qMin(nameWidth + st::dialogsScamSkip, rectForName.width() - width) - width
: rectForName.x() + qMin(nameWidth + st::dialogsScamSkip, rectForName.width() - width));
const auto rect = QRect(
x,
rectForName.y() + (rectForName.height() - height) / 2,
width,
height);
DrawScamFakeBadge(p, rect, outerWidth, *st.scam, phrase, phraseWidth);
return st::dialogsScamSkip + width;
}
return 0;
}
} // namespace Ui