From c6b788de1988404921b7d851eaf5ddb863466a87 Mon Sep 17 00:00:00 2001 From: Fernando Romiti Date: Thu, 12 Dec 2024 16:40:25 +1300 Subject: [PATCH] Make menu font monospaced using ImageRenderer --- Sources/AppBundle/MenuBar.swift | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Sources/AppBundle/MenuBar.swift b/Sources/AppBundle/MenuBar.swift index 20b7cfa7..070c6fb4 100644 --- a/Sources/AppBundle/MenuBar.swift +++ b/Sources/AppBundle/MenuBar.swift @@ -51,8 +51,35 @@ public func menuBar(viewModel: TrayMenuModel) -> some Scene { terminateApp() }.keyboardShortcut("Q", modifiers: .command) } label: { - // .font(.system(.body, design: .monospaced)) doesn't work unfortunately :( - Text(viewModel.isEnabled ? viewModel.trayText : "⏸️") + if viewModel.isEnabled { + menuLabel(viewModel: viewModel) + .id(viewModel.trayText.hashValue) + } else { + Text("⏸️") + } + } +} + +struct menuLabel: View { + var viewModel: TrayMenuModel + + var body: some View { + let renderer = ImageRenderer(content: imageContent) + if let cgImage = renderer.cgImage { + Image(cgImage, scale: 2, label: Text(viewModel.trayText)) + .resizable() + .aspectRatio(contentMode: .fit) + } else { + // In case image can't be rendered fallback to plain text + Text(viewModel.trayText) + } + } + + // I used a largeTitle font and then use a scale of 2 to make the image look smoother + private var imageContent: some View { + Text(viewModel.trayText) + .font(.system(.largeTitle, design: .monospaced)) + .foregroundStyle(Color.white) } }