Windows position/size wrong on some Mac

Mac OSX specific forum
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Windows position/size wrong on some Mac

Post 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
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Windows position/size wrong on some Mac

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Windows position/size wrong on some Mac

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Windows position/size wrong on some Mac

Post 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?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Windows position/size wrong on some Mac

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Windows position/size wrong on some Mac

Post 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?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Windows position/size wrong on some Mac

Post by wilbert »

I see no reason why that shouldn't be valid.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Windows position/size wrong on some Mac

Post 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
Post Reply