Centering window on screen [RESOLVED]

Mac OSX specific forum
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Centering window on screen [RESOLVED]

Post by coder14 »

Why are windows opened with the screen centered flag not centered vertically on the y axis?

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "Center Screen", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow
Last edited by coder14 on Sun Dec 18, 2016 11:01 am, edited 1 time in total.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Centering window on screen

Post by coder14 »

Still cannot get it to center vertically. A bug?
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Centering window on screen

Post by fsw »

Hi,
using:

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "Center Screen", #PB_Window_SystemMenu)
Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow
shows that #PB_Window_ScreenCentered really does something.

Using:

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "Center Screen", #PB_Window_SystemMenu)
CocoaMessage(0,WindowID(0),"center")
Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow
gives exactly the same result.

Doing a little bit more digging reveals this:
  • The window is placed exactly in the center horizontally and somewhat above center vertically.
    Such a placement carries a certain visual immediacy and importance.
This is the source: https://developer.apple.com/reference/a ... 090-center

So the answer is: it's by Apple's UI design :shock:

If you still want to have your window better "somewhat" centered you have to do it with code...
(quick and dirty...)

Code: Select all

ExamineDesktops()

Procedure TryToCenterTheWindow(window)
  ; not 100% centered on Y axis because of titlebar
  Protected xPos = (DesktopWidth(0) / 2) - (WindowWidth(window) / 2)
  Protected yPos = (DesktopHeight(0) / 2) - (WindowHeight(window) / 2) 
  
  ResizeWindow(window, xPos, yPos, #PB_Ignore, #PB_Ignore)
EndProcedure

OpenWindow(0, 0, 0, 300, 300, "Center Screen", #PB_Window_SystemMenu)
TryToCenterTheWindow(0)

Repeat 
Until WaitWindowEvent() = #PB_Event_CloseWindow
This quick and dirty code doesn't take into account how many monitors are used...

Looking at both solutions Apple's placement really seems to be "nicer" to look at.

As aways: Perception is reality...

:mrgreen:

I am to provide the public with beneficial shocks.
Alfred Hitshock
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Centering window on screen

Post by coder14 »

Thanks fsw! Sorry for the late reply. :oops:
Post Reply