Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/gui/src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <Windows.h>
#endif

static const int systemTrayWaitTime = 10000;
static const QString allFilesFilter(QObject::tr("All files (*.*)"));
#if defined(Q_OS_WIN)
static const char barrierConfigName[] = "barrier.sgc";
Expand Down Expand Up @@ -156,6 +157,9 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) :
m_pComboServerList->hide();
m_pLabelPadlock->hide();

m_OpenHiddenTrayTimer.setSingleShot(true);
connect(&m_OpenHiddenTrayTimer, &QTimer::timeout, this, &MainWindow::on_m_OpenHiddenTrayTimer_triggered);

updateSSLFingerprint();

// resize window to smallest reasonable size
Expand Down Expand Up @@ -190,6 +194,28 @@ void MainWindow::open()

if (appConfig().getAutoHide()) {
hide();

// If system tray is not available, start a timer to ensure we don't become
// stuck in a hidden state
//
// The previous solution for this would wait at the startup of the app to
// see if the system tray becomes available before showing any window - even
// if the user didn't have autohide enabled.
//
// This solution instead, hides the window if they have autohide enabled, or shows
// the window if they don't. Then if the user has selected to autohide the window
// it checks after a period of time if the system tray is not available - if it
// isn't then it forces the window to show.
//
// This provides a much better UX for the two main use cases (user starting app with
// autohide enabled with system tray available and user starting app with autohide
// disabled with no system tray available). And provides a workaround for the edge
// case of a user enabling autohide with no system tray available (this should now
// be harder to do as the option in settings will become disabled).
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
m_OpenHiddenTrayTimer.start(systemTrayWaitTime);
}
} else {
showNormal();
}
Expand All @@ -208,6 +234,18 @@ void MainWindow::open()
}
}

void MainWindow::on_m_OpenHiddenTrayTimer_triggered()
{
// If the system tray is still not available then force window to show
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
fprintf(stdout, "System tray not available, force disabling auto hide!\n");
m_AppConfig->setAutoHide(false);

showNormal();
}
}

void MainWindow::setStatus(const QString &status)
{
m_pStatusLabel->setText(status);
Expand Down Expand Up @@ -1277,7 +1315,8 @@ void MainWindow::bonjourInstallFinished()

void MainWindow::windowStateChanged()
{
if (windowState() == Qt::WindowMinimized && appConfig().getMinimizeToTray())
// If we are minimising and minimise to tray is enabled and system tray is available then hide the window
if (windowState() == Qt::WindowMinimized && appConfig().getMinimizeToTray() && QSystemTrayIcon::isSystemTrayAvailable())
hide();
}

Expand Down
3 changes: 3 additions & 0 deletions src/gui/src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <QSettings>
#include <QProcess>
#include <QThread>
#include <QTimer>

#include "ui_MainWindowBase.h"

Expand Down Expand Up @@ -201,11 +202,13 @@ public slots:
SslCertificate* m_pSslCertificate;
QStringList m_PendingClientNames;
LogWindow *m_pLogWindow;
QTimer m_OpenHiddenTrayTimer;

private slots:
void on_m_pCheckBoxAutoConfig_toggled(bool checked);
void on_m_pComboServerList_currentIndexChanged(QString );
void on_m_pButtonReload_clicked();
void on_m_OpenHiddenTrayTimer_triggered();
void installBonjour();

};
Expand Down
15 changes: 15 additions & 0 deletions src/gui/src/QBarrierApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ QBarrierApplication::QBarrierApplication(int& argc, char** argv) :
m_Translator(NULL)
{
s_Instance = this;

// By default do not quit when the last window is closed as we minimise
// to the system tray, but listen for the lastWindow closing so that
// if the system tray is not available we can quit.
setQuitOnLastWindowClosed(false);
connect(this, &QApplication::lastWindowClosed, this, &QBarrierApplication::onLastWindowClosed);
}

QBarrierApplication::~QBarrierApplication()
Expand Down Expand Up @@ -69,3 +75,12 @@ void QBarrierApplication::setTranslator(QTranslator* translator)
m_Translator = translator;
installTranslator(m_Translator);
}

void QBarrierApplication::onLastWindowClosed()
{
// If there is no system tray available then quit when the last window is closed
if (!QSystemTrayIcon::isSystemTrayAvailable())
{
quit();
}
}
3 changes: 3 additions & 0 deletions src/gui/src/QBarrierApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class QBarrierApplication : public QApplication

static QBarrierApplication* getInstance();

private Q_SLOTS:
void onLastWindowClosed();

private:
QTranslator* m_Translator;

Expand Down
4 changes: 4 additions & 0 deletions src/gui/src/SettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ SettingsDialog::SettingsDialog(QWidget* parent, AppConfig& config) :
m_pCheckBoxMinimizeToTray->setChecked(appConfig().getMinimizeToTray());
m_pCheckBoxEnableCrypto->setChecked(m_appConfig.getCryptoEnabled());

// Don't allow auto hide or minimise to tray if it's not available
m_pCheckBoxAutoHide->setEnabled(QSystemTrayIcon::isSystemTrayAvailable());
m_pCheckBoxMinimizeToTray->setEnabled(QSystemTrayIcon::isSystemTrayAvailable());

#if defined(Q_OS_WIN)
m_pComboElevate->setCurrentIndex(static_cast<int>(appConfig().elevateMode()));
#else
Expand Down
39 changes: 0 additions & 39 deletions src/gui/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#define TRAY_RETRY_COUNT 5
#define TRAY_RETRY_WAIT 2000

#include "QBarrierApplication.h"
#include "MainWindow.h"
#include "AppConfig.h"
Expand Down Expand Up @@ -47,8 +44,6 @@ class QThreadImpl : public QThread
}
};

int waitForTray();

#if defined(Q_OS_MAC)
bool checkMacAssistiveDevices();
#endif
Expand Down Expand Up @@ -93,20 +88,9 @@ int main(int argc, char* argv[])
}
#endif

int trayAvailable = waitForTray();

QApplication::setQuitOnLastWindowClosed(false);

QSettings settings;
AppConfig appConfig (&settings);

if (appConfig.getAutoHide() && !trayAvailable)
{
// force auto hide to false - otherwise there is no way to get the GUI back
fprintf(stdout, "System tray not available, force disabling auto hide!\n");
appConfig.setAutoHide(false);
}

app.switchTranslator(appConfig.language());

MainWindow mainWindow(settings, appConfig);
Expand All @@ -124,29 +108,6 @@ int main(int argc, char* argv[])
return app.exec();
}

int waitForTray()
{
// on linux, the system tray may not be available immediately after logging in,
// so keep retrying but give up after a short time.
int trayAttempts = 0;
while (true)
{
if (QSystemTrayIcon::isSystemTrayAvailable())
{
break;
}

if (++trayAttempts > TRAY_RETRY_COUNT)
{
fprintf(stdout, "System tray is unavailable.\n");
return false;
}

QThreadImpl::msleep(TRAY_RETRY_WAIT);
}
return true;
}

#if defined(Q_OS_MAC)
bool checkMacAssistiveDevices()
{
Expand Down