ZaStoGram_desktop/Telegram/SourceFiles/ui/effects/ttl_icon.cpp

65 lines
1.6 KiB
C++

/*
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/effects/ttl_icon.h"
#include "ui/arc_angles.h"
#include "ui/painter.h"
#include "styles/style_dialogs.h"
namespace Ui {
namespace {
[[nodiscard]] style::font FittingFont(const QString &text, int width) {
const auto &font = st::dialogsScamFont;
if (font->width(text) <= width) {
return font;
}
const auto min = st::dialogsTTLBadgeMinFont->size();
for (auto size = font->size() - 1; size >= min; --size) {
auto smaller = style::font(size, font->flags(), font->family());
if (smaller->width(text) <= width) {
return smaller;
}
}
return style::font();
}
} // namespace
void PaintTimerIcon(
QPainter &p,
const QRect &innerRect,
const QString &text,
const QColor &color) {
const auto font = FittingFont(text, innerRect.width());
p.setFont(font ? font : st::dialogsScamFont);
p.setPen(color);
p.drawText(
innerRect,
(font || text.size() <= 2) ? text : text.mid(0, 2),
style::al_center);
constexpr auto kPenWidth = 1.5;
const auto penWidth = style::ConvertScaleExact(kPenWidth);
auto pen = QPen(color);
pen.setJoinStyle(Qt::RoundJoin);
pen.setCapStyle(Qt::RoundCap);
pen.setWidthF(penWidth);
p.setPen(pen);
p.setBrush(Qt::NoBrush);
p.drawArc(innerRect, arc::kQuarterLength, arc::kHalfLength);
p.setClipRect(innerRect
- QMargins(innerRect.width() / 2, -penWidth, -penWidth, -penWidth));
pen.setStyle(Qt::DotLine);
p.setPen(pen);
p.drawEllipse(innerRect);
}
} // namespace Ui