summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-08-09 15:30:24 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-08-10 10:41:55 +0200
commit3c807347241130162cd809a726ef8a3b1e0e4068 (patch)
tree341fe91fcee1e3b71890814d6af3fc9c9ded73ec
parent8d63d703808caad01159638d50fcca8ead63bc51 (diff)
gui: fix tray icon regression
Issue: #410 Reported-by: @albertolg
-rw-r--r--gui/main-window.cpp102
-rw-r--r--gui/main-window.hpp11
-rw-r--r--gui/main.cpp10
3 files changed, 102 insertions, 21 deletions
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<void (base_value::*)(bool)>(&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<QSystemTrayIcon>(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<QSystemTrayIcon>(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);
diff --git a/gui/main-window.hpp b/gui/main-window.hpp
index 35973a15..66852e6f 100644
--- a/gui/main-window.hpp
+++ b/gui/main-window.hpp
@@ -16,6 +16,7 @@
#include <QSystemTrayIcon>
#include <QString>
#include <QMenu>
+#include <QEvent>
#include <vector>
#include <tuple>
@@ -39,7 +40,7 @@ class MainWindow : public QMainWindow, private State
{
Q_OBJECT
- Ui::ui_main_window ui;
+ Ui::main_window ui;
Shortcuts global_shortcuts;
module_settings m;
@@ -80,6 +81,10 @@ class MainWindow : public QMainWindow, private State
void set_profile(const QString& profile);
void register_shortcuts();
void set_keys_enabled(bool flag);
+
+ void changeEvent(QEvent* e) override;
+ bool maybe_hide_to_tray(QEvent* e);
+
private slots:
void save_modules();
void exit();
@@ -92,7 +97,6 @@ private slots:
void showCurveConfiguration();
void showHeadPose();
- void restore_from_tray(QSystemTrayIcon::ActivationReason);
void maybe_start_profile_from_executable();
void make_empty_config();
@@ -104,6 +108,8 @@ private slots:
void stopTracker();
void reload_options();
+ void toggle_restore_from_tray(QSystemTrayIcon::ActivationReason e);
+
signals:
void emit_start_tracker();
void emit_stop_tracker();
@@ -114,4 +120,5 @@ public:
~MainWindow();
static void set_working_directory();
void warn_on_config_not_writable();
+ bool is_tray_enabled();
};
diff --git a/gui/main.cpp b/gui/main.cpp
index 3220f31d..f8d27da7 100644
--- a/gui/main.cpp
+++ b/gui/main.cpp
@@ -130,9 +130,15 @@ int main(int argc, char** argv)
MainWindow::set_working_directory();
- auto w = std::make_shared<MainWindow>();
+ mem<MainWindow> w = std::make_shared<MainWindow>();
- w->show();
+ if (!w->is_tray_enabled())
+ w->show();
+ else
+ {
+ w->setVisible(false);
+ w->setHidden(true);
+ }
app.exec();
// on MSVC crashes in atexit