diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 0bc675b105..926cfe8f7c 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -217,6 +217,8 @@ PRIVATE boxes/language_box.h boxes/local_storage_box.cpp boxes/local_storage_box.h + boxes/max_invite_box.cpp + boxes/max_invite_box.h boxes/mute_settings_box.cpp boxes/mute_settings_box.h boxes/peer_list_box.cpp diff --git a/Telegram/SourceFiles/boxes/confirm_box.cpp b/Telegram/SourceFiles/boxes/confirm_box.cpp index 21782d589a..ed51835206 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.cpp +++ b/Telegram/SourceFiles/boxes/confirm_box.cpp @@ -357,90 +357,6 @@ InformBox::InformBox(QWidget*, const TextWithEntities &text, Fn closedCa InformBox::InformBox(QWidget*, const TextWithEntities &text, const QString &doneText, Fn closedCallback) : ConfirmBox(ConfirmBox::InformBoxTag(), text, doneText, std::move(closedCallback)) { } -MaxInviteBox::MaxInviteBox(QWidget*, not_null channel) : BoxContent() -, _channel(channel) -, _text( - st::boxLabelStyle, - tr::lng_participant_invite_sorry( - tr::now, - lt_count, - channel->session().serverConfig().chatSizeMax), - kInformBoxTextOptions, - (st::boxWidth - - st::boxPadding.left() - - st::defaultBox.buttonPadding.right())) { -} - -void MaxInviteBox::prepare() { - setMouseTracking(true); - - addButton(tr::lng_box_ok(), [=] { closeBox(); }); - - _textWidth = st::boxWidth - st::boxPadding.left() - st::defaultBox.buttonPadding.right(); - _textHeight = qMin(_text.countHeight(_textWidth), 16 * st::boxLabelStyle.lineHeight); - setDimensions(st::boxWidth, st::boxPadding.top() + _textHeight + st::boxTextFont->height + st::boxTextFont->height * 2 + st::newGroupLinkPadding.bottom()); - - _channel->session().changes().peerUpdates( - _channel, - Data::PeerUpdate::Flag::InviteLinks - ) | rpl::start_with_next([=] { - rtlupdate(_invitationLink); - }, lifetime()); -} - -void MaxInviteBox::mouseMoveEvent(QMouseEvent *e) { - updateSelected(e->globalPos()); -} - -void MaxInviteBox::mousePressEvent(QMouseEvent *e) { - mouseMoveEvent(e); - if (_linkOver) { - if (_channel->inviteLink().isEmpty()) { - _channel->session().api().inviteLinks().create(_channel); - } else { - QGuiApplication::clipboard()->setText(_channel->inviteLink()); - Ui::Toast::Show(tr::lng_create_channel_link_copied(tr::now)); - } - } -} - -void MaxInviteBox::leaveEventHook(QEvent *e) { - updateSelected(QCursor::pos()); -} - -void MaxInviteBox::updateSelected(const QPoint &cursorGlobalPosition) { - QPoint p(mapFromGlobal(cursorGlobalPosition)); - - bool linkOver = _invitationLink.contains(p); - if (linkOver != _linkOver) { - _linkOver = linkOver; - update(); - setCursor(_linkOver ? style::cur_pointer : style::cur_default); - } -} - -void MaxInviteBox::paintEvent(QPaintEvent *e) { - BoxContent::paintEvent(e); - - Painter p(this); - - // draw box title / text - p.setPen(st::boxTextFg); - _text.drawLeftElided(p, st::boxPadding.left(), st::boxPadding.top(), _textWidth, width(), 16, style::al_left); - - QTextOption option(style::al_left); - option.setWrapMode(QTextOption::WrapAnywhere); - p.setFont(_linkOver ? st::defaultInputField.font->underline() : st::defaultInputField.font); - p.setPen(st::defaultLinkButton.color); - auto inviteLinkText = _channel->inviteLink().isEmpty() ? tr::lng_group_invite_create(tr::now) : _channel->inviteLink(); - p.drawText(_invitationLink, inviteLinkText, option); -} - -void MaxInviteBox::resizeEvent(QResizeEvent *e) { - BoxContent::resizeEvent(e); - _invitationLink = myrtlrect(st::boxPadding.left(), st::boxPadding.top() + _textHeight + st::boxTextFont->height, width() - st::boxPadding.left() - st::boxPadding.right(), 2 * st::boxTextFont->height); -} - ConfirmDontWarnBox::ConfirmDontWarnBox( QWidget*, rpl::producer text, diff --git a/Telegram/SourceFiles/boxes/confirm_box.h b/Telegram/SourceFiles/boxes/confirm_box.h index ca038a3955..dd04a0744a 100644 --- a/Telegram/SourceFiles/boxes/confirm_box.h +++ b/Telegram/SourceFiles/boxes/confirm_box.h @@ -141,34 +141,6 @@ public: }; -class MaxInviteBox final : public Ui::BoxContent { -public: - MaxInviteBox(QWidget*, not_null channel); - -protected: - void prepare() override; - - void paintEvent(QPaintEvent *e) override; - void resizeEvent(QResizeEvent *e) override; - void mouseMoveEvent(QMouseEvent *e) override; - void mousePressEvent(QMouseEvent *e) override; - void leaveEventHook(QEvent *e) override; - -private: - void updateSelected(const QPoint &cursorGlobalPosition); - - not_null _channel; - - Ui::Text::String _text; - int32 _textWidth, _textHeight; - - QRect _invitationLink; - bool _linkOver = false; - - QPoint _lastMousePos; - -}; - class ConfirmDontWarnBox : public Ui::BoxContent { public: ConfirmDontWarnBox( diff --git a/Telegram/SourceFiles/boxes/max_invite_box.cpp b/Telegram/SourceFiles/boxes/max_invite_box.cpp new file mode 100644 index 0000000000..190a5e8927 --- /dev/null +++ b/Telegram/SourceFiles/boxes/max_invite_box.cpp @@ -0,0 +1,148 @@ +/* +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 +*/ +#include "boxes/max_invite_box.h" + +#include "api/api_invite_links.h" +#include "apiwrap.h" +#include "data/data_changes.h" +#include "data/data_channel.h" +#include "lang/lang_keys.h" +#include "main/main_session.h" +#include "mtproto/mtproto_config.h" +#include "ui/layers/generic_box.h" +#include "ui/text/text_utilities.h" +#include "ui/toast/toast.h" +#include "styles/style_layers.h" +#include "styles/style_boxes.h" + +#include +#include + +namespace { + +TextParseOptions kInformBoxTextOptions = { + (TextParseLinks + | TextParseMultiline + | TextParseMarkdown + | TextParseRichText), // flags + 0, // maxw + 0, // maxh + Qt::LayoutDirectionAuto, // dir +}; + +} // namespace + +MaxInviteBox::MaxInviteBox(QWidget*, not_null channel) +: BoxContent() +, _channel(channel) +, _text( + st::boxLabelStyle, + tr::lng_participant_invite_sorry( + tr::now, + lt_count, + channel->session().serverConfig().chatSizeMax), + kInformBoxTextOptions, + (st::boxWidth + - st::boxPadding.left() + - st::defaultBox.buttonPadding.right())) { +} + +void MaxInviteBox::prepare() { + setMouseTracking(true); + + addButton(tr::lng_box_ok(), [=] { closeBox(); }); + + _textWidth = st::boxWidth + - st::boxPadding.left() + - st::defaultBox.buttonPadding.right(); + _textHeight = std::min( + _text.countHeight(_textWidth), + 16 * st::boxLabelStyle.lineHeight); + setDimensions( + st::boxWidth, + st::boxPadding.top() + + _textHeight + + st::boxTextFont->height + + st::boxTextFont->height * 2 + + st::newGroupLinkPadding.bottom()); + + _channel->session().changes().peerUpdates( + _channel, + Data::PeerUpdate::Flag::InviteLinks + ) | rpl::start_with_next([=] { + rtlupdate(_invitationLink); + }, lifetime()); +} + +void MaxInviteBox::mouseMoveEvent(QMouseEvent *e) { + updateSelected(e->globalPos()); +} + +void MaxInviteBox::mousePressEvent(QMouseEvent *e) { + mouseMoveEvent(e); + if (_linkOver) { + if (_channel->inviteLink().isEmpty()) { + _channel->session().api().inviteLinks().create(_channel); + } else { + QGuiApplication::clipboard()->setText(_channel->inviteLink()); + Ui::Toast::Show(tr::lng_create_channel_link_copied(tr::now)); + } + } +} + +void MaxInviteBox::leaveEventHook(QEvent *e) { + updateSelected(QCursor::pos()); +} + +void MaxInviteBox::updateSelected(const QPoint &cursorGlobalPosition) { + const auto p = QPoint(mapFromGlobal(cursorGlobalPosition)); + + const auto linkOver = _invitationLink.contains(p); + if (linkOver != _linkOver) { + _linkOver = linkOver; + update(); + setCursor(_linkOver ? style::cur_pointer : style::cur_default); + } +} + +void MaxInviteBox::paintEvent(QPaintEvent *e) { + BoxContent::paintEvent(e); + + Painter p(this); + + // draw box title / text + p.setPen(st::boxTextFg); + _text.drawLeftElided( + p, + st::boxPadding.left(), + st::boxPadding.top(), + _textWidth, + width(), + 16, + style::al_left); + + auto option = QTextOption(style::al_left); + option.setWrapMode(QTextOption::WrapAnywhere); + p.setFont(_linkOver + ? st::defaultInputField.font->underline() + : st::defaultInputField.font); + p.setPen(st::defaultLinkButton.color); + const auto inviteLinkText = _channel->inviteLink().isEmpty() + ? tr::lng_group_invite_create(tr::now) + : _channel->inviteLink(); + p.drawText(_invitationLink, inviteLinkText, option); +} + +void MaxInviteBox::resizeEvent(QResizeEvent *e) { + BoxContent::resizeEvent(e); + _invitationLink = myrtlrect( + st::boxPadding.left(), + st::boxPadding.top() + _textHeight + st::boxTextFont->height, + width() - st::boxPadding.left() - st::boxPadding.right(), + 2 * st::boxTextFont->height); +} diff --git a/Telegram/SourceFiles/boxes/max_invite_box.h b/Telegram/SourceFiles/boxes/max_invite_box.h new file mode 100644 index 0000000000..0bc9a5f691 --- /dev/null +++ b/Telegram/SourceFiles/boxes/max_invite_box.h @@ -0,0 +1,38 @@ +/* +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 "boxes/abstract_box.h" + +class MaxInviteBox final : public Ui::BoxContent { +public: + MaxInviteBox(QWidget*, not_null channel); + +protected: + void prepare() override; + + void paintEvent(QPaintEvent *e) override; + void resizeEvent(QResizeEvent *e) override; + void mouseMoveEvent(QMouseEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; + void leaveEventHook(QEvent *e) override; + +private: + void updateSelected(const QPoint &cursorGlobalPosition); + + not_null _channel; + + Ui::Text::String _text; + int32 _textWidth, _textHeight; + + QRect _invitationLink; + bool _linkOver = false; + + QPoint _lastMousePos; + +}; diff --git a/Telegram/SourceFiles/boxes/peers/add_participants_box.cpp b/Telegram/SourceFiles/boxes/peers/add_participants_box.cpp index 06e079b898..610c42b42b 100644 --- a/Telegram/SourceFiles/boxes/peers/add_participants_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/add_participants_box.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/peers/edit_participant_box.h" #include "boxes/peers/edit_peer_type_box.h" #include "boxes/confirm_box.h" +#include "boxes/max_invite_box.h" #include "lang/lang_keys.h" #include "data/data_channel.h" #include "data/data_chat.h" diff --git a/Telegram/SourceFiles/boxes/peers/edit_participants_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_participants_box.cpp index f3707234ee..0cba068e15 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_participants_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_participants_box.cpp @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/peers/edit_participant_box.h" #include "boxes/peers/add_participants_box.h" #include "boxes/confirm_box.h" +#include "boxes/max_invite_box.h" #include "boxes/add_contact_box.h" #include "main/main_session.h" #include "mtproto/mtproto_config.h" diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 53e5c45d20..9766154f27 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "boxes/confirm_box.h" #include "boxes/delete_messages_box.h" +#include "boxes/max_invite_box.h" #include "boxes/mute_settings_box.h" #include "boxes/add_contact_box.h" #include "boxes/create_poll_box.h"