Window.Top is under menus on M2

A year or two ago, I was finding that a new document window would be opened at top=0
and the titlebar was under the menus.
I started adding
if self.top < 50 then self.top = 50

which worked for a while.
However, on my new machine (m2 macbook air with notch) ,that isnt enough.
It would need to be 65 to work.

Is there some API call I can make to find out what that number should be/height of the menus?

Check the Screen / DesktopDisplay class for the available rectangle.

when placing the windows.

How about this?
Sample code in the open event of a TextArea:

Declare Function NSClassFromString Lib "AppKit" (classname As CFStringRef) As ptr
Declare Function sharedApplication Lib "AppKit" Selector "sharedApplication" (classRef As Ptr) As Ptr
Declare Function mainMenu Lib "AppKit" Selector "mainMenu" (classRef As Ptr) As Ptr
Declare Function menuBarHeight Lib "AppKit" Selector "menuBarHeight" (classRef As Ptr) As CGFloat

Var sharedApp As Ptr = sharedApplication(NSClassFromString("NSApplication"))
Var mBar As Ptr      = mainMenu(sharedApp)
Var mh As CGFloat    = menuBarHeight(mBar)

Me.AddText("height: " + Str(mh) + EndOfLine)

Thanks both… will investigate!

Both approaches give me useful info.

But now, it looks like my actual problem was screen scaling.
If I set the window top to be something, then it ends up in the wrong place.
(i.e something/2)
By removing the code that handles the placement, OSX puts it under the menu anyway again now.

What’s your default window placement?

Position: Parent Window Screen places the window correctly for all my apps. Position: Default stands out and places things under the menu bar for some of our colleagues who refuse to change this value.

It was ‘stagger’

Hm perhaps we need a ticket / bug report for the Default / Stagger setting.

Parent Window Screen is [NSWindow center] (that slightly north of center position) on the screen of the last in-focus window. Not technically correct for document based apps like VersionTracker, but solves the under-the-menubar problem.

Are you perhaps talking about this effect:

Window top starts underneath menu bar on MacOS Big Sur by 2 pixels
https://tracker.xojo.com/xojoinc/xojo/-/issues/64130

Similar, but due to a combination of factors, it was more severe than that.

That’s the one! It’s very noticeable to me:

Also, you can do this: #64130 - Window top starts underneath menu bar on MacOS Big Sur by 2 pixels

to your tickets with a free app I made!

It was Geoff who changed it to add “by 2 pixels”

The following code worked for both display types:

Var iDisplay As Integer = WhichScreen( Self.Left + ( Self.Width / 2 ), Self.Top + ( Self.Height / 2 ) )
Var DisplayMinWinTop As Integer = DesktopDisplay.DisplayAt( iDisplay ).AvailableTop + ( Self.Top - Self.Bounds.Top )
If Self.Top < DisplayMinWinTop Then
  Self.Top = DisplayMinWinTop
End If

Public Function WhichScreen(x As Integer, y As Integer) As Integer
  Var oDisplay As DesktopDisplay
  
  For iDisplay As Integer = 0 To DesktopDisplay.LastDisplayIndex
    
    oDisplay = DesktopDisplay.DisplayAt( iDisplay )
    
    If x >= oDisplay.AvailableLeft And x <= oDisplay.AvailableWidth + oDisplay.AvailableLeft And _
      y >= oDisplay.AvailableTop And y <= oDisplay.AvailableHeight + oDisplay.AvailableTop Then
      
      Return iDisplay
      
    End If
    
  Next
End Function

I’m using the window centre to work out which screen you are on and then adjusting accordingly.

1 Like

Actually I meant Greg, my bad.