From 4b26971b48d18bf334b4b52fa76e7deab4affa1b Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 20 Apr 2026 15:37:58 +0300 Subject: [PATCH] Added menu action with dynamic image thumbnail. --- .../menu/menu_action_with_thumbnail.cpp | 47 +++++++++++++++++++ .../menu/menu_action_with_thumbnail.h | 37 +++++++++++++++ Telegram/cmake/td_ui.cmake | 2 + 3 files changed, 86 insertions(+) create mode 100644 Telegram/SourceFiles/menu/menu_action_with_thumbnail.cpp create mode 100644 Telegram/SourceFiles/menu/menu_action_with_thumbnail.h diff --git a/Telegram/SourceFiles/menu/menu_action_with_thumbnail.cpp b/Telegram/SourceFiles/menu/menu_action_with_thumbnail.cpp new file mode 100644 index 0000000000..c354748979 --- /dev/null +++ b/Telegram/SourceFiles/menu/menu_action_with_thumbnail.cpp @@ -0,0 +1,47 @@ +/* +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 "menu/menu_action_with_thumbnail.h" + +#include "ui/dynamic_image.h" +#include "ui/painter.h" + +namespace Menu { + +ActionWithThumbnail::ActionWithThumbnail( + not_null parent, + const style::Menu &st, + not_null action, + std::shared_ptr thumbnail, + int thumbnailSize) +: Ui::Menu::Action(parent, st, action, nullptr, nullptr) +, _thumbnail(std::move(thumbnail)) +, _thumbnailSize(thumbnailSize) { + if (_thumbnail) { + _thumbnail->subscribeToUpdates([=] { update(); }); + } +} + +ActionWithThumbnail::~ActionWithThumbnail() { + if (_thumbnail) { + _thumbnail->subscribeToUpdates(nullptr); + } +} + +void ActionWithThumbnail::paintEvent(QPaintEvent *e) { + Ui::Menu::Action::paintEvent(e); + if (!_thumbnail) { + return; + } + auto p = QPainter(this); + const auto pos = st().itemIconPosition; + p.drawImage( + QRect(pos.x(), pos.y(), _thumbnailSize, _thumbnailSize), + _thumbnail->image(_thumbnailSize)); +} + +} // namespace Menu diff --git a/Telegram/SourceFiles/menu/menu_action_with_thumbnail.h b/Telegram/SourceFiles/menu/menu_action_with_thumbnail.h new file mode 100644 index 0000000000..42831204b1 --- /dev/null +++ b/Telegram/SourceFiles/menu/menu_action_with_thumbnail.h @@ -0,0 +1,37 @@ +/* +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 "ui/widgets/menu/menu_action.h" + +namespace Ui { +class DynamicImage; +} // namespace Ui + +namespace Menu { + +class ActionWithThumbnail final : public Ui::Menu::Action { +public: + ActionWithThumbnail( + not_null parent, + const style::Menu &st, + not_null action, + std::shared_ptr thumbnail, + int thumbnailSize); + ~ActionWithThumbnail(); + +protected: + void paintEvent(QPaintEvent *e) override; + +private: + std::shared_ptr _thumbnail; + int _thumbnailSize = 0; + +}; + +} // namespace Menu diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index 840ab71395..6fdf4ec5f1 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -223,6 +223,8 @@ PRIVATE menu/gift_resale_filter.cpp menu/gift_resale_filter.h + menu/menu_action_with_thumbnail.cpp + menu/menu_action_with_thumbnail.h menu/menu_checked_action.cpp menu/menu_checked_action.h menu/menu_check_item.cpp