diff --git a/SpacesRenamer/AppDelegate.swift b/SpacesRenamer/AppDelegate.swift index ef8ce0c..6c6f0bb 100644 --- a/SpacesRenamer/AppDelegate.swift +++ b/SpacesRenamer/AppDelegate.swift @@ -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 diff --git a/SpacesRenamer/Info.plist b/SpacesRenamer/Info.plist index 5440f28..6d24d92 100644 --- a/SpacesRenamer/Info.plist +++ b/SpacesRenamer/Info.plist @@ -19,7 +19,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.3.1 + 1.4.0 CFBundleVersion 1 LSMinimumSystemVersion diff --git a/SpacesRenamer/NameChangeWindow.swift b/SpacesRenamer/NameChangeWindow.swift index 4ac2e66..b49d4f3 100644 --- a/SpacesRenamer/NameChangeWindow.swift +++ b/SpacesRenamer/NameChangeWindow.swift @@ -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" @@ -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 { diff --git a/SpacesRenamer/Utils.swift b/SpacesRenamer/Utils.swift index 599dd08..7ebbba9 100644 --- a/SpacesRenamer/Utils.swift +++ b/SpacesRenamer/Utils.swift @@ -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 } diff --git a/SpacesRenamer/ViewController.swift b/SpacesRenamer/ViewController.swift index b8cb74a..6ab375c 100644 --- a/SpacesRenamer/ViewController.swift +++ b/SpacesRenamer/ViewController.swift @@ -84,6 +84,7 @@ class ViewController: NSViewController { } snippet.label.stringValue = "\(i)" + snippet.textField.delegate = self self.view.addSubview(snippet) snippets.append(snippet) @@ -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) diff --git a/build/spaces-renamer.zip b/build/spaces-renamer.zip index 8b8169a..185d272 100644 Binary files a/build/spaces-renamer.zip and b/build/spaces-renamer.zip differ diff --git a/spaces-renamer.xcodeproj/project.xcworkspace/xcuserdata/alexbeals.xcuserdatad/UserInterfaceState.xcuserstate b/spaces-renamer.xcodeproj/project.xcworkspace/xcuserdata/alexbeals.xcuserdatad/UserInterfaceState.xcuserstate index 2507816..487304d 100644 Binary files a/spaces-renamer.xcodeproj/project.xcworkspace/xcuserdata/alexbeals.xcuserdatad/UserInterfaceState.xcuserstate and b/spaces-renamer.xcodeproj/project.xcworkspace/xcuserdata/alexbeals.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/spaces-renamer/Info.plist b/spaces-renamer/Info.plist index 1ea106f..a2350ca 100755 --- a/spaces-renamer/Info.plist +++ b/spaces-renamer/Info.plist @@ -17,7 +17,7 @@ CFBundlePackageType BNDL CFBundleShortVersionString - 1.3.1 + 1.4.0 CFBundleSignature ???? CFBundleVersion diff --git a/spaces-renamer/spacesRenamer.m b/spaces-renamer/spacesRenamer.m index 3a0577d..25c5627 100755 --- a/spaces-renamer/spacesRenamer.m +++ b/spaces-renamer/spacesRenamer.m @@ -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 *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]; @@ -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; } @@ -157,15 +173,35 @@ - (void)setBounds:(CGRect)arg1 { NSArray *unexpandedViews = self.sublayers[3].sublayers[0].sublayers; NSArray *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"]); } }