From 488672aa2ef0c33ab0aba4e30913777edd75750e Mon Sep 17 00:00:00 2001 From: Alex Gartner Date: Tue, 3 Dec 2024 22:02:00 -0800 Subject: [PATCH] Use image to make menu bar monospace --- Sources/AppBundle/MenuBar.swift | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Sources/AppBundle/MenuBar.swift b/Sources/AppBundle/MenuBar.swift index 760489eb..6754d3ca 100644 --- a/Sources/AppBundle/MenuBar.swift +++ b/Sources/AppBundle/MenuBar.swift @@ -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)" @@ -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 : "⏸️")) } }