-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Expand file tree
/
Copy pathtabbed_panel.cpp
More file actions
536 lines (458 loc) · 13 KB
/
Copy pathtabbed_panel.cpp
File metadata and controls
536 lines (458 loc) · 13 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
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 "chat_helpers/tabbed_panel.h"
#include "ui/widgets/shadow.h"
#include "ui/image/image_prepare.h"
#include "ui/ui_utility.h"
#include "chat_helpers/tabbed_selector.h"
#include "window/window_session_controller.h"
#include "main/main_session.h"
#include "data/data_session.h"
#include "data/stickers/data_stickers.h"
#include "core/application.h"
#include "base/options.h"
#include "styles/style_chat_helpers.h"
namespace ChatHelpers {
namespace {
constexpr auto kHideTimeoutMs = 300;
constexpr auto kDelayedHideTimeoutMs = 3000;
base::options::toggle TabbedPanelShowOnClick({
.id = kOptionTabbedPanelShowOnClick,
.name = "Show tabbed panel by click",
.description = "Show Emoji / Stickers / GIFs panel only after a click.",
});
} // namespace
const char kOptionTabbedPanelShowOnClick[] = "tabbed-panel-show-on-click";
bool ShowPanelOnClick() {
return TabbedPanelShowOnClick.value();
}
TabbedPanel::TabbedPanel(
QWidget *parent,
not_null<Window::SessionController*> controller,
not_null<TabbedSelector*> selector)
: TabbedPanel(parent, {
.regularWindow = controller,
.nonOwnedSelector = selector,
}) {
}
TabbedPanel::TabbedPanel(
QWidget *parent,
not_null<Window::SessionController*> controller,
object_ptr<TabbedSelector> selector)
: TabbedPanel(parent, {
.regularWindow = controller,
.ownedSelector = std::move(selector),
}) {
}
TabbedPanel::TabbedPanel(
QWidget *parent,
TabbedPanelDescriptor &&descriptor)
: RpWidget(parent)
, _regularWindow(descriptor.regularWindow)
, _ownedSelector(std::move(descriptor.ownedSelector))
, _selector(descriptor.nonOwnedSelector
? descriptor.nonOwnedSelector
: _ownedSelector.data())
, _heightRatio(st::emojiPanHeightRatio)
, _minContentHeight(st::emojiPanMinHeight)
, _maxContentHeight(st::emojiPanMaxHeight)
, _shadow(_selector->st().showAnimation.shadow) {
Expects(_selector != nullptr);
_selector->setParent(this);
_selector->setRoundRadius(st::emojiPanRadius);
_selector->setAfterShownCallback([=](SelectorTab tab) {
if (_regularWindow) {
_regularWindow->enableGifPauseReason(_selector->level());
}
_pauseAnimations.fire(true);
});
_selector->setBeforeHidingCallback([=](SelectorTab tab) {
if (_regularWindow) {
_regularWindow->disableGifPauseReason(_selector->level());
}
_pauseAnimations.fire(false);
});
_selector->showRequests(
) | rpl::on_next([=] {
showFromSelector();
}, lifetime());
resize(
QRect(0, 0, st::emojiPanWidth, st::emojiPanMaxHeight).marginsAdded(
innerPadding()).size());
_contentMaxHeight = st::emojiPanMaxHeight;
_contentHeight = _contentMaxHeight;
_selector->resize(st::emojiPanWidth, _contentHeight);
_selector->move(innerRect().topLeft());
_hideTimer.setCallback([this] { hideByTimerOrLeave(); });
_selector->checkForHide(
) | rpl::on_next([=] {
if (!rect().contains(mapFromGlobal(QCursor::pos()))) {
_hideTimer.callOnce(kDelayedHideTimeoutMs);
}
}, lifetime());
_selector->cancelled(
) | rpl::on_next([=] {
hideAnimated();
}, lifetime());
if (_regularWindow) {
_regularWindow->session().data().stickers().gifWithCaptionSent(
) | rpl::on_next([=] {
hideAnimated();
}, lifetime());
}
_selector->slideFinished(
) | rpl::on_next([=] {
InvokeQueued(this, [=] {
if (_hideAfterSlide) {
startOpacityAnimation(true);
}
});
}, lifetime());
macWindowDeactivateEvents(
) | rpl::filter([=] {
return !isHidden() && !preventAutoHide();
}) | rpl::on_next([=] {
hideAnimated();
}, lifetime());
setAttribute(Qt::WA_OpaquePaintEvent, false);
hideChildren();
hide();
}
not_null<TabbedSelector*> TabbedPanel::selector() const {
return _selector;
}
rpl::producer<bool> TabbedPanel::pauseAnimations() const {
return _pauseAnimations.events();
}
bool TabbedPanel::isSelectorStolen() const {
return (_selector->parent() != this);
}
void TabbedPanel::moveBottomRight(int bottom, int right) {
const auto isNew = (_bottom != bottom || _right != right);
_bottom = bottom;
_right = right;
// If the panel is already shown, update the position.
if (!isHidden() && isNew) {
moveHorizontally();
} else {
updateContentHeight();
}
}
void TabbedPanel::moveTopRight(int top, int right) {
const auto isNew = (_top != top || _right != right);
_top = top;
_right = right;
// If the panel is already shown, update the position.
if (!isHidden() && isNew) {
moveHorizontally();
} else {
updateContentHeight();
}
}
void TabbedPanel::setDesiredHeightValues(
float64 ratio,
int minHeight,
int maxHeight) {
_heightRatio = ratio;
_minContentHeight = minHeight;
_maxContentHeight = maxHeight;
updateContentHeight();
}
void TabbedPanel::setShowAnimationOrigin(Ui::PanelAnimation::Origin origin) {
_showAnimationOrigin = origin;
}
void TabbedPanel::setDropDown(bool dropDown) {
selector()->setDropDown(dropDown);
_dropDown = dropDown;
}
void TabbedPanel::updateContentHeight() {
auto addedHeight = innerPadding().top() + innerPadding().bottom();
auto marginsHeight = _selector->marginTop() + _selector->marginBottom();
auto availableHeight = _dropDown
? (parentWidget()->height() - _top - marginsHeight)
: (_bottom - marginsHeight);
const auto wanted = int(base::SafeRound(_heightRatio * availableHeight));
auto wantedContentHeight = wanted - addedHeight;
auto contentHeight = marginsHeight + std::clamp(
wantedContentHeight,
_minContentHeight,
_maxContentHeight);
auto resultTop = _dropDown
? _top
: (_bottom - addedHeight - contentHeight);
if (contentHeight == _contentHeight) {
move(x(), resultTop);
return;
}
_contentHeight = contentHeight;
resize(QRect(0, 0, innerRect().width(), _contentHeight).marginsAdded(innerPadding()).size());
move(x(), resultTop);
_selector->resize(innerRect().width(), _contentHeight);
update();
}
void TabbedPanel::paintEvent(QPaintEvent *e) {
auto p = QPainter(this);
// This call can finish _a_show animation and destroy _showAnimation.
auto opacityAnimating = _a_opacity.animating();
auto showAnimating = _a_show.animating();
if (_showAnimation && !showAnimating) {
_showAnimation.reset();
if (!opacityAnimating) {
showChildren();
_selector->afterShown();
}
}
if (showAnimating) {
Assert(_showAnimation != nullptr);
if (auto opacity = _a_opacity.value(_hiding ? 0. : 1.)) {
_showAnimation->paintFrame(p, 0, 0, width(), _a_show.value(1.), opacity);
}
} else if (opacityAnimating) {
p.setOpacity(_a_opacity.value(_hiding ? 0. : 1.));
p.drawPixmap(0, 0, _cache);
} else if (_hiding || isHidden()) {
hideFinished();
} else {
if (!_cache.isNull()) _cache = QPixmap();
_shadow.paint(p, innerRect(), st::emojiPanRadius);
}
}
void TabbedPanel::moveHorizontally() {
const auto padding = innerPadding();
const auto width = innerRect().width() + padding.left() + padding.right();
const auto right = std::max(
parentWidget()->width() - std::max(_right, width),
0);
moveToRight(right, y());
updateContentHeight();
}
void TabbedPanel::enterEventHook(QEnterEvent *e) {
Core::App().registerLeaveSubscription(this);
showAnimated();
}
bool TabbedPanel::preventAutoHide() const {
return _selector->preventAutoHide();
}
void TabbedPanel::leaveEventHook(QEvent *e) {
Core::App().unregisterLeaveSubscription(this);
if (preventAutoHide()) {
return;
}
if (_a_show.animating() || _a_opacity.animating()) {
hideAnimated();
} else {
_hideTimer.callOnce(kHideTimeoutMs);
}
return RpWidget::leaveEventHook(e);
}
void TabbedPanel::otherEnter() {
showAnimated();
}
void TabbedPanel::otherLeave() {
if (preventAutoHide()) {
return;
}
if (_a_opacity.animating()) {
hideByTimerOrLeave();
} else {
// In case of animations disabled add some delay before hiding.
// Otherwise if emoji suggestions panel is shown in between
// (z-order wise) the emoji toggle button and tabbed panel,
// we won't be able to move cursor from the button to the panel.
_hideTimer.callOnce(anim::Disabled() ? kHideTimeoutMs : 0);
}
}
void TabbedPanel::hideFast() {
if (isHidden()) return;
if (_selector && !_selector->isHidden()) {
_selector->beforeHiding();
}
_hideTimer.cancel();
_hiding = false;
_a_opacity.stop();
hideFinished();
}
void TabbedPanel::opacityAnimationCallback() {
update();
if (!_a_opacity.animating()) {
if (_hiding) {
_hiding = false;
hideFinished();
} else if (!_a_show.animating()) {
showChildren();
_selector->afterShown();
}
}
}
void TabbedPanel::hideByTimerOrLeave() {
if (isHidden() || preventAutoHide()) {
return;
}
hideAnimated();
}
void TabbedPanel::prepareCacheFor(bool hiding) {
if (_a_opacity.animating()) {
_hiding = hiding;
return;
}
auto showAnimation = base::take(_a_show);
auto showAnimationData = base::take(_showAnimation);
_hiding = false;
showChildren();
_cache = Ui::GrabWidget(this);
_a_show = base::take(showAnimation);
_showAnimation = base::take(showAnimationData);
_hiding = hiding;
if (_a_show.animating()) {
hideChildren();
}
}
void TabbedPanel::startOpacityAnimation(bool hiding) {
if (_selector && !_selector->isHidden()) {
_selector->beforeHiding();
}
prepareCacheFor(hiding);
hideChildren();
_a_opacity.start(
[=] { opacityAnimationCallback(); },
_hiding ? 1. : 0.,
_hiding ? 0. : 1.,
st::emojiPanDuration);
}
void TabbedPanel::startShowAnimation() {
if (!_a_show.animating()) {
auto image = grabForAnimation();
const auto origin = _showAnimationOrigin.value_or(_dropDown
? Ui::PanelAnimation::Origin::TopRight
: Ui::PanelAnimation::Origin::BottomRight);
_showAnimation = std::make_unique<Ui::PanelAnimation>(
_selector->st().showAnimation,
origin);
auto inner = rect().marginsRemoved(st::emojiPanMargins);
_showAnimation->setFinalImage(
std::move(image),
QRect(
inner.topLeft() * style::DevicePixelRatio(),
inner.size() * style::DevicePixelRatio()),
st::emojiPanRadius);
_showAnimation->setCornerMasks(Images::CornersMask(st::emojiPanRadius));
_showAnimation->start();
}
hideChildren();
_a_show.start([this] { update(); }, 0., 1., st::emojiPanShowDuration);
}
QImage TabbedPanel::grabForAnimation() {
auto cache = base::take(_cache);
auto opacityAnimation = base::take(_a_opacity);
auto showAnimationData = base::take(_showAnimation);
auto showAnimation = base::take(_a_show);
showChildren();
Ui::SendPendingMoveResizeEvents(this);
auto result = QImage(
size() * style::DevicePixelRatio(),
QImage::Format_ARGB32_Premultiplied);
result.setDevicePixelRatio(style::DevicePixelRatio());
result.fill(Qt::transparent);
if (_selector) {
QPainter p(&result);
Ui::RenderWidget(p, _selector, _selector->pos());
}
_a_show = base::take(showAnimation);
_showAnimation = base::take(showAnimationData);
_a_opacity = base::take(opacityAnimation);
_cache = base::take(cache);
return result;
}
void TabbedPanel::hideAnimated() {
if (isHidden() || _hiding) {
return;
}
_hideTimer.cancel();
if (_selector->isSliding()) {
_hideAfterSlide = true;
} else {
startOpacityAnimation(true);
}
// There is no reason to worry about the message scheduling box
// while it moves the user to the separate scheduled section.
_shouldFinishHide = _selector->hasMenu();
}
void TabbedPanel::toggleAnimated() {
if (isHidden() || _hiding || _hideAfterSlide) {
showAnimated();
} else {
hideAnimated();
}
}
void TabbedPanel::hideFinished() {
hide();
_a_show.stop();
_showAnimation.reset();
_cache = QPixmap();
_hiding = false;
_shouldFinishHide = false;
_selector->hideFinished();
}
void TabbedPanel::showAnimated() {
_hideTimer.cancel();
_hideAfterSlide = false;
showStarted();
}
void TabbedPanel::showStarted() {
if (_shouldFinishHide) {
return;
}
if (isHidden()) {
_selector->showStarted();
moveHorizontally();
raise();
show();
startShowAnimation();
} else if (_hiding) {
startOpacityAnimation(false);
}
}
bool TabbedPanel::eventFilter(QObject *obj, QEvent *e) {
if (TabbedPanelShowOnClick.value()) {
return false;
} else if (e->type() == QEvent::Enter) {
otherEnter();
} else if (e->type() == QEvent::Leave) {
otherLeave();
}
return false;
}
void TabbedPanel::showFromSelector() {
if (isHidden()) {
moveHorizontally();
startShowAnimation();
show();
}
showChildren();
showAnimated();
}
style::margins TabbedPanel::innerPadding() const {
return st::emojiPanMargins;
}
QRect TabbedPanel::innerRect() const {
return rect().marginsRemoved(innerPadding());
}
bool TabbedPanel::overlaps(const QRect &globalRect) const {
if (isHidden() || !_cache.isNull()) return false;
auto testRect = QRect(mapFromGlobal(globalRect.topLeft()), globalRect.size());
auto inner = rect().marginsRemoved(st::emojiPanMargins);
const auto radius = st::emojiPanRadius;
return inner.marginsRemoved(QMargins(radius, 0, radius, 0)).contains(testRect)
|| inner.marginsRemoved(QMargins(0, radius, 0, radius)).contains(testRect);
}
TabbedPanel::~TabbedPanel() {
hideFast();
if (!_ownedSelector && _regularWindow) {
_regularWindow->takeTabbedSelectorOwnershipFrom(this);
}
}
} // namespace ChatHelpers