Skip to content

Commit

Permalink
add per monitor icons
Browse files Browse the repository at this point in the history
  • Loading branch information
dshnkao committed Feb 9, 2017
1 parent 0d431ea commit 3c54094
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
35 changes: 26 additions & 9 deletions SpaceId/ButtonImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class ButtonImage {
func createImage(spaceInfo: SpaceInfo) -> NSImage {
guard let color = Preference.Color(rawValue: defaults.integer(forKey: Preference.color)),
let style = Preference.Icon(rawValue: defaults.integer(forKey: Preference.icon))
else { return whiteOnBlackOneIcon(space: spaceInfo.keyboardFocusSpace) }
else { return whiteOnBlackOneIcon(text: getTextForSpace(space: spaceInfo.keyboardFocusSpace)) }
switch style {
case Preference.Icon.one:
if color == Preference.Color.blackOnWhite {
return blackOnWhiteOneIcon(space: spaceInfo.keyboardFocusSpace)
return blackOnWhiteOneIcon(text: getTextForSpace(space: spaceInfo.keyboardFocusSpace))
} else {
return whiteOnBlackOneIcon(space: spaceInfo.keyboardFocusSpace)
return whiteOnBlackOneIcon(text: getTextForSpace(space: spaceInfo.keyboardFocusSpace))
}
case Preference.Icon.perMonitor:
if color == Preference.Color.blackOnWhite {
Expand Down Expand Up @@ -43,8 +43,7 @@ class ButtonImage {
] as [String : Any]
}

private func blackOnWhiteOneIcon(space: Space?) -> NSImage {
let text = getTextForSpace(space: space)
private func blackOnWhiteOneIcon(text: String) -> NSImage {
let rect = NSRect(x: 0, y: 0, width: size.width, height: size.height)
let image = NSImage(size: size)
image.lockFocus()
Expand All @@ -57,8 +56,7 @@ class ButtonImage {
return image
}

private func whiteOnBlackOneIcon(space: Space?) -> NSImage {
let text = getTextForSpace(space: space)
private func whiteOnBlackOneIcon(text: String) -> NSImage {
let rect = NSRect(x: 0, y: 0, width: size.width, height: size.height)
let image = NSImage(size: size)
let image1 = NSImage(size: size)
Expand All @@ -82,15 +80,34 @@ class ButtonImage {
return image
}

private func perMonitor(icons: [NSImage], count: Int) -> NSImage {
let image = NSImage(size: CGSize(width: (size.width + 1) * CGFloat(count), height: size.height))
image.lockFocus()
var x: CGFloat = 0
for i in icons {
i.draw(at: NSPoint(x: x, y: 0), from: NSZeroRect, operation: NSCompositingOperation.color, fraction: 1.0)
x += size.width + 2
}
image.unlockFocus()
image.isTemplate = true
return image
}

private func whiteOnBlackPerMonitor(spaceInfo: SpaceInfo) -> NSImage {
return NSImage()
let spaces = spaceInfo.activeSpaces.sorted{ $0.order < $1.order }
let icons = spaces.map { whiteOnBlackOneIcon(text: getTextForSpace(space: $0)) }
return perMonitor(icons: icons, count: spaces.count)
}

private func blackOnWhitePerMonitor(spaceInfo:SpaceInfo) -> NSImage {
return NSImage()
let spaces = spaceInfo.activeSpaces.sorted{ $0.order < $1.order }
let icons = spaces.map { blackOnWhiteOneIcon(text: getTextForSpace(space: $0)) }
return perMonitor(icons: icons, count: spaces.count)
}

private func whiteOnBlackPerSpace(spaceInfo: SpaceInfo) -> NSImage {
let all = Array(1...spaceInfo.totalSpaceCount).map { whiteOnBlackOneIcon(text: String($0)) }
let spaces = spaceInfo.activeSpaces.sorted{ $0.order < $1.order }
return NSImage()
}

Expand Down
1 change: 1 addition & 0 deletions SpaceId/Space.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ struct Space {
let type: Int
let managedSpaceId: Int
let number: Int? // fullscreen apps don't have a number
let order: Int // spaces are in order including fullscreen apps
}
16 changes: 10 additions & 6 deletions SpaceId/SpaceIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ class SpaceIdentifier {
guard let monitors = CGSCopyManagedDisplaySpaces(conn) as? [[String : Any]],
let mainDisplay = NSScreen.main(),
let screenNumber = mainDisplay.deviceDescription["NSScreenNumber"] as? UInt32
else { return SpaceInfo(keyboardFocusSpace: nil, spaces: []) }
else { return SpaceInfo(keyboardFocusSpace: nil, activeSpaces: [], totalSpaceCount: 0) }

//print(monitors)
let cfuuid = CGDisplayCreateUUIDFromDisplayID(screenNumber).takeRetainedValue()
let screenUUID = CFUUIDCreateString(kCFAllocatorDefault, cfuuid) as String
print(screenUUID)
let activeSpaces = parseSpaces(monitors: monitors)
let (activeSpaces, spaceCount) = parseSpaces(monitors: monitors)

print(parseSpaces(monitors: monitors))
return SpaceInfo(keyboardFocusSpace: activeSpaces[screenUUID], spaces: activeSpaces.map{ $0.value })
return SpaceInfo(keyboardFocusSpace: activeSpaces[screenUUID], activeSpaces: activeSpaces.map{ $0.value }, totalSpaceCount: spaceCount)
}

/* returns a mapping of screen uuids and their active space */
private func parseSpaces(monitors: [[String : Any]]) -> [ScreenUUID : Space] {
private func parseSpaces(monitors: [[String : Any]]) -> ([ScreenUUID : Space], Int) {
var ret: [ScreenUUID : Space] = [:]
var spaceCount = 0
var counter = 1
var order = 0
for m in monitors {
guard let current = m["Current Space"] as? [String : Any],
let spaces = m["Spaces"] as? [[String : Any]],
Expand All @@ -45,10 +47,12 @@ class SpaceIdentifier {
let target = filterFullscreen.enumerated().first(where: { $1["uuid"] as? String == uuid})
let number = target == nil ? nil : target!.offset + counter

ret[displayIdentifier] = Space(id64: id64, uuid: uuid, type: type, managedSpaceId: managedSpaceId, number: number)
ret[displayIdentifier] = Space(id64: id64, uuid: uuid, type: type, managedSpaceId: managedSpaceId, number: number, order: order)
spaceCount += spaces.count
counter += filterFullscreen.count
order += 1
}
return ret
return (ret, spaceCount)
}
}

3 changes: 2 additions & 1 deletion SpaceId/SpaceInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import Foundation

struct SpaceInfo {
let keyboardFocusSpace: Space?
let spaces: [Space]
let activeSpaces: [Space]
let totalSpaceCount: Int
}
2 changes: 1 addition & 1 deletion SpaceId/StatusItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class StatusItem: NSObject, NSMenuDelegate {
private let item = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
private let defaults = UserDefaults.standard
private let buttonImage = ButtonImage()
private var currentSpaceInfo = SpaceInfo(keyboardFocusSpace: nil, spaces: [])
private var currentSpaceInfo = SpaceInfo(keyboardFocusSpace: nil, activeSpaces: [], totalSpaceCount: 0)

func createMenu() {
item.menu = menuItems()
Expand Down

0 comments on commit 3c54094

Please sign in to comment.