In general I only use 1 monitor because moving the head easily gives me headaches. A user complained about the following when using 2 monitors:
- move application window/windows to second monitor
- shut down monitor
- windows are gone
- only after changing resolution the windows come back
I was able to reproduce the issue. Even Xojo acts the same way. However, for Xojo after changing the resolution the windows are still lost.
What is the expected behavior here? Do I need to check for multiple monitors? If yes, how? If the main window is on one monitor should the helper windows open on the same monitor?
You have 2 situations at least:
- to stay as is - like you described
- to move on active monitor
On your place I would make option in settings where you will leave choice to user what will happen in situation with more then one monitor.
You can count screens and check resolution vs. your app current position of window and then see which monitor is in use and according to your app settings (which I wrote above) do a any action and apply to your app.
Also keep in mind that you can have many monitors even more then 2 attached to one workstation.
You have on forum already part of code in Xojo how you can see which monitor is in use.
What if the user shut down the two monitors ?
Also: since the number of attached (not powered ON / OFF) is detectd by the OS, I do not know how one can by code know if the user shut down a monitor.
To be clear, do the same (Power OFF) one monitor while you have Apple applications on with window(s) in that monitor and watch what happens (nothing).
This may be a complain from a naive user (a Joe Newbie) or a vicious rant from someone (who may want a refund ?).
BTW: same apply when you clear the monitor lighting !
It is just a case of a Phone user - who is in a black zone - who cry because your application does not works and sent you a screen shot where you can read No carrier available (or so). Fun to read, but what can you tell to the user ?
(try any other application - say map - there and report if it works ?)
I may sound rude, but I am not in that case (or for the user ?).
IMHO: Bogdan advice is fine if the user disconnect the second monitor, but in that case macOS will detect that and act accordingly (report all windows to the other monitor).
@Emile Schwarz There are two different cases at least - not only one! I say that
@Emile: the problem is that macOS doesn’t move the windows back to the remaining monitor.
@Bogdan: thanks for the reply
Okay, did a bit of Googling. The problem is old and one with macOS. The button “Gather windows” in the System Preferences is only available when there are 2 monitors.
I found a bug in the code that restores the window position so I’ll concentrate on this.
Just to be sure: do you meant Power OFF the monitor ?
(or disconnect the Video Connector ?)
@Beatrix Willius …also consider if user of app change system graphic settings and monitors mapping and/or locations of them which is which (primary and so on) while your app is running/up.
does WIndow Activate for the window on the powered off window fire?
if it does, then check SCREEN info to that the left/top of the window are “available”, if not move the window
@Emile Schwarz : yes, I noticed that macOS treats a powered off monitor as still available. I needed to take the cable out to get macOS recognise only one monitor.
@Bogdan Pavlovic : good catch. I noticed that I get negative coordinates when the secondary monitor is placed to the left. The cable I had was too short.
@Dave S: I’ll check this tomorrow. For now my code checks on window open if the saved valued for window left points to a screen that doesn’t exist anymore.
For the record that also happens if a non-primary monitor starts above the primary monitor (since it starts at 0,0). Regardless of the length of your cables and physical placement, you can drag monitors around in the “Arrangement” tab of the display preferences and play with the resulting screen coordinates. It just gets more confusing moving the mouse pointer between them. But it is a quick way to test how your code reacts to various configurations.
- 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
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 )
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.
Noticed this as well. Using its Stand-by functionality is a killer in this manner,
it needs to be switched off completely.
*) It’s me, connected a TV device as an external Macintosh monitor.
Apple Engineers never think someone will power off a connected monitor to a macOS computer and continue to work with. Probably.
Remember: just like Windows today, when you remove the monitor connector, macOS react immediately repositionning all windows / objects previously displayed in the removed Monitor.