mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-23 02:00:20 +07:00
update icons
This commit is contained in:
@@ -7,93 +7,55 @@
|
||||
|
||||
namespace TrayIconCommon
|
||||
{
|
||||
qreal opacityForState(Vpn::ConnectionState state)
|
||||
QString resourcePathForState(Vpn::ConnectionState state, bool darkTheme)
|
||||
{
|
||||
switch (state) {
|
||||
case Vpn::ConnectionState::Error:
|
||||
return QString::fromLatin1(kIconError);
|
||||
case Vpn::ConnectionState::Connected:
|
||||
case Vpn::ConnectionState::Error: return kConnectedOpacity;
|
||||
return QString::fromLatin1(darkTheme ? kIconOnWhite : kIconOnBlack);
|
||||
case Vpn::ConnectionState::Disconnected:
|
||||
case Vpn::ConnectionState::Preparing:
|
||||
case Vpn::ConnectionState::Connecting:
|
||||
case Vpn::ConnectionState::Disconnecting:
|
||||
case Vpn::ConnectionState::Reconnecting:
|
||||
case Vpn::ConnectionState::Unknown:
|
||||
default: return kDisconnectedOpacity;
|
||||
default:
|
||||
return QString::fromLatin1(darkTheme ? kIconOffLight : kIconOffBlack);
|
||||
}
|
||||
}
|
||||
|
||||
QColor indicatorColorForState(Vpn::ConnectionState state)
|
||||
{
|
||||
switch (state) {
|
||||
case Vpn::ConnectionState::Connected: return QColor(52, 199, 89);
|
||||
case Vpn::ConnectionState::Error: return QColor(235, 87, 87);
|
||||
default: return QColor();
|
||||
}
|
||||
}
|
||||
|
||||
QPixmap renderTemplate(const QString &resourcePath, qreal opacity, int size)
|
||||
QPixmap renderIcon(const QString &resourcePath, int size)
|
||||
{
|
||||
QSvgRenderer renderer(resourcePath);
|
||||
QPixmap pixmap(size, size);
|
||||
pixmap.fill(Qt::transparent);
|
||||
|
||||
if (!renderer.isValid()) {
|
||||
qWarning() << "Failed to load tray icon template:" << resourcePath;
|
||||
qWarning() << "Failed to load tray icon:" << resourcePath;
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
QPainter painter(&pixmap);
|
||||
painter.setOpacity(opacity);
|
||||
renderer.render(&painter, QRectF(0, 0, size, size));
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
QPixmap colorizeTemplate(const QPixmap &mask, const QColor &foreground, int size)
|
||||
QPixmap buildPixmap(int size, Vpn::ConnectionState state, bool darkTheme)
|
||||
{
|
||||
QPixmap result(size, size);
|
||||
result.fill(Qt::transparent);
|
||||
|
||||
QPainter painter(&result);
|
||||
painter.fillRect(result.rect(), foreground);
|
||||
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
||||
painter.drawPixmap(0, 0, mask);
|
||||
return result;
|
||||
return renderIcon(resourcePathForState(state, darkTheme), size);
|
||||
}
|
||||
|
||||
void drawStatusIndicator(QPainter &painter, const QColor &color, int size)
|
||||
{
|
||||
const qreal dotSize = size * 0.35;
|
||||
const qreal dotOrigin = (size - dotSize) * 0.8;
|
||||
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.setBrush(color);
|
||||
painter.drawEllipse(QRectF(dotOrigin, dotOrigin, dotSize, dotSize));
|
||||
}
|
||||
|
||||
QPixmap buildPixmap(int size, qreal opacity, bool darkTheme, const QColor &indicatorColor)
|
||||
{
|
||||
const QPixmap mask = renderTemplate(QString::fromLatin1(kTrayTemplateIconPath), opacity, size);
|
||||
const QColor foreground = darkTheme ? Qt::white : Qt::black;
|
||||
QPixmap pixmap = colorizeTemplate(mask, foreground, size);
|
||||
|
||||
if (indicatorColor.isValid()) {
|
||||
QPainter painter(&pixmap);
|
||||
drawStatusIndicator(painter, indicatorColor, size);
|
||||
}
|
||||
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
QIcon buildIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor)
|
||||
QIcon buildIcon(Vpn::ConnectionState state, bool darkTheme)
|
||||
{
|
||||
QIcon icon;
|
||||
icon.addPixmap(buildPixmap(kDefaultTrayIconSize, opacity, darkTheme, indicatorColor));
|
||||
icon.addPixmap(buildPixmap(kDefaultTrayIconSize, state, darkTheme));
|
||||
return icon;
|
||||
}
|
||||
|
||||
QByteArray buildTemplatePng(qreal opacity)
|
||||
QByteArray buildTemplatePng(Vpn::ConnectionState state)
|
||||
{
|
||||
const QPixmap pixmap = renderTemplate(QString::fromLatin1(kTrayTemplateIconPath), opacity, kDefaultTrayIconSize);
|
||||
const QPixmap pixmap = renderIcon(resourcePathForState(state, /*darkTheme*/ true), kDefaultTrayIconSize);
|
||||
|
||||
QByteArray bytes;
|
||||
QBuffer buffer(&bytes);
|
||||
|
||||
@@ -2,31 +2,29 @@
|
||||
#define TRAYICONCOMMON_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QColor>
|
||||
#include <QIcon>
|
||||
#include <QPainter>
|
||||
#include <QPixmap>
|
||||
#include <QString>
|
||||
|
||||
#include "core/protocols/vpnProtocol.h"
|
||||
|
||||
namespace TrayIconCommon
|
||||
{
|
||||
constexpr int kDefaultTrayIconSize = 128;
|
||||
constexpr char kTrayTemplateIconPath[] = ":/images/tray/icon.svg";
|
||||
|
||||
constexpr qreal kDisconnectedOpacity = 0.5;
|
||||
constexpr qreal kConnectedOpacity = 1.0;
|
||||
constexpr char kIconOffBlack[] = ":/images/tray/off-black.svg";
|
||||
constexpr char kIconOffLight[] = ":/images/tray/off-light.svg";
|
||||
constexpr char kIconOnBlack[] = ":/images/tray/on-black.svg";
|
||||
constexpr char kIconOnWhite[] = ":/images/tray/on-white.svg";
|
||||
constexpr char kIconError[] = ":/images/tray/error.svg";
|
||||
|
||||
qreal opacityForState(Vpn::ConnectionState state);
|
||||
QColor indicatorColorForState(Vpn::ConnectionState state);
|
||||
QString resourcePathForState(Vpn::ConnectionState state, bool darkTheme);
|
||||
|
||||
QPixmap renderTemplate(const QString &resourcePath, qreal opacity, int size);
|
||||
QPixmap colorizeTemplate(const QPixmap &mask, const QColor &foreground, int size);
|
||||
void drawStatusIndicator(QPainter &painter, const QColor &color, int size);
|
||||
QPixmap renderIcon(const QString &resourcePath, int size);
|
||||
|
||||
QPixmap buildPixmap(int size, qreal opacity, bool darkTheme, const QColor &indicatorColor);
|
||||
QIcon buildIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor);
|
||||
QByteArray buildTemplatePng(qreal opacity);
|
||||
QPixmap buildPixmap(int size, Vpn::ConnectionState state, bool darkTheme);
|
||||
QIcon buildIcon(Vpn::ConnectionState state, bool darkTheme);
|
||||
QByteArray buildTemplatePng(Vpn::ConnectionState state);
|
||||
|
||||
} // namespace TrayIconCommon
|
||||
|
||||
|
||||
Reference in New Issue
Block a user