- There is a built-in function for storing (and restoring) the window locations in the apps preferences. This is good for palettes and such.
However modern macOS versions no longer check to see if that screen is still valid when restoring the window position and sometimes it just doesn’t even bother.
-
It’s pretty easy to store window location with your document, although make sure you use an integer, because if. Screen is to the left of the main screen, you get negative dimensions.
-
When restoring the window dimensions, this is the code I use.
[code]Private Shared Sub setBounds(w as window, inBounds as string)
dim v() as string = split( inBounds, " " )
Dim r as new realbasic.rect( cdbl( v( 0 ) ), cdbl( v( 1 ) ), cdbl( v( 2 ) ), cdbl( v( 3 ) ) )
// — Find screen.
Dim screenID as integer
For l as integer = 0 to screenCount-1
if r.top >= screen( l ).top and r.center.x > screen( l ).left and r.center.x < ( screen( l ).left + screen( l ).width ) then screenID = l
next
Dim s as screen = screen( screenID )
r.top = min( max( r.top, s.availabletop ), s.availableTop + s.availableHeight )
r.left = min( max( r.left, s.availableLeft ), s.availableLeft + ( s.availableWidth - 50 ) )
w.bounds = r
w.width = r.width
w.height = r.height
w.height = r.height - ( w.bounds.height - r.height )
End Sub
[/code]
It basically creates a rect from the saved dimensions, then loops through each connected monitor until it finds one that it thinks is appropriate, if it doesn’t find it, it resorts to screen( 0 ).
Once it has a screen, it checks to make sure that the window fits inside the screen. Please note that this doesn’t recalculate if the user changes the screens while the application is running.