From 73bb1382b17967d735f693c19419e1463afd92d3 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 23 Jul 2021 02:11:27 +0300 Subject: [PATCH] Replaced MatrixRowShift with layout utils. --- .../chat_helpers/emoji_list_widget.cpp | 37 ++++++++++--------- Telegram/SourceFiles/core/utils.h | 2 - .../history/history_item_components.cpp | 9 ++--- Telegram/SourceFiles/layout/layout_utils.cpp | 11 ++++-- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp index c7da2d5895..14ccbcfdd3 100644 --- a/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/emoji_list_widget.cpp @@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/ui_utility.h" #include "ui/cached_round_corners.h" #include "lang/lang_keys.h" +#include "layout/layout_utils.h" #include "emoji_suggestions_data.h" #include "emoji_suggestions_helper.h" #include "main/main_session.h" @@ -553,7 +554,12 @@ void EmojiListWidget::paintEvent(QPaintEvent *e) { auto index = i * _columnCount + j; if (index >= info.count) break; - auto selected = (!_picker->isHidden() && info.section * MatrixRowShift + index == _pickerSel) || (info.section * MatrixRowShift + index == _selected); + const auto selectedIndex = Layout::PositionToIndex( + info.section, + index); + auto selected = (selectedIndex == _selected) + || (!_picker->isHidden() + && selectedIndex == _pickerSel); auto w = QPoint(_rowsLeft + j * _singleSize.width(), info.rowsTop + i * _singleSize.height()); if (selected) { @@ -593,8 +599,7 @@ void EmojiListWidget::mousePressEvent(QMouseEvent *e) { _pressedSel = _selected; if (_selected >= 0) { - auto section = (_selected / MatrixRowShift); - auto sel = _selected % MatrixRowShift; + const auto &[section, sel] = Layout::IndexToPosition(_selected); if (section < kEmojiSectionCount && sel < _emoji[section].size() && _emoji[section][sel]->hasVariants()) { @@ -619,8 +624,7 @@ void EmojiListWidget::mouseReleaseEvent(QMouseEvent *e) { if (_picker->rect().contains(_picker->mapFromGlobal(_lastMousePos))) { return _picker->handleMouseRelease(QCursor::pos()); } else if (_pickerSel >= 0) { - auto section = (_pickerSel / MatrixRowShift); - auto sel = _pickerSel % MatrixRowShift; + const auto &[section, sel] = Layout::IndexToPosition(_pickerSel); if (section < kEmojiSectionCount && sel < _emoji[section].size() && _emoji[section][sel]->hasVariants()) { @@ -642,12 +646,11 @@ void EmojiListWidget::mouseReleaseEvent(QMouseEvent *e) { if (_selected < 0 || _selected != pressed) return; - if (_selected >= kEmojiSectionCount * MatrixRowShift) { + if (_selected >= Layout::PositionToIndex(kEmojiSectionCount, 0)) { return; } - auto section = (_selected / MatrixRowShift); - auto sel = _selected % MatrixRowShift; + const auto &[section, sel] = Layout::IndexToPosition(_selected); if (sel < _emoji[section].size()) { auto emoji = _emoji[section][sel]; if (emoji->hasVariants() && !_picker->isHidden()) return; @@ -664,8 +667,7 @@ void EmojiListWidget::selectEmoji(EmojiPtr emoji) { void EmojiListWidget::showPicker() { if (_pickerSel < 0) return; - auto section = (_pickerSel / MatrixRowShift); - auto sel = _pickerSel % MatrixRowShift; + const auto &[section, sel] = Layout::IndexToPosition(_pickerSel); if (section < kEmojiSectionCount && sel < _emoji[section].size() && _emoji[section][sel]->hasVariants()) { _picker->showEmoji(_emoji[section][sel]); @@ -708,8 +710,7 @@ void EmojiListWidget::colorChosen(EmojiPtr emoji) { Core::App().settings().saveEmojiVariant(emoji); } if (_pickerSel >= 0) { - auto section = (_pickerSel / MatrixRowShift); - auto sel = _pickerSel % MatrixRowShift; + const auto &[section, sel] = Layout::IndexToPosition(_pickerSel); if (section >= 0 && section < kEmojiSectionCount) { _emoji[section][sel] = emoji; rtlupdate(emojiRect(section, sel)); @@ -756,8 +757,7 @@ Ui::Emoji::Section EmojiListWidget::currentSection(int yOffset) const { QString EmojiListWidget::tooltipText() const { const auto &replacements = Ui::Emoji::internal::GetAllReplacements(); - const auto section = (_selected / MatrixRowShift); - const auto sel = _selected % MatrixRowShift; + const auto &[section, sel] = Layout::IndexToPosition(_selected); if (_selected >= 0 && section < kEmojiSectionCount && sel < _emoji[section].size()) { const auto emoji = _emoji[section][sel]->original(); const auto text = emoji->text(); @@ -822,7 +822,7 @@ void EmojiListWidget::updateSelected() { if (newSelected >= _emoji[info.section].size()) { newSelected = -1; } else { - newSelected += info.section * MatrixRowShift; + newSelected += Layout::PositionToIndex(info.section, 0); } } } @@ -835,8 +835,11 @@ void EmojiListWidget::setSelected(int newSelected) { return; } auto updateSelected = [this]() { - if (_selected < 0) return; - rtlupdate(emojiRect(_selected / MatrixRowShift, _selected % MatrixRowShift)); + if (_selected < 0) { + return; + } + const auto &[section, sel] = Layout::IndexToPosition(_selected); + rtlupdate(emojiRect(section, sel)); }; updateSelected(); _selected = newSelected; diff --git a/Telegram/SourceFiles/core/utils.h b/Telegram/SourceFiles/core/utils.h index 3f932644ea..2d73b601f2 100644 --- a/Telegram/SourceFiles/core/utils.h +++ b/Telegram/SourceFiles/core/utils.h @@ -121,8 +121,6 @@ void memset_rand(void *data, uint32 len); QString translitRusEng(const QString &rus); QString rusKeyboardLayoutSwitch(const QString &from); -static const int MatrixRowShift = 40000; - inline int rowscount(int fullCount, int countPerRow) { return (fullCount + countPerRow - 1) / countPerRow; } diff --git a/Telegram/SourceFiles/history/history_item_components.cpp b/Telegram/SourceFiles/history/history_item_components.cpp index 0e758b4f8a..d88725bf20 100644 --- a/Telegram/SourceFiles/history/history_item_components.cpp +++ b/Telegram/SourceFiles/history/history_item_components.cpp @@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/history_view_service_message.h" #include "history/view/media/history_view_document.h" #include "core/click_handler_types.h" +#include "layout/layout_utils.h" #include "mainwindow.h" #include "media/audio/media_audio.h" #include "media/player/media_player_instance.h" @@ -723,7 +724,7 @@ void ReplyKeyboard::clickHandlerPressedChanged( void ReplyKeyboard::startAnimation(int i, int j, int direction) { auto notStarted = _animations.empty(); - int indexForAnimation = (i * MatrixRowShift + j + 1) * direction; + int indexForAnimation = Layout::PositionToIndex(i, j + 1) * direction; _animations.remove(-indexForAnimation); if (!_animations.contains(indexForAnimation)) { @@ -741,8 +742,7 @@ bool ReplyKeyboard::selectedAnimationCallback(crl::time now) { } for (auto i = _animations.begin(); i != _animations.end();) { const auto index = std::abs(i->first) - 1; - const auto row = (index / MatrixRowShift); - const auto col = index % MatrixRowShift; + const auto &[row, col] = Layout::IndexToPosition(index); const auto dt = float64(now - i->second) / st::botKbDuration; if (dt >= 1) { _rows[row][col].howMuchOver = (i->first > 0) ? 1 : 0; @@ -759,8 +759,7 @@ bool ReplyKeyboard::selectedAnimationCallback(crl::time now) { void ReplyKeyboard::clearSelection() { for (const auto &[relativeIndex, time] : _animations) { const auto index = std::abs(relativeIndex) - 1; - const auto row = (index / MatrixRowShift); - const auto col = index % MatrixRowShift; + const auto &[row, col] = Layout::IndexToPosition(index); _rows[row][col].howMuchOver = 0; } _animations.clear(); diff --git a/Telegram/SourceFiles/layout/layout_utils.cpp b/Telegram/SourceFiles/layout/layout_utils.cpp index 5dfe6e645c..f82571aebb 100644 --- a/Telegram/SourceFiles/layout/layout_utils.cpp +++ b/Telegram/SourceFiles/layout/layout_utils.cpp @@ -8,15 +8,20 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "layout/layout_utils.h" namespace Layout { +namespace { + +constexpr auto kMatrixRowShift = 40000; + +} // namespace Layout::Position IndexToPosition(int index) { return { - (index >= 0) ? (index / MatrixRowShift) : -1, - (index >= 0) ? (index % MatrixRowShift) : -1 }; + (index >= 0) ? (index / kMatrixRowShift) : -1, + (index >= 0) ? (index % kMatrixRowShift) : -1 }; } int PositionToIndex(int row, int column) { - return row * MatrixRowShift + column; + return row * kMatrixRowShift + column; } int PositionToIndex(const Layout::Position &position) {