mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-01-09 16:19:43 +00:00
custom links support done
This commit is contained in:
parent
7bc27ba0bf
commit
96e69c091f
@ -502,6 +502,9 @@ Copyright (c) 2014 John Preston, https://desktop.telegram.org
|
||||
"lng_bot_description" = "What can this bot do?";
|
||||
"lng_unblock_button" = "Unblock";
|
||||
|
||||
"lng_open_this_link" = "Open this link?";
|
||||
"lng_open_link" = "Open";
|
||||
|
||||
"lng_bot_start" = "Start";
|
||||
"lng_bot_choose_group" = "Choose Group";
|
||||
"lng_bot_no_groups" = "You have no groups";
|
||||
|
@ -49,7 +49,7 @@ void ConfirmBox::init(const QString &text) {
|
||||
_text.setText(st::boxFont, text, (_infoMsg ? _confirmBoxTextOptions : _textPlainOptions));
|
||||
|
||||
_textWidth = st::boxWidth - st::boxPadding.left() - st::boxPadding.right();
|
||||
_textHeight = _text.countHeight(_textWidth);
|
||||
_textHeight = qMin(_text.countHeight(_textWidth), 16 * st::boxFont->height);
|
||||
setMaxHeight(st::boxPadding.top() + _textHeight + st::boxPadding.bottom() + (_infoMsg ? _close.height() : _confirm.height()));
|
||||
|
||||
if (_infoMsg) {
|
||||
@ -171,7 +171,7 @@ void ConfirmBox::paintEvent(QPaintEvent *e) {
|
||||
// draw box title / text
|
||||
p.setFont(st::boxFont->f);
|
||||
p.setPen(st::black->p);
|
||||
_text.draw(p, st::boxPadding.left(), st::boxPadding.top(), _textWidth, (_text.maxWidth() < width()) ? style::al_center : style::al_left);
|
||||
_text.drawElided(p, st::boxPadding.left(), st::boxPadding.top(), _textWidth, 16, (_text.maxWidth() < width()) ? style::al_center : style::al_left);
|
||||
}
|
||||
|
||||
void ConfirmBox::resizeEvent(QResizeEvent *e) {
|
||||
@ -182,3 +182,16 @@ void ConfirmBox::resizeEvent(QResizeEvent *e) {
|
||||
_cancel.move(0, st::boxPadding.top() + _textHeight + st::boxPadding.bottom());
|
||||
}
|
||||
}
|
||||
|
||||
ConfirmLinkBox::ConfirmLinkBox(const QString &url) : ConfirmBox(lang(lng_open_this_link) + qsl("\n\n") + url, lang(lng_open_link)), _url(url) {
|
||||
connect(this, SIGNAL(confirmed()), this, SLOT(onOpenLink()));
|
||||
}
|
||||
|
||||
void ConfirmLinkBox::onOpenLink() {
|
||||
if (reMailStart().match(_url).hasMatch()) {
|
||||
EmailLink(_url).onClick(Qt::LeftButton);
|
||||
} else {
|
||||
TextLink(_url).onClick(Qt::LeftButton);
|
||||
}
|
||||
App::wnd()->hideLayer();
|
||||
}
|
||||
|
@ -67,3 +67,20 @@ private:
|
||||
QPoint _lastMousePos;
|
||||
TextLinkPtr _myLink;
|
||||
};
|
||||
|
||||
class ConfirmLinkBox : public ConfirmBox {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
ConfirmLinkBox(const QString &url);
|
||||
|
||||
public slots:
|
||||
|
||||
void onOpenLink();
|
||||
|
||||
private:
|
||||
|
||||
QString _url;
|
||||
|
||||
};
|
||||
|
@ -21,6 +21,8 @@ Copyright (c) 2014 John Preston, https://desktop.telegram.org
|
||||
#include "lang.h"
|
||||
|
||||
#include "pspecific.h"
|
||||
#include "boxes/confirmbox.h"
|
||||
#include "window.h"
|
||||
|
||||
#include <private/qharfbuzz_p.h>
|
||||
|
||||
@ -60,6 +62,10 @@ const QRegularExpression &reMailName() {
|
||||
return _reMailName;
|
||||
}
|
||||
|
||||
const QRegularExpression &reMailStart() {
|
||||
return _reMailStart;
|
||||
}
|
||||
|
||||
const QRegularExpression &reHashtag() {
|
||||
return _reHashtag;
|
||||
}
|
||||
@ -336,9 +342,16 @@ public:
|
||||
|
||||
createBlock();
|
||||
|
||||
QString lnkUrl = QString(start + waitingLink->offset, waitingLink->length), lnkText;
|
||||
int32 fullDisplayed;
|
||||
getLinkData(lnkUrl, lnkText, fullDisplayed);
|
||||
QString lnkUrl, lnkText;
|
||||
if (waitingLink->type == LinkInTextCustomUrl) {
|
||||
lnkUrl = waitingLink->text;
|
||||
lnkText = QString(start + waitingLink->offset, waitingLink->length);
|
||||
fullDisplayed = -5;
|
||||
} else {
|
||||
lnkUrl = QString(start + waitingLink->offset, waitingLink->length);
|
||||
getLinkData(lnkUrl, lnkText, fullDisplayed);
|
||||
}
|
||||
|
||||
links.push_back(TextLinkData(lnkUrl, fullDisplayed));
|
||||
lnkIndex = 0x8000 + links.size();
|
||||
@ -617,7 +630,9 @@ public:
|
||||
_t->_links.resize(lnkIndex);
|
||||
const TextLinkData &data(links[lnkIndex - maxLnkIndex - 1]);
|
||||
TextLinkPtr lnk;
|
||||
if (data.fullDisplayed < -3) { // bot command
|
||||
if (data.fullDisplayed < -4) { // hidden link
|
||||
lnk = TextLinkPtr(new CustomTextLink(data.url));
|
||||
} else if (data.fullDisplayed < -3) { // bot command
|
||||
lnk = TextLinkPtr(new BotCommandLink(data.url));
|
||||
} else if (data.fullDisplayed < -2) { // mention
|
||||
if (options.flags & TextTwitterMentions) {
|
||||
@ -664,7 +679,7 @@ private:
|
||||
TextLinkData(const QString &url = QString(), int32 fullDisplayed = 1) : url(url), fullDisplayed(fullDisplayed) {
|
||||
}
|
||||
QString url;
|
||||
int32 fullDisplayed; // -4 - bot command, -3 - mention, -2 - hashtag, -1 - email
|
||||
int32 fullDisplayed; // -5 - custom text link, -4 - bot command, -3 - mention, -2 - hashtag, -1 - email
|
||||
};
|
||||
typedef QVector<TextLinkData> TextLinks;
|
||||
TextLinks links;
|
||||
@ -820,6 +835,10 @@ void EmailLink::onClick(Qt::MouseButton button) const {
|
||||
}
|
||||
}
|
||||
|
||||
void CustomTextLink::onClick(Qt::MouseButton button) const {
|
||||
App::wnd()->showLayer(new ConfirmLinkBox(text()));
|
||||
}
|
||||
|
||||
void MentionLink::onClick(Qt::MouseButton button) const {
|
||||
if (button == Qt::LeftButton || button == Qt::MiddleButton) {
|
||||
App::openUserByName(_tag.mid(1), true);
|
||||
|
@ -357,6 +357,14 @@ private:
|
||||
|
||||
};
|
||||
|
||||
class CustomTextLink : public TextLink {
|
||||
public:
|
||||
|
||||
CustomTextLink(const QString &url) : TextLink(url, false) {
|
||||
}
|
||||
void onClick(Qt::MouseButton button) const;
|
||||
};
|
||||
|
||||
class EmailLink : public ITextLink {
|
||||
TEXT_LINK_CLASS(EmailLink)
|
||||
|
||||
@ -603,6 +611,7 @@ const QSet<int32> &validProtocols();
|
||||
const QSet<int32> &validTopDomains();
|
||||
const QRegularExpression &reDomain();
|
||||
const QRegularExpression &reMailName();
|
||||
const QRegularExpression &reMailStart();
|
||||
const QRegularExpression &reHashtag();
|
||||
const QRegularExpression &reBotCommand();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user