Skip to content

Commit

Permalink
Merge pull request #69 from dado3212/CrashFix
Browse files Browse the repository at this point in the history
v1.9.0
  • Loading branch information
dado3212 authored Sep 23, 2020
2 parents 838f61d + 5d38329 commit 31aeb73
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
13 changes: 11 additions & 2 deletions SpacesRenamer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class ViewController: NSViewController {

var monitorPairings: [[NSScrollView: [DesktopSnippet]]] = []

let widthInDesktops = 6
var widthInDesktops = 6
let desktopWidth = 140.0

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -63,6 +64,14 @@ class ViewController: NSViewController {

// Get the # of spaces in the maximum monitor
let maxSpacesPerMonitor = allMonitors.reduce(Int.min, { max($0, (($1 as? NSDictionary)?.value(forKey: "Spaces") as! NSArray).count) })


guard let mainScreen = NSScreen.main else {
fatalError("Bugged")
}
// Maximize the number of desktops
let screenWidth = Double(mainScreen.visibleFrame.width)
widthInDesktops = Int(screenWidth / desktopWidth) - 1

// For each monitor
for j in 1...allMonitors.count {
Expand Down Expand Up @@ -202,7 +211,7 @@ class ViewController: NSViewController {
maxSpacesPerMonitor == 1 ? 2 : 1, // minimum width of two spaces so the buttons render correctly
allSpaces.count
))
) * 140.0 + 10.0
) * desktopWidth + 10.0
))
constraints.append(widthConstraint)
self.view.addConstraints([widthConstraint])
Expand Down
Binary file modified build/spaces-renamer.zip
Binary file not shown.
8 changes: 4 additions & 4 deletions spaces-renamer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
INFOPLIST_FILE = "$(SRCROOT)/spaces-renamer/Info.plist";
INSTALL_PATH = "/Library/Application Support/MacEnhance/Plugins";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.alexbeals.spaces-renamer";
PRODUCT_NAME = "spaces-renamer";
WRAPPER_EXTENSION = bundle;
Expand All @@ -352,7 +352,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
INFOPLIST_FILE = "$(SRCROOT)/spaces-renamer/Info.plist";
INSTALL_PATH = "/Library/Application Support/MacEnhance/Plugins";
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.alexbeals.spaces-renamer";
PRODUCT_NAME = "spaces-renamer";
WRAPPER_EXTENSION = bundle;
Expand Down Expand Up @@ -465,7 +465,7 @@
INFOPLIST_FILE = SpacesRenamer/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.alexbeals.SpacesRenamer;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -516,7 +516,7 @@
INFOPLIST_FILE = SpacesRenamer/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 1.8.1;
MARKETING_VERSION = 1.9.0;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.alexbeals.SpacesRenamer;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
33 changes: 21 additions & 12 deletions spaces-renamer/spacesRenamer.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,27 @@ static void overrideTextLayer(CALayer *view, NSString *newString, double width,

// Gets the text area, and renders how large it would be with the new dimensions
// Uses this for calculating how far they should be offset by
static double getTextSizeHelper(CATextLayer *textLayer, NSString *string) {
CFRange textRange = CFRangeMake(0, string.length);
CFMutableAttributedStringRef attributedString = CFAttributedStringCreateMutable(kCFAllocatorDefault, string.length);
CFAttributedStringReplaceString(attributedString, CFRangeMake(0, 0), (CFStringRef) string);
CFAttributedStringSetAttribute(attributedString, textRange, kCTFontAttributeName, ((CATextLayer *)textLayer).font);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedString);
CFRange fitRange;
CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, textRange, NULL, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX), &fitRange);
CFRelease(framesetter);
CFRelease(attributedString);
return frameSize.width;
}

static double getTextSize(CALayer *view, NSString *string) {
double textSize = -1;
CATextLayer *textLayer = getTextLayer(view);
if (textLayer != nil) {
CFRange textRange = CFRangeMake(0, string.length);
CFMutableAttributedStringRef attributedString = CFAttributedStringCreateMutable(kCFAllocatorDefault, string.length);
CFAttributedStringReplaceString(attributedString, CFRangeMake(0, 0), (CFStringRef) string);
CFAttributedStringSetAttribute(attributedString, textRange, kCTFontAttributeName, ((CATextLayer *)textLayer).font);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedString);
CFRange fitRange;
CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, textRange, NULL, CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX), &fitRange);
CFRelease(framesetter);
CFRelease(attributedString);
return frameSize.width;
// Works around bug where CTFramesetterSuggestFrameSizeWithConstraints returns 0 for
// strings entirely composed of whitespace
return getTextSizeHelper(textLayer, [string stringByAppendingString:@".."]) - getTextSizeHelper(textLayer, @".");
}
return textSize;
return -1;
}

// The highlighted space has 2 sublayers, while as a normal space only has 1
Expand Down Expand Up @@ -195,6 +200,7 @@ static int getSelected(NSArray<CALayer *> *views) {
ZKSwizzleInterface(_SRCALayer, CALayer, CALayer);
@implementation _SRCALayer
- (void)setFrame:(CGRect)arg1 {
CGRect orig = arg1;
id possibleWidth = objc_getAssociatedObject(self, &OVERRIDDEN_WIDTH);
if (possibleWidth && [possibleWidth isKindOfClass:[NSNumber class]] && self.class == NSClassFromString(@"CALayer")) {
arg1.size.width = [possibleWidth doubleValue] + 20;
Expand Down Expand Up @@ -225,6 +231,9 @@ - (void)setFrame:(CGRect)arg1 {
}
}
}
if (arg1.size.width == 0.0 && orig.size.width != 0.0) {
return ZKOrig(void, orig);
}

return ZKOrig(void, arg1);
}
Expand Down

0 comments on commit 31aeb73

Please sign in to comment.