mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-20 02:00:55 +07:00
fixed update icon windows & ref code
This commit is contained in:
@@ -312,6 +312,7 @@ if(APPLE AND MACOS_NE)
|
||||
${CLIENT_ROOT_DIR}/ui/utils/platformTrayTheme.h
|
||||
${CLIENT_ROOT_DIR}/ui/utils/trayIconCommon.h
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintrayiconbackend.h
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintrayicon.h
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintraytheme.h
|
||||
${CLIENT_ROOT_DIR}/ui/utils/trayThemeChangeFilter.h
|
||||
)
|
||||
@@ -322,6 +323,7 @@ if(APPLE AND MACOS_NE)
|
||||
${CLIENT_ROOT_DIR}/ui/utils/platformTrayTheme.cpp
|
||||
${CLIENT_ROOT_DIR}/ui/utils/trayIconCommon.cpp
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintrayiconbackend.cpp
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintrayicon.cpp
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintraytheme.cpp
|
||||
${CLIENT_ROOT_DIR}/ui/utils/trayThemeChangeFilter.cpp
|
||||
)
|
||||
@@ -331,12 +333,14 @@ if(WIN32)
|
||||
set(HEADERS ${HEADERS}
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/windowsutils.h
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintrayiconbackend.h
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintrayicon.h
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintraytheme.h
|
||||
${CLIENT_ROOT_DIR}/ui/utils/trayThemeChangeFilter.h
|
||||
)
|
||||
set(SOURCES ${SOURCES}
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/windowsutils.cpp
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintrayiconbackend.cpp
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintrayicon.cpp
|
||||
${CLIENT_ROOT_DIR}/platforms/windows/wintraytheme.cpp
|
||||
${CLIENT_ROOT_DIR}/ui/utils/trayThemeChangeFilter.cpp
|
||||
)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#include "wintrayicon.h"
|
||||
|
||||
#include "ui/utils/trayIconCommon.h"
|
||||
|
||||
#include <QMenu>
|
||||
|
||||
namespace WinTrayIcon
|
||||
{
|
||||
QIcon buildIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor)
|
||||
{
|
||||
return TrayIconCommon::buildIcon(opacity, darkTheme, indicatorColor);
|
||||
}
|
||||
|
||||
void applyTo(QSystemTrayIcon &trayIcon, Vpn::ConnectionState state, bool darkTheme)
|
||||
{
|
||||
const qreal opacity = TrayIconCommon::opacityForState(state);
|
||||
const QColor indicatorColor = TrayIconCommon::indicatorColorForState(state);
|
||||
trayIcon.setIcon(buildIcon(opacity, darkTheme, indicatorColor));
|
||||
}
|
||||
|
||||
QIcon buildNotifyIcon(bool darkTheme)
|
||||
{
|
||||
return buildIcon(TrayIconCommon::kConnectedOpacity, darkTheme,
|
||||
TrayIconCommon::indicatorColorForState(Vpn::ConnectionState::Connected));
|
||||
}
|
||||
|
||||
void configure(QSystemTrayIcon &trayIcon, QMenu *menu, const QString &tooltip)
|
||||
{
|
||||
trayIcon.setContextMenu(menu);
|
||||
trayIcon.setToolTip(tooltip);
|
||||
}
|
||||
|
||||
void show(QSystemTrayIcon &trayIcon)
|
||||
{
|
||||
trayIcon.show();
|
||||
}
|
||||
|
||||
void showMessage(QSystemTrayIcon &trayIcon, const QString &title, const QString &message, bool darkTheme,
|
||||
int timerMsec)
|
||||
{
|
||||
trayIcon.showMessage(title, message, buildNotifyIcon(darkTheme), timerMsec);
|
||||
}
|
||||
} // namespace WinTrayIcon
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef WINTRAYICON_H
|
||||
#define WINTRAYICON_H
|
||||
|
||||
#include "core/protocols/vpnProtocol.h"
|
||||
|
||||
#include <QColor>
|
||||
#include <QIcon>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
class QMenu;
|
||||
class QString;
|
||||
|
||||
namespace WinTrayIcon
|
||||
{
|
||||
QIcon buildIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor);
|
||||
void applyTo(QSystemTrayIcon &trayIcon, Vpn::ConnectionState state, bool darkTheme);
|
||||
QIcon buildNotifyIcon(bool darkTheme);
|
||||
|
||||
void configure(QSystemTrayIcon &trayIcon, QMenu *menu, const QString &tooltip);
|
||||
void show(QSystemTrayIcon &trayIcon);
|
||||
void showMessage(QSystemTrayIcon &trayIcon, const QString &title, const QString &message, bool darkTheme,
|
||||
int timerMsec);
|
||||
} // namespace WinTrayIcon
|
||||
|
||||
#endif // WINTRAYICON_H
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "wintrayiconbackend.h"
|
||||
|
||||
#include "ui/utils/trayIconCommon.h"
|
||||
#include "platforms/windows/wintrayicon.h"
|
||||
|
||||
#include <QObject>
|
||||
|
||||
@@ -20,23 +20,18 @@ void WinTrayIconBackend::setToolTip(const QString &tooltip)
|
||||
|
||||
void WinTrayIconBackend::show()
|
||||
{
|
||||
m_trayIcon.show();
|
||||
WinTrayIcon::show(m_trayIcon);
|
||||
}
|
||||
|
||||
void WinTrayIconBackend::applyVisual(const TrayIconVisual &visual)
|
||||
{
|
||||
const qreal opacity = TrayIconCommon::opacityForState(visual.connectionState);
|
||||
const QColor indicatorColor = TrayIconCommon::indicatorColorForState(visual.connectionState);
|
||||
m_trayIcon.setIcon(buildTrayIcon(opacity, visual.darkTheme, indicatorColor));
|
||||
WinTrayIcon::applyTo(m_trayIcon, visual.connectionState, visual.darkTheme);
|
||||
}
|
||||
|
||||
void WinTrayIconBackend::showMessage(const QString &title, const QString &message, const TrayIconVisual &visual,
|
||||
int timerMsec)
|
||||
{
|
||||
m_trayIcon.showMessage(title, message,
|
||||
buildTrayIcon(TrayIconCommon::kConnectedOpacity, visual.darkTheme,
|
||||
TrayIconCommon::indicatorColorForState(Vpn::ConnectionState::Connected)),
|
||||
timerMsec);
|
||||
WinTrayIcon::showMessage(m_trayIcon, title, message, visual.darkTheme, timerMsec);
|
||||
}
|
||||
|
||||
void WinTrayIconBackend::rebuildMenu()
|
||||
@@ -53,11 +48,6 @@ void WinTrayIconBackend::setActivatedHandler(std::function<void(QSystemTrayIcon:
|
||||
[handler](QSystemTrayIcon::ActivationReason reason) { handler(reason); });
|
||||
}
|
||||
|
||||
QIcon WinTrayIconBackend::buildTrayIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor) const
|
||||
{
|
||||
return TrayIconCommon::buildIcon(opacity, darkTheme, indicatorColor);
|
||||
}
|
||||
|
||||
std::unique_ptr<TrayIconBackend> createTrayIconBackend(QObject *parent)
|
||||
{
|
||||
return std::make_unique<WinTrayIconBackend>(parent);
|
||||
|
||||
@@ -21,8 +21,6 @@ public:
|
||||
void setActivatedHandler(std::function<void(QSystemTrayIcon::ActivationReason)> handler) override;
|
||||
|
||||
private:
|
||||
QIcon buildTrayIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor) const;
|
||||
|
||||
QSystemTrayIcon m_trayIcon;
|
||||
};
|
||||
|
||||
|
||||
@@ -15,12 +15,14 @@ void WinTrayTheme::installThemeObserver(const std::function<void()> &onThemeChan
|
||||
}
|
||||
|
||||
if (QStyleHints *styleHints = QGuiApplication::styleHints()) {
|
||||
QObject::connect(styleHints, &QStyleHints::colorSchemeChanged, parent, [onThemeChanged]() { onThemeChanged(); });
|
||||
QObject::connect(styleHints, &QStyleHints::colorSchemeChanged, parent, [onThemeChanged]() {
|
||||
onThemeChanged();
|
||||
});
|
||||
}
|
||||
|
||||
qApp->installEventFilter(new TrayThemeChangeFilter(onThemeChanged, parent));
|
||||
qApp->installEventFilter(new TrayThemeChangeFilter([onThemeChanged]() {
|
||||
onThemeChanged();
|
||||
}, parent));
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
WindowsUtils::installThemeChangeObserver(onThemeChanged);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "platformTrayTheme.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
|
||||
#if defined(Q_OS_MAC) && !defined(MACOS_NE)
|
||||
# include "platforms/macos/mactraytheme.h"
|
||||
#elif defined(Q_OS_WIN) || (defined(Q_OS_MAC) && defined(MACOS_NE))
|
||||
|
||||
@@ -82,7 +82,6 @@ void SystemTrayNotificationHandler::refreshTheme()
|
||||
m_isDarkTheme = isDarkTheme;
|
||||
|
||||
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
|
||||
// Palette can change without gtk/portal settings updating; always repaint on Linux.
|
||||
updateTrayIcon();
|
||||
#else
|
||||
if (themeChanged) {
|
||||
@@ -106,6 +105,9 @@ void SystemTrayNotificationHandler::updateTrayIcon()
|
||||
}
|
||||
|
||||
m_trayIcon->applyVisual(currentTrayVisual());
|
||||
#if defined(Q_OS_WIN)
|
||||
qDebug() << "Windows tray: setIcon darkTheme=" << m_isDarkTheme << "state=" << static_cast<int>(m_trayState);
|
||||
#endif
|
||||
}
|
||||
|
||||
void SystemTrayNotificationHandler::onTrayActivated(QSystemTrayIcon::ActivationReason reason)
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include "trayIconBackend.h"
|
||||
|
||||
#include <QMenu>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
||||
@@ -5,34 +5,29 @@
|
||||
#include <QPainter>
|
||||
#include <QSvgRenderer>
|
||||
|
||||
namespace TrayIconCommon {
|
||||
|
||||
namespace TrayIconCommon
|
||||
{
|
||||
qreal opacityForState(Vpn::ConnectionState state)
|
||||
{
|
||||
switch (state) {
|
||||
case Vpn::ConnectionState::Connected:
|
||||
case Vpn::ConnectionState::Error:
|
||||
return kConnectedOpacity;
|
||||
case Vpn::ConnectionState::Error: return kConnectedOpacity;
|
||||
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 kDisconnectedOpacity;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
case Vpn::ConnectionState::Connected: return QColor(52, 199, 89);
|
||||
case Vpn::ConnectionState::Error: return QColor(235, 87, 87);
|
||||
default: return QColor();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,24 +9,24 @@
|
||||
|
||||
#include "core/protocols/vpnProtocol.h"
|
||||
|
||||
namespace TrayIconCommon {
|
||||
namespace TrayIconCommon
|
||||
{
|
||||
constexpr int kDefaultTrayIconSize = 128;
|
||||
constexpr char kTrayTemplateIconPath[] = ":/images/tray/icon.svg";
|
||||
|
||||
constexpr int kDefaultTrayIconSize = 128;
|
||||
constexpr char kTrayTemplateIconPath[] = ":/images/tray/icon.svg";
|
||||
constexpr qreal kDisconnectedOpacity = 0.5;
|
||||
constexpr qreal kConnectedOpacity = 1.0;
|
||||
|
||||
constexpr qreal kDisconnectedOpacity = 0.5;
|
||||
constexpr qreal kConnectedOpacity = 1.0;
|
||||
qreal opacityForState(Vpn::ConnectionState state);
|
||||
QColor indicatorColorForState(Vpn::ConnectionState state);
|
||||
|
||||
qreal opacityForState(Vpn::ConnectionState state);
|
||||
QColor indicatorColorForState(Vpn::ConnectionState state);
|
||||
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 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 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, qreal opacity, bool darkTheme, const QColor &indicatorColor);
|
||||
QIcon buildIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor);
|
||||
QByteArray buildTemplatePng(qreal opacity);
|
||||
|
||||
} // namespace TrayIconCommon
|
||||
|
||||
|
||||
Reference in New Issue
Block a user