Skip to content

Commit

Permalink
Merge pull request #26 from dado3212/MaxMonitors
Browse files Browse the repository at this point in the history
v1.4.2
  • Loading branch information
dado3212 authored Aug 10, 2018
2 parents fc8a8d9 + 7f2bbac commit b430aff
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 18 deletions.
4 changes: 2 additions & 2 deletions SpacesRenamer/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
button.image = NSImage(named:NSImage.Name("StatusBarIcon"))
}

// Listen for left click
// Listen for left click (without Command)
NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { [weak self] event in
if event.window == self?.statusItem.button?.window {
if event.window == self?.statusItem.button?.window && !event.modifierFlags.contains(NSEvent.ModifierFlags.command) {
self?.togglePopover(self?.statusItem.button)
return nil
}
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.4.1</string>
<string>1.4.2</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
Expand Down
97 changes: 83 additions & 14 deletions SpacesRenamer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class ViewController: NSViewController {
var desktops: [String: NSTextField] = [String: NSTextField]()
var constraints: [NSLayoutConstraint] = []
var snippets: [DesktopSnippet] = []
var viewsToRemove: [NSView] = []

let widthInDesktops = 6

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -28,9 +31,14 @@ class ViewController: NSViewController {
view.removeFromSuperview()
}

for view in viewsToRemove {
view.removeFromSuperview()
}

constraints = []
snippets = []
desktops = [String: NSTextField]()
viewsToRemove = []
}

func setupViews() {
Expand All @@ -39,17 +47,21 @@ class ViewController: NSViewController {
let allMonitors = spacesDict.value(forKeyPath: "Monitors") as? NSArray else { return }

// Keep reference to previous for constraint
var prev: DesktopSnippet?
var prev: NSView?
var above: NSView?

// 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) })

// For each monitor
for j in 1...allMonitors.count {
// Get the spaces for that monitor
let allSpaces = (allMonitors[j-1] as? NSDictionary)?.value(forKey: "Spaces") as! NSArray

// And the selected monitor
let currentSpace = (allMonitors[j-1] as? NSDictionary)?.value(forKeyPath: "Current Space.uuid") as! String

// Create a label for the monitor (if there is more than one monitor)
// If there is more than one monitor, make a label for it, and use it as the 'above' marker
if (allMonitors.count > 1) {
let monitorLabel = NSTextField(labelWithString: "Monitor \(j)")
monitorLabel.font = NSFont(name: "HelveticaNeue-Bold", size: 14)
Expand All @@ -64,14 +76,51 @@ class ViewController: NSViewController {

let leftConstraint = NSLayoutConstraint(item: monitorLabel, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 10)

// Add to arrays so this can be undone later
constraints.append(topConstraint!)
constraints.append(leftConstraint)
self.view.addSubview(monitorLabel)
self.view.addConstraints([topConstraint!, leftConstraint])
viewsToRemove.append(monitorLabel)

above = monitorLabel
}

// Create a scrollview for the monitors
let monitorScrollView = NSScrollView()
monitorScrollView.translatesAutoresizingMaskIntoConstraints = false
monitorScrollView.verticalScrollElasticity = .none
monitorScrollView.drawsBackground = false
// Don't let it scroll if it's not necessary
if (allSpaces.count <= widthInDesktops) {
monitorScrollView.horizontalScrollElasticity = .none
} else {
monitorScrollView.hasHorizontalScroller = true
}

// Make the view that will hold all of the desktops
let snippetView = NSView()
snippetView.translatesAutoresizingMaskIntoConstraints = false
snippetView.wantsLayer = true
self.view.addSubview(monitorScrollView)

// Remember to make it removable
viewsToRemove.append(snippetView)
viewsToRemove.append(monitorScrollView)

// Attach the scrollview to the top left of the window
var verticalConstraint: NSLayoutConstraint?
if (above != nil) {
verticalConstraint = NSLayoutConstraint(item: monitorScrollView, attribute: .top , relatedBy: .equal, toItem: above, attribute: .bottom, multiplier: 1.0, constant: 10)
} else {
verticalConstraint = NSLayoutConstraint(item: monitorScrollView, attribute: .top , relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 10)
}
var horizontalConstraint = NSLayoutConstraint(item: monitorScrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 10)

constraints.append(verticalConstraint!)
constraints.append(horizontalConstraint)
self.view.addConstraints([verticalConstraint!, horizontalConstraint])

prev = nil

// For each space, make a text field
Expand All @@ -86,40 +135,60 @@ class ViewController: NSViewController {

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

desktops[uuid] = snippet.textField

// Attach the desktop to the left of the snippet beforehand
var horizontalConstraint: NSLayoutConstraint?
var verticalConstraint: NSLayoutConstraint?
if (above != nil) {
verticalConstraint = NSLayoutConstraint(item: snippet, attribute: .top , relatedBy: .equal, toItem: above, attribute: .bottom, multiplier: 1.0, constant: 10)
} else {
verticalConstraint = NSLayoutConstraint(item: snippet, attribute: .top , relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 10)
}

let verticalConstraint = NSLayoutConstraint(item: snippet, attribute: .top , relatedBy: .equal, toItem: snippetView, attribute: .top, multiplier: 1.0, constant: 10)

if (prev == nil) {
horizontalConstraint = NSLayoutConstraint(item: snippet, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 10)
horizontalConstraint = NSLayoutConstraint(item: snippet, attribute: .leading, relatedBy: .equal, toItem: snippetView, attribute: .leading, multiplier: 1.0, constant: 10)
} else {
horizontalConstraint = NSLayoutConstraint(item: snippet, attribute: .leading, relatedBy: .equal, toItem: prev, attribute: .trailing, multiplier: 1.0, constant: 10)
}

constraints.append(verticalConstraint!)
constraints.append(verticalConstraint)
constraints.append(horizontalConstraint!)
self.view.addConstraints([verticalConstraint!, horizontalConstraint!])
snippetView.addConstraints([verticalConstraint, horizontalConstraint!])
prev = snippet
}
above = prev

// Attach the bottom right snippet to the bottom right of the snippet view
verticalConstraint = NSLayoutConstraint(item: snippetView, attribute: .trailing, relatedBy: .equal, toItem: prev, attribute: .trailing, multiplier: 1.0, constant: 10)
horizontalConstraint = NSLayoutConstraint(item: snippetView, attribute: .bottom, relatedBy: .equal, toItem: prev, attribute: .bottom, multiplier: 1.0, constant: 10)

constraints.append(verticalConstraint!)
constraints.append(horizontalConstraint)
snippetView.addConstraints([verticalConstraint!, horizontalConstraint])

// Set the scrollView to be the snippetView
monitorScrollView.documentView = snippetView

// Make sure they're the same height
let equalHeight = NSLayoutConstraint(item: monitorScrollView, attribute: .height, relatedBy: .equal, toItem: snippetView, attribute: .height, multiplier: 1.0, constant: 0)
constraints.append(equalHeight)
self.view.addConstraint(equalHeight)

// Set the scrollView to be the width of 6.5 monitors max, or just the normal width
let widthConstraint = NSLayoutConstraint(item: monitorScrollView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: CGFloat(min(Double(widthInDesktops) + 0.5, Double(allSpaces.count)) * 140.0 + 10.0))
constraints.append(widthConstraint)
self.view.addConstraints([widthConstraint])

prev = monitorScrollView
above = monitorScrollView

// If it's the largest one, then make sure that the overall screen is at least that large
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])
}
}

// Move the update button to the bottom
let verticalConstraint = NSLayoutConstraint(item: updateButton, attribute: .top, relatedBy: .equal, toItem: prev!, attribute: .bottom, multiplier: 1.0, constant: 10)
constraints.append(verticalConstraint)

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.4.1</string>
<string>1.4.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down

0 comments on commit b430aff

Please sign in to comment.