Restoring window location - Mac, Windows, Linux

Yes, macOS is strange.

This is the last code is used before switching to some macOS function from Sam:

'load the position of the ParentWindow

if ParentWindow = nil then return
dim isNewPrefs as Boolean = not Globals.thePrefs.IsPrefAvailable(WindowTitle + "_Top")

'get pref values
dim PrefValueTop as Integer = Globals.thePrefs.GetPrefNumber(WindowTitle + "_Top")
dim PrefValueLeft as Integer = Globals.thePrefs.GetPrefNumber(WindowTitle + "_Left")
dim PrefValueWidth as Integer = Globals.thePrefs.GetPrefNumber(WindowTitle + "_Width")
dim PrefValueHeight as Integer = Globals.thePrefs.GetPrefNumber(WindowTitle + "_Height")

if PrefValueWidth = 0 then PrefValueWidth = ParentWindow.Bounds.Width
if PrefValueHeight = 0 then PrefValueHeight = ParentWindow.Bounds.Height
if ParentWindow.MinimumWidth > PrefValueWidth then PrefValueWidth = ParentWindow.MinimumWidth
if ParentWindow.MinimumHeight > PrefValueHeight then PrefValueHeight = ParentWindow.MinimumHeight

'set the rect
dim theRect as REALbasic.Rect
if isNewPrefs then
  theRect = new REALbasic.Rect(100, 100, ParentWindow.Width, ParentWindow.Height) 'dummy values
else
  theRect = new REALbasic.Rect(PrefValueLeft, PrefValueTop, PrefValueWidth, PrefValueHeight)
end if

'Find screen.
Dim screenID as integer
For l as integer = 0 to screenCount - 1
  if theRect.top >= screen(l).top and theRect.center.x > screen(l).left and theRect.center.x < (screen(l).left + screen(l).width) then screenID = l
next
lastScreen = screenID

''extra logging
'Globals.theErrorLog.logitem(currentMethodName + " Screen: " + Str(screenID))
Dim ShowOnScreen as screen = screen(screenID)

'for left and top we need to recalculate the values if the prefs are new, only for top if the prefs are not new
if isNewPrefs then
  if ParentWindow.Width < 900 then PrefValueTop = (Screen(ShowOnScreen).Height - ParentWindow.Bounds.Height)/2
  PrefValueLeft = (Screen(ShowOnScreen).Width - ParentWindow.Width)/2
  theRect = new REALbasic.Rect(PrefValueLeft, PrefValueTop, PrefValueWidth, PrefValueHeight)
end if

dim ScreenHeight as Integer = ShowOnScreen.AvailableHeight
dim theTop as Integer
if ShowOnScreen.AvailableTop < 0 then
  theTop = -min(-theRect.top, -ShowOnScreen.availabletop)
else
  theTop = max(theRect.top, ShowOnScreen.availabletop)
end if

if ShowOnScreen.AvailableTop > 0 then
  theRect.Top = Min(theTop - ShowOnScreen.AvailableTop, ScreenHeight) + ShowOnScreen.AvailableTop
else
  theRect.Top = min(theTop, ScreenHeight)
end if
theRect.Left = min(max(theRect.left, ShowOnScreen.availableLeft), ShowOnScreen.availableLeft + (ShowOnScreen.availableWidth))

'only needed for < Monterey
dim theVersion as System.VersionData = System.Version
if theVersion.MajorVersion < 12 then
  if ParentWindow.ToolbarVisibleMBS and theRect.Height <= ParentWindow.MinimumHeight + 78 then
    theRect.Height = ParentWindow.MinimumHeight + 78
  end if
end if
theRect.Height = min(theRect.height, ShowOnScreen.AvailableHeight)
theRect.Width = min(theRect.Width, ShowOnScreen.AvailableWidth - 20)

ParentWindow.Bounds = theRect

HTH

I don’t think that the code covers every possibility but it should do most.

1 Like