Skip to content

Commit e4da8e0

Browse files
authored
Merge pull request #73 from dado3212/CrashFix
[v1.9.1] Remove reliance on AppKit to work with Big Sur
2 parents 31aeb73 + b39f9df commit e4da8e0

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

build/spaces-renamer.zip

128 KB
Binary file not shown.

spaces-renamer.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@
333333
GCC_PRECOMPILE_PREFIX_HEADER = YES;
334334
INFOPLIST_FILE = "$(SRCROOT)/spaces-renamer/Info.plist";
335335
INSTALL_PATH = "/Library/Application Support/MacEnhance/Plugins";
336-
MARKETING_VERSION = 1.9.0;
336+
MARKETING_VERSION = 1.9.1;
337337
PRODUCT_BUNDLE_IDENTIFIER = "com.alexbeals.spaces-renamer";
338338
PRODUCT_NAME = "spaces-renamer";
339339
WRAPPER_EXTENSION = bundle;
@@ -352,7 +352,7 @@
352352
GCC_PRECOMPILE_PREFIX_HEADER = YES;
353353
INFOPLIST_FILE = "$(SRCROOT)/spaces-renamer/Info.plist";
354354
INSTALL_PATH = "/Library/Application Support/MacEnhance/Plugins";
355-
MARKETING_VERSION = 1.9.0;
355+
MARKETING_VERSION = 1.9.1;
356356
PRODUCT_BUNDLE_IDENTIFIER = "com.alexbeals.spaces-renamer";
357357
PRODUCT_NAME = "spaces-renamer";
358358
WRAPPER_EXTENSION = bundle;
@@ -465,7 +465,7 @@
465465
INFOPLIST_FILE = SpacesRenamer/Info.plist;
466466
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
467467
MACOSX_DEPLOYMENT_TARGET = 10.10;
468-
MARKETING_VERSION = 1.9.0;
468+
MARKETING_VERSION = 1.9.1;
469469
MTL_ENABLE_DEBUG_INFO = YES;
470470
PRODUCT_BUNDLE_IDENTIFIER = com.alexbeals.SpacesRenamer;
471471
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -516,7 +516,7 @@
516516
INFOPLIST_FILE = SpacesRenamer/Info.plist;
517517
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
518518
MACOSX_DEPLOYMENT_TARGET = 10.10;
519-
MARKETING_VERSION = 1.9.0;
519+
MARKETING_VERSION = 1.9.1;
520520
MTL_ENABLE_DEBUG_INFO = NO;
521521
PRODUCT_BUNDLE_IDENTIFIER = com.alexbeals.SpacesRenamer;
522522
PRODUCT_NAME = "$(TARGET_NAME)";

spaces-renamer/spacesRenamer.m

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Copyright 2017 Alex Beals.
77
//
88

9-
@import AppKit;
9+
@import Foundation;
1010
#import "ZKSwizzle.h"
1111
#import <QuartzCore/QuartzCore.h>
1212

@@ -20,6 +20,15 @@
2020
#define listOfSpacesPlist [@"~/Library/Containers/com.alexbeals.spacesrenamer/com.alexbeals.spacesrenamer.currentspaces.plist" stringByExpandingTildeInPath]
2121
#define spacesPath [@"~/Library/Preferences/com.apple.spaces.plist" stringByExpandingTildeInPath]
2222

23+
// Maximum online or active displays.
24+
//
25+
// SpacesRenamer uses the core graphics API to get online/active
26+
// displays by calling CGGetActiveDisplayList() and CGGetOnlineDisplayList(),
27+
// this definition is the count that will be used when calling those functions.
28+
//
29+
// If you have more than 12 monitors, this tweak can't help you with organization, good luck.
30+
#define kMaxDisplays 12
31+
2332
int monitorIndex = 0;
2433

