Skip to content

Commit

Permalink
Merge pull request #16 from dado3212/Folders
Browse files Browse the repository at this point in the history
Fixing some multi-monitor bugs, blank screen setting
  • Loading branch information
dado3212 authored Apr 18, 2018
2 parents c827dcf + cbc4e9a commit 7e37a2c
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 9 deletions.
7 changes: 7 additions & 0 deletions SpacesRenamer/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return event
}

// Create the bundle folder if it doesn't exist
do {
try FileManager.default.createDirectory(atPath: Utils.libraryPath.appending("/Containers/\(Bundle.main.bundleIdentifier!)"), withIntermediateDirectories: true, attributes: nil)
} catch {
print("Not really sure.")
}

nameChangeWindow.contentViewController = ViewController.freshController()
hiddenPopover.contentViewController = ViewController.freshController()
hiddenPopover.animates = false
Expand Down
2 changes: 1 addition & 1 deletion SpacesRenamer/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.3.1</string>
<string>1.4.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
Expand Down
11 changes: 11 additions & 0 deletions SpacesRenamer/NameChangeWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Cocoa

class NameChangeWindow: NSWindow {

override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
self.title = "Spaces Renamer"
Expand All @@ -30,6 +31,16 @@ class NameChangeWindow: NSWindow {
self.standardWindowButton(.zoomButton)?.isHidden = true
}

// Close self on 'esc'
override func keyDown(with event: NSEvent) {
if (event.keyCode == Utils.escapeKey) {
if let appDelegate = NSApplication.shared.delegate as? AppDelegate {
appDelegate.closeNameChangeWindow(sender: nil)
}
}
super.keyDown(with: event)
}

func refresh() {
DispatchQueue.main.async {
if let appDelegate = NSApplication.shared.delegate as? AppDelegate, let button = appDelegate.statusItem.button {
Expand Down
2 changes: 2 additions & 0 deletions SpacesRenamer/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ class Utils {
static let customNamesPlist = Utils.libraryPath.appending("/Containers/\(Bundle.main.bundleIdentifier!)/com.alexbeals.spacesrenamer.plist")
static let listOfSpacesPlist = Utils.libraryPath.appending("/Containers/\(Bundle.main.bundleIdentifier!)/com.alexbeals.spacesrenamer.currentspaces.plist")
static let spacesPath = Utils.libraryPath.appending("/Preferences/com.apple.spaces.plist")

static let escapeKey: UInt16 = 0x35
}
9 changes: 9 additions & 0 deletions SpacesRenamer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ViewController: NSViewController {
}

snippet.label.stringValue = "\(i)"
snippet.textField.delegate = self
self.view.addSubview(snippet)
snippets.append(snippet)

Expand Down Expand Up @@ -173,6 +174,14 @@ class ViewController: NSViewController {
}
}

extension ViewController: NSTextFieldDelegate {
override func cancelOperation(_ sender: Any?) {
if let appDelegate = NSApplication.shared.delegate as? AppDelegate {
appDelegate.closeNameChangeWindow(sender: nil)
}
}
}

extension ViewController {
static func freshController() -> ViewController {
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
Expand Down
Binary file modified build/spaces-renamer.zip
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion spaces-renamer/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.3.1</string>
<string>1.4.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
50 changes: 43 additions & 7 deletions spaces-renamer/spacesRenamer.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ static void setTextLayer(CALayer *view, NSString *newString) {
}
}

// The highlighted space has 2 sublayers, while as a normal space only has 1
static int getSelected(NSArray<CALayer *> *views) {
for (int i = 0; i < views.count; i++) {
if (views[i].sublayers.count > 1) {
return i;
}
}
return -1;
}

/*
1. Load the customNamesPlist for named spaces
2. Load the listOfSpacesPlist to get the current list of spaces
3. Crosslist and return the custom names for each plist
3. Crosslist and return the custom names for each plist, and whether it's selected
*/
static NSMutableArray *getNamesFromPlist() {
NSDictionary *dictOfNames = [NSDictionary dictionaryWithContentsOfFile:customNamesPlist];
Expand All @@ -61,15 +71,21 @@ static void setTextLayer(CALayer *view, NSString *newString) {

for (int i = 0; i < listOfMonitors.count; i++) {
NSArray *listOfSpaces = [listOfMonitors[i] valueForKeyPath:@"Spaces"];
NSString *selected = [listOfMonitors[i] valueForKeyPath:@"Current Space.uuid"];

NSMutableArray *monitorNames = [NSMutableArray arrayWithCapacity:listOfSpaces.count];
for (int j = 0; j < listOfSpaces.count; j++) {
id name = [dict objectForKey:listOfSpaces[j][@"uuid"]];
NSString *uuid = listOfSpaces[j][@"uuid"];
id name = [dict objectForKey:uuid];
NSMutableDictionary *screenDict = [NSMutableDictionary dictionary];
screenDict[@"selected"] = @([uuid isEqualToString:selected]);
monitorNames[j] = screenDict;
if (name != nil) {
monitorNames[j] = name;
screenDict[@"name"] = name;
} else {
monitorNames[j] = @"";
screenDict[@"name"] = @"";
}
monitorNames[j] = screenDict;
}
newNames[i] = monitorNames;
}
Expand Down Expand Up @@ -157,15 +173,35 @@ - (void)setBounds:(CGRect)arg1 {
NSArray<CALayer *> *unexpandedViews = self.sublayers[3].sublayers[0].sublayers;
NSArray<CALayer *> *expandedViews = self.sublayers[3].sublayers[1].sublayers;

// Get which of the spaces in the current dock is selected
int selected = getSelected(unexpandedViews);

// Get all of the names
NSMutableArray* names = getNamesFromPlist();

// Take a best guess at which monitor it is
NSMutableArray *possibleMonitors = [[NSMutableArray alloc] init];
for (int i = 0; i < names.count; i++) {
if (
((NSArray *)names[i]).count == unexpandedViews.count && // Same number of monitors
[names[i][selected][@"selected"] boolValue] // Same index is selected
) {
[possibleMonitors addObject:[NSNumber numberWithInt:i]];
}
}
// If only one monitor, good to go
// If more than one monitor, then just go with the same cycling as it appears to have been last time it was good to go
if (possibleMonitors.count == 1) {
monitorIndex = [possibleMonitors[0] intValue];
}
[possibleMonitors release];

monitorIndex = monitorIndex % names.count;

for (int i = 0; i < ((NSArray*)names[monitorIndex]).count; i++) {
if (names[monitorIndex][i] != nil && ![names[monitorIndex][i] isEqualToString:@""] && i < MAX(expandedViews.count, unexpandedViews.count)) {
setTextLayer(expandedViews[i], names[monitorIndex][i]);
setTextLayer(unexpandedViews[i], names[monitorIndex][i]);
if (names[monitorIndex][i][@"name"] != nil && ![names[monitorIndex][i][@"name"] isEqualToString:@""] && i < unexpandedViews.count) {
setTextLayer(expandedViews[i], names[monitorIndex][i][@"name"]);
setTextLayer(unexpandedViews[i], names[monitorIndex][i][@"name"]);
}
}

Expand Down

0 comments on commit 7e37a2c

Please sign in to comment.