Page 1 of 1

Windows position/size wrong on some Mac

Posted: Wed Jul 01, 2015 10:20 am
by Kukulkan
Hello,

I get position and dimensions of a PureBasic Window using WindowX(), WindowY(), WindowWidth() and WindowHeight(). On my Mac it returns correct values. On the Mac of some customers it returns only half the number of real pixels. So all the coordinates and sizes seem to be divided by 2 before I get them from PureBasic.

I also check the system DPI (snippet):

Code: Select all

; Get DPI on Mac (default 72 DPI)
Protected dpi.NSSize
Protected Factor.f      = 1
Protected screen.i      = CocoaMessage(0, 0, "NSScreen mainScreen")
Protected description.i = CocoaMessage(0, screen, "deviceDescription")
CocoaMessage(@dpi, CocoaMessage(0, description, "objectForKey:$", @"NSDeviceResolution"), "sizeValue")
If dpi\width > 0
  Factor.f = dpi\width / 72; 72 DPI is MacOS default for 100%
EndIf

Protected in.s = "WindowX: "+Str(WindowX(tWindow))+#CR$+
                 "WindowY: "+Str(WindowY(tWindow))+#CR$+
                 "WindowWidth: "+Str(WindowWidth(tWindow))+#CR$+
                 "WindowHeight: "+Str(WindowHeight(tWindow))+#CR$+
                 "DPI factor: "+Str(Factor.f)

MessageRequester("Debug!", in.s)
But on the affected systems, this is returning a factor of 1 only.

How I get the values: I do a full screenshot and measure the pixels in a painting program. The debug values are exactly the half of the real number of pixels.

Any idea what's wrong?

Best

Kukulkan

Re: Windows position/size wrong on some Mac

Posted: Wed Jul 01, 2015 7:24 pm
by Danilo
See
- High Resolution Guidelines for OS X - High Resolution Explained: Features and Benefits
OS X refers to screen size in points, not pixels.
[...]
(For more information, see Getting Scale Information)
The backingScaleFactor method of NSScreen and NSWindow seems to be the key here. It points to => Converting to and from the Backing Store

Re: Windows position/size wrong on some Mac

Posted: Wed Jul 01, 2015 7:36 pm
by wilbert
Danilo is right.
There are logical pixels and physical pixels.

Code: Select all

CocoaMessage(@bsf.CGFloat, CocoaMessage(0, 0, "NSScreen mainScreen"), "backingScaleFactor")
Debug bsf
will display 2.0 for a retina display

Re: Windows position/size wrong on some Mac

Posted: Thu Jul 02, 2015 5:03 pm
by Kukulkan
Thank you Wilbert, Danilo, but even on the affected Machines it still returns 1 as the value. I do not get a factor of 2.

Any other idea?

Re: Windows position/size wrong on some Mac

Posted: Thu Jul 02, 2015 5:38 pm
by wilbert
Does any of these return the value you want ?

Code: Select all

Define S.s, Frame.NSRect

If OpenWindow(0, 0, 0, 400, 250, "Coordinate test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    EditorGadget(0, 10, 10, 380, 230)
    
    CocoaMessage(@Frame, WindowID(0), "frame")
    S = "Frame size: (" + Frame\size\width + "," + Frame\size\height + ")" + #CRLF$
    
    CocoaMessage(@Frame, CocoaMessage(0, 0, "NSScreen mainScreen"), "convertRectToBacking:@", @Frame)
    S + "Converted size: (" + Frame\size\width + "," + Frame\size\height + ")" + #CRLF$
    
    SetGadgetText(0, S)
    
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Windows position/size wrong on some Mac

Posted: Mon Jul 06, 2015 5:01 pm
by Kukulkan
Hello Wilbert,

indeed, this seem to return the correct values. I also found some post in the internet saying that the backingScaleFactor does not return correct values if NSHighResolutionCapable is not set to "yes" inside Info.plist. I do not want to set this value as it is having other drawbacks for me.

Now I'm thinking about calculation of the factor by myself using the code above? Simply by letting the function convert the value 100 with the backing and do some division to get the factor.

Code: Select all

Protected r.NSRect
r\origin\x = 100
r\origin\y = 100
r\size\height = 100
r\size\width = 100
CocoaMessage(@r, CocoaMessage(0, 0, "NSScreen mainScreen"), "convertRectToBacking:@", @r)
Protected Factor.f = r\size\width / 100
What do you think? Is this a valid way?

Re: Windows position/size wrong on some Mac

Posted: Mon Jul 06, 2015 8:17 pm
by wilbert
I see no reason why that shouldn't be valid.

Re: Windows position/size wrong on some Mac

Posted: Tue Jul 07, 2015 3:57 pm
by Kukulkan
This also does not work. The values are only calculated if I add

<key>NSHighResolutionCapable</key> <true/>

to info.plist.

Any idea about how to get this factor without telling MacOS that I'm HiRes capable (because it is not)?

Best,

Volker