dev: .mm / obj-c files -> clang-format and simplify includes
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run

This commit is contained in:
shenleban tongying 2024-11-12 05:19:59 -05:00 committed by GitHub
parent 8fd5b37335
commit 8fc71c9586
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 521 additions and 529 deletions

View file

@ -142,4 +142,7 @@ StatementMacros:
- QT_REQUIRE_VERSION
UseCRLF: false
UseTab: Never
---
Language: ObjC
BasedOnStyle: WebKit
...

View file

@ -30,8 +30,7 @@
#endif
#ifdef Q_OS_MAC
#define __SECURITYHI__
#include <Carbon/Carbon.h>
#import <Carbon/Carbon.h>
#endif
//////////////////////////////////////////////////////////////////////////

View file

@ -2,10 +2,10 @@
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "hotkeywrapper.hh"
#include <QTimer>
#include <QMessageBox>
#include <QPushButton>
#include <QObject>
#include <QPushButton>
#include <QTimer>
#include <memory>
#import <Appkit/Appkit.h>
@ -23,12 +23,10 @@
/// https://github.com/sindresorhus/KeyboardShortcuts/blob/9369a045a72a5296150879781321aecd228171db/readme.md?plain=1#L207
///
namespace MacKeyMapping
{
namespace MacKeyMapping {
// Convert Qt key codes to Mac OS X native codes
struct ReverseMapEntry
{
struct ReverseMapEntry {
UniChar character;
UInt16 keyCode;
};
@ -38,8 +36,7 @@ static int mapEntries = 0;
void createMapping()
{
if( mapping == NULL )
{
if (mapping == NULL) {
TISInputSourceRef inputSourceRef = TISCopyInputSourceForLanguage(CFSTR("en"));
if (!inputSourceRef) {
inputSourceRef = TISCopyCurrentKeyboardInputSource();
@ -51,8 +48,7 @@ void createMapping()
CFDataRef dataRef = (CFDataRef)TISGetInputSourceProperty(inputSourceRef,
kTISPropertyUnicodeKeyLayoutData);
// this method returns null under macos Japanese input method(and also Chinese), which causes cmd+C+C not to be registered as a hotkey
if( !dataRef )
{
if (!dataRef) {
// solve the null value under Japanese keyboard
inputSourceRef = TISCopyCurrentKeyboardLayoutInputSource();
dataRef = static_cast<CFDataRef>((TISGetInputSourceProperty(inputSourceRef, kTISPropertyUnicodeKeyLayoutData)));
@ -73,16 +69,15 @@ void createMapping()
mapEntries = 0;
for( int i = 0; i < 128; i++ )
{
for (int i = 0; i < 128; i++) {
UInt32 theDeadKeyState = 0;
UniCharCount theLength = 0;
if (UCKeyTranslate(keyboardLayoutPtr, i, kUCKeyActionDisplay, 0, LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit, &theDeadKeyState, 1, &theLength,
&mapping[ mapEntries ].character ) == noErr && theLength > 0 )
{
if( isprint( mapping[ mapEntries ].character ) )
{
&mapping[mapEntries].character)
== noErr
&& theLength > 0) {
if (isprint(mapping[mapEntries].character)) {
mapping[mapEntries++].keyCode = i;
}
}
@ -97,8 +92,7 @@ quint32 qtKeyToNativeKey( quint32 key )
return 0;
}
for( int i = 0; i < mapEntries; i++ )
{
for (int i = 0; i < mapEntries; i++) {
if (mapping[i].character == key) {
return mapping[i].keyCode;
}
@ -161,26 +155,20 @@ void checkAndRequestAccessibilityPermission()
void HotkeyWrapper::activated(int hkId)
{
if ( state2 )
{ // wait for 2nd key
if (state2) { // wait for 2nd key
waitKey2(); // Cancel the 2nd-key wait stage
if ( hkId == state2waiter.id + 1 ||
( hkId == state2waiter.id && state2waiter.key == state2waiter.key2 ) )
{
if (hkId == state2waiter.id + 1 || (hkId == state2waiter.id && state2waiter.key == state2waiter.key2)) {
emit hotkeyActivated(state2waiter.handle);
return;
}
}
for ( int i = 0; i < hotkeys.count(); i++ )
{
for (int i = 0; i < hotkeys.count(); i++) {
HotkeyStruct& hs = hotkeys[i];
if( hkId == hs.id )
{
if( hs.key == keyC && hs.modifier == cmdKey )
{
if (hkId == hs.id) {
if (hs.key == keyC && hs.modifier == cmdKey) {
checkAndRequestAccessibilityPermission();
// If that was a copy-to-clipboard shortcut, re-emit it back so it could
@ -214,8 +202,7 @@ void HotkeyWrapper::activated( int hkId )
void HotkeyWrapper::unregister()
{
for ( int i = 0; i < hotkeys.count(); i++ )
{
for (int i = 0; i < hotkeys.count(); i++) {
HotkeyStruct const& hk = hotkeys.at(i);
UnregisterEventHotKey(hk.hkRef);
@ -279,8 +266,7 @@ bool HotkeyWrapper::setGlobalKey( int key, int key2, Qt::KeyboardModifiers modif
return false;
}
if ( vk2 && vk2 != vk )
{
if (vk2 && vk2 != vk) {
hotKeyID.id = nextId + 1;
ret = RegisterEventHotKey(vk2, mod, hotKeyID, GetApplicationEventTarget(), 0, &hk.hkRef2);
}
@ -293,38 +279,70 @@ bool HotkeyWrapper::setGlobalKey( int key, int key2, Qt::KeyboardModifiers modif
quint32 HotkeyWrapper::nativeKey(int key)
{
switch (key) {
case Qt::Key_Escape: return 0x35;
case Qt::Key_Tab: return 0x30;
case Qt::Key_Backspace: return 0x33;
case Qt::Key_Return: return 0x24;
case Qt::Key_Enter: return 0x4c;
case Qt::Key_Delete: return 0x75;
case Qt::Key_Clear: return 0x47;
case Qt::Key_Home: return 0x73;
case Qt::Key_End: return 0x77;
case Qt::Key_Left: return 0x7b;
case Qt::Key_Up: return 0x7e;
case Qt::Key_Right: return 0x7c;
case Qt::Key_Down: return 0x7d;
case Qt::Key_PageUp: return 0x74;
case Qt::Key_PageDown: return 0x79;
case Qt::Key_CapsLock: return 0x57;
case Qt::Key_F1: return 0x7a;
case Qt::Key_F2: return 0x78;
case Qt::Key_F3: return 0x63;
case Qt::Key_F4: return 0x76;
case Qt::Key_F5: return 0x60;
case Qt::Key_F6: return 0x61;
case Qt::Key_F7: return 0x62;
case Qt::Key_F8: return 0x64;
case Qt::Key_F9: return 0x65;
case Qt::Key_F10: return 0x6d;
case Qt::Key_F11: return 0x67;
case Qt::Key_F12: return 0x6f;
case Qt::Key_F13: return 0x69;
case Qt::Key_F14: return 0x6b;
case Qt::Key_F15: return 0x71;
case Qt::Key_Help: return 0x72;
case Qt::Key_Escape:
return 0x35;
case Qt::Key_Tab:
return 0x30;
case Qt::Key_Backspace:
return 0x33;
case Qt::Key_Return:
return 0x24;
case Qt::Key_Enter:
return 0x4c;
case Qt::Key_Delete:
return 0x75;
case Qt::Key_Clear:
return 0x47;
case Qt::Key_Home:
return 0x73;
case Qt::Key_End:
return 0x77;
case Qt::Key_Left:
return 0x7b;
case Qt::Key_Up:
return 0x7e;
case Qt::Key_Right:
return 0x7c;
case Qt::Key_Down:
return 0x7d;
case Qt::Key_PageUp:
return 0x74;
case Qt::Key_PageDown:
return 0x79;
case Qt::Key_CapsLock:
return 0x57;
case Qt::Key_F1:
return 0x7a;
case Qt::Key_F2:
return 0x78;
case Qt::Key_F3:
return 0x63;
case Qt::Key_F4:
return 0x76;
case Qt::Key_F5:
return 0x60;
case Qt::Key_F6:
return 0x61;
case Qt::Key_F7:
return 0x62;
case Qt::Key_F8:
return 0x64;
case Qt::Key_F9:
return 0x65;
case Qt::Key_F10:
return 0x6d;
case Qt::Key_F11:
return 0x67;
case Qt::Key_F12:
return 0x6f;
case Qt::Key_F13:
return 0x69;
case Qt::Key_F14:
return 0x6b;
case Qt::Key_F15:
return 0x71;
case Qt::Key_Help:
return 0x72;
default:;
}
return MacKeyMapping::qtKeyToNativeKey(QChar(key).toLower().toLatin1());

View file

@ -1,14 +1,5 @@
#include "macmouseover.hh"
#include <AppKit/NSTouch.h>
#include <AppKit/NSEvent.h>
#include <AppKit/NSScreen.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/Foundation.h>
#ifndef AVAILABLE_MAC_OS_X_VERSION_10_11_AND_LATER
#define kAXValueTypeCGPoint kAXValueCGPointType
#define kAXValueTypeCFRange kAXValueCFRangeType
#endif
#import <AppKit/AppKit.h>
const int mouseOverInterval = 300;
@ -36,8 +27,7 @@ static CGPoint carbonScreenPointFromCocoaScreenPoint( NSPoint cocoaPoint )
if (foundScreen) {
CGFloat screenHeight = [foundScreen frame].size.height;
thePoint = CGPointMake(cocoaPoint.x, screenHeight - cocoaPoint.y - 1);
}
else {
} else {
thePoint = CGPointMake(0.0, 0.0);
}
@ -51,8 +41,8 @@ MacMouseOver & MacMouseOver::instance()
return m;
}
MacMouseOver::MacMouseOver() :
pPref(NULL)
MacMouseOver::MacMouseOver()
: pPref(NULL)
, tapRef(0)
, loop(0)
{
@ -165,8 +155,7 @@ void MacMouseOver::handlePosition()
return;
}
for( ; ; )
{
for (;;) {
CFTypeRef parameter = AXValueCreate(kAXValueTypeCGPoint, &pt);
CFTypeRef rangeValue;
err = AXUIElementCopyParameterizedAttributeNames(elem, &names);
@ -175,8 +164,7 @@ void MacMouseOver::handlePosition()
}
int numOfAttributes = CFArrayGetCount(names);
if( CFArrayContainsValue( names, CFRangeMake( 0, numOfAttributes ), CFSTR( "AXRangeForPosition" ) ) )
{
if (CFArrayContainsValue(names, CFRangeMake(0, numOfAttributes), CFSTR("AXRangeForPosition"))) {
// Standard interface
err = AXUIElementCopyParameterizedAttributeValue(elem, kAXRangeForPositionParameterizedAttribute,
parameter, (CFTypeRef*)&rangeValue);
@ -190,8 +178,7 @@ void MacMouseOver::handlePosition()
CFRange decodedRange = CFRangeMake(0, 0);
bool b = AXValueGetValue((AXValueRef)rangeValue, kAXValueTypeCFRange, &decodedRange);
CFRelease(rangeValue);
if( b )
{
if (b) {
int fromPos = decodedRange.location - 127;
if (fromPos < 0) {
fromPos = 0;
@ -211,8 +198,7 @@ void MacMouseOver::handlePosition()
CFRelease(stringValue);
// Read string further
for( int i = 1; i < 128; i++ )
{
for (int i = 1; i < 128; i++) {
range = CFRangeMake(decodedRange.location + i, 1);
parameter = AXValueCreate(kAXValueTypeCFRange, &range);
err = AXUIElementCopyParameterizedAttributeValue(elem, kAXStringForRangeParameterizedAttribute,
@ -235,9 +221,7 @@ void MacMouseOver::handlePosition()
handleRetrievedString(strToTranslate, wordPos);
}
}
else if( CFArrayContainsValue( names, CFRangeMake( 0, numOfAttributes ), CFSTR( "AXTextMarkerForPosition" ) ) )
{
} else if (CFArrayContainsValue(names, CFRangeMake(0, numOfAttributes), CFSTR("AXTextMarkerForPosition"))) {
// Safari interface
CFTypeRef marker, range;
CFStringRef str;
@ -258,8 +242,7 @@ void MacMouseOver::handlePosition()
err = AXUIElementCopyParameterizedAttributeValue(elem, CFSTR("AXStringForTextMarkerRange"),
range, (CFTypeRef*)&str);
CFRelease(range);
if( err == kAXErrorSuccess )
{
if (err == kAXErrorSuccess) {
strToTranslate = CFStringRefToQString(str);
CFRelease(str);
handleRetrievedString(strToTranslate, 0);
@ -286,14 +269,10 @@ void MacMouseOver::handleRetrievedString( QString & wordSeq, int wordSeqPos )
QString word;
if ( wordSeq[ wordSeqPos ].isSpace() )
{
if (wordSeq[wordSeqPos].isSpace()) {
// Currently we ignore such cases
return;
}
else
if ( !wordSeq[ wordSeqPos ].isLetterOrNumber() )
{
} else if (!wordSeq[wordSeqPos].isLetterOrNumber()) {
// Special case: the cursor points to something which doesn't look like a
// middle of the word -- assume that it's something that joins two words
// together.
@ -314,16 +293,13 @@ void MacMouseOver::handleRetrievedString( QString & wordSeq, int wordSeqPos )
}
}
if ( end - begin == 1 )
{
if (end - begin == 1) {
// Well, turns out it was just a single non-letter char, discard it
return;
}
word = wordSeq.mid(begin, end - begin);
}
else
{
} else {
// Cursor points to a letter -- cut the word it points to
int begin = wordSeqPos;
@ -336,8 +312,7 @@ void MacMouseOver::handleRetrievedString( QString & wordSeq, int wordSeqPos )
int end = wordSeqPos;
while( ++end < wordSeq.size() )
{
while (++end < wordSeq.size()) {
if (!wordSeq[end].isLetterOrNumber()) {
break;
}
@ -347,13 +322,10 @@ void MacMouseOver::handleRetrievedString( QString & wordSeq, int wordSeqPos )
// See if we have an RTL char. Reverse the whole string if we do.
for( int x = 0; x < word.size(); ++x )
{
for (int x = 0; x < word.size(); ++x) {
QChar::Direction d = word[x].direction();
if ( d == QChar::DirR || d == QChar::DirAL ||
d == QChar::DirRLE || d == QChar::DirRLO )
{
if (d == QChar::DirR || d == QChar::DirAL || d == QChar::DirRLE || d == QChar::DirRLO) {
std::reverse(word.begin(), word.end());
break;
}