fix(ui): disable global shortcut on unsupported platforms

Fixes #1106.
This commit is contained in:
Oleg Shparber 2019-09-30 22:19:36 -04:00
parent 9a394d3c97
commit 26cb27ad41
4 changed files with 36 additions and 8 deletions

View File

@ -67,13 +67,15 @@ MainWindow::MainWindow(Core::Application *app, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_application(app),
m_settings(app->settings()),
m_globalShortcut(new QxtGlobalShortcut(m_settings->showShortcut, this))
m_settings(app->settings())
{
ui->setupUi(this);
// initialise key grabber
connect(m_globalShortcut, &QxtGlobalShortcut::activated, this, &MainWindow::toggleWindow);
// Initialize the global shortcut handler if supported.
if (QxtGlobalShortcut::isSupported()) {
m_globalShortcut = new QxtGlobalShortcut(m_settings->showShortcut, this);
connect(m_globalShortcut, &QxtGlobalShortcut::activated, this, &MainWindow::toggleWindow);
}
setupTabBar();
@ -119,10 +121,16 @@ MainWindow::MainWindow(Core::Application *app, QWidget *parent) :
}
connect(ui->actionPreferences, &QAction::triggered, this, [this]() {
m_globalShortcut->setEnabled(false);
if (m_globalShortcut) {
m_globalShortcut->setEnabled(false);
}
QScopedPointer<SettingsDialog> dialog(new SettingsDialog(this));
dialog->exec();
m_globalShortcut->setEnabled(true);
if (m_globalShortcut) {
m_globalShortcut->setEnabled(true);
}
});
ui->actionBack->setShortcut(QKeySequence::Back);
@ -518,7 +526,9 @@ void MainWindow::keyPressEvent(QKeyEvent *keyEvent)
void MainWindow::applySettings()
{
m_globalShortcut->setShortcut(m_settings->showShortcut);
if (m_globalShortcut) {
m_globalShortcut->setShortcut(m_settings->showShortcut);
}
if (m_settings->showSystrayIcon)
createTrayIcon();
@ -557,7 +567,7 @@ void MainWindow::applySettings()
void MainWindow::toggleWindow()
{
const bool checkActive = sender() == m_globalShortcut;
const bool checkActive = m_globalShortcut && sender() == m_globalShortcut;
if (!isVisible() || (checkActive && !isActiveWindow())) {
bringToFront();

View File

@ -54,6 +54,7 @@
#include "qxtglobalshortcut_p.h"
#include <QAbstractEventDispatcher>
#include <QGuiApplication>
#ifndef Q_OS_MACOS
int QxtGlobalShortcutPrivate::ref = 0;
@ -235,6 +236,16 @@ bool QxtGlobalShortcut::isEnabled() const
return d->enabled;
}
/*!
* \brief QxtGlobalShortcut::isSupported checks if the current platform is supported.
*/
bool QxtGlobalShortcut::isSupported()
{
return QGuiApplication::platformName() == QLatin1String("windows")
|| QGuiApplication::platformName() == QLatin1String("xcb")
|| QGuiApplication::platformName() == QLatin1String("cocoa");
}
void QxtGlobalShortcut::setEnabled(bool enabled)
{
Q_D(QxtGlobalShortcut);

View File

@ -77,6 +77,8 @@ public:
bool isEnabled() const;
static bool isSupported();
public slots:
void setEnabled(bool enabled);

View File

@ -24,6 +24,8 @@
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include <qxtglobalshortcut/qxtglobalshortcut.h>
#include <core/application.h>
#include <core/settings.h>
@ -75,6 +77,9 @@ SettingsDialog::SettingsDialog(QWidget *parent) :
setTabOrder(ui->defaultFontComboBox, ui->fontSizeComboBox);
setTabOrder(ui->fontSizeComboBox, ui->serifFontComboBox);
// Disable global shortcut settings if not supported.
ui->globalHotKeyGroupBox->setEnabled(QxtGlobalShortcut::isSupported());
QWebSettings *webSettings = QWebSettings::globalSettings();
// Avoid casting in each connect.