From 349fbeeb23dfce84ef2cf26e5f2c1c6ddd21e1d1 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 22 Dec 2022 17:42:44 +0400 Subject: [PATCH] Add original userpic to "Reset to Original" menu item. --- Telegram/SourceFiles/boxes/boxes.style | 4 + .../ui/controls/userpic_button.cpp | 75 +++++++++++++------ .../SourceFiles/ui/controls/userpic_button.h | 6 ++ 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/Telegram/SourceFiles/boxes/boxes.style b/Telegram/SourceFiles/boxes/boxes.style index 7cc7fdccc..8805d5330 100644 --- a/Telegram/SourceFiles/boxes/boxes.style +++ b/Telegram/SourceFiles/boxes/boxes.style @@ -70,6 +70,10 @@ uploadUserpicButton: UserpicButton(defaultUserpicButton) { changeIconPosition: point(4px, 4px); } uploadUserpicButtonBorder: 2px; +restoreUserpicIcon: UserpicButton(defaultUserpicButton) { + size: size(22px, 22px); + photoSize: 22px; +} confirmInviteTitle: FlatLabel(defaultFlatLabel) { align: align(center); diff --git a/Telegram/SourceFiles/ui/controls/userpic_button.cpp b/Telegram/SourceFiles/ui/controls/userpic_button.cpp index 49eaa48f5..76a6ae29e 100644 --- a/Telegram/SourceFiles/ui/controls/userpic_button.cpp +++ b/Telegram/SourceFiles/ui/controls/userpic_button.cpp @@ -23,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/application.h" #include "ui/layers/generic_box.h" #include "ui/text/text_utilities.h" +#include "ui/widgets/menu/menu_action.h" #include "ui/painter.h" #include "editor/photo_editor_common.h" #include "editor/photo_editor_layer_widget.h" @@ -301,30 +302,27 @@ void UserpicButton::choosePhotoLocally() { callback(type)); })); }; - if (!IsCameraAvailable()) { - chooseFile(); - } else { - _menu = base::make_unique_q( - this, - st::popupMenuWithIcons); - const auto user = _peer ? _peer->asUser() : nullptr; - if (user && !user->isSelf()) { + _menu = base::make_unique_q( + this, + st::popupMenuWithIcons); + const auto user = _peer ? _peer->asUser() : nullptr; + if (user && !user->isSelf()) { + _menu->addAction( + tr::lng_profile_set_photo_for(tr::now), + [=] { chooseFile(); }, + &st::menuIconPhotoSet); + if (canSuggestPhoto(user)) { _menu->addAction( - tr::lng_profile_set_photo_for(tr::now), - [=] { chooseFile(); }, - &st::menuIconPhotoSet); - if (canSuggestPhoto(user)) { - _menu->addAction( - tr::lng_profile_suggest_photo(tr::now), - [=] { chooseFile(ChosenType::Suggest); }, - &st::menuIconPhotoSuggest); - } - if (hasPersonalPhotoLocally()) { - _menu->addAction( - tr::lng_profile_photo_reset(tr::now), - [=] { _resetPersonalRequests.fire({}); }, - &st::menuIconRemove); - } + tr::lng_profile_suggest_photo(tr::now), + [=] { chooseFile(ChosenType::Suggest); }, + &st::menuIconPhotoSuggest); + } + if (hasPersonalPhotoLocally()) { + _menu->addAction(makeResetToOriginalAction()); + } + } else { + if (!IsCameraAvailable()) { + chooseFile(); } else { _menu->addAction(tr::lng_attach_file(tr::now), [=] { chooseFile(); @@ -337,8 +335,37 @@ void UserpicButton::choosePhotoLocally() { callback(ChosenType::Set))); }, &st::menuIconPhotoSet); } - _menu->popup(QCursor::pos()); } + _menu->popup(QCursor::pos()); +} + +auto UserpicButton::makeResetToOriginalAction() +-> base::unique_qptr { + auto item = base::make_unique_q( + _menu.get(), + _menu->st().menu, + Menu::CreateAction( + _menu.get(), + tr::lng_profile_photo_reset(tr::now), + [=] { _resetPersonalRequests.fire({}); }), + nullptr, + nullptr); + const auto icon = CreateChild( + item.get(), + _controller, + _peer, + Ui::UserpicButton::Role::Custom, + Ui::UserpicButton::Source::NonPersonalIfHasPersonal, + st::restoreUserpicIcon); + if (_source == Source::Custom) { + icon->showCustom(base::duplicate(_result)); + } + icon->setAttribute(Qt::WA_TransparentForMouseEvents); + icon->move(_menu->st().menu.itemIconPosition + + QPoint( + (st::menuIconRemove.width() - icon->width()) / 2, + (st::menuIconRemove.height() - icon->height()) / 2)); + return item; } void UserpicButton::openPeerPhoto() { diff --git a/Telegram/SourceFiles/ui/controls/userpic_button.h b/Telegram/SourceFiles/ui/controls/userpic_button.h index 0feb43f44..66efbedc5 100644 --- a/Telegram/SourceFiles/ui/controls/userpic_button.h +++ b/Telegram/SourceFiles/ui/controls/userpic_button.h @@ -34,6 +34,10 @@ namespace style { struct UserpicButton; } // namespace style +namespace Ui::Menu { +class ItemBase; +} // namespace Ui::Menu + namespace Ui { class PopupMenu; @@ -142,6 +146,8 @@ private: void choosePhotoLocally(); [[nodiscard]] bool canSuggestPhoto(not_null user) const; [[nodiscard]] bool hasPersonalPhotoLocally() const; + [[nodiscard]] auto makeResetToOriginalAction() + -> base::unique_qptr; const style::UserpicButton &_st; ::Window::SessionController *_controller = nullptr;