/* 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/settings_common.h" #include "lottie/lottie_icon.h" #include "ui/painter.h" #include "ui/widgets/buttons.h" #include "ui/widgets/continuous_sliders.h" #include "ui/widgets/labels.h" #include "ui/wrap/vertical_layout.h" #include "styles/style_settings.h" #include namespace Settings { Icon::Icon(IconDescriptor descriptor) : _icon(descriptor.icon) { const auto background = [&]() -> const style::color* { if (descriptor.type == IconType::Simple) { return nullptr; } return descriptor.background; }(); if (background) { const auto radius = (descriptor.type == IconType::Rounded) ? st::settingsIconRadius : (std::min(_icon->width(), _icon->height()) / 2); _background.emplace(radius, *background); } else if (const auto brush = descriptor.backgroundBrush) { const auto radius = (descriptor.type == IconType::Rounded) ? st::settingsIconRadius : (std::min(_icon->width(), _icon->height()) / 2); _backgroundBrush.emplace(radius, std::move(*brush)); } } void Icon::paint(QPainter &p, QPoint position) const { paint(p, position.x(), position.y()); } void Icon::paint(QPainter &p, int x, int y) const { if (_background) { _background->paint(p, { { x, y }, _icon->size() }); } else if (_backgroundBrush) { PainterHighQualityEnabler hq(p); p.setPen(Qt::NoPen); p.setBrush(_backgroundBrush->second); p.drawRoundedRect( QRect(QPoint(x, y), _icon->size()), _backgroundBrush->first, _backgroundBrush->first); } _icon->paint(p, { x, y }, 2 * x + _icon->width()); } int Icon::width() const { return _icon->width(); } int Icon::height() const { return _icon->height(); } QSize Icon::size() const { return _icon->size(); } void AddButtonIcon( not_null button, const style::SettingsButton &st, IconDescriptor &&descriptor) { Expects(descriptor.icon != nullptr); struct IconWidget { IconWidget(QWidget *parent, IconDescriptor &&descriptor) : widget(parent) , icon(std::move(descriptor)) { } Ui::RpWidget widget; Icon icon; }; const auto icon = button->lifetime().make_state( button, std::move(descriptor)); icon->widget.setAttribute(Qt::WA_TransparentForMouseEvents); icon->widget.resize(icon->icon.size()); button->sizeValue( ) | rpl::start_with_next([=, left = st.iconLeft](QSize size) { icon->widget.moveToLeft( left, (size.height() - icon->widget.height()) / 2, size.width()); }, icon->widget.lifetime()); icon->widget.paintRequest( ) | rpl::start_with_next([=] { auto p = QPainter(&icon->widget); icon->icon.paint(p, 0, 0); }, icon->widget.lifetime()); } object_ptr