How to center pref window once before frame saved in defaults

  • I am using a preferences window that is created in code (thanks to
    Dave Batton's DBPrefsWindowController package!)

    What I would like to do is have the window come up centered the first
    time, and from then on save the frame using the saved frame
    capability of NSWindow.

    My thought was to probe the defaults to see if the frame was set
    already, and if not, center the window. However, when you use a saved
    frame names of @"foo", it is actually stored in the user preferences
    as @"NSWindow Frame foo".

    So, my first idea was to create a new string using my frame name, and
    probe the defaults with it:

      if(![[NSUserDefaults standardUserDefaults] objectForKey: @"NSWindow
    Frame foo"]) ...

    However, this seems like a real hack. So I had another idea, I'll
    look and see if the frame changes after I create the window:

        NSString *pos1 = [window stringWithSavedFrame];
        [self setWindowFrameAutosaveName:@foo"];
        NSString *pos2 = [window stringWithSavedFrame];

        if pos1 == pos2 then there was no saved frame so center window

    This also seems like a hack, but maybe a better hack.

    If someone has a better more elegant way of achieving my objective,
    I'd love to hear it.

    David
  • On 6 sept. 07, at 17:40, David Hoerl wrote:

    > I am using a preferences window that is created in code (thanks to
    > Dave Batton's DBPrefsWindowController package!)
    >
    > What I would like to do is have the window come up centered the first
    > time, and from then on save the frame using the saved frame capability
    > of NSWindow.
    >
    > My thought was to probe the defaults to see if the frame was set
    > already, and if not, center the window. However, when you use a saved
    > frame names of @"foo", it is actually stored in the user preferences
    > as @"NSWindow Frame foo".
    >
    > So, my first idea was to create a new string using my frame name, and
    > probe the defaults with it:
    >
    > if(![[NSUserDefaults standardUserDefaults] objectForKey: @"NSWindow
    > Frame foo"]) ...
    >
    > However, this seems like a real hack. So I had another idea, I'll look
    > and see if the frame changes after I create the window:
    >
    > NSString *pos1 = [window stringWithSavedFrame];
    > [self setWindowFrameAutosaveName:@foo"];
    > NSString *pos2 = [window stringWithSavedFrame];
    >
    > if pos1 == pos2 then there was no saved frame so center window

    I have a better solution to suggest:

    "Don't save the frame of the Preferences window".

    This way you will just have to call center on the window after you
    created it and then you will have a behavior that's close to Apple's
    application. Of course, Apple's applications are not working the same
    way as in some applications, if you close the Prefs window and then
    display it again, it will be centered on the main screen and with other
    applications, it will be shown at the location it was before it got
    closed. But at least, in their vast majority, they all center the
    window the first time you display it after the application has been
    launched.
  • I believe the solution is simple (but I have been known to believe a
    great many bizarre things).
    First, don't set any auto save name in interface builder.
    Second, in awakeFromNib, do this

        // Set/Restore window position
        [_mainWindow center];
        [_mainWindow setFrameAutosaveName:@"MainWindow"];

    That should do the trick.

    Chad

    On Sep 6, 2007, at 12:21 PM, <cocoa-dev-request...> wrote:
    > Message: 4
    > Date: Thu, 6 Sep 2007 11:40:03 -0400
    > From: David Hoerl <dhoerl...>
    > Subject: How to center pref window once before frame saved in defaults
    > To: Cocoa <cocoa-dev...>
    > Message-ID: <p06240805c305cc8a85c0@[192.168.2.5]>
    > Content-Type: text/plain; charset="us-ascii" ; format="flowed"
    >
    > I am using a preferences window that is created in code (thanks to
    > Dave Batton's DBPrefsWindowController package!)
    >
    > What I would like to do is have the window come up centered the first
    > time, and from then on save the frame using the saved frame
    > capability of NSWindow.
    >
    > My thought was to probe the defaults to see if the frame was set
    > already, and if not, center the window. However, when you use a saved
    > frame names of @"foo", it is actually stored in the user preferences
    > as @"NSWindow Frame foo".
    >
    > So, my first idea was to create a new string using my frame name, and
    > probe the defaults with it:
    >
    > if(![[NSUserDefaults standardUserDefaults] objectForKey: @"NSWindow
    > Frame foo"]) ...
    >
    > However, this seems like a real hack. So I had another idea, I'll
    > look and see if the frame changes after I create the window:
    >
    > NSString *pos1 = [window stringWithSavedFrame];
    > [self setWindowFrameAutosaveName:@foo"];
    > NSString *pos2 = [window stringWithSavedFrame];
    >
    > if pos1 == pos2 then there was no saved frame so center window
    >
    > This also seems like a hack, but maybe a better hack.
    >
    > If someone has a better more elegant way of achieving my objective,
    > I'd love to hear it.
    >
    > David
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today