I'm setting up the preferences window for my cool app, which displays some text. In preferences I've set up a button that opens an NSFontPanel
. My app stores the user's preferred text color and font, and always opens with those settings so that the user never has to see text displayed in a color or font they don't prefer.
The problem is, while my app is able to remember these preferences, the NSFontPanel
has trouble. When I first open the font panel, the default values for all the fields are reset. After fiddling with them, closing the panel, and then reopening it, the correct values are retained. The problem only occurs when I first open the panel.
I don't understand why this is happening!
I've been careful to set the font and color for the panel when my app starts, as you can see from this snippet:
def show_entry_font_menu(sender)
font_manager = NSFontManager.sharedFontManager
color_panel = NSColorPanel.sharedColorPanel
font_manager.setDelegate self
color_panel.setDelegate self
font_manager.setSelectedFont(preferences.entry_font, isMultiple:false)
font_panel = font_manager.fontPanel(true)
font_panel.makeKeyAndOrderFront(sender)
attributes = preferences.entry_font_attributes
color = preferences.entry_font_color
font_manager.setSelectedAttributes(attributes, isMultiple:false)
color_panel.setColor(color) if preferences.entry_font_color
self.did_open_font_panel = true
end
There is an oddness in initializing the sharedFontPanel. If you set the font before creating it the first time, that works fine, but setting attributes does not.
What you have to do is makeKeyAndOrderFront first, and then set the attributes. Once the panel has been thus created the first time it will properly reflect setSelectedAttributes.
font_panel.makeKeyAndOrderFront(sender)
font_manager.setSelectedAttributes(attributes, isMultiple:false)