NSWindow with NSWindowTitleVisibilityNone saving the wrong frame for user defaults?

My application has an option that allows the user to choose between the standard "full-size" window title / toolbar and the "compact" title bar / toolbar available in the NSWindow 10.10 API. In particular, I use the -titleVisibility method to set either NSWindowTitleVisible or NSWindowTitleHidden depending on the user's preferences. If the user checks the "Compact Titlebar" flag, NSWindowTitleHidden is applied to the window, otherwise the window uses the default style. When the value of the flag changes, the value is saved in the default settings of the application user, and the window is updated / redrawn.

Everything works fine until the application restarts. Each time the application starts, the window grows by how much space is saved, switching from the default window style ( NSWindowTitleVisible ) to the new style ( NSWindowTitleHidden ). Therefore, restarting the application 5-6 times will make the window flush with the menu bar and dock, depending on how large the window is when the flag was originally set.

In other words, it does not look like the window frame is being updated in NSUserDefaults when the property is set. Is there a workaround for this, or am I just missing something? Any advice would be helpful.

Thanks!

+6
source share
3 answers

Try titleVisibility number 1 in Custom Runtime Attributes

enter image description here

1 - corresponding value for NSWindowTitleHidden

 typedef NS_ENUM(NSInteger, NSWindowTitleVisibility) { /* The default mode has a normal window title and titlebar buttons. */ NSWindowTitleVisible = 0, /* The always hidden mode hides the title and moves the toolbar up into the area previously occupied by the title. */ NSWindowTitleHidden = 1, } NS_ENUM_AVAILABLE_MAC(10_10); 

However, this will print a message to the console, complaining that NSWindow does not match the key value encoding for the key titleVisibility in OS X versions prior to 10.10

+2
source

The best (and confirmed working) solution was published at https://openradar.appspot.com/18510665 by pointum:

The problem is that the window size is restored by the system using -[NSWindow setFrameUsingName:] to titleVisibility . Decision:

  • Delete the value "Autosave name" in the interface builder.
  • Define it in the code immediately after setting titleVisibility using -[NSWindow setFrameAutosaveName:] .
+1
source

A simple fix for now is to save and restore the window frame manually , here's how to do it:

In application deletion, when the application terminates, save the window frame

 - (void)applicationWillTerminate:(NSNotification *)notification { [[NSUserDefaults standardUserDefaults] setObject:NSStringFromRect(self.windowController.window.frame) forKey:@"WindowFrameKey"]; } 

In your -awakeFromNib window controller, restore the frame

 - (void)awakeFromNib { if([NSWindow instancesRespondToSelector:@selector(setTitleVisibility:)]) { // Hide Titlebar [self.window setTitleVisibility:NSWindowTitleHidden]; NSString *winFrameString = [[NSUserDefaults standardUserDefaults] stringForKey:@"WindowFrameKey"]; if(winFrameString != nil) { NSRect savedRect = NSRectFromString(winFrameString); if(!NSEqualRects(self.window.frame, savedRect)) { [self.window setFrame:savedRect display:YES animate:NO]; } } } 
0
source

Source: https://habr.com/ru/post/978066/


All Articles