mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
feat: macOS, check and request Accessibility permission for Cmd+C
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
This commit is contained in:
parent
7f497b0cdf
commit
c5ce1cae20
|
@ -49,6 +49,15 @@ include(FeatureSummary)
|
|||
project(goldendict-ng
|
||||
VERSION 24.11.0
|
||||
LANGUAGES CXX C)
|
||||
|
||||
if (APPLE)
|
||||
enable_language(OBJCXX)
|
||||
set(CMAKE_OBJCXX_STANDARD 17)
|
||||
endif ()
|
||||
|
||||
set(CMAKE_C_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
set(GOLDENDICT "goldendict") # binary/executable name
|
||||
if (USE_ALTERNATIVE_NAME )
|
||||
|
@ -58,8 +67,6 @@ if (APPLE)
|
|||
set(GOLDENDICT "GoldenDict-ng")
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
#### Qt
|
||||
|
||||
|
|
|
@ -3,7 +3,25 @@
|
|||
|
||||
#include "hotkeywrapper.hh"
|
||||
#include <QTimer>
|
||||
#include <ApplicationServices/ApplicationServices.h>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QObject>
|
||||
#include <memory>
|
||||
|
||||
#import <Appkit/Appkit.h>
|
||||
|
||||
///
|
||||
/// Implementation of the Cmd+C+C trick:
|
||||
/// These two methods from `Carbon` API are the core part:
|
||||
/// * `RegisterEventHotKey` -> For trapping Cmd+C and waiting for second Cmd+C press.
|
||||
/// * `CGEventCreateKeyboardEvent` -> For simulating Cmd+C press.
|
||||
///
|
||||
/// The capturing of the first Cmd+C will prevent user's normal copy action.
|
||||
/// The trick is to insert a Cmd+C in between two user generated Cmd+C.
|
||||
///
|
||||
/// Note: Based an expert's opinion, despite `Carbon` APIs are mostly deprecated, the used two are not.
|
||||
/// https://github.com/sindresorhus/KeyboardShortcuts/blob/9369a045a72a5296150879781321aecd228171db/readme.md?plain=1#L207
|
||||
///
|
||||
|
||||
namespace MacKeyMapping
|
||||
{
|
||||
|
@ -114,6 +132,26 @@ void HotkeyWrapper::waitKey2()
|
|||
{
|
||||
state2 = false;
|
||||
}
|
||||
void checkAndRequestAccessibilityPermission()
|
||||
{
|
||||
if (AXIsProcessTrusted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto msgBox = std::make_unique<QMessageBox>(nullptr);
|
||||
auto* turnOnPermission = new QPushButton(QObject::tr("Turn on Accessibility"), msgBox.get());
|
||||
|
||||
msgBox->setInformativeText(QObject::tr("Global shortcut using ⌘+C needs Accessibility permission. Please grant it to Goldendict or change ⌘+C to something else."));
|
||||
|
||||
msgBox->addButton(QMessageBox::Ok);
|
||||
msgBox->addButton(turnOnPermission, QMessageBox::AcceptRole); // the role is unused.
|
||||
msgBox->setDefaultButton(turnOnPermission);
|
||||
msgBox->exec();
|
||||
|
||||
if (msgBox->clickedButton() == turnOnPermission) {
|
||||
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"]];
|
||||
}
|
||||
}
|
||||
|
||||
void HotkeyWrapper::activated( int hkId )
|
||||
{
|
||||
|
@ -137,6 +175,8 @@ void HotkeyWrapper::activated( int hkId )
|
|||
{
|
||||
if( hs.key == keyC && hs.modifier == cmdKey )
|
||||
{
|
||||
checkAndRequestAccessibilityPermission();
|
||||
|
||||
// If that was a copy-to-clipboard shortcut, re-emit it back so it could
|
||||
// reach its original destination so it could be acted upon.
|
||||
UnregisterEventHotKey( hs.hkRef );
|
||||
|
|
Loading…
Reference in a new issue