diff --git a/CMakeLists.txt b/CMakeLists.txt index 717e7d2..a24ac2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -170,6 +170,8 @@ PRIVATE ui/widgets/checkbox.h ui/widgets/dropdown_menu.cpp ui/widgets/dropdown_menu.h + ui/widgets/icon_button_with_text.cpp + ui/widgets/icon_button_with_text.h ui/widgets/inner_dropdown.cpp ui/widgets/inner_dropdown.h ui/widgets/input_fields.cpp diff --git a/ui/widgets/buttons.cpp b/ui/widgets/buttons.cpp index f253d14..97f1c78 100644 --- a/ui/widgets/buttons.cpp +++ b/ui/widgets/buttons.cpp @@ -476,14 +476,19 @@ void IconButton::setRippleColorOverride(const style::color *colorOverride) { _rippleColorOverride = colorOverride; } +float64 IconButton::iconOverOpacity() const { + return (isDown() || forceRippled()) + ? 1. + : _a_over.value(isOver() ? 1. : 0.); +} + void IconButton::paintEvent(QPaintEvent *e) { Painter p(this); paintRipple(p, _st.rippleAreaPosition, _rippleColorOverride ? &(*_rippleColorOverride)->c : nullptr); - auto down = isDown(); - auto overIconOpacity = (down || forceRippled()) ? 1. : _a_over.value(isOver() ? 1. : 0.); - auto overIcon = [this] { + const auto overIconOpacity = iconOverOpacity(); + const auto overIcon = [&] { if (_iconOverrideOver) { return _iconOverrideOver; } else if (!_st.iconOver.empty()) { @@ -493,13 +498,13 @@ void IconButton::paintEvent(QPaintEvent *e) { } return &_st.icon; }; - auto justIcon = [this] { + const auto justIcon = [&] { if (_iconOverride) { return _iconOverride; } return &_st.icon; }; - auto icon = (overIconOpacity == 1.) ? overIcon() : justIcon(); + const auto icon = (overIconOpacity == 1.) ? overIcon() : justIcon(); auto position = _st.iconPosition; if (position.x() < 0) { position.setX((width() - icon->width()) / 2); @@ -509,7 +514,7 @@ void IconButton::paintEvent(QPaintEvent *e) { } icon->paint(p, position, width()); if (overIconOpacity > 0. && overIconOpacity < 1.) { - auto iconOver = overIcon(); + const auto iconOver = overIcon(); if (iconOver != icon) { p.setOpacity(overIconOpacity); iconOver->paint(p, position, width()); diff --git a/ui/widgets/buttons.h b/ui/widgets/buttons.h index 58d4709..89c32cf 100644 --- a/ui/widgets/buttons.h +++ b/ui/widgets/buttons.h @@ -196,6 +196,8 @@ protected: QImage prepareRippleMask() const override; QPoint prepareRippleStartPosition() const override; + [[nodiscard]] float64 iconOverOpacity() const; + private: const style::IconButton &_st; const style::icon *_iconOverride = nullptr; diff --git a/ui/widgets/icon_button_with_text.cpp b/ui/widgets/icon_button_with_text.cpp new file mode 100644 index 0000000..7e28158 --- /dev/null +++ b/ui/widgets/icon_button_with_text.cpp @@ -0,0 +1,41 @@ +/* +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 "ui/widgets/icon_button_with_text.h" + +namespace Ui { + +IconButtonWithText::IconButtonWithText( + not_null parent, + const style::IconButtonWithText &st) +: IconButton(parent, st.iconButton) +, _st(st) { +} + +void IconButtonWithText::paintEvent(QPaintEvent *e) { + IconButton::paintEvent(e); + + const auto r = rect() - _st.textPadding; + + Painter p(this); + p.setFont(_st.font); + p.setPen(_st.textFg); + p.drawText(r, _text, style::al_center); + + const auto overIconOpacity = IconButton::iconOverOpacity(); + if (overIconOpacity > 0. && overIconOpacity < 1.) { + p.setPen(_st.textFgOver); + p.setOpacity(overIconOpacity); + p.drawText(r, _text, style::al_center); + } +} + +void IconButtonWithText::setText(const QString &text) { + _text = text; +} + +} // namespace Ui diff --git a/ui/widgets/icon_button_with_text.h b/ui/widgets/icon_button_with_text.h new file mode 100644 index 0000000..d4a31f8 --- /dev/null +++ b/ui/widgets/icon_button_with_text.h @@ -0,0 +1,31 @@ +/* +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/buttons.h" + +namespace Ui { + +class IconButtonWithText final : public Ui::IconButton { +public: + IconButtonWithText( + not_null parent, + const style::IconButtonWithText &st); + + void setText(const QString &text); + +protected: + void paintEvent(QPaintEvent *e) override; + +private: + const style::IconButtonWithText &_st; + QString _text; + +}; + +} // namespace Ui diff --git a/ui/widgets/widgets.style b/ui/widgets/widgets.style index f17cf40..d54e19f 100644 --- a/ui/widgets/widgets.style +++ b/ui/widgets/widgets.style @@ -338,6 +338,17 @@ IconButton { ripple: RippleAnimation; } +IconButtonWithText { + iconButton: IconButton; + height: pixels; + + textFg: color; + textFgOver: color; + textPadding: margins; + + font: font; +} + MediaSlider { width: pixels; activeFg: color;