Centering a window on the *current* screen, not the *main* screen

  • Hi,

    It seems that [NSWindow center] centers the window on the *main* screen.

    Now how do I center a window on the screen it currently shows on, on a
    multi-monitor Mac?

    I could write my own code to position the window, but I don't know how
    to reproduce the recommended position of the centered window, which is
    slightly above the center.

    Thanks!
  • On 26/01/2010, at 9:10 PM, Oleg Krupnov wrote:

    > Now how do I center a window on the screen it currently shows on, on a
    > multi-monitor Mac?

    You need to consider what this really means. For example, what if a window is placed so appears on more than one monitor at the same time?

    Actually, NSWindow already works this out - its -screen method returns the screen it mostly is placed on (i.e. contains the largest intersected area). That's handy.

    > I could write my own code to position the window, but I don't know how
    > to reproduce the recommended position of the centered window, which is
    > slightly above the center.

    Traditionally, this has been at a position leaving one third of the space above and two thirds below, though I'm not sure if that's still currently the exact placing.

    --Graham
  • Thanks Graham,

    >
    > You need to consider what this really means. For example, what if a window is placed so appears on more than one monitor at the same time?
    >
    > Actually, NSWindow already works this out - its -screen method returns the screen it mostly is placed on (i.e. contains the largest intersected area). That's handy.
    >

    I have actually considered this, and yes, I want my window to center
    on the screen returned by [NSWindow screen].

    >> I could write my own code to position the window, but I don't know how
    >> to reproduce the recommended position of the centered window, which is
    >> slightly above the center.
    >
    >
    > Traditionally, this has been at a position leaving one third of the space above and two thirds below, though I'm not sure if that's still currently the exact placing.
    >

    You are right, except that, according to my experiment, it is one
    fourth above and three fourths below.

    I have written that code and it works identical to [NSWindow center],
    as far as I can see. But maybe there are still caveats I'm unaware of.

    Anyway, it's an annoyance that the [NSWindow center] uses [NSScreen
    mainScreen] instead of [NSWindow screen]. I'd consider it a bug of
    AppKit, indeed.

    Thanks,
  • Am 26.01.2010 um 11:34 Uhr schrieb Graham Cox:

    > Traditionally, this has been at a position leaving one third of the
    > space above and two thirds below, though I'm not sure if that's
    > still currently the exact placing.

    Yup, that's what the Human Interface Guidelines say:

    http://developer.apple.com/mac/library/documentation/UserExperience/Concept
    ual/AppleHIGuidelines/XHIGWindows/XHIGWindows.html#/

    /apple_ref/doc/uid/20000961-BACFHDHE

    "For nondocument windows, the preference is to open new windows
    horizontally centered as shown in Figure 14-32. The vertical position
    should be visually centered: The distance from the bottom of the
    window to the top of the Dock (if it’s at the bottom of the screen)
    should be approximately twice the distance as that from the bottom of
    the menu bar to the top of the window."

    Andreas
  • Oleg Krupnov wrote:

    > Anyway, it's an annoyance that the [NSWindow center] uses [NSScreen
    > mainScreen] instead of [NSWindow screen]. I'd consider it a bug of
    > AppKit, indeed.

    No, just a philosophical difference regarding the meaning and, more importantly, intent of "centering" a window. And it predates AppKit. The goal isn't really to physically center a window on the screen. It's to position it in a defined location that is a focus of user attention. Consider the docs:

    "You typically use this method to place a window—most likely an alert dialog—where the user can’t miss it. This method is invoked automatically when a panel is placed on the screen by the runModalForWindow: method of the NSApplication class."

    Why? Because:

    "Such a placement carries a certain visual immediacy and importance."
  • On Jan 26, 2010, at 2:34 AM, Graham Cox wrote:

    >> I could write my own code to position the window, but I don't know how
    >> to reproduce the recommended position of the centered window, which is
    >> slightly above the center.
    >
    >
    > Traditionally, this has been at a position leaving one third of the space above and two thirds below, though I'm not sure if that's still currently the exact placing.

    Except that this is really "alertPosition" -- not really "centered" (which has other connotations).

    At least there's now a single message to move the window to a "centered" position.

    --
    Glenn L. Austin, Computer Wizard and Race Car Driver        <><
    <http://www.austin-soft.com>
previous month january 2010 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 31
Go to today