-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Expand file tree
/
Copy pathmenu_checked_action.cpp
More file actions
230 lines (200 loc) · 5.59 KB
/
Copy pathmenu_checked_action.cpp
File metadata and controls
230 lines (200 loc) · 5.59 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
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
/*
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 "menu/menu_checked_action.h"
#include "base/unique_qptr.h"
#include "ui/effects/premium_graphics.h"
#include "ui/widgets/menu/menu_action.h"
#include "ui/widgets/menu/menu_common.h"
#include "ui/widgets/popup_menu.h"
#include "ui/painter.h"
#include "styles/style_media_player.h"
#include "styles/style_widgets.h"
#include "styles/palette.h"
#include <QtSvg/QSvgRenderer>
namespace {
// Extra room to the right of the premium star in menu actions.
constexpr auto kPremiumStarRightSkip = 10;
[[nodiscard]] QImage PremiumStarImage(int size) {
const auto factor = style::DevicePixelRatio();
const auto side = QSize(size, size);
auto image = QImage(
side * factor,
QImage::Format_ARGB32_Premultiplied);
image.setDevicePixelRatio(factor);
image.fill(Qt::transparent);
{
auto p = QPainter(&image);
auto svg = QSvgRenderer(
Ui::Premium::ColorizedSvg(Ui::Premium::ButtonGradientStops()));
svg.render(&p, QRectF(QPointF(), QSizeF(side)));
}
return image;
}
[[nodiscard]] style::Menu PatchedActiveStyle(
const style::Menu &base,
bool active,
int premiumStarSize,
bool withShortcut) {
auto result = base;
if (active) {
result.itemFg = st::windowActiveTextFg;
result.itemFgOver = st::windowActiveTextFg;
result.itemFgShortcut = st::windowActiveTextFg;
result.itemFgShortcutOver = st::windowActiveTextFg;
}
if (premiumStarSize > 0 && withShortcut) {
result.itemPadding.setRight(result.itemRightSkip
+ style::ConvertScale(kPremiumStarRightSkip)
+ premiumStarSize
+ result.itemRightSkip);
}
return result;
}
// Holds the patched style so it outlives the Ui::Menu::Action base, which keeps
// only a reference to its style::Menu. Declared first => constructed first.
struct OwnedMenuStyle {
explicit OwnedMenuStyle(style::Menu st) : value(std::move(st)) {
}
style::Menu value;
};
class ActiveColorAction final
: private OwnedMenuStyle
, public Ui::Menu::Action {
public:
ActiveColorAction(
not_null<Ui::Menu::Menu*> parent,
const style::Menu &st,
not_null<QAction*> action,
const style::icon *icon,
bool active,
int premiumStarSize);
private:
void paintEvent(QPaintEvent *e) override;
const style::icon *_activeIcon = nullptr;
const bool _active = false;
QImage _premiumStar;
};
ActiveColorAction::ActiveColorAction(
not_null<Ui::Menu::Menu*> parent,
const style::Menu &st,
not_null<QAction*> action,
const style::icon *icon,
bool active,
int premiumStarSize)
: OwnedMenuStyle(PatchedActiveStyle(
st,
active,
premiumStarSize,
action->text().contains(QChar('\t'))))
, Ui::Menu::Action(parent, OwnedMenuStyle::value, action, icon, icon)
, _activeIcon(icon)
, _active(active)
, _premiumStar((premiumStarSize > 0)
? PremiumStarImage(premiumStarSize)
: QImage()) {
if (premiumStarSize > 0
&& !action->text().contains(QChar('\t'))) {
setMinWidth(minWidth()
+ OwnedMenuStyle::value.itemRightSkip
+ style::ConvertScale(kPremiumStarRightSkip)
+ premiumStarSize);
}
}
void ActiveColorAction::paintEvent(QPaintEvent *e) {
Ui::Menu::Action::paintEvent(e);
Painter p(this);
if (_active && _activeIcon) {
_activeIcon->paint(
p,
OwnedMenuStyle::value.itemIconPosition,
width(),
st::windowActiveTextFg->c);
}
if (!_premiumStar.isNull()) {
const auto factor = style::DevicePixelRatio();
const auto starWidth = _premiumStar.width() / factor;
const auto starHeight = _premiumStar.height() / factor;
const auto left = width()
- OwnedMenuStyle::value.itemRightSkip
- style::ConvertScale(kPremiumStarRightSkip)
- starWidth;
const auto top = (height() - starHeight) / 2;
p.drawImage(left, top, _premiumStar);
}
}
class CheckedAction final : public Ui::Menu::Action {
public:
CheckedAction(
not_null<Ui::Menu::Menu*> parent,
const style::Menu &st,
not_null<QAction*> action,
const style::icon *icon,
bool checked);
private:
void paintEvent(QPaintEvent *e) override;
const bool _checked = false;
};
CheckedAction::CheckedAction(
not_null<Ui::Menu::Menu*> parent,
const style::Menu &st,
not_null<QAction*> action,
const style::icon *icon,
bool checked)
: Ui::Menu::Action(parent, st, action, icon, icon)
, _checked(checked) {
setMinWidth(minWidth() + st.itemRightSkip + st::mediaPlayerMenuCheck.width());
}
void CheckedAction::paintEvent(QPaintEvent *e) {
Ui::Menu::Action::paintEvent(e);
if (!_checked) {
return;
}
Painter p(this);
const auto &icon = st::mediaPlayerMenuCheck;
const auto left = width() - st().itemRightSkip - icon.width();
const auto top = (height() - icon.height()) / 2;
icon.paint(p, left, top, width());
}
} // namespace
namespace Menu {
not_null<QAction*> AddCheckedAction(
not_null<Ui::PopupMenu*> menu,
const QString &text,
Fn<void()> callback,
const style::icon *icon,
bool checked) {
auto item = base::make_unique_q<CheckedAction>(
menu->menu(),
menu->st().menu,
Ui::Menu::CreateAction(
menu->menu().get(),
text,
std::move(callback)),
icon,
checked);
return menu->addAction(std::move(item));
}
not_null<QAction*> AddActiveColorAction(
not_null<Ui::PopupMenu*> menu,
const QString &text,
Fn<void()> callback,
const style::icon *icon,
bool active,
int premiumStarSize) {
auto item = base::make_unique_q<ActiveColorAction>(
menu->menu(),
menu->st().menu,
Ui::Menu::CreateAction(
menu->menu().get(),
text,
std::move(callback)),
icon,
active,
premiumStarSize);
return menu->addAction(std::move(item));
}
} // namespace Menu