/* This file is part of Telegram Desktop, the official desktop version of Telegram messaging app, see https://telegram.org Telegram Desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. In addition, as a special exception, the copyright holders give permission to link the code of portions of this program with the OpenSSL library. Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org */ #include "chat_helpers/gifs_list_widget.h" #include "styles/style_chat_helpers.h" #include "ui/widgets/buttons.h" #include "ui/widgets/input_fields.h" #include "ui/effects/ripple_animation.h" #include "boxes/stickers_box.h" #include "inline_bots/inline_bot_result.h" #include "chat_helpers/stickers.h" #include "storage/localstorage.h" #include "lang.h" #include "mainwindow.h" #include "window/window_controller.h" namespace ChatHelpers { namespace { constexpr auto kSaveChosenTabTimeout = 1000; constexpr auto kSearchRequestDelay = 400; constexpr auto kStickersPanelPerRow = Stickers::kPanelPerRow; constexpr auto kInlineItemsMaxPerRow = 5; constexpr auto kSearchBotUsername = str_const("gif"); } // namespace class GifsListWidget::Footer : public TabbedSelector::InnerFooter { public: Footer(gsl::not_null parent); void stealFocus(); void returnFocus(); void setLoading(bool loading) { _cancel->setLoadingAnimation(loading); } protected: void paintEvent(QPaintEvent *e) override; void processPanelHideFinished() override; private: gsl::not_null _pan; object_ptr _field; object_ptr _cancel; QPointer _focusTakenFrom; }; GifsListWidget::Footer::Footer(gsl::not_null parent) : InnerFooter(parent) , _pan(parent) , _field(this, st::gifsSearchField, lang(lng_gifs_search)) , _cancel(this, st::gifsSearchCancel) { _field->resize(width() - st::gifsSearchFieldPosition.x() - st::gifsSearchCancelPosition.x() - st::gifsSearchCancel.width, _field->height()); _field->moveToLeft(st::gifsSearchFieldPosition.x(), st::gifsSearchFieldPosition.y()); connect(_field, &Ui::InputField::submitted, this, [this](bool ctrlShiftEnter) { _pan->sendInlineRequest(); }); connect(_field, &Ui::InputField::cancelled, this, [this] { if (_field->getLastText().isEmpty()) { emit _pan->cancelled(); } else { _field->setText(QString()); } }); connect(_field, &Ui::InputField::changed, this, [this] { _cancel->toggleAnimated(!_field->getLastText().isEmpty()); _pan->searchForGifs(_field->getLastText()); }); _cancel->moveToRight(st::gifsSearchCancelPosition.x(), st::gifsSearchCancelPosition.y()); _cancel->setClickedCallback([this] { _field->setText(QString()); }); } void GifsListWidget::Footer::stealFocus() { if (!_focusTakenFrom) { _focusTakenFrom = QApplication::focusWidget(); } _field->setFocus(); } void GifsListWidget::Footer::returnFocus() { if (_focusTakenFrom) { if (_field->hasFocus()) { _focusTakenFrom->setFocus(); } _focusTakenFrom = nullptr; } } void GifsListWidget::Footer::paintEvent(QPaintEvent *e) { Painter p(this); st::gifsSearchIcon.paint(p, st::gifsSearchIconPosition.x(), st::gifsSearchIconPosition.y(), width()); } void GifsListWidget::Footer::processPanelHideFinished() { _field->setText(QString()); } GifsListWidget::GifsListWidget(QWidget *parent, gsl::not_null controller) : Inner(parent, controller) , _section(Section::Gifs) { resize(st::emojiPanWidth - st::emojiScroll.width - st::buttonRadius, countHeight()); setMouseTracking(true); setAttribute(Qt::WA_OpaquePaintEvent); _previewTimer.setSingleShot(true); connect(&_previewTimer, SIGNAL(timeout()), this, SLOT(onPreview())); _updateInlineItems.setSingleShot(true); connect(&_updateInlineItems, SIGNAL(timeout()), this, SLOT(onUpdateInlineItems())); _inlineRequestTimer.setSingleShot(true); connect(&_inlineRequestTimer, &QTimer::timeout, this, [this] { sendInlineRequest(); }); subscribe(AuthSession::Current().data().savedGifsUpdated(), [this] { refreshSavedGifs(); }); subscribe(AuthSession::CurrentDownloaderTaskFinished(), [this] { update(); }); subscribe(controller->gifPauseLevelChanged(), [this] { if (!this->controller()->isGifPausedAtLeastFor(Window::GifPauseReason::SavedGifs)) { update(); } }); } object_ptr GifsListWidget::createFooter() { Expects(_footer == nullptr); auto result = object_ptr