Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions Spaceman/Helpers/IconCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class IconCreator {
case .desktopNumbersAndRects:
icons = createRectWithNumbersIcons(icons, spaces, desktopsOnly: true)
case .text:
iconSize.width = 49
icons = createNamedIcons(icons, spaces)
default:
break
Expand Down Expand Up @@ -117,37 +116,58 @@ class IconCreator {
}

private func createNamedIcons(_ icons: [NSImage], _ spaces: [Space]) -> [NSImage] {
var index = 0
var newIcons = [NSImage]()
let padding = CGFloat(12)
let cornerRadius = CGFloat(3)

for space in spaces {
let textRect = NSRect(origin: CGPoint.zero, size: iconSize)
let spaceText = NSString(string: "\(space.spaceNumber): \(space.spaceName.uppercased())")
let iconImage = NSImage(size: iconSize)
let textImage = NSImage(size: iconSize)
let isActive = space.isCurrentSpace

textImage.lockFocus()
spaceText.drawVerticallyCentered(
in: textRect,
withAttributes: getStringAttributes(alpha: 1))
textImage.unlockFocus()
let textAttrs = getStringAttributes(alpha: 1, fontSize: 10)
let textSize = spaceText.size(withAttributes: textAttrs)
let dynamicWidth = max(textSize.width + padding, 49)
let dynamicSize = NSSize(width: dynamicWidth, height: iconSize.height)
let iconImage = NSImage(size: dynamicSize)

iconImage.lockFocus()
icons[index].draw(
in: textRect,
from: NSRect.zero,
operation: NSCompositingOperation.sourceOver,
fraction: 1.0)
textImage.draw(
in: textRect,
from: NSRect.zero,
operation: NSCompositingOperation.destinationOut,
fraction: 1.0)

let bgRect = NSRect(origin: CGPoint.zero, size: dynamicSize)
let bgPath = NSBezierPath(roundedRect: bgRect.insetBy(dx: 1, dy: 0.5), xRadius: cornerRadius, yRadius: cornerRadius)

if isActive {
// Active: filled background, text cut out via destinationOut
NSColor.black.setFill()
bgPath.fill()

let textImage = NSImage(size: dynamicSize)
textImage.lockFocus()
spaceText.drawVerticallyCentered(
in: bgRect,
withAttributes: textAttrs)
textImage.unlockFocus()

textImage.draw(
in: bgRect,
from: NSRect.zero,
operation: NSCompositingOperation.destinationOut,
fraction: 1.0)
} else {
// Inactive: border + direct text, all in black (template handles color)
NSColor.black.withAlphaComponent(0.6).setStroke()
bgPath.lineWidth = 1.0
bgPath.stroke()

let inactiveAttrs = getStringAttributes(alpha: 0.6, fontSize: 10)
spaceText.drawVerticallyCentered(
in: bgRect,
withAttributes: inactiveAttrs)
}

iconImage.isTemplate = true
iconImage.unlockFocus()

newIcons.append(iconImage)
index += 1
}

return newIcons
Expand Down Expand Up @@ -177,8 +197,8 @@ class IconCreator {
}

func mergeIcons(_ iconsWithDisplayProperties: [(image: NSImage, nextSpaceOnDifferentDisplay: Bool)]) -> NSImage {
let combinedIconWidth = iconsWithDisplayProperties.reduce(CGFloat.zero) { $0 + $1.image.size.width }
let numIcons = iconsWithDisplayProperties.count
let combinedIconWidth = CGFloat(numIcons) * iconSize.width
let accomodatingGapWidth = CGFloat(numIcons - 1) * gapWidth
let accomodatingDisplayGapWidth = CGFloat(displayCount - 1) * displayGapWidth
let totalWidth = combinedIconWidth + accomodatingGapWidth + accomodatingDisplayGapWidth
Expand All @@ -193,9 +213,9 @@ class IconCreator {
operation: NSCompositingOperation.sourceOver,
fraction: 1.0)
if icon.nextSpaceOnDifferentDisplay {
xOffset += iconSize.width + displayGapWidth
xOffset += icon.image.size.width + displayGapWidth
} else {
xOffset += iconSize.width + gapWidth
xOffset += icon.image.size.width + gapWidth
}
}
image.isTemplate = true
Expand Down
2 changes: 1 addition & 1 deletion Spaceman/Helpers/SpaceObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class SpaceObserver {
if let pid = spaceInfo["pid"] as? pid_t,
let app = NSRunningApplication(processIdentifier: pid),
let name = app.localizedName {
return name.prefix(3).uppercased()
return name.prefix(10).uppercased()
}

return "FUL"
Expand Down
4 changes: 2 additions & 2 deletions Spaceman/View/PreferencesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ struct PreferencesView: View {
}
}
TextField(
"Name (max 3 char.)",
"Name (max 10 char.)",
text: Binding(
get: {prefsVM.spaceName},
set: {prefsVM.spaceName = $0.prefix(3).trimmingCharacters(in: .whitespacesAndNewlines)}),
set: {prefsVM.spaceName = $0.prefix(10).trimmingCharacters(in: .whitespacesAndNewlines)}),
onCommit: updateName)

Button("Update name") {
Expand Down