2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-25 20:07:13 +00:00

194 lines
4.9 KiB
C
Raw Normal View History

/*
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
*/
#pragma once
#include "ui/effects/animations.h"
2019-03-30 14:00:31 +04:00
#include "ui/rp_widget.h"
2018-11-21 14:09:46 +04:00
#include "base/unique_qptr.h"
#include "base/timer.h"
2019-09-04 10:19:15 +03:00
#include <QtWidgets/QTextEdit>
namespace Main {
class Session;
} // namespace Main
namespace Ui {
class InnerDropdown;
2018-11-21 14:09:46 +04:00
class InputField;
2022-07-22 14:39:28 +03:00
} // namespace Ui
2022-07-25 11:30:38 +03:00
namespace Ui::Text {
class CustomEmoji;
} // namespace Ui::Text
2022-07-22 14:39:28 +03:00
namespace Ui::Emoji {
2019-03-30 14:00:31 +04:00
class SuggestionsWidget final : public Ui::RpWidget {
public:
2022-07-18 20:30:28 +03:00
SuggestionsWidget(QWidget *parent, not_null<Main::Session*> session);
~SuggestionsWidget();
2019-03-29 20:04:05 +04:00
void showWithQuery(const QString &query, bool force = false);
2019-03-30 14:00:31 +04:00
void selectFirstResult();
bool handleKeyEvent(int key);
2022-07-18 20:30:28 +03:00
[[nodiscard]] rpl::producer<bool> toggleAnimated() const;
struct Chosen {
QString emoji;
QString customData;
};
[[nodiscard]] rpl::producer<Chosen> triggered() const;
2019-03-30 14:00:31 +04:00
private:
struct Row {
Row(not_null<EmojiPtr> emoji, const QString &replacement);
2022-07-25 11:30:38 +03:00
Ui::Text::CustomEmoji *custom = nullptr;
2022-07-18 20:30:28 +03:00
DocumentData *document = nullptr;
2019-03-30 14:00:31 +04:00
not_null<EmojiPtr> emoji;
QString replacement;
};
bool eventHook(QEvent *e) override;
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;
void enterEventHook(QEnterEvent *e) override;
void leaveEventHook(QEvent *e) override;
2019-03-30 14:00:31 +04:00
void scrollByWheelEvent(not_null<QWheelEvent*> e);
void paintFadings(Painter &p) const;
2022-07-18 20:30:28 +03:00
[[nodiscard]] std::vector<Row> getRowsByQuery() const;
[[nodiscard]] std::vector<Row> prependCustom(std::vector<Row> rows);
void resizeToRows();
void setSelected(
int selected,
anim::type animated = anim::type::instant);
void setPressed(int pressed);
void clearMouseSelection();
void clearSelection();
void updateSelectedItem();
void updateItem(int index);
2019-03-30 14:00:31 +04:00
[[nodiscard]] QRect inner() const;
[[nodiscard]] QPoint innerShift() const;
[[nodiscard]] QPoint mapToInner(QPoint globalPosition) const;
void selectByMouse(QPoint globalPosition);
2019-03-30 14:00:31 +04:00
bool triggerSelectedRow() const;
void triggerRow(const Row &row) const;
[[nodiscard]] int scrollCurrent() const;
void scrollTo(int value, anim::type animated = anim::type::instant);
void stopAnimations();
2022-07-25 11:30:38 +03:00
[[nodiscard]] not_null<Ui::Text::CustomEmoji*> resolveCustomEmoji(
2022-07-18 20:30:28 +03:00
not_null<DocumentData*> document);
2022-07-22 14:39:28 +03:00
void customEmojiRepaint();
2022-07-18 20:30:28 +03:00
const not_null<Main::Session*> _session;
QString _query;
std::vector<Row> _rows;
2022-07-22 14:39:28 +03:00
2022-07-18 20:30:28 +03:00
base::flat_map<
not_null<DocumentData*>,
2022-07-25 11:30:38 +03:00
std::unique_ptr<Ui::Text::CustomEmoji>> _customEmoji;
2022-07-22 14:39:28 +03:00
bool _repaintScheduled = false;
std::optional<QPoint> _lastMousePosition;
bool _mouseSelection = false;
int _selected = -1;
int _pressed = -1;
int _scrollValue = 0;
Ui::Animations::Simple _scrollAnimation;
Ui::Animations::Simple _selectedAnimation;
2019-03-30 14:00:31 +04:00
int _scrollMax = 0;
int _oneWidth = 0;
QMargins _padding;
2019-04-01 14:35:32 +04:00
QPoint _mousePressPosition;
int _dragScrollStart = -1;
2018-11-21 14:09:46 +04:00
rpl::event_stream<bool> _toggleAnimated;
2022-07-18 20:30:28 +03:00
rpl::event_stream<Chosen> _triggered;
2018-11-21 14:09:46 +04:00
};
2018-11-21 14:09:46 +04:00
class SuggestionsController {
public:
struct Options {
2019-05-27 15:56:33 +02:00
Options() : suggestExactFirstWord(true) {
}
bool suggestExactFirstWord;
};
2018-11-21 14:09:46 +04:00
SuggestionsController(
not_null<QWidget*> outer,
not_null<QTextEdit*> field,
not_null<Main::Session*> session,
const Options &options);
void raise();
void setReplaceCallback(Fn<void(
int from,
int till,
2022-07-18 20:30:28 +03:00
const QString &replacement,
const QString &customEmojiData)> callback);
2018-11-21 14:09:46 +04:00
static SuggestionsController *Init(
not_null<QWidget*> outer,
not_null<Ui::InputField*> field,
not_null<Main::Session*> session,
const Options &options = Options());
private:
void handleCursorPositionChange();
void handleTextChange();
void showWithQuery(const QString &query);
2019-03-30 14:00:31 +04:00
[[nodiscard]] QString getEmojiQuery();
void suggestionsUpdated(bool visible);
void updateGeometry();
void updateForceHidden();
2022-07-18 20:30:28 +03:00
void replaceCurrent(
const QString &replacement,
const QString &customEmojiData);
2018-11-21 14:09:46 +04:00
bool fieldFilter(not_null<QEvent*> event);
bool outerFilter(not_null<QEvent*> event);
bool _shown = false;
bool _forceHidden = false;
int _queryStartPosition = 0;
bool _ignoreCursorPositionChange = false;
bool _textChangeAfterKeyPress = false;
QPointer<QTextEdit> _field;
const not_null<Main::Session*> _session;
Fn<void(
int from,
int till,
2022-07-18 20:30:28 +03:00
const QString &replacement,
const QString &customEmojiData)> _replaceCallback;
2018-11-21 14:09:46 +04:00
base::unique_qptr<InnerDropdown> _container;
QPointer<SuggestionsWidget> _suggestions;
2018-11-21 14:09:46 +04:00
base::unique_qptr<QObject> _fieldFilter;
base::unique_qptr<QObject> _outerFilter;
base::Timer _showExactTimer;
bool _keywordsRefreshed = false;
QString _lastShownQuery;
Options _options;
2018-11-21 14:09:46 +04:00
rpl::lifetime _lifetime;
};
2022-07-22 14:39:28 +03:00
} // namespace Ui::Emoji