mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-23 02:00:20 +07:00
ref code sort files
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
#include "linuxtrayiconbackend.h"
|
||||
|
||||
#include "ui/utils/trayIconCommon.h"
|
||||
|
||||
#include <QObject>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
constexpr int kLinuxTrayIconSizes[] = { 16, 22, 24, 32, 48, 64, 128 };
|
||||
|
||||
} // namespace
|
||||
|
||||
LinuxTrayIconBackend::LinuxTrayIconBackend(QObject *parent) : m_trayIcon(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void LinuxTrayIconBackend::setMenu(QMenu *menu)
|
||||
{
|
||||
m_trayIcon.setContextMenu(menu);
|
||||
}
|
||||
|
||||
void LinuxTrayIconBackend::setToolTip(const QString &tooltip)
|
||||
{
|
||||
m_trayIcon.setToolTip(tooltip);
|
||||
}
|
||||
|
||||
void LinuxTrayIconBackend::show()
|
||||
{
|
||||
m_trayIcon.show();
|
||||
}
|
||||
|
||||
void LinuxTrayIconBackend::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));
|
||||
}
|
||||
|
||||
void LinuxTrayIconBackend::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);
|
||||
}
|
||||
|
||||
void LinuxTrayIconBackend::rebuildMenu()
|
||||
{
|
||||
}
|
||||
|
||||
void LinuxTrayIconBackend::setActivatedHandler(std::function<void(QSystemTrayIcon::ActivationReason)> handler)
|
||||
{
|
||||
if (!handler) {
|
||||
return;
|
||||
}
|
||||
|
||||
QObject::connect(&m_trayIcon, &QSystemTrayIcon::activated, m_trayIcon.parent(),
|
||||
[handler](QSystemTrayIcon::ActivationReason reason) { handler(reason); });
|
||||
}
|
||||
|
||||
QIcon LinuxTrayIconBackend::buildTrayIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor) const
|
||||
{
|
||||
QIcon icon;
|
||||
for (int size : kLinuxTrayIconSizes) {
|
||||
icon.addPixmap(TrayIconCommon::buildPixmap(size, opacity, darkTheme, indicatorColor));
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
std::unique_ptr<TrayIconBackend> createTrayIconBackend(QObject *parent)
|
||||
{
|
||||
return std::make_unique<LinuxTrayIconBackend>(parent);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef LINUXTRAYICONBACKEND_H
|
||||
#define LINUXTRAYICONBACKEND_H
|
||||
|
||||
#include "ui/utils/trayIconBackend.h"
|
||||
|
||||
#include <QColor>
|
||||
#include <QIcon>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
class LinuxTrayIconBackend final : public TrayIconBackend
|
||||
{
|
||||
public:
|
||||
explicit LinuxTrayIconBackend(QObject *parent);
|
||||
|
||||
void setMenu(QMenu *menu) override;
|
||||
void setToolTip(const QString &tooltip) override;
|
||||
void show() override;
|
||||
void applyVisual(const TrayIconVisual &visual) override;
|
||||
void showMessage(const QString &title, const QString &message, const TrayIconVisual &visual, int timerMsec) override;
|
||||
void rebuildMenu() override;
|
||||
void setActivatedHandler(std::function<void(QSystemTrayIcon::ActivationReason)> handler) override;
|
||||
|
||||
private:
|
||||
QIcon buildTrayIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor) const;
|
||||
|
||||
QSystemTrayIcon m_trayIcon;
|
||||
};
|
||||
|
||||
#endif // LINUXTRAYICONBACKEND_H
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "linuxtraytheme.h"
|
||||
|
||||
#include "platforms/linux/linuxutils.h"
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QObject>
|
||||
#include <QStyleHints>
|
||||
|
||||
void LinuxTrayTheme::installThemeObserver(const std::function<void()> &onThemeChanged, QObject *parent)
|
||||
{
|
||||
if (!onThemeChanged || !parent) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (QStyleHints *styleHints = QGuiApplication::styleHints()) {
|
||||
QObject::connect(styleHints, &QStyleHints::colorSchemeChanged, parent, [onThemeChanged]() { onThemeChanged(); });
|
||||
}
|
||||
|
||||
LinuxUtils::installThemeChangeObserver(onThemeChanged);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef LINUXTRAYTHEME_H
|
||||
#define LINUXTRAYTHEME_H
|
||||
|
||||
#include <functional>
|
||||
|
||||
class QObject;
|
||||
|
||||
namespace LinuxTrayTheme
|
||||
{
|
||||
|
||||
void installThemeObserver(const std::function<void()> &onThemeChanged, QObject *parent);
|
||||
|
||||
} // namespace LinuxTrayTheme
|
||||
|
||||
#endif // LINUXTRAYTHEME_H
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef MACTRAYICONBACKEND_H
|
||||
#define MACTRAYICONBACKEND_H
|
||||
|
||||
#include "ui/utils/trayIconBackend.h"
|
||||
|
||||
#include "macosstatusicon.h"
|
||||
|
||||
class MacTrayIconBackend final : public TrayIconBackend
|
||||
{
|
||||
public:
|
||||
explicit MacTrayIconBackend(QObject *parent);
|
||||
|
||||
void setMenu(QMenu *menu) override;
|
||||
void setToolTip(const QString &tooltip) override;
|
||||
void show() override;
|
||||
void applyVisual(const TrayIconVisual &visual) override;
|
||||
void showMessage(const QString &title, const QString &message, const TrayIconVisual &visual, int timerMsec) override;
|
||||
void rebuildMenu() override;
|
||||
void setActivatedHandler(std::function<void(QSystemTrayIcon::ActivationReason)> handler) override;
|
||||
|
||||
private:
|
||||
MacOSStatusIcon m_statusIcon;
|
||||
};
|
||||
|
||||
#endif // MACTRAYICONBACKEND_H
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "mactrayiconbackend.h"
|
||||
|
||||
#include "ui/utils/trayIconCommon.h"
|
||||
|
||||
MacTrayIconBackend::MacTrayIconBackend(QObject *parent)
|
||||
: m_statusIcon(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void MacTrayIconBackend::setMenu(QMenu *menu)
|
||||
{
|
||||
m_statusIcon.setMenu(menu);
|
||||
}
|
||||
|
||||
void MacTrayIconBackend::setToolTip(const QString &tooltip)
|
||||
{
|
||||
m_statusIcon.setToolTip(tooltip);
|
||||
}
|
||||
|
||||
void MacTrayIconBackend::show()
|
||||
{
|
||||
}
|
||||
|
||||
void MacTrayIconBackend::applyVisual(const TrayIconVisual &visual)
|
||||
{
|
||||
const qreal opacity = TrayIconCommon::opacityForState(visual.connectionState);
|
||||
m_statusIcon.setIconFromData(TrayIconCommon::buildTemplatePng(opacity));
|
||||
m_statusIcon.setIndicatorColor(TrayIconCommon::indicatorColorForState(visual.connectionState));
|
||||
}
|
||||
|
||||
void MacTrayIconBackend::showMessage(const QString &title, const QString &message, const TrayIconVisual &visual, int timerMsec)
|
||||
{
|
||||
Q_UNUSED(visual);
|
||||
Q_UNUSED(timerMsec);
|
||||
m_statusIcon.showMessage(title, message);
|
||||
}
|
||||
|
||||
void MacTrayIconBackend::rebuildMenu()
|
||||
{
|
||||
m_statusIcon.rebuildNativeMenu();
|
||||
}
|
||||
|
||||
void MacTrayIconBackend::setActivatedHandler(std::function<void(QSystemTrayIcon::ActivationReason)> handler)
|
||||
{
|
||||
Q_UNUSED(handler);
|
||||
}
|
||||
|
||||
std::unique_ptr<TrayIconBackend> createTrayIconBackend(QObject *parent)
|
||||
{
|
||||
return std::make_unique<MacTrayIconBackend>(parent);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include "mactraytheme.h"
|
||||
|
||||
#include <QObject>
|
||||
|
||||
void MacTrayTheme::installThemeObserver(const std::function<void()> &onThemeChanged, QObject *parent)
|
||||
{
|
||||
Q_UNUSED(onThemeChanged);
|
||||
Q_UNUSED(parent);
|
||||
// macOS template tray icons follow the menu bar appearance automatically.
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef MACTRAYTHEME_H
|
||||
#define MACTRAYTHEME_H
|
||||
|
||||
#include <functional>
|
||||
|
||||
class QObject;
|
||||
|
||||
namespace MacTrayTheme
|
||||
{
|
||||
|
||||
void installThemeObserver(const std::function<void()> &onThemeChanged, QObject *parent);
|
||||
|
||||
} // namespace MacTrayTheme
|
||||
|
||||
#endif // MACTRAYTHEME_H
|
||||
@@ -0,0 +1,64 @@
|
||||
#include "wintrayiconbackend.h"
|
||||
|
||||
#include "ui/utils/trayIconCommon.h"
|
||||
|
||||
#include <QObject>
|
||||
|
||||
WinTrayIconBackend::WinTrayIconBackend(QObject *parent) : m_trayIcon(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void WinTrayIconBackend::setMenu(QMenu *menu)
|
||||
{
|
||||
m_trayIcon.setContextMenu(menu);
|
||||
}
|
||||
|
||||
void WinTrayIconBackend::setToolTip(const QString &tooltip)
|
||||
{
|
||||
m_trayIcon.setToolTip(tooltip);
|
||||
}
|
||||
|
||||
void WinTrayIconBackend::show()
|
||||
{
|
||||
m_trayIcon.show();
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void WinTrayIconBackend::rebuildMenu()
|
||||
{
|
||||
}
|
||||
|
||||
void WinTrayIconBackend::setActivatedHandler(std::function<void(QSystemTrayIcon::ActivationReason)> handler)
|
||||
{
|
||||
if (!handler) {
|
||||
return;
|
||||
}
|
||||
|
||||
QObject::connect(&m_trayIcon, &QSystemTrayIcon::activated, m_trayIcon.parent(),
|
||||
[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);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef WINTRAYICONBACKEND_H
|
||||
#define WINTRAYICONBACKEND_H
|
||||
|
||||
#include "ui/utils/trayIconBackend.h"
|
||||
|
||||
#include <QColor>
|
||||
#include <QIcon>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
class WinTrayIconBackend final : public TrayIconBackend
|
||||
{
|
||||
public:
|
||||
explicit WinTrayIconBackend(QObject *parent);
|
||||
|
||||
void setMenu(QMenu *menu) override;
|
||||
void setToolTip(const QString &tooltip) override;
|
||||
void show() override;
|
||||
void applyVisual(const TrayIconVisual &visual) override;
|
||||
void showMessage(const QString &title, const QString &message, const TrayIconVisual &visual, int timerMsec) override;
|
||||
void rebuildMenu() override;
|
||||
void setActivatedHandler(std::function<void(QSystemTrayIcon::ActivationReason)> handler) override;
|
||||
|
||||
private:
|
||||
QIcon buildTrayIcon(qreal opacity, bool darkTheme, const QColor &indicatorColor) const;
|
||||
|
||||
QSystemTrayIcon m_trayIcon;
|
||||
};
|
||||
|
||||
#endif // WINTRAYICONBACKEND_H
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "wintraytheme.h"
|
||||
|
||||
#include "platforms/windows/windowsutils.h"
|
||||
#include "ui/utils/trayThemeChangeFilter.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QGuiApplication>
|
||||
#include <QObject>
|
||||
#include <QStyleHints>
|
||||
|
||||
void WinTrayTheme::installThemeObserver(const std::function<void()> &onThemeChanged, QObject *parent)
|
||||
{
|
||||
if (!onThemeChanged || !parent) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (QStyleHints *styleHints = QGuiApplication::styleHints()) {
|
||||
QObject::connect(styleHints, &QStyleHints::colorSchemeChanged, parent, [onThemeChanged]() { onThemeChanged(); });
|
||||
}
|
||||
|
||||
qApp->installEventFilter(new TrayThemeChangeFilter(onThemeChanged, parent));
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
WindowsUtils::installThemeChangeObserver(onThemeChanged);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef WINTRAYTHEME_H
|
||||
#define WINTRAYTHEME_H
|
||||
|
||||
#include <functional>
|
||||
|
||||
class QObject;
|
||||
|
||||
namespace WinTrayTheme
|
||||
{
|
||||
|
||||
void installThemeObserver(const std::function<void()> &onThemeChanged, QObject *parent);
|
||||
|
||||
} // namespace WinTrayTheme
|
||||
|
||||
#endif // WINTRAYTHEME_H
|
||||
Reference in New Issue
Block a user