From 3c807347241130162cd809a726ef8a3b1e0e4068 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 9 Aug 2016 15:30:24 +0200 Subject: gui: fix tray icon regression Issue: #410 Reported-by: @albertolg --- gui/main-window.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 85 insertions(+), 17 deletions(-) (limited to 'gui/main-window.cpp') diff --git a/gui/main-window.cpp b/gui/main-window.cpp index d3f9e96e..21a47ef4 100644 --- a/gui/main-window.cpp +++ b/gui/main-window.cpp @@ -86,8 +86,6 @@ MainWindow::MainWindow() : connect(&det_timer, SIGNAL(timeout()), this, SLOT(maybe_start_profile_from_executable())); det_timer.start(1000); - ensure_tray(); - if (!QFile(group::ini_pathname()).exists()) { set_profile(OPENTRACK_DEFAULT_CONFIG); @@ -124,6 +122,12 @@ MainWindow::MainWindow() : register_shortcuts(); ui.btnStartTracker->setFocus(); + + connect(&s.tray_enabled, + static_cast(&base_value::valueChanged), + this, + [&](bool) { ensure_tray(); }); + ensure_tray(); } void MainWindow::register_shortcuts() @@ -282,10 +286,8 @@ void MainWindow::reload_options() { if (work) work->reload_shortcuts(); - ensure_tray(); } - void MainWindow::startTracker() { if (work) @@ -538,25 +540,78 @@ void MainWindow::profile_selected(const QString& name) void MainWindow::ensure_tray() { - if (tray) - tray->hide(); - tray = nullptr; + if (!QSystemTrayIcon::isSystemTrayAvailable()) + return; + if (s.tray_enabled) { - tray = std::make_shared(this); - tray->setIcon(QIcon(":/images/facetracknoir.png")); - tray->show(); - connect(tray.get(), SIGNAL(activated(QSystemTrayIcon::ActivationReason)), - this, SLOT(restore_from_tray(QSystemTrayIcon::ActivationReason))); + if (!tray) + { + tray = std::make_shared(this); + tray->setIcon(QIcon(":/images/facetracknoir.png")); + tray->show(); + + connect(tray.get(), + &QSystemTrayIcon::activated, + this, + &MainWindow::toggle_restore_from_tray); + } + } + else + { + if (isHidden()) + show(); + if (!isVisible()) + setVisible(true); + + raise(); // for OSX + activateWindow(); // for Windows + + if (tray) + tray->hide(); + tray = nullptr; } } -void MainWindow::restore_from_tray(QSystemTrayIcon::ActivationReason) +void MainWindow::toggle_restore_from_tray(QSystemTrayIcon::ActivationReason e) { - show(); - setWindowState( (windowState() & ~Qt::WindowMinimized) | Qt::WindowActive); - raise(); // for MacOS - activateWindow(); // for Windows + if (e != QSystemTrayIcon::DoubleClick) + return; + + ensure_tray(); + + const bool is_minimized = isHidden() || !is_tray_enabled(); + + setVisible(is_minimized); + setHidden(!is_minimized); + + if (!is_minimized) + setWindowState(Qt::WindowNoState); + + if (is_minimized) + { + raise(); // for OSX + activateWindow(); // for Windows + } + else + { + lower(); + clearFocus(); + } +} + +bool MainWindow::maybe_hide_to_tray(QEvent* e) +{ + if (e->type() == QEvent::WindowStateChange && is_tray_enabled()) + { + e->accept(); + ensure_tray(); + hide(); + + return true; + } + + return false; } void MainWindow::maybe_start_profile_from_executable() @@ -592,6 +647,19 @@ void MainWindow::set_keys_enabled(bool flag) qDebug() << "keybindings set to" << flag; } +void MainWindow::changeEvent(QEvent* e) +{ + if (maybe_hide_to_tray(e)) + e->accept(); + else + QMainWindow::changeEvent(e); +} + +bool MainWindow::is_tray_enabled() +{ + return s.tray_enabled && QSystemTrayIcon::isSystemTrayAvailable(); +} + void MainWindow::set_profile(const QString &profile) { QSettings settings(OPENTRACK_ORG); -- cgit v1.2.3