some refactorings, tray icon test

This commit is contained in:
Martin Rotter 2021-02-25 09:30:46 +01:00
parent 1fc0337211
commit 44ee50ab76
24 changed files with 88 additions and 79 deletions

View File

@ -30,7 +30,7 @@
<url type="donation">https://martinrotter.github.io/donate/</url>
<content_rating type="oars-1.1" />
<releases>
<release version="3.9.2" date="2021-04-28"/>
<release version="3.9.0" date="2021-02-25"/>
</releases>
<content_rating type="oars-1.0">
<content_attribute id="violence-cartoon">none</content_attribute>

View File

@ -204,7 +204,7 @@ QList<QAction*> FormMain::allActions() const {
void FormMain::prepareMenus() {
// Setup menu for tray icon.
if (SystemTrayIcon::isSystemTrayAvailable()) {
if (SystemTrayIcon::isSystemTrayAreaAvailable()) {
#if defined(Q_OS_WIN)
m_trayMenu = new TrayIconMenu(APP_NAME, this);
#else
@ -449,7 +449,7 @@ void FormMain::updateFeedButtonsAvailability() {
void FormMain::switchVisibility(bool force_hide) {
if (force_hide || isVisible()) {
if (SystemTrayIcon::isSystemTrayActivated()) {
if (SystemTrayIcon::isSystemTrayDesired() && SystemTrayIcon::isSystemTrayAreaAvailable()) {
hide();
}
else {
@ -813,7 +813,8 @@ void FormMain::changeEvent(QEvent* event) {
switch (event->type()) {
case QEvent::WindowStateChange: {
if ((windowState() & Qt::WindowState::WindowMinimized) == Qt::WindowState::WindowMinimized &&
SystemTrayIcon::isSystemTrayActivated() &&
SystemTrayIcon::isSystemTrayDesired() &&
SystemTrayIcon::isSystemTrayAreaAvailable() &&
qApp->settings()->value(GROUP(GUI), SETTING(GUI::HideMainWindowWhenMinimized)).toBool()) {
event->ignore();
QTimer::singleShot(CHANGE_EVENT_DELAY, this, [this]() {

View File

@ -84,14 +84,11 @@ void SettingsGui::loadSettings() {
onBeginLoadSettings();
// Load settings of tray icon.
if (SystemTrayIcon::isSystemTrayAvailable()) {
m_ui->m_grpTray->setChecked(settings()->value(GROUP(GUI), SETTING(GUI::UseTrayIcon)).toBool());
}
// Tray icon is not supported on this machine.
else {
m_ui->m_grpTray->setTitle(m_ui->m_grpTray->title() + QL1C(' ') + tr("(Tray icon is not available.)"));
m_ui->m_grpTray->setChecked(false);
if (!SystemTrayIcon::isSystemTrayAreaAvailable()) {
m_ui->m_grpTray->setTitle(m_ui->m_grpTray->title() + QL1C(' ') + tr("(Your OS does not support tray icons at the moment.)"));
m_ui->m_grpTray->setEnabled(false);
}
m_ui->m_checkHidden->setChecked(settings()->value(GROUP(GUI), SETTING(GUI::MainWindowStartsHidden)).toBool());
@ -200,7 +197,7 @@ void SettingsGui::saveSettings() {
m_ui->m_cmbToolbarButtonStyle->itemData(m_ui->m_cmbToolbarButtonStyle->currentIndex()));
// Save tray icon.
if (SystemTrayIcon::isSystemTrayAvailable()) {
if (SystemTrayIcon::isSystemTrayAreaAvailable()) {
settings()->setValue(GROUP(GUI), GUI::UseTrayIcon, m_ui->m_grpTray->isChecked());
if (m_ui->m_grpTray->isChecked()) {

View File

@ -60,12 +60,12 @@ void SystemTrayIcon::onActivated(QSystemTrayIcon::ActivationReason reason) {
}
}
bool SystemTrayIcon::isSystemTrayAvailable() {
bool SystemTrayIcon::isSystemTrayAreaAvailable() {
return QSystemTrayIcon::isSystemTrayAvailable() && QSystemTrayIcon::supportsMessages();
}
bool SystemTrayIcon::isSystemTrayActivated() {
return SystemTrayIcon::isSystemTrayAvailable() && qApp->settings()->value(GROUP(GUI), SETTING(GUI::UseTrayIcon)).toBool();
bool SystemTrayIcon::isSystemTrayDesired() {
return qApp->settings()->value(GROUP(GUI), SETTING(GUI::UseTrayIcon)).toBool();
}
bool SystemTrayIcon::areNotificationsEnabled() {
@ -92,8 +92,8 @@ void SystemTrayIcon::show() {
showPrivate();
#else
// Delay avoids race conditions and tray icon is properly displayed.
qDebugNN << LOGSEC_GUI << "Showing tray icon with 1000 ms delay.";
QTimer::singleShot(1000, this, &SystemTrayIcon::showPrivate);
qDebugNN << LOGSEC_GUI << "Showing tray icon with 3000 ms delay.";
QTimer::singleShot(3000, this, &SystemTrayIcon::showPrivate);
#endif
}

View File

@ -46,12 +46,11 @@ class SystemTrayIcon : public QSystemTrayIcon {
void showMessage(const QString& title, const QString& message, MessageIcon icon = Information,
int milliseconds_timeout_hint = TRAY_ICON_BUBBLE_TIMEOUT, std::function<void()> functor = nullptr);
// Returns true if tray icon CAN be constructed on this machine.
static bool isSystemTrayAvailable();
// Returns true if tray area is available and icon can be displayed.
static bool isSystemTrayAreaAvailable();
// Returns true if tray icon CAN be costructed and IS enabled in
// application settings.
static bool isSystemTrayActivated();
// Returns true if user wants to have tray icon displayed.
static bool isSystemTrayDesired();
// Determines whether balloon tips are enabled or not on tray icons.
static bool areNotificationsEnabled();

View File

@ -26,6 +26,7 @@
#include <QProcess>
#include <QSessionManager>
#include <QTimer>
#if defined(USE_WEBENGINE)
#include "network-web/adblock/adblockicon.h"
@ -122,7 +123,9 @@ void Application::reactOnForeignNotifications() {
void Application::hideOrShowMainForm() {
// Display main window.
if (qApp->settings()->value(GROUP(GUI), SETTING(GUI::MainWindowStartsHidden)).toBool() && SystemTrayIcon::isSystemTrayActivated()) {
if (qApp->settings()->value(GROUP(GUI), SETTING(GUI::MainWindowStartsHidden)).toBool() &&
SystemTrayIcon::isSystemTrayDesired() &&
SystemTrayIcon::isSystemTrayAreaAvailable()) {
qDebugNN << LOGSEC_CORE << "Hiding the main window when the application is starting.";
mainForm()->switchVisibility(true);
}
@ -412,8 +415,15 @@ QIcon Application::desktopAwareIcon() const {
void Application::showTrayIcon() {
// Display tray icon if it is enabled and available.
if (SystemTrayIcon::isSystemTrayActivated()) {
qDebugNN << LOGSEC_CORE << "Showing tray icon.";
if (SystemTrayIcon::isSystemTrayDesired()) {
#if !defined(Q_OS_LINUX)
if (!SystemTrayIcon::isSystemTrayAreaAvailable()) {
qWarningNN << LOGSEC_GUI << "Tray icon area is not available.";
return;
}
#endif
qDebugNN << LOGSEC_GUI << "Showing tray icon.";
trayIcon()->show();
}
}
@ -433,7 +443,9 @@ void Application::deleteTrayIcon() {
void Application::showGuiMessage(const QString& title, const QString& message,
QSystemTrayIcon::MessageIcon message_type, QWidget* parent,
bool show_at_least_msgbox, std::function<void()> functor) {
if (SystemTrayIcon::areNotificationsEnabled() && SystemTrayIcon::isSystemTrayActivated()) {
if (SystemTrayIcon::areNotificationsEnabled() &&
SystemTrayIcon::isSystemTrayDesired() &&
SystemTrayIcon::isSystemTrayAreaAvailable()) {
trayIcon()->showMessage(title, message, message_type, TRAY_ICON_BUBBLE_TIMEOUT, std::move(functor));
}
else if (show_at_least_msgbox) {