2017-07-19 18:37:49 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-07-19 18:37:49 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-07-19 18:37:49 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2019-04-01 11:16:16 +00:00
|
|
|
#include "ui/effects/animations.h"
|
2019-03-30 10:00:31 +00:00
|
|
|
#include "ui/rp_widget.h"
|
2018-11-21 10:09:46 +00:00
|
|
|
#include "base/unique_qptr.h"
|
2019-03-30 08:06:15 +00:00
|
|
|
#include "base/timer.h"
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtWidgets/QTextEdit>
|
|
|
|
|
2019-08-02 10:40:35 +00:00
|
|
|
namespace Main {
|
|
|
|
class Session;
|
|
|
|
} // namespace Main
|
|
|
|
|
2017-07-22 08:35:18 +00:00
|
|
|
namespace Ui {
|
|
|
|
class InnerDropdown;
|
2018-11-21 10:09:46 +00:00
|
|
|
class InputField;
|
2022-07-22 11:39:28 +00:00
|
|
|
} // namespace Ui
|
|
|
|
|
2022-07-25 08:30:38 +00:00
|
|
|
namespace Ui::Text {
|
|
|
|
class CustomEmoji;
|
|
|
|
} // namespace Ui::Text
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2022-07-22 11:39:28 +00:00
|
|
|
namespace Ui::Emoji {
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2022-08-02 14:57:59 +00:00
|
|
|
using SuggestionsQuery = std::variant<QString, EmojiPtr>;
|
|
|
|
|
2019-03-30 10:00:31 +00:00
|
|
|
class SuggestionsWidget final : public Ui::RpWidget {
|
2017-07-22 08:35:18 +00:00
|
|
|
public:
|
2022-08-02 16:23:06 +00:00
|
|
|
SuggestionsWidget(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
bool suggestCustomEmoji,
|
|
|
|
Fn<bool(not_null<DocumentData*>)> allowCustomWithoutPremium);
|
2022-07-18 17:30:28 +00:00
|
|
|
~SuggestionsWidget();
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2022-08-02 14:57:59 +00:00
|
|
|
void showWithQuery(SuggestionsQuery query, bool force = false);
|
2019-03-30 10:00:31 +00:00
|
|
|
void selectFirstResult();
|
|
|
|
bool handleKeyEvent(int key);
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2022-07-18 17:30:28 +00:00
|
|
|
[[nodiscard]] rpl::producer<bool> toggleAnimated() const;
|
|
|
|
|
|
|
|
struct Chosen {
|
|
|
|
QString emoji;
|
|
|
|
QString customData;
|
|
|
|
};
|
|
|
|
[[nodiscard]] rpl::producer<Chosen> triggered() const;
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2019-03-30 10:00:31 +00:00
|
|
|
private:
|
|
|
|
struct Row {
|
|
|
|
Row(not_null<EmojiPtr> emoji, const QString &replacement);
|
|
|
|
|
2022-07-25 08:30:38 +00:00
|
|
|
Ui::Text::CustomEmoji *custom = nullptr;
|
2022-07-18 17:30:28 +00:00
|
|
|
DocumentData *document = nullptr;
|
2019-03-30 10:00:31 +00:00
|
|
|
not_null<EmojiPtr> emoji;
|
|
|
|
QString replacement;
|
|
|
|
};
|
2022-08-02 14:57:59 +00:00
|
|
|
struct Custom {
|
|
|
|
not_null<DocumentData*> document;
|
|
|
|
not_null<EmojiPtr> emoji;
|
|
|
|
QString replacement;
|
|
|
|
};
|
2019-03-30 10:00:31 +00:00
|
|
|
|
|
|
|
bool eventHook(QEvent *e) override;
|
2017-07-22 08:35:18 +00:00
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
void keyPressEvent(QKeyEvent *e) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *e) override;
|
|
|
|
void mousePressEvent(QMouseEvent *e) override;
|
|
|
|
void mouseReleaseEvent(QMouseEvent *e) override;
|
2021-10-19 13:00:21 +00:00
|
|
|
void enterEventHook(QEnterEvent *e) override;
|
2017-07-22 08:35:18 +00:00
|
|
|
void leaveEventHook(QEvent *e) override;
|
|
|
|
|
2019-03-30 10:00:31 +00:00
|
|
|
void scrollByWheelEvent(not_null<QWheelEvent*> e);
|
2022-09-16 20:23:27 +00:00
|
|
|
void paintFadings(QPainter &p) const;
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2022-08-02 14:57:59 +00:00
|
|
|
[[nodiscard]] std::vector<Row> getRowsByQuery(const QString &text) const;
|
|
|
|
[[nodiscard]] base::flat_multi_map<int, Custom> lookupCustom(
|
|
|
|
const std::vector<Row> &rows) const;
|
2022-08-02 16:23:06 +00:00
|
|
|
[[nodiscard]] std::vector<Row> appendCustom(
|
2022-08-02 14:57:59 +00:00
|
|
|
std::vector<Row> rows);
|
2022-08-02 16:23:06 +00:00
|
|
|
[[nodiscard]] std::vector<Row> appendCustom(
|
2022-08-02 14:57:59 +00:00
|
|
|
std::vector<Row> rows,
|
|
|
|
const base::flat_multi_map<int, Custom> &custom);
|
2017-07-22 08:35:18 +00:00
|
|
|
void resizeToRows();
|
2019-04-01 11:16:16 +00:00
|
|
|
void setSelected(
|
|
|
|
int selected,
|
|
|
|
anim::type animated = anim::type::instant);
|
2017-07-22 08:35:18 +00:00
|
|
|
void setPressed(int pressed);
|
|
|
|
void clearMouseSelection();
|
|
|
|
void clearSelection();
|
|
|
|
void updateSelectedItem();
|
|
|
|
void updateItem(int index);
|
2019-03-30 10:00:31 +00:00
|
|
|
[[nodiscard]] QRect inner() const;
|
|
|
|
[[nodiscard]] QPoint innerShift() const;
|
|
|
|
[[nodiscard]] QPoint mapToInner(QPoint globalPosition) const;
|
2018-12-25 12:41:40 +00:00
|
|
|
void selectByMouse(QPoint globalPosition);
|
2019-03-30 10:00:31 +00:00
|
|
|
bool triggerSelectedRow() const;
|
|
|
|
void triggerRow(const Row &row) const;
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2019-04-01 11:16:16 +00:00
|
|
|
[[nodiscard]] int scrollCurrent() const;
|
|
|
|
void scrollTo(int value, anim::type animated = anim::type::instant);
|
|
|
|
void stopAnimations();
|
|
|
|
|
2022-07-25 08:30:38 +00:00
|
|
|
[[nodiscard]] not_null<Ui::Text::CustomEmoji*> resolveCustomEmoji(
|
2022-07-18 17:30:28 +00:00
|
|
|
not_null<DocumentData*> document);
|
2022-07-22 11:39:28 +00:00
|
|
|
void customEmojiRepaint();
|
2022-07-18 17:30:28 +00:00
|
|
|
|
|
|
|
const not_null<Main::Session*> _session;
|
2022-08-02 14:57:59 +00:00
|
|
|
SuggestionsQuery _query;
|
2017-07-22 08:35:18 +00:00
|
|
|
std::vector<Row> _rows;
|
2022-08-02 16:23:06 +00:00
|
|
|
bool _suggestCustomEmoji = false;
|
|
|
|
Fn<bool(not_null<DocumentData*>)> _allowCustomWithoutPremium;
|
2022-07-22 11:39:28 +00:00
|
|
|
|
2022-07-18 17:30:28 +00:00
|
|
|
base::flat_map<
|
|
|
|
not_null<DocumentData*>,
|
2022-07-25 08:30:38 +00:00
|
|
|
std::unique_ptr<Ui::Text::CustomEmoji>> _customEmoji;
|
2022-07-22 11:39:28 +00:00
|
|
|
bool _repaintScheduled = false;
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2018-12-25 12:41:40 +00:00
|
|
|
std::optional<QPoint> _lastMousePosition;
|
2017-07-22 08:35:18 +00:00
|
|
|
bool _mouseSelection = false;
|
|
|
|
int _selected = -1;
|
|
|
|
int _pressed = -1;
|
|
|
|
|
2019-04-01 11:16:16 +00:00
|
|
|
int _scrollValue = 0;
|
|
|
|
Ui::Animations::Simple _scrollAnimation;
|
|
|
|
Ui::Animations::Simple _selectedAnimation;
|
2019-03-30 10:00:31 +00:00
|
|
|
int _scrollMax = 0;
|
|
|
|
int _oneWidth = 0;
|
|
|
|
QMargins _padding;
|
|
|
|
|
2019-04-01 10:35:32 +00:00
|
|
|
QPoint _mousePressPosition;
|
|
|
|
int _dragScrollStart = -1;
|
|
|
|
|
2018-11-21 10:09:46 +00:00
|
|
|
rpl::event_stream<bool> _toggleAnimated;
|
2022-07-18 17:30:28 +00:00
|
|
|
rpl::event_stream<Chosen> _triggered;
|
2018-11-21 10:09:46 +00:00
|
|
|
|
2017-07-22 08:35:18 +00:00
|
|
|
};
|
|
|
|
|
2018-11-21 10:09:46 +00:00
|
|
|
class SuggestionsController {
|
2017-07-22 08:35:18 +00:00
|
|
|
public:
|
2019-05-27 10:16:14 +00:00
|
|
|
struct Options {
|
2022-08-02 16:23:06 +00:00
|
|
|
bool suggestExactFirstWord = true;
|
|
|
|
bool suggestCustomEmoji = false;
|
|
|
|
Fn<bool(not_null<DocumentData*>)> allowCustomWithoutPremium;
|
2019-05-27 10:16:14 +00:00
|
|
|
};
|
|
|
|
|
2018-11-21 10:09:46 +00:00
|
|
|
SuggestionsController(
|
|
|
|
not_null<QWidget*> outer,
|
2019-05-27 10:16:14 +00:00
|
|
|
not_null<QTextEdit*> field,
|
2019-08-02 10:40:35 +00:00
|
|
|
not_null<Main::Session*> session,
|
2019-05-27 10:16:14 +00:00
|
|
|
const Options &options);
|
2017-07-22 08:35:18 +00:00
|
|
|
|
|
|
|
void raise();
|
2018-06-04 15:35:11 +00:00
|
|
|
void setReplaceCallback(Fn<void(
|
2018-05-13 15:56:08 +00:00
|
|
|
int from,
|
|
|
|
int till,
|
2022-07-18 17:30:28 +00:00
|
|
|
const QString &replacement,
|
|
|
|
const QString &customEmojiData)> callback);
|
2017-07-22 08:35:18 +00:00
|
|
|
|
2022-08-03 17:41:01 +00:00
|
|
|
static not_null<SuggestionsController*> Init(
|
|
|
|
not_null<QWidget*> outer,
|
|
|
|
not_null<Ui::InputField*> field,
|
|
|
|
not_null<Main::Session*> session) {
|
|
|
|
return Init(outer, field, session, {});
|
|
|
|
}
|
2022-08-02 16:23:06 +00:00
|
|
|
static not_null<SuggestionsController*> Init(
|
2018-11-21 10:09:46 +00:00
|
|
|
not_null<QWidget*> outer,
|
2019-05-27 10:16:14 +00:00
|
|
|
not_null<Ui::InputField*> field,
|
2019-08-02 10:40:35 +00:00
|
|
|
not_null<Main::Session*> session,
|
2022-08-03 17:41:01 +00:00
|
|
|
const Options &options);
|
2017-07-22 08:35:18 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void handleCursorPositionChange();
|
|
|
|
void handleTextChange();
|
2022-08-02 14:57:59 +00:00
|
|
|
void showWithQuery(SuggestionsQuery query);
|
|
|
|
[[nodiscard]] SuggestionsQuery getEmojiQuery();
|
2017-07-22 08:35:18 +00:00
|
|
|
void suggestionsUpdated(bool visible);
|
|
|
|
void updateGeometry();
|
|
|
|
void updateForceHidden();
|
2022-07-18 17:30:28 +00:00
|
|
|
void replaceCurrent(
|
|
|
|
const QString &replacement,
|
|
|
|
const QString &customEmojiData);
|
2018-11-21 10:09:46 +00:00
|
|
|
bool fieldFilter(not_null<QEvent*> event);
|
|
|
|
bool outerFilter(not_null<QEvent*> event);
|
2017-07-22 08:35:18 +00:00
|
|
|
|
|
|
|
bool _shown = false;
|
|
|
|
bool _forceHidden = false;
|
|
|
|
int _queryStartPosition = 0;
|
2022-08-08 12:24:11 +00:00
|
|
|
int _emojiQueryLength = 0;
|
2017-07-22 08:35:18 +00:00
|
|
|
bool _ignoreCursorPositionChange = false;
|
|
|
|
bool _textChangeAfterKeyPress = false;
|
|
|
|
QPointer<QTextEdit> _field;
|
2019-08-02 10:40:35 +00:00
|
|
|
const not_null<Main::Session*> _session;
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(
|
2018-05-13 15:56:08 +00:00
|
|
|
int from,
|
|
|
|
int till,
|
2022-07-18 17:30:28 +00:00
|
|
|
const QString &replacement,
|
|
|
|
const QString &customEmojiData)> _replaceCallback;
|
2018-11-21 10:09:46 +00:00
|
|
|
base::unique_qptr<InnerDropdown> _container;
|
2017-07-22 08:35:18 +00:00
|
|
|
QPointer<SuggestionsWidget> _suggestions;
|
2018-11-21 10:09:46 +00:00
|
|
|
base::unique_qptr<QObject> _fieldFilter;
|
|
|
|
base::unique_qptr<QObject> _outerFilter;
|
2019-03-30 08:06:15 +00:00
|
|
|
base::Timer _showExactTimer;
|
|
|
|
bool _keywordsRefreshed = false;
|
2022-08-02 14:57:59 +00:00
|
|
|
SuggestionsQuery _lastShownQuery;
|
2019-05-27 10:16:14 +00:00
|
|
|
Options _options;
|
2018-11-21 10:09:46 +00:00
|
|
|
|
|
|
|
rpl::lifetime _lifetime;
|
2017-07-22 08:35:18 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-07-22 11:39:28 +00:00
|
|
|
} // namespace Ui::Emoji
|