/* 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 "settings/business/settings_quick_replies.h" #include "core/application.h" #include "data/business/data_shortcut_messages.h" #include "data/data_session.h" #include "lang/lang_keys.h" #include "main/main_session.h" #include "settings/business/settings_recipients_helper.h" #include "settings/business/settings_shortcut_messages.h" #include "ui/layers/generic_box.h" #include "ui/text/text_utilities.h" #include "ui/widgets/buttons.h" #include "ui/widgets/fields/input_field.h" #include "ui/wrap/slide_wrap.h" #include "ui/wrap/vertical_layout.h" #include "ui/vertical_list.h" #include "window/window_session_controller.h" #include "styles/style_chat_helpers.h" #include "styles/style_layers.h" #include "styles/style_settings.h" namespace Settings { namespace { class QuickReplies : public BusinessSection { public: QuickReplies( QWidget *parent, not_null controller); ~QuickReplies(); [[nodiscard]] rpl::producer title() override; private: void setupContent(not_null controller); void save(); }; QuickReplies::QuickReplies( QWidget *parent, not_null controller) : BusinessSection(parent, controller) { setupContent(controller); } QuickReplies::~QuickReplies() { if (!Core::Quitting()) { save(); } } rpl::producer QuickReplies::title() { return tr::lng_replies_title(); } void QuickReplies::setupContent( not_null controller) { using namespace rpl::mappers; const auto content = Ui::CreateChild(this); AddDividerTextWithLottie(content, { .lottie = u"writing"_q, .lottieSize = st::settingsCloudPasswordIconSize, .lottieMargins = st::peerAppearanceIconPadding, .showFinished = showFinishes(), .about = tr::lng_replies_about(Ui::Text::WithEntities), .aboutMargins = st::peerAppearanceCoverLabelMargin, }); Ui::AddSkip(content); const auto add = content->add(object_ptr( content, tr::lng_replies_add(), st::settingsButtonNoIcon )); const auto owner = &controller->session().data(); const auto messages = &owner->shortcutMessages(); add->setClickedCallback([=] { const auto submit = [=](QString name, Fn close) { const auto id = messages->emplaceShortcut(name); showOther(ShortcutMessagesId(id)); close(); }; controller->show( Box(EditShortcutNameBox, QString(), crl::guard(this, submit))); }); const auto dividerWrap = content->add( object_ptr(content)); const auto inner = content->add( object_ptr(content)); rpl::single(rpl::empty) | rpl::then( messages->shortcutsChanged() ) | rpl::start_with_next([=] { auto old = inner->count(); const auto &shortcuts = messages->shortcuts(); auto i = 0; for (const auto &[_, shortcut] : shortcuts.list) { if (!shortcut.count) { continue; } const auto name = shortcut.name; AddButtonWithLabel( inner, rpl::single('/' + name), tr::lng_forum_messages( lt_count, rpl::single(1. * shortcut.count)), st::settingsButtonNoIcon )->setClickedCallback([=] { const auto id = messages->emplaceShortcut(name); showOther(ShortcutMessagesId(id)); }); if (old) { delete inner->widgetAt(0); --old; } } while (old--) { delete inner->widgetAt(0); } if (!inner->count()) { while (dividerWrap->count()) { delete dividerWrap->widgetAt(0); } } else if (!dividerWrap->count()) { AddSkip(dividerWrap); AddDivider(dividerWrap); AddSkip(dividerWrap); } if (const auto width = content->width()) { content->resizeToWidth(width); } }, content->lifetime()); Ui::ResizeFitChild(this, content); } void QuickReplies::save() { } } // namespace Type QuickRepliesId() { return QuickReplies::Id(); } void EditShortcutNameBox( not_null box, QString name, Fn)> submit) { name = name.trimmed(); const auto editing = !name.isEmpty(); box->setTitle(editing ? tr::lng_replies_edit_title() : tr::lng_replies_add_title()); box->addRow(object_ptr( box, (editing ? tr::lng_replies_edit_about() : tr::lng_replies_add_shortcut()), st::settingsAddReplyLabel)); const auto field = box->addRow(object_ptr( box, st::settingsAddReplyField, tr::lng_replies_add_placeholder(), name)); box->setFocusCallback([=] { field->setFocusFast(); }); field->selectAll(); const auto callback = [=] { const auto name = field->getLastText().trimmed(); if (name.isEmpty()) { field->showError(); } else { submit(name, [weak = Ui::MakeWeak(box)] { if (const auto strong = weak.data()) { strong->closeBox(); } }); } }; field->submits( ) | rpl::start_with_next(callback, field->lifetime()); box->addButton(tr::lng_settings_save(), callback); box->addButton(tr::lng_cancel(), [=] { box->closeBox(); }); } } // namespace Settings