Skip to content

Commit

Permalink
Merge pull request #12 from dado3212/Settings
Browse files Browse the repository at this point in the history
Open Settings Update (v1.3.1)
  • Loading branch information
dado3212 authored Apr 8, 2018
2 parents d0144ba + 5f26f11 commit e8b29e2
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 26 deletions.
60 changes: 46 additions & 14 deletions SpacesRenamer/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
//

import Cocoa
import Foundation

@NSApplicationMain
@objc
class AppDelegate: NSObject, NSApplicationDelegate {

let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
let popover = NSPopover()
var nameChangeWindow: NameChangeWindow = NameChangeWindow()
let hiddenPopover = NSPopover()
var eventMonitor: EventMonitor?

var workspace: NSWorkspace?
Expand All @@ -37,6 +38,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
)
}

// Watches the file to determine if the spaces update (new one added or deleted)
fileprivate func configureSpaceMonitor() {
let fullPath = (Utils.spacesPath as NSString).expandingTildeInPath
let queue = DispatchQueue.global(qos: .default)
Expand Down Expand Up @@ -64,23 +66,42 @@ class AppDelegate: NSObject, NSApplicationDelegate {
source.resume()
}

// Runs when a space is moved or switched, which confirms that the current list is in the right order
@objc func updateActiveSpaces() {
let info = CGSCopyManagedDisplaySpaces(conn) as! [NSDictionary]
let spacesDict = NSMutableDictionary()
spacesDict.setValue(info, forKey: "Monitors")
spacesDict.write(toFile: Utils.listOfSpacesPlist, atomically: true)

if (nameChangeWindow.isVisible) {
nameChangeWindow.refresh()
}
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
if let button = statusItem.button {
button.image = NSImage(named:NSImage.Name("StatusBarIcon"))
button.action = #selector(togglePopover(_:))
}
popover.contentViewController = ViewController.freshController()

// Listen for left click
NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { [weak self] event in
if event.window == self?.statusItem.button?.window {
self?.togglePopover(self?.statusItem.button)
return nil
}

return event
}

nameChangeWindow.contentViewController = ViewController.freshController()
hiddenPopover.contentViewController = ViewController.freshController()
hiddenPopover.animates = false

eventMonitor = EventMonitor(mask: [.leftMouseDown, .rightMouseDown]) { [weak self] event in
if let strongSelf = self, strongSelf.popover.isShown {
strongSelf.closePopover(sender: event)
if let strongSelf = self {
if (strongSelf.nameChangeWindow.isVisible) {
strongSelf.closeNameChangeWindow(sender: event)
}
}
}

Expand All @@ -100,24 +121,35 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

@objc func togglePopover(_ sender: Any?) {
if popover.isShown {
closePopover(sender: sender)
if nameChangeWindow.isVisible {
closeNameChangeWindow(sender: sender)
} else {
showPopover(sender: sender)
showNameChangeWindow(sender: sender)
}
}

func showPopover(sender: Any?) {
func showNameChangeWindow(sender: Any?) {
NSApplication.shared.activate(ignoringOtherApps: true)
eventMonitor?.start()
self.statusItem.button?.isHighlighted = true
if let button = statusItem.button {
popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
// Use the hidden popover to get the dimensions, and then immediately hide it
hiddenPopover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
if let frame = hiddenPopover.contentViewController?.view.window?.frame {
nameChangeWindow.setFrame(frame, display: true)
}
hiddenPopover.close()

nameChangeWindow.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}
}

func closePopover(sender: Any?) {
popover.performClose(sender)
@objc func closeNameChangeWindow(sender: Any?) {
nameChangeWindow.setIsVisible(false)
DispatchQueue.main.async {
self.statusItem.button?.isHighlighted = false
}
eventMonitor?.stop()
}
}

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.0</string>
<string>1.3.1</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
Expand Down
52 changes: 52 additions & 0 deletions SpacesRenamer/NameChangeWindow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// NameChangeWindow.swift
// SpacesRenamer
//
// Created by Alex Beals on 4/1/18.
// Copyright © 2018 Alex Beals. All rights reserved.
//

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"
self.isOpaque = false
self.isMovable = false
self.backgroundColor = NSColor(calibratedHue: 0, saturation: 0.0, brightness: 100, alpha: 0.95)
// To make it auto-hide on F3
self.collectionBehavior = [.transient, .ignoresCycle, .canJoinAllSpaces]
self.level = .modalPanel

// Adapted from https://stackoverflow.com/a/27613308/3951475 for rounded corners
self.styleMask = [.resizable, .titled, .fullSizeContentView]
self.titlebarAppearsTransparent = true
self.titleVisibility = .hidden
self.showsToolbarButton = false

self.standardWindowButton(.miniaturizeButton)?.isHidden = true
self.standardWindowButton(.closeButton)?.isHidden = true
self.standardWindowButton(.zoomButton)?.isHidden = true
}

func refresh() {
DispatchQueue.main.async {
if let appDelegate = NSApplication.shared.delegate as? AppDelegate, let button = appDelegate.statusItem.button {
// Use the hidden popover to get the dimensions, and then immediately hide it
appDelegate.hiddenPopover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
if let frame = appDelegate.hiddenPopover.contentViewController?.view.window?.frame {
appDelegate.nameChangeWindow.setFrame(frame, display: true)
}
appDelegate.hiddenPopover.close()

if let viewController = appDelegate.nameChangeWindow.contentViewController as? ViewController {
viewController.refreshViews()
}

appDelegate.nameChangeWindow.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}
}
}
}
26 changes: 15 additions & 11 deletions SpacesRenamer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Cocoa

class ViewController: NSViewController {

@IBOutlet var updateButton: NSButton!

var desktops: [String: NSTextField] = [String: NSTextField]()
Expand Down Expand Up @@ -43,7 +42,7 @@ class ViewController: NSViewController {
var prev: DesktopSnippet?
var above: NSView?

print(spacesDict)
let maxSpacesPerMonitor = allMonitors.reduce(Int.min, { max($0, (($1 as? NSDictionary)?.value(forKey: "Spaces") as! NSArray).count) })

for j in 1...allMonitors.count {
let allSpaces = (allMonitors[j-1] as? NSDictionary)?.value(forKey: "Spaces") as! NSArray
Expand Down Expand Up @@ -82,7 +81,6 @@ class ViewController: NSViewController {
let snippet = DesktopSnippet.instanceFromNib()
if (uuid == currentSpace) {
snippet.monitorImage.image = NSImage(named: NSImage.Name("MonitorSelected") )
// snippet.starImage.isHidden = false
}

snippet.label.stringValue = "\(i)"
Expand Down Expand Up @@ -113,9 +111,11 @@ class ViewController: NSViewController {
}
above = prev

let horizontalLayout = NSLayoutConstraint(item: self.view, attribute: .trailing, relatedBy: .greaterThanOrEqual, toItem: prev!, attribute: .trailing, multiplier: 1.0, constant: 10)
constraints.append(horizontalLayout)
self.view.addConstraints([horizontalLayout])
if (allSpaces.count == maxSpacesPerMonitor) {
let horizontalLayout = NSLayoutConstraint(item: self.view, attribute: .trailing, relatedBy: .equal, toItem: prev!, attribute: .trailing, multiplier: 1.0, constant: 10)
constraints.append(horizontalLayout)
self.view.addConstraints([horizontalLayout])
}
}

let verticalConstraint = NSLayoutConstraint(item: updateButton, attribute: .top, relatedBy: .equal, toItem: prev!, attribute: .bottom, multiplier: 1.0, constant: 10)
Expand All @@ -124,9 +124,7 @@ class ViewController: NSViewController {
self.view.addConstraints([verticalConstraint])
}

override func viewWillAppear() {
super.viewWillAppear()

func refreshViews() {
teardownViews()
setupViews()

Expand All @@ -135,7 +133,7 @@ class ViewController: NSViewController {
let spacesRemaining = preferencesDict.value(forKey: "spaces_renaming") as? NSMutableDictionary {
currentMapping = spacesRemaining
}

// Update with the current names
for (uuid, textField) in desktops {
if let newName = currentMapping.value(forKey: uuid) {
Expand All @@ -144,6 +142,12 @@ class ViewController: NSViewController {
}
}

override func viewWillAppear() {
super.viewWillAppear()

refreshViews()
}

@IBAction func quitMenuApp(_ sender: Any) {
NSApp.terminate(nil)
}
Expand All @@ -165,7 +169,7 @@ class ViewController: NSViewController {

// Close the popup
let delegate = NSApplication.shared.delegate as! AppDelegate
delegate.closePopover(sender: delegate)
delegate.closeNameChangeWindow(sender: delegate)
}
}

Expand Down
Binary file modified build/spaces-renamer.zip
Binary file not shown.
4 changes: 4 additions & 0 deletions spaces-renamer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
A80DDE3C1FBCEBDE00794F8F /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = A80DDE3B1FBCEBDE00794F8F /* Utils.swift */; };
A80DDE421FBE2D9C00794F8F /* DesktopSnippet.xib in Resources */ = {isa = PBXBuildFile; fileRef = A80DDE411FBE2D9C00794F8F /* DesktopSnippet.xib */; };
A80DDE441FBE2ECA00794F8F /* DesktopSnippet.swift in Sources */ = {isa = PBXBuildFile; fileRef = A80DDE431FBE2ECA00794F8F /* DesktopSnippet.swift */; };
A81F4EFC2070AEED00660338 /* NameChangeWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81F4EFB2070AEED00660338 /* NameChangeWindow.swift */; };
C398D84715BD8B0700543187 /* spacesRenamer.m in Sources */ = {isa = PBXBuildFile; fileRef = C398D84615BD8B0700543187 /* spacesRenamer.m */; };
FBAE21EE1CFCCA4F007E5A8E /* ZKSwizzle.m in Sources */ = {isa = PBXBuildFile; fileRef = FBAE21ED1CFCCA4F007E5A8E /* ZKSwizzle.m */; };
/* End PBXBuildFile section */
Expand All @@ -34,6 +35,7 @@
A80DDE411FBE2D9C00794F8F /* DesktopSnippet.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = DesktopSnippet.xib; sourceTree = "<group>"; };
A80DDE431FBE2ECA00794F8F /* DesktopSnippet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DesktopSnippet.swift; sourceTree = "<group>"; };
A81F4EFA206C012500660338 /* SpacesRenamerBridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SpacesRenamerBridge.h; sourceTree = "<group>"; };
A81F4EFB2070AEED00660338 /* NameChangeWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NameChangeWindow.swift; sourceTree = "<group>"; };
C398D84615BD8B0700543187 /* spacesRenamer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = spacesRenamer.m; path = "spaces-renamer/spacesRenamer.m"; sourceTree = "<group>"; };
FBAE21EC1CFCCA4F007E5A8E /* ZKSwizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ZKSwizzle.h; path = "spaces-renamer/ZKSwizzle.h"; sourceTree = "<group>"; };
FBAE21ED1CFCCA4F007E5A8E /* ZKSwizzle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ZKSwizzle.m; path = "spaces-renamer/ZKSwizzle.m"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -99,6 +101,7 @@
isa = PBXGroup;
children = (
A80DDE2B1FBCE4D600794F8F /* AppDelegate.swift */,
A81F4EFB2070AEED00660338 /* NameChangeWindow.swift */,
A81F4EFA206C012500660338 /* SpacesRenamerBridge.h */,
A80DDE391FBCE89B00794F8F /* EventMonitor.swift */,
A80DDE2D1FBCE4D600794F8F /* ViewController.swift */,
Expand Down Expand Up @@ -221,6 +224,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A81F4EFC2070AEED00660338 /* NameChangeWindow.swift in Sources */,
A80DDE3A1FBCE89B00794F8F /* EventMonitor.swift in Sources */,
A80DDE441FBE2ECA00794F8F /* DesktopSnippet.swift in Sources */,
A80DDE2E1FBCE4D600794F8F /* ViewController.swift in Sources */,
Expand Down
Binary file not shown.

0 comments on commit e8b29e2

Please sign in to comment.