2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Move stickers state variables to AuthSessionData.

Also allow to click on the selected set when choosing megagroup
sticker set and allow to paste a t.me link to the set there.
This commit is contained in:
John Preston
2017-11-05 21:07:27 +04:00
parent 9a56b2d20f
commit 554eb3a342
31 changed files with 673 additions and 479 deletions

View File

@@ -52,8 +52,8 @@ constexpr auto kHandleMegagroupSetAddressChangeTimeout = TimeMs(1000);
int stickerPacksCount(bool includeArchivedOfficial) {
auto result = 0;
auto &order = Global::StickerSetsOrder();
auto &sets = Global::StickerSets();
auto &order = Auth().data().stickerSetsOrder();
auto &sets = Auth().data().stickerSets();
for (auto i = 0, l = order.size(); i < l; ++i) {
auto it = sets.constFind(order.at(i));
if (it != sets.cend()) {
@@ -65,24 +65,23 @@ int stickerPacksCount(bool includeArchivedOfficial) {
return result;
}
class StickersBox::CounterWidget : public TWidget, private base::Subscriber {
class StickersBox::CounterWidget : public Ui::RpWidget {
public:
CounterWidget(QWidget *parent);
void setCounter(int counter);
protected:
void paintEvent(QPaintEvent *e) override;
private:
void updateCounter();
void setCounter(int counter);
QString _text;
Dialogs::Layout::UnreadBadgeStyle _st;
};
StickersBox::CounterWidget::CounterWidget(QWidget *parent) : TWidget(parent) {
StickersBox::CounterWidget::CounterWidget(QWidget *parent)
: RpWidget(parent) {
setAttribute(Qt::WA_TransparentForMouseEvents);
_st.sizeId = Dialogs::Layout::UnreadBadgeInStickersBox;
@@ -91,8 +90,11 @@ StickersBox::CounterWidget::CounterWidget(QWidget *parent) : TWidget(parent) {
_st.padding = st::stickersFeaturedBadgePadding;
_st.font = st::stickersFeaturedBadgeFont;
subscribe(Global::RefFeaturedStickerSetsUnreadCountChanged(), [this] { updateCounter(); });
updateCounter();
Auth().data().featuredStickerSetsUnreadCountValue()
| rpl::start_with_next([this](int count) {
setCounter(count);
update();
}, lifetime());
}
void StickersBox::CounterWidget::setCounter(int counter) {
@@ -116,11 +118,6 @@ void StickersBox::CounterWidget::paintEvent(QPaintEvent *e) {
}
}
void StickersBox::CounterWidget::updateCounter() {
setCounter(Global::FeaturedStickerSetsUnreadCount());
update();
}
template <typename ...Args>
StickersBox::Tab::Tab(int index, Args&&... args)
: _index(index)
@@ -166,7 +163,7 @@ void StickersBox::getArchivedDone(uint64 offsetId, const MTPmessages_ArchivedSti
}
auto &stickers = result.c_messages_archivedStickers();
auto &archived = Global::RefArchivedStickerSetsOrder();
auto &archived = Auth().data().archivedStickerSetsOrderRef();
if (offsetId) {
auto index = archived.indexOf(offsetId);
if (index >= 0) {
@@ -241,7 +238,7 @@ void StickersBox::prepare() {
requestArchivedSets();
}
if (_tabs) {
if (Global::ArchivedStickerSetsOrder().isEmpty()) {
if (Auth().data().archivedStickerSetsOrder().isEmpty()) {
preloadArchivedSets();
}
setNoContentMargin(true);
@@ -280,7 +277,10 @@ void StickersBox::prepare() {
setInnerWidget(_tab->takeWidget(), getTopSkip());
setDimensions(st::boxWideWidth, st::boxMaxListHeight);
subscribe(Auth().data().stickersUpdated(), [this] { handleStickersUpdated(); });
Auth().data().stickersUpdated()
| rpl::start_with_next(
[this] { handleStickersUpdated(); },
lifetime());
Auth().api().updateStickers();
if (_installed.widget()) {
@@ -304,11 +304,11 @@ void StickersBox::refreshTabs() {
auto sections = QStringList();
sections.push_back(lang(lng_stickers_installed_tab).toUpper());
_tabIndices.push_back(Section::Installed);
if (!Global::FeaturedStickerSetsOrder().isEmpty()) {
if (!Auth().data().featuredStickerSetsOrder().isEmpty()) {
sections.push_back(lang(lng_stickers_featured_tab).toUpper());
_tabIndices.push_back(Section::Featured);
}
if (!Global::ArchivedStickerSetsOrder().isEmpty()) {
if (!Auth().data().archivedStickerSetsOrder().isEmpty()) {
sections.push_back(lang(lng_stickers_archived_tab).toUpper());
_tabIndices.push_back(Section::Archived);
}
@@ -330,10 +330,10 @@ void StickersBox::loadMoreArchived() {
}
uint64 lastId = 0;
for (auto setIt = Global::ArchivedStickerSetsOrder().cend(), e = Global::ArchivedStickerSetsOrder().cbegin(); setIt != e;) {
for (auto setIt = Auth().data().archivedStickerSetsOrder().cend(), e = Auth().data().archivedStickerSetsOrder().cbegin(); setIt != e;) {
--setIt;
auto it = Global::StickerSets().constFind(*setIt);
if (it != Global::StickerSets().cend()) {
auto it = Auth().data().stickerSets().constFind(*setIt);
if (it != Auth().data().stickerSets().cend()) {
if (it->flags & MTPDstickerSet::Flag::f_archived) {
lastId = it->id;
break;
@@ -445,7 +445,7 @@ QPixmap StickersBox::grabContentCache() {
}
void StickersBox::installSet(uint64 setId) {
auto &sets = Global::RefStickerSets();
auto &sets = Auth().data().stickerSetsRef();
auto it = sets.find(setId);
if (it == sets.cend()) {
rebuildList();
@@ -474,7 +474,7 @@ void StickersBox::installDone(const MTPmessages_StickerSetInstallResult &result)
bool StickersBox::installFail(uint64 setId, const RPCError &error) {
if (MTP::isDefaultHandledError(error)) return false;
auto &sets = Global::RefStickerSets();
auto &sets = Auth().data().stickerSetsRef();
auto it = sets.find(setId);
if (it == sets.cend()) {
rebuildList();
@@ -498,8 +498,8 @@ void StickersBox::requestArchivedSets() {
preloadArchivedSets();
}
auto &sets = Global::StickerSets();
for_const (auto setId, Global::ArchivedStickerSetsOrder()) {
auto &sets = Auth().data().stickerSets();
for_const (auto setId, Auth().data().archivedStickerSetsOrder()) {
auto it = sets.constFind(setId);
if (it != sets.cend()) {
if (it->stickers.isEmpty() && (it->flags & MTPDstickerSet_ClientFlag::f_not_loaded)) {
@@ -530,7 +530,7 @@ void StickersBox::handleStickersUpdated() {
} else {
_tab->widget()->updateRows();
}
if (Global::ArchivedStickerSetsOrder().isEmpty()) {
if (Auth().data().archivedStickerSetsOrder().isEmpty()) {
preloadArchivedSets();
} else {
refreshTabs();
@@ -612,8 +612,20 @@ StickersBox::Inner::Inner(QWidget *parent, not_null<ChannelData*> megagroup) : T
_megagroupSetField->setLinkPlaceholder(Messenger::Instance().createInternalLink(qsl("addstickers/")));
_megagroupSetField->setPlaceholderHidden(false);
_megagroupSetAddressChangedTimer.setCallback([this] { handleMegagroupSetAddressChange(); });
connect(_megagroupSetField, &Ui::MaskedInputField::changed, this, [this] { _megagroupSetAddressChangedTimer.callOnce(kHandleMegagroupSetAddressChangeTimeout); });
connect(_megagroupSetField, &Ui::MaskedInputField::submitted, this, [this] { _megagroupSetAddressChangedTimer.cancel(); handleMegagroupSetAddressChange(); });
connect(
_megagroupSetField,
&Ui::MaskedInputField::changed,
[this] {
_megagroupSetAddressChangedTimer.callOnce(
kHandleMegagroupSetAddressChangeTimeout);
});
connect(
_megagroupSetField,
&Ui::MaskedInputField::submitted,
[this] {
_megagroupSetAddressChangedTimer.cancel();
handleMegagroupSetAddressChange();
});
setup();
}
@@ -717,7 +729,13 @@ void StickersBox::Inner::paintRow(Painter &p, Row *set, int index, TimeMs ms) {
if (xadd || yadd) p.translate(xadd, yadd);
if (_megagroupSet) {
if (index >= 0 && index == _selected) {
auto selectedIndex = [&] {
if (auto index = base::get_if<int>(&_selected)) {
return *index;
}
return -1;
}();
if (index >= 0 && index == selectedIndex) {
p.fillRect(0, 0, width(), _rowHeight, st::contactsBgOver);
if (set->ripple) {
set->ripple->paint(p, 0, 0, width(), ms);
@@ -854,9 +872,11 @@ void StickersBox::Inner::mousePressEvent(QMouseEvent *e) {
if (_actionSel >= 0) {
setActionDown(_actionSel);
update(0, _itemsTop + _actionSel * _rowHeight, width(), _rowHeight);
} else if (_selected >= 0 && _section == Section::Installed && !_rows.at(_selected)->isRecentSet() && _inDragArea) {
_above = _dragging = _started = _selected;
_dragStart = mapFromGlobal(_mouse);
} else if (auto selectedIndex = base::get_if<int>(&_selected)) {
if (_section == Section::Installed && !_rows[*selectedIndex]->isRecentSet() && _inDragArea) {
_above = _dragging = _started = *selectedIndex;
_dragStart = mapFromGlobal(_mouse);
}
}
}
@@ -900,41 +920,65 @@ void StickersBox::Inner::setActionDown(int newActionDown) {
}
}
void StickersBox::Inner::setSelected(int selected) {
void StickersBox::Inner::setSelected(SelectedRow selected) {
if (_selected == selected) {
return;
}
if (_megagroupSet && _selected >= 0 && _selected < _rows.size()) {
update(0, _itemsTop + _selected * _rowHeight, width(), _rowHeight);
if ((_megagroupSet || _section != Section::Installed)
&& ((_selected.has_value() || _pressed.has_value()) != (selected.has_value() || _pressed.has_value()))) {
if (!_inDragArea) {
setCursor((selected.has_value() || _pressed.has_value())
? style::cur_pointer
: style::cur_default);
}
}
auto countSelectedIndex = [&] {
if (auto index = base::get_if<int>(&_selected)) {
return *index;
}
return -1;
};
auto selectedIndex = countSelectedIndex();
if (_megagroupSet && selectedIndex >= 0 && selectedIndex < _rows.size()) {
update(0, _itemsTop + selectedIndex * _rowHeight, width(), _rowHeight);
}
_selected = selected;
if (_megagroupSet && _selected >= 0 && _selected < _rows.size()) {
update(0, _itemsTop + _selected * _rowHeight, width(), _rowHeight);
selectedIndex = countSelectedIndex();
if (_megagroupSet && selectedIndex >= 0 && selectedIndex < _rows.size()) {
update(0, _itemsTop + selectedIndex * _rowHeight, width(), _rowHeight);
}
}
void StickersBox::Inner::setPressed(int pressed) {
void StickersBox::Inner::setPressed(SelectedRow pressed) {
if (_pressed == pressed) {
return;
}
if (_megagroupSet && _pressed >= 0 && _pressed < _rows.size()) {
update(0, _itemsTop + _pressed * _rowHeight, width(), _rowHeight);
auto &set = _rows[_pressed];
auto countPressedIndex = [&] {
if (auto index = base::get_if<int>(&_pressed)) {
return *index;
}
return -1;
};
auto pressedIndex = countPressedIndex();
if (_megagroupSet && pressedIndex >= 0 && pressedIndex < _rows.size()) {
update(0, _itemsTop + pressedIndex * _rowHeight, width(), _rowHeight);
auto &set = _rows[pressedIndex];
if (set->ripple) {
set->ripple->lastStop();
}
}
_pressed = pressed;
if (_megagroupSet && _pressed >= 0 && _pressed < _rows.size()) {
update(0, _itemsTop + _pressed * _rowHeight, width(), _rowHeight);
auto &set = _rows[_pressed];
pressedIndex = countPressedIndex();
if (_megagroupSet && pressedIndex >= 0 && pressedIndex < _rows.size()) {
update(0, _itemsTop + pressedIndex * _rowHeight, width(), _rowHeight);
auto &set = _rows[pressedIndex];
auto rippleMask = Ui::RippleAnimation::rectMask(QSize(width(), _rowHeight));
if (!_rows[_pressed]->ripple) {
_rows[_pressed]->ripple = std::make_unique<Ui::RippleAnimation>(st::contactsRipple, std::move(rippleMask), [this, index = _pressed] {
update(0, _itemsTop + index * _rowHeight, width(), _rowHeight);
if (!_rows[pressedIndex]->ripple) {
_rows[pressedIndex]->ripple = std::make_unique<Ui::RippleAnimation>(st::contactsRipple, std::move(rippleMask), [this, pressedIndex] {
update(0, _itemsTop + pressedIndex * _rowHeight, width(), _rowHeight);
});
}
_rows[_pressed]->ripple->add(mapFromGlobal(QCursor::pos()) - QPoint(0, _itemsTop + _pressed * _rowHeight));
_rows[pressedIndex]->ripple->add(mapFromGlobal(QCursor::pos()) - QPoint(0, _itemsTop + pressedIndex * _rowHeight));
}
}
@@ -997,17 +1041,18 @@ void StickersBox::Inner::onUpdateSelected() {
emit draggingScrollDelta(countDraggingScrollDelta());
} else {
bool in = rect().marginsRemoved(QMargins(0, _itemsTop, 0, st::membersMarginBottom)).contains(local);
auto selected = -1;
auto selected = SelectedRow();
auto actionSel = -1;
auto inDragArea = false;
if (in && !_rows.empty()) {
selected = floorclamp(local.y() - _itemsTop, _rowHeight, 0, _rows.size() - 1);
local.setY(local.y() - _itemsTop - selected * _rowHeight);
auto &set = _rows[selected];
auto selectedIndex = floorclamp(local.y() - _itemsTop, _rowHeight, 0, _rows.size() - 1);
selected = selectedIndex;
local.setY(local.y() - _itemsTop - selectedIndex * _rowHeight);
auto &set = _rows[selectedIndex];
if (!_megagroupSet && (_section == Section::Installed || !set->installed || set->archived || set->removed)) {
auto removeButton = (_section == Section::Installed && !set->removed);
auto rect = myrtlrect(relativeButtonRect(removeButton));
actionSel = rect.contains(local) ? selected : -1;
actionSel = rect.contains(local) ? selectedIndex : -1;
} else {
actionSel = -1;
}
@@ -1016,20 +1061,20 @@ void StickersBox::Inner::onUpdateSelected() {
auto dragArea = myrtlrect(0, 0, dragAreaWidth, _rowHeight);
inDragArea = dragArea.contains(local);
}
} else {
selected = -1;
}
if (_selected != selected) {
if ((_megagroupSet || _section != Section::Installed) && ((_selected >= 0 || _pressed >= 0) != (selected >= 0 || _pressed >= 0))) {
if (!inDragArea) {
setCursor((selected >= 0 || _pressed >= 0) ? style::cur_pointer : style::cur_default);
}
} else if (_megagroupSelectedSet) {
auto setTop = _megagroupDivider->y() - _rowHeight;
if (QRect(0, setTop, width(), _rowHeight).contains(local)) {
selected = MegagroupSet();
}
setSelected(selected);
}
setSelected(selected);
if (_inDragArea != inDragArea) {
_inDragArea = inDragArea;
setCursor(_inDragArea ? style::cur_sizeall : (_selected >= 0 || _pressed >= 0) ? style::cur_pointer : style::cur_default);
setCursor(_inDragArea
? style::cur_sizeall
: ((_selected.has_value() || _pressed.has_value())
? style::cur_pointer
: style::cur_default));
}
setActionSel(actionSel);
emit draggingScrollDelta(0);
@@ -1045,9 +1090,9 @@ float64 StickersBox::Inner::aboveShadowOpacity() const {
}
void StickersBox::Inner::mouseReleaseEvent(QMouseEvent *e) {
auto pressed = std::exchange(_pressed, -1);
auto pressed = std::exchange(_pressed, SelectedRow());
if (_section != Section::Installed && _selected < 0 && pressed >= 0) {
if (_section != Section::Installed && !_selected.has_value() && pressed.has_value()) {
setCursor(style::cur_default);
}
@@ -1070,22 +1115,43 @@ void StickersBox::Inner::mouseReleaseEvent(QMouseEvent *e) {
_dragging = _started = -1;
} else if (pressed == _selected && _actionSel < 0 && _actionDown < 0) {
if (_selected >= 0 && !_inDragArea) {
auto &sets = Global::RefStickerSets();
auto &row = _rows[pressed];
if (!row->isRecentSet()) {
auto it = sets.find(row->id);
auto selectedIndex = [&] {
if (auto index = base::get_if<int>(&_selected)) {
return *index;
}
return -1;
}();
auto getSetByRow = [&](const Row &row) -> const Stickers::Set* {
auto &sets = Auth().data().stickerSetsRef();
if (!row.isRecentSet()) {
auto it = sets.find(row.id);
if (it != sets.cend()) {
if (_megagroupSet) {
setMegagroupSelectedSet(MTP_inputStickerSetID(MTP_long(it->id), MTP_long(it->access)));
} else {
setSelected(-1);
Ui::show(
Box<StickerSetBox>(Stickers::inputSetId(*it)),
LayerOption::KeepOther);
}
return &*it;
}
}
return nullptr;
};
auto showSetByRow = [&](const Row &row) {
if (auto set = getSetByRow(row)) {
setSelected(SelectedRow());
Ui::show(
Box<StickerSetBox>(Stickers::inputSetId(*set)),
LayerOption::KeepOther);
}
};
if (selectedIndex >= 0 && !_inDragArea) {
auto &row = *_rows[selectedIndex];
if (_megagroupSet) {
if (auto set = getSetByRow(row)) {
setMegagroupSelectedSet(MTP_inputStickerSetID(
MTP_long(set->id),
MTP_long(set->access)));
}
} else {
showSetByRow(row);
}
} else if (_megagroupSelectedSet && _selected.is<MegagroupSet>()) {
showSetByRow(*_megagroupSelectedSet);
}
}
setActionDown(-1);
@@ -1177,8 +1243,8 @@ void StickersBox::Inner::clear() {
_aboveShadowFadeOpacity = anim::value();
_a_shifting.stop();
_above = _dragging = _started = -1;
setSelected(-1);
setPressed(-1);
setSelected(SelectedRow());
setPressed(SelectedRow());
setActionSel(-1);
setActionDown(-1);
update();
@@ -1195,12 +1261,39 @@ void StickersBox::Inner::setActionSel(int32 actionSel) {
}
}
void StickersBox::Inner::AddressField::correctValue(
const QString &was,
int wasCursor,
QString &now,
int &nowCursor) {
auto newText = now;
auto newCursor = nowCursor;
auto removeFromBeginning = {
qstr("http://"),
qstr("https://"),
qstr("www.t.me/"),
qstr("www.telegram.me/"),
qstr("www.telegram.dog/"),
qstr("t.me/"),
qstr("telegram.me/"),
qstr("telegram.dog/"),
qstr("addstickers/"),
};
for (auto &removePhrase : removeFromBeginning) {
if (newText.startsWith(removePhrase)) {
newText = newText.mid(removePhrase.size());
newCursor = newText.size();
}
}
setCorrectedText(now, nowCursor, newText, newCursor);
}
void StickersBox::Inner::handleMegagroupSetAddressChange() {
auto text = _megagroupSetField->getLastText().trimmed();
if (text.isEmpty()) {
if (_megagroupSelectedSet) {
auto it = Global::StickerSets().constFind(_megagroupSelectedSet->id);
if (it != Global::StickerSets().end() && !it->shortName.isEmpty()) {
auto it = Auth().data().stickerSets().constFind(_megagroupSelectedSet->id);
if (it != Auth().data().stickerSets().cend() && !it->shortName.isEmpty()) {
setMegagroupSelectedSet(MTP_inputStickerSetEmpty());
}
}
@@ -1232,7 +1325,7 @@ void StickersBox::Inner::rebuildMegagroupSet() {
}
auto &set = _megagroupSetInput.c_inputStickerSetID();
auto setId = set.vid.v;
auto &sets = Global::StickerSets();
auto &sets = Auth().data().stickerSets();
auto it = sets.find(setId);
if (it == sets.cend() || (it->flags & MTPDstickerSet_ClientFlag::f_not_loaded)) {
Auth().api().scheduleStickerSetRequest(set.vid.v, set.vaccess_hash.v);
@@ -1279,16 +1372,16 @@ void StickersBox::Inner::rebuild() {
clear();
auto &order = ([this]() -> const Stickers::Order & {
if (_section == Section::Installed) {
return Global::StickerSetsOrder();
return Auth().data().stickerSetsOrder();
} else if (_section == Section::Featured) {
return Global::FeaturedStickerSetsOrder();
return Auth().data().featuredStickerSetsOrder();
}
return Global::ArchivedStickerSetsOrder();
return Auth().data().archivedStickerSetsOrder();
})();
_rows.reserve(order.size() + 1);
_animStartTimes.reserve(order.size() + 1);
auto &sets = Global::StickerSets();
auto &sets = Auth().data().stickerSets();
if (!_megagroupSet && _section == Section::Installed) {
auto cloudIt = sets.constFind(Stickers::CloudRecentSetId);
if (cloudIt != sets.cend() && !cloudIt->stickers.isEmpty()) {
@@ -1331,7 +1424,7 @@ void StickersBox::Inner::updateSize(int newWidth) {
void StickersBox::Inner::updateRows() {
int maxNameWidth = countMaxNameWidth();
auto &sets = Global::StickerSets();
auto &sets = Auth().data().stickerSets();
for_const (auto &row, _rows) {
auto it = sets.constFind(row->id);
if (it != sets.cend()) {
@@ -1376,9 +1469,14 @@ bool StickersBox::Inner::appendSet(const Stickers::Set &set) {
int StickersBox::Inner::countMaxNameWidth() const {
int namex = st::contactsPadding.left() + st::contactsPhotoSize + st::contactsPadding.left();
if (!_megagroupSet && _section == Section::Installed) {
namex += st::stickersReorderIcon.width() + st::stickersReorderSkip;
}
int namew = st::boxWideWidth - namex - st::contactsPadding.right() - st::contactsCheckPosition.x();
if (_section == Section::Installed) {
namew -= _undoWidth - st::stickersUndoRemove.width;
if (!_megagroupSet) {
namew -= _undoWidth - st::stickersUndoRemove.width;
}
} else {
namew -= _addWidth - st::stickersTrendingAdd.width;
if (_section == Section::Featured) {
@@ -1438,8 +1536,8 @@ void StickersBox::Inner::fillSetCover(const Stickers::Set &set, DocumentData **o
int StickersBox::Inner::fillSetCount(const Stickers::Set &set) const {
int result = set.stickers.isEmpty() ? set.count : set.stickers.size(), added = 0;
if (set.id == Stickers::CloudRecentSetId) {
auto customIt = Global::StickerSets().constFind(Stickers::CustomSetId);
if (customIt != Global::StickerSets().cend()) {
auto customIt = Auth().data().stickerSets().constFind(Stickers::CustomSetId);
if (customIt != Auth().data().stickerSets().cend()) {
added = customIt->stickers.size();
for_const (auto &sticker, cGetRecentStickers()) {
if (customIt->stickers.indexOf(sticker.first) < 0) {
@@ -1572,7 +1670,7 @@ void StickersBox::Inner::readVisibleSets() {
continue;
}
if (!_rows[i]->sticker || _rows[i]->sticker->thumb->loaded() || _rows[i]->sticker->loaded()) {
Stickers::MarkFeaturedAsRead(_rows[i]->id);
Auth().api().readFeaturedSetDelayed(_rows[i]->id);
}
}
}