NSWindow with NSWindowTitleVisibilityNone saving incorrect frame to user defaults?
Asked Answered
B

4

7

My app has an option that allows the user to choose between the standard "full-size" window titlebar/toolbar and the "compact" titlebar/toolbar made available in the NSWindow 10.10 API. Specifically, I'm using the -titleVisibility method to set either NSWindowTitleVisible or NSWindowTitleHidden depending on the user's preference. If the user checks the "Compact Titlebar" checkbox, NSWindowTitleHidden is applied to the window, otherwise, the window uses the default style. When the checkbox value changes, the value is stored in the app's user defaults, and the window is updated/redrawn.

Everything works great until the application is relaunched. Each time the app starts up, the window grows by exactly how much space is saved by switching from the default window style (NSWindowTitleVisible) to the new style (NSWindowTitleHidden). So restarting the app 5 – 6 times will make the window flush with the menubar and the dock, depending on how big the window was when the checkbox was initially checked.

In other words, it doesn't seem like the window's frame is being updated in NSUserDefaults when the property is set. Is there a workaround for this, or am I just overlooking something? Any advice would be muy helpful.

Thanks!

Broomrape answered 12/11, 2014 at 6:11 Comment(2)
It's insane that they haven't fixed that yet, I'm having the same issue, setting [self.window setTitleVisibility:NSWindowTitleHidden]; in my window controller's awakeFromNib method. Maybe we're doing something wrong…Psychotic
OSX 10.10's calendar app has that particular look and doesn't have the issue.Psychotic
Q
4

A better (and confirmed working) solution was posted at https://openradar.appspot.com/18510665 by pointum:

The problem is that window size is restored by the system using -[NSWindow setFrameUsingName:] before titleVisibility is set. Solution:

  1. Remove "Autosave Name" value in Interface Builder.
  2. Set it in code right after setting titleVisibility using -[NSWindow setFrameAutosaveName:].
Quillon answered 2/12, 2015 at 18:34 Comment(0)
T
2

Try setting the titleVisibility property to the number 1 in the User Defined Runtime Attributes

enter image description here

1 is the 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 would print a message to the console complaining that NSWindow is not key value coding-compliant for the key titleVisibility on OS X versions previous to 10.10

Tantara answered 6/6, 2015 at 8:0 Comment(0)
P
0

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

In your app delegate, when application terminates, save the window's frame

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

In your window controller's -awakeFromNib method, 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];
            }
        }
    }
Psychotic answered 10/1, 2015 at 15:24 Comment(0)
B
0

In my case, in addition to calling setFrameAutosaveName() and setFrameUsingName() at the end of the whole window setup, I also had to make sure that the window's contentView was being initialized with a non-zero size. Using CGRect(x: 0, y: 0, width: 1, height: 1) was enough.

Baronet answered 7/3, 2023 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.