Skip to content

Commit

Permalink
Use image to make menu bar monospace
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Dec 4, 2024
1 parent de93349 commit 488672a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Sources/AppBundle/MenuBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import Common
import Foundation
import SwiftUI

func createTextImage(_ text: String) -> NSImage {
let attributes: [NSAttributedString.Key: Any] = [
.font: NSFont.monospacedSystemFont(ofSize: 0, weight: .regular),
.foregroundColor: NSColor.white
]
let size = (text as NSString).size(withAttributes: attributes)
let image = NSImage(size: size)
image.lockFocus()
(text as NSString).draw(at: .zero, withAttributes: attributes)
image.unlockFocus()
return image
}

public func menuBar(viewModel: TrayMenuModel) -> some Scene {
MenuBarExtra {
let shortIdentification = "\(aeroSpaceAppName) v\(aeroSpaceAppVersion) \(gitShortHash)"
Expand Down Expand Up @@ -52,7 +65,7 @@ public func menuBar(viewModel: TrayMenuModel) -> some Scene {
}.keyboardShortcut("Q", modifiers: .command)
} label: {
// .font(.system(.body, design: .monospaced)) doesn't work unfortunately :(
Text(viewModel.isEnabled ? viewModel.trayText : "⏸️")
Image(nsImage: createTextImage(viewModel.isEnabled ? viewModel.trayText : "⏸️"))
}
}

Expand Down

0 comments on commit 488672a

Please sign in to comment.