Added IconButtonWithText control.

This commit is contained in:
23rd 2022-03-30 18:38:49 +03:00
parent 9f28a0b106
commit c035b78f87
6 changed files with 98 additions and 6 deletions

View file

@ -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

View file

@ -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());

View file

@ -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;

View file

@ -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<RpWidget*> 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

View file

@ -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<RpWidget*> 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

View file

@ -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;