Skip to content
Draft
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
12 changes: 12 additions & 0 deletions qxt/qxtglobalshortcut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
namespace {

int referenceCounter = 0;
std::function<void()> layoutChangedCallback;

Check failure on line 41 in qxt/qxtglobalshortcut.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Global variables should be const.

See more on https://sonarcloud.io/project/issues?id=hluk_CopyQ&issues=AZ1mgTRQ3sR7M3LsPX1y&open=AZ1mgTRQ3sR7M3LsPX1y&pullRequest=3536

QHash<QPair<quint32, quint32>, QxtGlobalShortcut*> shortcuts;

Expand Down Expand Up @@ -128,6 +129,12 @@
emit shortcut->activated(shortcut);
}

void QxtGlobalShortcutPrivate::onKeyboardLayoutChanged()
{
if (layoutChangedCallback)
layoutChangedCallback();
}

/*!
\class QxtGlobalShortcut
\brief The QxtGlobalShortcut class provides a global shortcut aka "hotkey".
Expand Down Expand Up @@ -306,3 +313,8 @@
{
d_ptr->enabled = !disabled;
}

void QxtGlobalShortcut::setLayoutChangedCallback(LayoutChangedCallback callback)
{
layoutChangedCallback = std::move(callback);
}
4 changes: 4 additions & 0 deletions qxt/qxtglobalshortcut.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <QObject>
#include <QKeySequence>
#include <functional>

class QxtGlobalShortcutPrivate;

Expand Down Expand Up @@ -61,6 +62,9 @@ class QxtGlobalShortcut final : public QObject

static void notifyRestartNeeded();

using LayoutChangedCallback = std::function<void()>;
static void setLayoutChangedCallback(LayoutChangedCallback callback);

public Q_SLOTS:
void setEnabled(bool enabled = true);
void setDisabled(bool disabled = true);
Expand Down
16 changes: 16 additions & 0 deletions qxt/qxtglobalshortcut_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
static QHash<Identifier, quint32> keyIDs;
static quint32 hotKeySerial = 0;
static bool qxt_mac_handler_installed = false;
static bool qxt_mac_layout_observer_installed = false;

Check failure on line 43 in qxt/qxtglobalshortcut_mac.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Global variables should be const.

See more on https://sonarcloud.io/project/issues?id=hluk_CopyQ&issues=AZ1jyO69hX-kxWhegvfj&open=AZ1jyO69hX-kxWhegvfj&pullRequest=3536

void qxt_mac_keyboard_layout_changed(CFNotificationCenterRef, void *, CFStringRef, const void *, CFDictionaryRef)

Check failure on line 45 in qxt/qxtglobalshortcut_mac.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this use of "void *" with a more meaningful type.

See more on https://sonarcloud.io/project/issues?id=hluk_CopyQ&issues=AZ1jyO69hX-kxWhegvfk&open=AZ1jyO69hX-kxWhegvfk&pullRequest=3536

Check failure on line 45 in qxt/qxtglobalshortcut_mac.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this use of "void *" with a more meaningful type.

See more on https://sonarcloud.io/project/issues?id=hluk_CopyQ&issues=AZ1jyO69hX-kxWhegvfl&open=AZ1jyO69hX-kxWhegvfl&pullRequest=3536
{
QxtGlobalShortcutPrivate::onKeyboardLayoutChanged();
}

OSStatus qxt_mac_handle_hot_key(EventHandlerCallRef nextHandler, EventRef event, void* data)
{
Expand All @@ -57,6 +63,16 @@

void QxtGlobalShortcutPrivate::init()
{
if (!qxt_mac_layout_observer_installed) {
qxt_mac_layout_observer_installed = true;
CFNotificationCenterAddObserver(
CFNotificationCenterGetDistributedCenter(),
nullptr,
qxt_mac_keyboard_layout_changed,
kTISNotifySelectedKeyboardInputSourceChanged,
nullptr,
CFNotificationSuspensionBehaviorDeliverImmediately);
}
}

void QxtGlobalShortcutPrivate::destroy()
Expand Down
1 change: 1 addition & 0 deletions qxt/qxtglobalshortcut_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class QxtGlobalShortcutPrivate
const QByteArray &eventType, void *message, NativeEventResult *result) override;

static void activateShortcut(quint32 nativeKey, quint32 nativeMods);
static void onKeyboardLayoutChanged();

private:
void initFallback();
Expand Down
5 changes: 3 additions & 2 deletions qxt/qxtglobalshortcut_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ bool QxtGlobalShortcutPrivate::nativeEventFilter(const QByteArray & eventType,
Q_UNUSED(eventType)
Q_UNUSED(result)
MSG* msg = static_cast<MSG*>(message);
if (msg->message == WM_HOTKEY)
{
if (msg->message == WM_HOTKEY) {
const quint32 keycode = HIWORD(msg->lParam);
const quint32 modifiers = LOWORD(msg->lParam);
activateShortcut(keycode, modifiers);
} else if (msg->message == WM_INPUTLANGCHANGE) {
onKeyboardLayoutChanged();
}
return false;
}
Expand Down
36 changes: 31 additions & 5 deletions src/app/clipboardserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "common/clientsocket.h"
#include "common/client_server.h"
#include "common/commandstatus.h"
#include "common/commandstore.h"
#include "common/config.h"
#include "common/log.h"
#include "common/mimetypes.h"
Expand Down Expand Up @@ -233,13 +234,22 @@ ClipboardServer::ClipboardServer(QApplication *app, const QString &sessionName)
setClipboardMonitorRunning(false);
startMonitoring();

#ifdef COPYQ_GLOBAL_SHORTCUTS
QxtGlobalShortcut::setLayoutChangedCallback([this]() {
onKeyboardLayoutChanged();
});
#endif

callback(QStringLiteral("onStart"));
}

ClipboardServer::~ClipboardServer()
{
qApp->setProperty("CopyQ_server", QVariant());

#ifdef COPYQ_GLOBAL_SHORTCUTS
QxtGlobalShortcut::setLayoutChangedCallback(nullptr);
#endif
removeGlobalShortcuts();

delete m_wnd;
Expand Down Expand Up @@ -303,13 +313,12 @@ void ClipboardServer::removeGlobalShortcuts()
m_shortcutActions.clear();
}

void ClipboardServer::onCommandsSaved(const QVector<Command> &commands)
void ClipboardServer::addGlobalShortcuts(const QVector<Command> &commands)
{
#ifdef COPYQ_GLOBAL_SHORTCUTS
removeGlobalShortcuts();

#ifndef COPYQ_GLOBAL_SHORTCUTS
Q_UNUSED(commands)
#else
QList<QKeySequence> usedShortcuts;

for (const auto &command : commands) {
if (command.type() & CommandType::GlobalShortcut) {
for (const auto &shortcutText : command.globalShortcuts) {
Expand All @@ -322,6 +331,23 @@ void ClipboardServer::onCommandsSaved(const QVector<Command> &commands)
}
}
#endif
}

void ClipboardServer::onKeyboardLayoutChanged()
{
#ifdef COPYQ_GLOBAL_SHORTCUTS
COPYQ_LOG("Keyboard layout changed, re-registering shortcuts");
removeGlobalShortcuts();
addGlobalShortcuts(loadAllCommands());
#endif
}

void ClipboardServer::onCommandsSaved(const QVector<Command> &commands)
{
#ifdef COPYQ_GLOBAL_SHORTCUTS
removeGlobalShortcuts();
addGlobalShortcuts(commands);
#endif

const auto hash = monitorCommandStateHash(commands);
if ( m_monitor && hash != m_monitorCommandsStateHash ) {
Expand Down
2 changes: 2 additions & 0 deletions src/app/clipboardserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class ClipboardServer final : public QObject, public App
void shortcutActivated(QxtGlobalShortcut *shortcut);

void removeGlobalShortcuts();
void addGlobalShortcuts(const QVector<Command> &commands);
void onKeyboardLayoutChanged();

/** Called when new commands are available. */
void onCommandsSaved(const QVector<Command> &commands);
Expand Down
Loading