Skip to content

Commit

Permalink
Support Big Sur and Monterey
Browse files Browse the repository at this point in the history
  • Loading branch information
janten committed Nov 18, 2021
1 parent 83842d0 commit e3dc04d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
12 changes: 7 additions & 5 deletions keypad-layout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
EE4347451E78371000ECAADF /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1220;
LastUpgradeCheck = 1310;
ORGANIZATIONNAME = "Jan-Gerd Tenberge";
TargetAttributes = {
EE43474C1E78371000ECAADF = {
Expand Down Expand Up @@ -282,12 +282,13 @@
EE4347621E78371100ECAADF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 23;
CURRENT_PROJECT_VERSION = 27;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "keypad-layout/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MARKETING_VERSION = 1.9;
MARKETING_VERSION = 1.10;
PRODUCT_BUNDLE_IDENTIFIER = "com.jan-gerd.keypad-layout";
PRODUCT_NAME = "Keypad Layout";
};
Expand All @@ -296,12 +297,13 @@
EE4347631E78371100ECAADF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 23;
CURRENT_PROJECT_VERSION = 27;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "keypad-layout/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MARKETING_VERSION = 1.9;
MARKETING_VERSION = 1.10;
PRODUCT_BUNDLE_IDENTIFIER = "com.jan-gerd.keypad-layout";
PRODUCT_NAME = "Keypad Layout";
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1220"
LastUpgradeVersion = "1310"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
46 changes: 25 additions & 21 deletions keypad-layout/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -211,37 +211,41 @@ - (void)handleHotkeyChar:(char)c {
- (void)setFrontmostWindowFrame:(CGRect)frame {
NSRunningApplication *app = [[NSWorkspace sharedWorkspace] frontmostApplication];
pid_t pid = [app processIdentifier];
AXUIElementRef appElem = AXUIElementCreateApplication(pid);
AXUIElementRef application = AXUIElementCreateApplication(pid);
AXUIElementRef window = NULL;

if (AXUIElementCopyAttributeValue(appElem, kAXFocusedWindowAttribute, (CFTypeRef *)&window) != kAXErrorSuccess) {
CFRelease(appElem);
if (AXUIElementCopyAttributeValue(application, kAXFocusedWindowAttribute, (CFTypeRef *)&window) !=
kAXErrorSuccess) {
CFRelease(application);
return;
}

Boolean canSetFrame = false;
CFStringRef kAXFrameAttribute = CFSTR("AXFrame");
AXUIElementIsAttributeSettable(window, kAXFrameAttribute, &canSetFrame);
CFBooleanRef hasEnhancedUserInterface = kCFBooleanFalse;
CFStringRef kAXEnhancedUserInterfaceAttribute = CFSTR("AXEnhancedUserInterface");
AXUIElementCopyAttributeValue(application, kAXEnhancedUserInterfaceAttribute,
(CFTypeRef *)&hasEnhancedUserInterface);

if (canSetFrame) {
if (hasEnhancedUserInterface) {
AXUIElementSetAttributeValue(application, kAXEnhancedUserInterfaceAttribute, kCFBooleanFalse);
}

// FIXME: Big Sur starts an animation for the first call and ignores the second one if the animation does not
// finish soon enough.
AXValueRef sizeValue = AXValueCreate(kAXValueTypeCGSize, &frame.size);
AXError error = AXUIElementSetAttributeValue(window, kAXSizeAttribute, sizeValue);
CFRelease(sizeValue);

AXValueRef positionValue = AXValueCreate(kAXValueTypeCGPoint, &frame.origin);
error = AXUIElementSetAttributeValue(window, kAXPositionAttribute, positionValue);
CFRelease(positionValue);

if (hasEnhancedUserInterface) {
// Should macOS ever allow us to set origin and size in one call, this should work.
AXValueRef frameValue = AXValueCreate(kAXValueTypeCGRect, &frame);
AXUIElementSetAttributeValue(window, kAXFrameAttribute, frameValue);
CFRelease(frameValue);
} else {
// FIXME: Big Sur starts an animation for the first call and ignores the second one if the animation does not
// finish soon enough.
AXValueRef sizeValue = AXValueCreate(kAXValueTypeCGSize, &frame.size);
AXError error = AXUIElementSetAttributeValue(window, kAXSizeAttribute, sizeValue);
CFRelease(sizeValue);

AXValueRef positionValue = AXValueCreate(kAXValueTypeCGPoint, &frame.origin);
error = AXUIElementSetAttributeValue(window, kAXPositionAttribute, positionValue);
CFRelease(positionValue);
AXUIElementSetAttributeValue(application, kAXEnhancedUserInterfaceAttribute, kCFBooleanTrue);
}

CFRelease(window);
CFRelease(appElem);
CFRelease(application);
}

@end

0 comments on commit e3dc04d

Please sign in to comment.