2534
@interface ECMaterialLayer : CALayer
@@ -38,8 +47,9 @@ static void refreshFrames(CALayer *frame) {
3847

3948
static void refreshFramesSur(CALayer *frame, CALayer* exception) {
4049
for (CALayer *layer in frame.sublayers) {
41-
if (![layer isEqualTo:exception])
42-
[layer setFrame:layer.frame];
50+
if (![layer isEqualTo:exception]) {
51+
[layer setFrame:layer.frame];
52+
}
4353
refreshFramesSur(layer, exception);
4454
}
4555
}
@@ -148,8 +158,8 @@ static double getTextSize(CALayer *view, NSString *string) {
148158
static int getSelected(NSArray<CALayer *> *views) {
149159
NSUInteger selectedIndex = [views indexOfObjectPassingTest:
150160
^(CALayer *layer, NSUInteger idx, BOOL *stop) {
151-
return (BOOL)(layer.sublayers.count > 1);
152-
}];
161+
return (BOOL)(layer.sublayers.count > 1);
162+
}];
153163

154164
return selectedIndex == NSNotFound ? -1 : (int)selectedIndex;
155165
}
@@ -159,7 +169,7 @@ static int getSelected(NSArray<CALayer *> *views) {
159169
2. Load the listOfSpacesPlist to get the current list of spaces
160170
3. Crosslist and return the custom names for each plist, and whether it's selected
161171
*/
162-
static NSMutableArray *getNamesFromPlist() {
172+
static NSMutableArray<NSMutableArray<NSMutableDictionary *> *> *getNamesFromPlist() {
163173
NSDictionary *dictOfNames = [NSDictionary dictionaryWithContentsOfFile:customNamesPlist];
164174
if (!dictOfNames) {
165175
return [NSMutableArray arrayWithCapacity:0];
@@ -287,18 +297,22 @@ @implementation _SRECMaterialLayer
287297
- (void)setFrame:(CGRect)arg1 {
288298
// Almost surely the desktop switcher
289299
if ([self probablyDesktopSwitcher:arg1]) {
290-
CALayer *rootLayer = self;
291-
Boolean bigSurOrNewer = false;
292300
NSOperatingSystemVersion macOS = NSProcessInfo.processInfo.operatingSystemVersion;
293-
if (macOS.majorVersion >= 11 || macOS.minorVersion >= 16) bigSurOrNewer = true;
294-
if (bigSurOrNewer) rootLayer = self.superlayer;
295-
301+
bool bigSurOrNewer = (macOS.majorVersion >= 11 || macOS.minorVersion >= 16);
302+
303+
CALayer *rootLayer;
304+
if (bigSurOrNewer) {
305+
rootLayer = self.superlayer;
306+
} else {
307+
rootLayer = self;
308+
}
309+
296310
NSArray<CALayer *> *unexpandedViews = rootLayer.sublayers[self.sublayers.count - 1].sublayers[0].sublayers;
297311
NSArray<CALayer *> *expandedViews = rootLayer.sublayers[self.sublayers.count - 1].sublayers[1].sublayers;
298-
312+
299313
if (bigSurOrNewer) {
300-
unexpandedViews = rootLayer.sublayers[2].sublayers[0].sublayers;
301-
expandedViews = rootLayer.sublayers[2].sublayers[1].sublayers;
314+
unexpandedViews = rootLayer.sublayers[2].sublayers[0].sublayers;
315+
expandedViews = rootLayer.sublayers[2].sublayers[1].sublayers;
302316
}
303317

304318
int numMonitors = MAX((int)unexpandedViews.count, (int)expandedViews.count);
@@ -307,7 +321,7 @@ - (void)setFrame:(CGRect)arg1 {
307321
int selected = getSelected((!unexpandedViews || !unexpandedViews.count) ? expandedViews : unexpandedViews);
308322

309323
// Get all of the names
310-
NSMutableArray* names = getNamesFromPlist();
324+
NSMutableArray<NSMutableArray<NSMutableDictionary *> *> *names = getNamesFromPlist();
311325

312326
if (names.count == 0) {
313327
ZKOrig(void, arg1);
@@ -318,7 +332,7 @@ - (void)setFrame:(CGRect)arg1 {
318332
NSMutableArray *possibleMonitors = [[NSMutableArray alloc] init];
319333
for (int i = 0; i < names.count; i++) {
320334
if (
321-
((NSArray *)names[i]).count == numMonitors && // Same number of monitors
335+
names[i].count == numMonitors && // Same number of monitors
322336
[names[i][selected][@"selected"] boolValue] // Same index is selected
323337
) {
324338
[possibleMonitors addObject:[NSNumber numberWithInt:i]];
@@ -368,10 +382,11 @@ - (void)setFrame:(CGRect)arg1 {
368382
monitorIndex += 1;
369383

370384
// So that it doesn't change sizes on switching spaces
371-
if (!bigSurOrNewer)
372-
refreshFrames(rootLayer);
373-
else
374-
refreshFramesSur(rootLayer, self);
385+
if (!bigSurOrNewer) {
386+
refreshFrames(rootLayer);
387+
} else {
388+
refreshFramesSur(rootLayer, self);
389+
}
375390
}
376391
ZKOrig(void, arg1);
377392
}
@@ -391,16 +406,19 @@ - (BOOL)probablyDesktopSwitcher:(CGRect)rect {
391406
return false;
392407
}
393408

409+
// Get all of the monitors
410+
CGDirectDisplayID displayArray[kMaxDisplays];
411+
uint32_t displayCount;
412+
CGGetActiveDisplayList(kMaxDisplays, displayArray, &displayCount);
413+
394414
// Is the width of the full screen (one of them)
395-
NSArray *const screenArray = [NSScreen screens];
396-
for (int i = 0; i < screenArray.count; i++) {
397-
NSScreen *const screen = [screenArray objectAtIndex:i];
398-
if (screen.visibleFrame.size.width == rect.size.width) {
415+
for (int i = 0; i < displayCount; i++) {
416+
if (CGDisplayPixelsWide(displayArray[i]) == rect.size.width) {
399417
return true;
400418
}
401419
}
402420

403-
// Default to NO
421+
// Default to false
404422
return false;
405423
}
406424

0 commit comments

Comments
 (0)