Improve unread counter for HistoryTopBarWidget.

This commit is contained in:
John Preston 2017-11-17 18:54:01 +04:00
parent bef87c6dff
commit 88d7f172ca
5 changed files with 118 additions and 52 deletions

View File

@ -44,6 +44,44 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "observer_peer.h" #include "observer_peer.h"
#include "apiwrap.h" #include "apiwrap.h"
class HistoryTopBarWidget::UnreadBadge : public Ui::RpWidget {
public:
using RpWidget::RpWidget;
void setText(const QString &text, bool active) {
_text = text;
_active = active;
update();
}
protected:
void paintEvent(QPaintEvent *e) override;
private:
QString _text;
bool _active = false;
};
void HistoryTopBarWidget::UnreadBadge::paintEvent(QPaintEvent *e) {
if (_text.isEmpty()) {
return;
}
Painter p(this);
Dialogs::Layout::UnreadBadgeStyle unreadSt;
unreadSt.muted = !_active;
auto unreadRight = width();
auto unreadTop = 0;
Dialogs::Layout::paintUnreadCount(
p,
_text,
unreadRight,
unreadTop,
unreadSt);
}
HistoryTopBarWidget::HistoryTopBarWidget( HistoryTopBarWidget::HistoryTopBarWidget(
QWidget *parent, QWidget *parent,
not_null<Window::Controller*> controller) not_null<Window::Controller*> controller)
@ -95,15 +133,13 @@ HistoryTopBarWidget::HistoryTopBarWidget(
_search->setForceRippled(searchInHistoryPeer, animated); _search->setForceRippled(searchInHistoryPeer, animated);
}, lifetime()); }, lifetime());
subscribe(Adaptive::Changed(), [this]() { updateAdaptiveLayout(); }); subscribe(Adaptive::Changed(), [this] { updateAdaptiveLayout(); });
if (Adaptive::OneColumn()) { if (Adaptive::OneColumn()) {
_unreadCounterSubscription = subscribe(Global::RefUnreadCounterUpdate(), [this] { createUnreadBadge();
rtlupdate(0, 0, st::titleUnreadCounterRight, st::titleUnreadCounterTop);
});
} }
subscribe(App::histories().sendActionAnimationUpdated(), [this](const Histories::SendActionAnimationUpdate &update) { subscribe(App::histories().sendActionAnimationUpdated(), [this](const Histories::SendActionAnimationUpdate &update) {
if (update.history->peer == _historyPeer) { if (update.history->peer == _historyPeer) {
rtlupdate(0, 0, width(), height()); this->update();
} }
}); });
using UpdateFlag = Notify::PeerUpdate::Flag; using UpdateFlag = Notify::PeerUpdate::Flag;
@ -253,12 +289,7 @@ void HistoryTopBarWidget::paintEvent(QPaintEvent *e) {
p.fillRect(QRect(0, 0, width(), st::topBarHeight), st::topBarBg); p.fillRect(QRect(0, 0, width(), st::topBarHeight), st::topBarBg);
if (selectedButtonsTop < 0) { if (selectedButtonsTop < 0) {
p.translate(0, selectedButtonsTop + st::topBarHeight); p.translate(0, selectedButtonsTop + st::topBarHeight);
p.save();
paintTopBar(p, ms); paintTopBar(p, ms);
p.restore();
paintUnreadCounter(p, width(), _historyPeer);
} }
} }
@ -289,38 +320,6 @@ QRect HistoryTopBarWidget::getMembersShowAreaGeometry() const {
return myrtlrect(membersTextLeft, membersTextTop, membersTextWidth, membersTextHeight); return myrtlrect(membersTextLeft, membersTextTop, membersTextWidth, membersTextHeight);
} }
void HistoryTopBarWidget::paintUnreadCounter(
Painter &p,
int outerWidth,
PeerData *substractPeer) {
if (!Adaptive::OneColumn()) {
return;
}
auto mutedCount = App::histories().unreadMutedCount();
auto fullCounter = App::histories().unreadBadge() + (Global::IncludeMuted() ? 0 : mutedCount);
// Do not include currently shown chat in the top bar unread counter.
if (auto historyShown = App::historyLoaded(substractPeer)) {
auto shownUnreadCount = historyShown->unreadCount();
if (!historyShown->mute() || Global::IncludeMuted()) {
fullCounter -= shownUnreadCount;
}
if (historyShown->mute()) {
mutedCount -= shownUnreadCount;
}
}
if (auto counter = (fullCounter - (Global::IncludeMuted() ? 0 : mutedCount))) {
auto counterText = (counter > 99) ? qsl("..%1").arg(counter % 100) : QString::number(counter);
Dialogs::Layout::UnreadBadgeStyle unreadSt;
unreadSt.muted = (mutedCount >= fullCounter);
auto unreadRight = st::titleUnreadCounterRight;
if (rtl()) unreadRight = outerWidth - st::titleUnreadCounterRight;
auto unreadTop = st::titleUnreadCounterTop;
Dialogs::Layout::paintUnreadCount(p, counterText, unreadRight, unreadTop, unreadSt);
}
}
void HistoryTopBarWidget::mousePressEvent(QMouseEvent *e) { void HistoryTopBarWidget::mousePressEvent(QMouseEvent *e) {
auto handleClick = (e->button() == Qt::LeftButton) auto handleClick = (e->button() == Qt::LeftButton)
&& (e->pos().y() < st::topBarHeight) && (e->pos().y() < st::topBarHeight)
@ -346,6 +345,7 @@ void HistoryTopBarWidget::setHistoryPeer(
not_null<PeerData*> historyPeer) { not_null<PeerData*> historyPeer) {
if (_historyPeer != historyPeer) { if (_historyPeer != historyPeer) {
_historyPeer = historyPeer; _historyPeer = historyPeer;
updateUnreadBadge();
if (_historyPeer) { if (_historyPeer) {
_info.create( _info.create(
this, this,
@ -392,6 +392,9 @@ void HistoryTopBarWidget::updateControlsGeometry() {
_delete->moveToLeft(buttonsLeft, selectedButtonsTop); _delete->moveToLeft(buttonsLeft, selectedButtonsTop);
_clearSelection->moveToRight(st::topBarActionSkip, selectedButtonsTop); _clearSelection->moveToRight(st::topBarActionSkip, selectedButtonsTop);
if (_unreadBadge) {
_unreadBadge->setGeometryToLeft(0, st::titleUnreadCounterTop, _back->width(), st::dialogsUnreadHeight);
}
if (_back->isHidden()) { if (_back->isHidden()) {
_leftTaken = st::topBarArrowPadding.right(); _leftTaken = st::topBarArrowPadding.right();
} else { } else {
@ -415,6 +418,8 @@ void HistoryTopBarWidget::updateControlsGeometry() {
_rightTaken += _search->width() + st::topBarCallSkip; _rightTaken += _search->width() + st::topBarCallSkip;
_call->moveToRight(_rightTaken, otherButtonsTop); _call->moveToRight(_rightTaken, otherButtonsTop);
_rightTaken += _call->width(); _rightTaken += _call->width();
updateMembersShowArea();
} }
void HistoryTopBarWidget::setAnimationMode(bool enabled) { void HistoryTopBarWidget::setAnimationMode(bool enabled) {
@ -422,7 +427,6 @@ void HistoryTopBarWidget::setAnimationMode(bool enabled) {
_animationMode = enabled; _animationMode = enabled;
setAttribute(Qt::WA_OpaquePaintEvent, !_animationMode); setAttribute(Qt::WA_OpaquePaintEvent, !_animationMode);
_selectedShown.finish(); _selectedShown.finish();
updateMembersShowArea();
updateControlsVisibility(); updateControlsVisibility();
} }
} }
@ -442,6 +446,9 @@ void HistoryTopBarWidget::updateControlsVisibility() {
if (_info) { if (_info) {
_info->setVisible(backVisible); _info->setVisible(backVisible);
} }
if (_unreadBadge) {
_unreadBadge->show();
}
_search->show(); _search->show();
_menuToggle->show(); _menuToggle->show();
_infoToggle->setVisible(!Adaptive::OneColumn() _infoToggle->setVisible(!Adaptive::OneColumn()
@ -459,6 +466,9 @@ void HistoryTopBarWidget::updateControlsVisibility() {
} }
void HistoryTopBarWidget::updateMembersShowArea() { void HistoryTopBarWidget::updateMembersShowArea() {
if (!App::main()) {
return;
}
auto membersShowAreaNeeded = [this]() { auto membersShowAreaNeeded = [this]() {
auto peer = App::main()->peer(); auto peer = App::main()->peer();
if ((_selectedCount > 0) || !peer) { if ((_selectedCount > 0) || !peer) {
@ -530,18 +540,59 @@ void HistoryTopBarWidget::selectedShowCallback() {
} }
void HistoryTopBarWidget::updateAdaptiveLayout() { void HistoryTopBarWidget::updateAdaptiveLayout() {
updateMembersShowArea();
updateControlsVisibility(); updateControlsVisibility();
if (!Adaptive::OneColumn()) { if (Adaptive::OneColumn()) {
createUnreadBadge();
} else if (_unreadBadge) {
unsubscribe(base::take(_unreadCounterSubscription)); unsubscribe(base::take(_unreadCounterSubscription));
} else if (!_unreadCounterSubscription) { _unreadBadge.destroy();
_unreadCounterSubscription = subscribe(Global::RefUnreadCounterUpdate(), [this] {
rtlupdate(0, 0, st::titleUnreadCounterRight, st::titleUnreadCounterTop);
});
} }
updateInfoToggleActive(); updateInfoToggleActive();
} }
void HistoryTopBarWidget::createUnreadBadge() {
if (_unreadBadge) {
return;
}
_unreadBadge.create(this);
_unreadBadge->setGeometryToLeft(0, st::titleUnreadCounterTop, _back->width(), st::dialogsUnreadHeight);
_unreadBadge->show();
_unreadBadge->setAttribute(Qt::WA_TransparentForMouseEvents);
_unreadCounterSubscription = subscribe(
Global::RefUnreadCounterUpdate(),
[this] { updateUnreadBadge(); });
updateUnreadBadge();
}
void HistoryTopBarWidget::updateUnreadBadge() {
if (!_unreadBadge) return;
auto mutedCount = App::histories().unreadMutedCount();
auto fullCounter = App::histories().unreadBadge()
+ (Global::IncludeMuted() ? 0 : mutedCount);
// Do not include currently shown chat in the top bar unread counter.
if (auto historyShown = App::historyLoaded(_historyPeer)) {
auto shownUnreadCount = historyShown->unreadCount();
if (!historyShown->mute() || Global::IncludeMuted()) {
fullCounter -= shownUnreadCount;
}
if (historyShown->mute()) {
mutedCount -= shownUnreadCount;
}
}
auto active = (mutedCount < fullCounter);
_unreadBadge->setText([&] {
if (auto counter = (fullCounter - (Global::IncludeMuted() ? 0 : mutedCount))) {
return (counter > 999)
? qsl("..%1").arg(counter % 100, 2, 10, QChar('0'))
: QString::number(counter);
}
return QString();
}(), active);
}
void HistoryTopBarWidget::updateInfoToggleActive() { void HistoryTopBarWidget::updateInfoToggleActive() {
auto infoThirdActive = Adaptive::ThreeColumn() auto infoThirdActive = Adaptive::ThreeColumn()
&& (Auth().data().thirdSectionInfoEnabled() && (Auth().data().thirdSectionInfoEnabled()

View File

@ -68,6 +68,8 @@ protected:
bool eventFilter(QObject *obj, QEvent *e) override; bool eventFilter(QObject *obj, QEvent *e) override;
private: private:
class UnreadBadge;
void refreshLang(); void refreshLang();
void updateControlsGeometry(); void updateControlsGeometry();
void selectedShowCallback(); void selectedShowCallback();
@ -94,6 +96,9 @@ private:
void infoClicked(); void infoClicked();
void backClicked(); void backClicked();
void createUnreadBadge();
void updateUnreadBadge();
not_null<Window::Controller*> _controller; not_null<Window::Controller*> _controller;
PeerData *_historyPeer = nullptr; PeerData *_historyPeer = nullptr;
@ -107,6 +112,7 @@ private:
object_ptr<Ui::RoundButton> _forward, _delete; object_ptr<Ui::RoundButton> _forward, _delete;
object_ptr<Ui::IconButton> _back; object_ptr<Ui::IconButton> _back;
object_ptr<UnreadBadge> _unreadBadge = { nullptr };
object_ptr<Ui::UserpicButton> _info = { nullptr }; object_ptr<Ui::UserpicButton> _info = { nullptr };
object_ptr<Ui::IconButton> _call; object_ptr<Ui::IconButton> _call;

View File

@ -509,7 +509,7 @@ void WrapWidget::showBackFromStack() {
last.section.get(), last.section.get(),
params); params);
//_anotherTabMemento = std::move(last.anotherTab); //_anotherTabMemento = std::move(last.anotherTab);
} else { } else if (wrap() != Wrap::Layer) {
_controller->window()->showBackFromStack(params); _controller->window()->showBackFromStack(params);
} }
} }
@ -864,6 +864,14 @@ void WrapWidget::resizeEvent(QResizeEvent *e) {
updateContentGeometry(); updateContentGeometry();
} }
void WrapWidget::keyPressEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Escape) {
showBackFromStack();
} else {
SectionWidget::keyPressEvent(e);
}
}
void WrapWidget::updateContentGeometry() { void WrapWidget::updateContentGeometry() {
if (_content) { if (_content) {
_topShadow->resizeToWidth(width()); _topShadow->resizeToWidth(width());

View File

@ -128,6 +128,7 @@ public:
protected: protected:
void resizeEvent(QResizeEvent *e) override; void resizeEvent(QResizeEvent *e) override;
void keyPressEvent(QKeyEvent *e) override;
void doSetInnerFocus() override; void doSetInnerFocus() override;
void showFinishedHook() override; void showFinishedHook() override;

View File

@ -20,7 +20,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/ */
#include "profile/profile_back_button.h" #include "profile/profile_back_button.h"
#include "history/history_top_bar_widget.h" //#include "history/history_top_bar_widget.h"
#include "styles/style_widgets.h" #include "styles/style_widgets.h"
#include "styles/style_window.h" #include "styles/style_window.h"
#include "styles/style_profile.h" #include "styles/style_profile.h"
@ -54,7 +54,7 @@ void BackButton::paintEvent(QPaintEvent *e) {
p.setPen(st::topBarButton.textFg); p.setPen(st::topBarButton.textFg);
p.drawTextLeft(st::topBarArrowPadding.left(), st::topBarButton.padding.top() + st::topBarButton.textTop, width(), _text); p.drawTextLeft(st::topBarArrowPadding.left(), st::topBarButton.padding.top() + st::topBarButton.textTop, width(), _text);
HistoryTopBarWidget::paintUnreadCounter(p, width()); // HistoryTopBarWidget::paintUnreadCounter(p, width());
} }
void BackButton::onStateChanged(State was, StateChangeSource source) { void BackButton::onStateChanged(State was, StateChangeSource source) {