2016-04-12 21:31:28 +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.
|
2016-04-12 21:31:28 +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
|
2016-04-12 21:31:28 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
class ClickHandler;
|
2017-12-18 09:07:18 +00:00
|
|
|
using ClickHandlerPtr = std::shared_ptr<ClickHandler>;
|
2016-04-12 21:31:28 +00:00
|
|
|
|
2016-04-29 12:00:48 +00:00
|
|
|
enum ExpandLinksMode {
|
|
|
|
ExpandLinksNone,
|
|
|
|
ExpandLinksShortened,
|
|
|
|
ExpandLinksAll,
|
2016-05-31 19:27:11 +00:00
|
|
|
ExpandLinksUrlOnly, // For custom urls leaves only url instead of text.
|
2016-04-29 12:00:48 +00:00
|
|
|
};
|
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
class ClickHandlerHost {
|
|
|
|
protected:
|
|
|
|
virtual void clickHandlerActiveChanged(const ClickHandlerPtr &action, bool active) {
|
|
|
|
}
|
|
|
|
virtual void clickHandlerPressedChanged(const ClickHandlerPtr &action, bool pressed) {
|
|
|
|
}
|
|
|
|
virtual ~ClickHandlerHost() = 0;
|
|
|
|
friend class ClickHandler;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-04-29 12:00:48 +00:00
|
|
|
class EntityInText;
|
2016-05-06 17:33:48 +00:00
|
|
|
struct TextWithEntities;
|
2016-04-12 21:31:28 +00:00
|
|
|
class ClickHandler {
|
|
|
|
public:
|
2016-04-29 12:00:48 +00:00
|
|
|
virtual ~ClickHandler() {
|
|
|
|
}
|
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
virtual void onClick(Qt::MouseButton) const = 0;
|
|
|
|
|
2016-04-29 12:00:48 +00:00
|
|
|
// What text to show in a tooltip when mouse is over that click handler as a link in Text.
|
2016-04-12 21:31:28 +00:00
|
|
|
virtual QString tooltip() const {
|
|
|
|
return QString();
|
|
|
|
}
|
2016-04-29 12:00:48 +00:00
|
|
|
|
|
|
|
// What to drop in the input fields when dragging that click handler as a link from Text.
|
|
|
|
virtual QString dragText() const {
|
2016-04-12 21:31:28 +00:00
|
|
|
return QString();
|
|
|
|
}
|
2016-04-29 12:00:48 +00:00
|
|
|
|
|
|
|
// Copy to clipboard support.
|
2018-01-25 10:10:52 +00:00
|
|
|
virtual QString copyToClipboardText() const {
|
|
|
|
return QString();
|
2016-04-12 21:31:28 +00:00
|
|
|
}
|
2016-04-29 12:00:48 +00:00
|
|
|
virtual QString copyToClipboardContextItemText() const {
|
|
|
|
return QString();
|
2016-04-12 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 12:00:48 +00:00
|
|
|
// Entities in text support.
|
|
|
|
|
|
|
|
// This method returns empty string if just textPart should be used (nothing to expand).
|
|
|
|
virtual QString getExpandedLinkText(ExpandLinksMode mode, const QStringRef &textPart) const;
|
2016-05-06 17:33:48 +00:00
|
|
|
virtual TextWithEntities getExpandedLinkTextWithEntities(ExpandLinksMode mode, int entityOffset, const QStringRef &textPart) const;
|
2016-04-12 21:31:28 +00:00
|
|
|
|
2016-04-29 12:00:48 +00:00
|
|
|
// This method should be called on mouse over a click handler.
|
|
|
|
// It returns true if the active handler was changed or false otherwise.
|
2016-04-12 21:31:28 +00:00
|
|
|
static bool setActive(const ClickHandlerPtr &p, ClickHandlerHost *host = nullptr);
|
|
|
|
|
2016-04-29 12:00:48 +00:00
|
|
|
// This method should be called when mouse leaves the host.
|
|
|
|
// It returns true if the active handler was changed or false otherwise.
|
2016-04-12 21:31:28 +00:00
|
|
|
static bool clearActive(ClickHandlerHost *host = nullptr) {
|
|
|
|
if (host && _activeHost != host) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return setActive(ClickHandlerPtr(), host);
|
|
|
|
}
|
|
|
|
|
2016-04-29 12:00:48 +00:00
|
|
|
// This method should be called on mouse press event.
|
2016-04-12 21:31:28 +00:00
|
|
|
static void pressed() {
|
|
|
|
unpressed();
|
|
|
|
if (!_active || !*_active) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-12 19:34:25 +00:00
|
|
|
_pressed.createIfNull();
|
2016-04-12 21:31:28 +00:00
|
|
|
*_pressed = *_active;
|
|
|
|
if ((_pressedHost = _activeHost)) {
|
|
|
|
_pressedHost->clickHandlerPressedChanged(*_pressed, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 12:00:48 +00:00
|
|
|
// This method should be called on mouse release event.
|
|
|
|
// The activated click handler (if any) is returned.
|
2016-04-12 21:31:28 +00:00
|
|
|
static ClickHandlerPtr unpressed() {
|
|
|
|
if (_pressed && *_pressed) {
|
2017-12-18 09:07:18 +00:00
|
|
|
const auto activated = (_active && *_active == *_pressed);
|
|
|
|
const auto waspressed = base::take(*_pressed);
|
2016-04-12 21:31:28 +00:00
|
|
|
if (_pressedHost) {
|
|
|
|
_pressedHost->clickHandlerPressedChanged(waspressed, false);
|
|
|
|
_pressedHost = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activated) {
|
|
|
|
return *_active;
|
|
|
|
} else if (_active && *_active && _activeHost) {
|
|
|
|
// emit clickHandlerActiveChanged for current active
|
|
|
|
// click handler, which we didn't emit while we has
|
|
|
|
// a pressed click handler
|
|
|
|
_activeHost->clickHandlerActiveChanged(*_active, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ClickHandlerPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ClickHandlerPtr getActive() {
|
|
|
|
return _active ? *_active : ClickHandlerPtr();
|
|
|
|
}
|
|
|
|
static ClickHandlerPtr getPressed() {
|
|
|
|
return _pressed ? *_pressed : ClickHandlerPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool showAsActive(const ClickHandlerPtr &p) {
|
|
|
|
if (!p || !_active || p != *_active) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !_pressed || !*_pressed || (p == *_pressed);
|
|
|
|
}
|
|
|
|
static bool showAsPressed(const ClickHandlerPtr &p) {
|
|
|
|
if (!p || !_active || p != *_active) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _pressed && (p == *_pressed);
|
|
|
|
}
|
|
|
|
static void hostDestroyed(ClickHandlerHost *host) {
|
|
|
|
if (_activeHost == host) {
|
2017-12-18 09:07:18 +00:00
|
|
|
if (_active) {
|
|
|
|
*_active = nullptr;
|
|
|
|
}
|
2016-04-12 21:31:28 +00:00
|
|
|
_activeHost = nullptr;
|
2016-04-13 05:55:01 +00:00
|
|
|
}
|
|
|
|
if (_pressedHost == host) {
|
2017-12-18 09:07:18 +00:00
|
|
|
if (_pressed) {
|
|
|
|
*_pressed = nullptr;
|
|
|
|
}
|
2016-04-12 21:31:28 +00:00
|
|
|
_pressedHost = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 17:33:48 +00:00
|
|
|
protected:
|
|
|
|
// For click handlers like mention or hashtag in getExpandedLinkTextWithEntities()
|
|
|
|
// we return just an empty string ("use original string part") with single entity.
|
|
|
|
TextWithEntities simpleTextWithEntity(const EntityInText &entity) const;
|
2016-04-12 21:31:28 +00:00
|
|
|
|
2016-05-06 17:33:48 +00:00
|
|
|
private:
|
2016-04-12 21:31:28 +00:00
|
|
|
static NeverFreedPointer<ClickHandlerPtr> _active;
|
|
|
|
static NeverFreedPointer<ClickHandlerPtr> _pressed;
|
|
|
|
static ClickHandlerHost *_activeHost;
|
|
|
|
static ClickHandlerHost *_pressedHost;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class LeftButtonClickHandler : public ClickHandler {
|
|
|
|
public:
|
|
|
|
void onClick(Qt::MouseButton button) const override final {
|
|
|
|
if (button != Qt::LeftButton) return;
|
|
|
|
onClickImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void onClickImpl() const = 0;
|
|
|
|
|
|
|
|
};
|
2016-11-16 10:44:06 +00:00
|
|
|
|
|
|
|
class LambdaClickHandler : public ClickHandler {
|
|
|
|
public:
|
2017-02-26 11:32:13 +00:00
|
|
|
LambdaClickHandler(base::lambda<void()> handler) : _handler(std::move(handler)) {
|
2016-11-16 10:44:06 +00:00
|
|
|
}
|
|
|
|
void onClick(Qt::MouseButton button) const override final {
|
|
|
|
if (button == Qt::LeftButton && _handler) {
|
|
|
|
_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-20 12:54:07 +00:00
|
|
|
base::lambda<void()> _handler;
|
2016-11-16 10:44:06 +00:00
|
|
|
|
|
|
|
};
|