feat(macos): Add fontFamily support for System Tray title#77
Open
lucaslopes wants to merge 1 commit intoantler119:mainfrom
Open
feat(macos): Add fontFamily support for System Tray title#77lucaslopes wants to merge 1 commit intoantler119:mainfrom
lucaslopes wants to merge 1 commit intoantler119:mainfrom
Conversation
- Introduced `fontFamily` and `preferMonospacedFallback` parameters to customize the title's font in the system tray. - Updated the macOS implementation to handle custom fonts and fallback options. - Adjusted Dart method signatures and added necessary logic for font handling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Custom Font
Problem
The macOS tray title currently enforces the standard variable-width system font. This causes visual jitter when displaying dynamic data (like clocks, countdowns, or network speeds) because the width of the text changes constantly.
Solution
This PR adds an optional
fontFamilyparameter toinitSystemTrayandsetSystemTrayInfo.fontFamilyis omitted, it behaves exactly as before (standard system font)."system_monospaced"to automatically useNSFont.monospacedSystemFont(SF Mono) on macOS 10.15+, ensuring perfect alignment for tabular data.Overview
The system tray plugin supports custom fonts for the tray title, allowing you to:
Important: Stateless Font Behavior
fontFamily(andpreferMonospacedFallbackif needed) every time you update the titlesetTitle()without font parameters if you want to preserve a custom fontMethods Overview
1.
initSystemTray()- Initial SetupUse this method when first creating the system tray. This is where you set up the initial font.
2.
setSystemTrayInfo()- Update Title with Font (Recommended)Use this method to update the title while preserving or changing the font. This is the recommended method for updating titles with custom fonts.
3.
setTitle()- Convenience Method (Also Works)The
setTitle()method now accepts font parameters for convenience. It's equivalent tosetSystemTrayInfo()but with a simpler API.Font Options
System Monospaced Font
The easiest way to get a monospaced font is to use the special
"system_monospaced"keyword:This uses the system's built-in monospaced font (SF Mono on macOS 10.15+, or FixedPitch on older versions).
Custom Fonts
To use a custom font that you've downloaded:
Step 1: Install the Font
.ttf,.otf, etc.) to install it in Font BookStep 2: Find the Correct Font Name
⌘+i(or View → Show Font Info)Step 3: Use the Font
Fallback Behavior
The
preferMonospacedFallbackparameter controls what happens when a custom font fails to load:preferMonospacedFallback: true→ Falls back to system monospaced fontpreferMonospacedFallback: false(default) → Falls back to standard system font (proportional)When to Use Each
Use
preferMonospacedFallback: truewhen:Use
preferMonospacedFallback: falsewhen:Complete Example: Clock Display
Here's a complete example showing how to create and update a clock display:
Common Mistakes
❌ Wrong: Using
setTitle()without font parameters✅ Correct: Always pass font parameters
❌ Wrong: Setting font only once
✅ Correct: Pass font every time
Finding Font Names
Method 1: Font Book (GUI)
⌘+ito show font infoMethod 2: Terminal (Command Line)
Method 3: Programmatic (Swift/macOS)
If you're debugging, you can print available fonts in Swift:
Troubleshooting
Font Not Working?
Check if font is installed:
Try different font name formats:
"FiraCode-Regular""Fira Code""Fira Code Regular"Check fallback behavior:
preferMonospacedFallback: trueto see if fallback worksVerify font is being passed:
fontFamilyevery time you update the titlesetTitle()without font parametersFont Reverts to Standard?
This happens when you don't pass
fontFamilyin an update. Remember:fontFamilyevery time you update the titlesetSystemTrayInfo()orsetTitle()with font parametersAPI Reference
initSystemTray()setSystemTrayInfo()setTitle()Summary
initSystemTray()for initial setupsetSystemTrayInfo()orsetTitle()with font parameters to updatefontFamilywhen updating titles with custom fontspreferMonospacedFallback: truefor monospaced fonts"system_monospaced"for the easiest monospaced optionsetTitle()without font parameters if you want custom fonts