Skip to content

Commit

Permalink
Fix crashes on Wayland by not loading the HotkeyManager (#804)
Browse files Browse the repository at this point in the history
* Fix crashes on Wayland by not loading the HotkeyManager

On Wayland, global hotkeys are often handled by the compositor instead. On sway
for instance, a config file is edited to launch specific commands when a hotkey
is entered.

On such compositors, it should be enough to show Albert by configuring the
compositor to launch "albert show" or "albert toggle" a hotkey manager in
Albert itself is redundant.

* Fix crash when exiting settings

Co-authored-by: Johan Klokkhammer Helsing <johan.helsing@qt.io>
  • Loading branch information
johanhelsing and Johan Klokkhammer Helsing authored Nov 3, 2020
1 parent cc6dfc5 commit b979b03
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
23 changes: 14 additions & 9 deletions src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,20 @@ int main(int argc, char **argv) {
frontendManager = new FrontendManager(pluginDirs);
extensionManager = new ExtensionManager(pluginDirs);
extensionManager->reloadExtensions();
hotkeyManager = new HotkeyManager;
if ( parser.isSet("hotkey") ) {
QString hotkey = parser.value("hotkey");
if ( !hotkeyManager->registerHotkey(hotkey) )
qFatal("Failed to set hotkey to %s.", hotkey.toLocal8Bit().constData());
} else if ( settings.contains("hotkey") ) {
QString hotkey = settings.value("hotkey").toString();
if ( !hotkeyManager->registerHotkey(hotkey) )
qFatal("Failed to set hotkey to %s.", hotkey.toLocal8Bit().constData());

if ( !QGuiApplication::platformName().contains("wayland") )
hotkeyManager = new HotkeyManager;

if ( hotkeyManager ) {
if ( parser.isSet("hotkey") ) {
QString hotkey = parser.value("hotkey");
if ( !hotkeyManager->registerHotkey(hotkey) )
qFatal("Failed to set hotkey to %s.", hotkey.toLocal8Bit().constData());
} else if ( settings.contains("hotkey") ) {
QString hotkey = settings.value("hotkey").toString();
if ( !hotkeyManager->registerHotkey(hotkey) )
qFatal("Failed to set hotkey to %s.", hotkey.toLocal8Bit().constData());
}
}
queryManager = new QueryManager(extensionManager);
telemetry = new Telemetry;
Expand Down
22 changes: 14 additions & 8 deletions src/app/settingswidget/settingswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ Core::SettingsWidget::SettingsWidget(ExtensionManager *extensionManager,
*/

// HOTKEY
QSet<int> hks = hotkeyManager->hotkeys();
if (hks.size() < 1)
ui.grabKeyButton_hotkey->setText("Press to set hotkey");
else
ui.grabKeyButton_hotkey->setText(QKeySequence(*hks.begin()).toString()); // OMG
connect(ui.grabKeyButton_hotkey, &GrabKeyButton::keyCombinationPressed,
this, &SettingsWidget::changeHotkey);
if (hotkeyManager) {
QSet<int> hks = hotkeyManager->hotkeys();
if (hks.size() < 1)
ui.grabKeyButton_hotkey->setText("Press to set hotkey");
else
ui.grabKeyButton_hotkey->setText(QKeySequence(*hks.begin()).toString()); // OMG
connect(ui.grabKeyButton_hotkey, &GrabKeyButton::keyCombinationPressed,
this, &SettingsWidget::changeHotkey);
} else {
ui.grabKeyButton_hotkey->setVisible(false);
ui.label_hotkey->setVisible(false);
}

// TRAY
ui.checkBox_showTray->setChecked(trayIcon_->isVisible());
Expand Down Expand Up @@ -308,6 +313,7 @@ void SettingsWidget::updatePluginInformations(const QModelIndex & current) {

/** ***************************************************************************/
void SettingsWidget::changeHotkey(int newhk) {
Q_ASSERT(hotkeyManager_);
int oldhk = *hotkeyManager_->hotkeys().begin(); //TODO Make cool sharesdpointer design

// Try to set the hotkey
Expand Down Expand Up @@ -349,7 +355,7 @@ void SettingsWidget::keyPressEvent(QKeyEvent *event) {

/** ***************************************************************************/
void SettingsWidget::closeEvent(QCloseEvent *event) {
if (hotkeyManager_->hotkeys().empty()) {
if (hotkeyManager_ && hotkeyManager_->hotkeys().empty()) {
QMessageBox msgBox(QMessageBox::Warning, "Hotkey Missing",
"Hotkey is invalid, please set it. Press OK to go "\
"back to the settings.",
Expand Down

0 comments on commit b979b03

Please sign in to comment.