Best practice to move a window to another screen?

Hello,
I’m considering adding a method so that new windows appear on the main screen (the screen having the focus on Mac OS X, where the user is currently being busy).
Currently, my untested code looks like this:

[code]Public Sub ShowWindowOnMainScreen(Extends w As Window)
#if TargetMacOS then
dim nsc As NSScreenMBS

nsc=NSScreenMBS.mainScreen
if nsc=nil then nsc=NSScreenMBS.firstScreen

if nsc<>nil then
  w.Top=Screen(0).Top+nsc.frame.MinY+w.Top
  w.left=Screen(0).Left+nsc.frame.MinX+w.Left
end if

#endif
End Sub
[/code]
(this expects the window to come from screen(0), but, as I recall, Xojo is broken here and always shows windows on screen(0) regardless of the default position settings)
However, this is only a bad attempt. If both screens have a different size (which is often the case), I’m wondering what’s the expected result.
If the window was centered in the first screen, it should stay centered in the destination screen; otherwise, I guess it should remain at the same distance of the same edge it was before.
In the first case, “where” should it be centered (middle of the window, top-left corner; include or not the title bar)?
And, how to differentiate if the window was centered or not in the first place? If the user (or code) moved a centered window by, say, 50 pixels (or 10), is it still being considered “centered”?

A lot of questions for something that looks easy at first.
Advices welcome.

Ok, but what do you refer as “the same location”? Imagine two simple cases to begin; for both, screen(0) is 1024x768 and screen(1) is 640x480:
•a centered window in screen(0) should appear in screen(1). It should be kept centered, right?
•a non-centered window in screen(0) again should appear in screen(1). Here, it must be kept relative to an edge.

But then, if the window is “almost centered” in screen(0) (e.g. 50 pixels above the real centered position), should it be centered/“partially centered”/relative to edge in screen(1)?

This is what the quoted code should do (“should”, as I haven’t tested yet):

w.Top=Screen(0).Top+nsc.frame.MinY+w.Top w.left=Screen(0).Left+nsc.frame.MinX+w.Left
This moves a window from screen(0) to the “main” screen, keeping the same left/top amount from the new screen’s origin point.

Granted. Or simply a desktop computer with 3 screens (mine had this in the past).
For now, I want to move windows when they are created (not after being “idle”). Basically, I often turn off my screen(0), using it for another task (it has several inputs); in such cases, I use another screen to work on my computer. It’s not only for me: I started to recognise any user would prefer to have windows defaulting to the screen (s)he is working on rather than a “unused-at-the-moment” one.
Considering all Xojo windows appear in screen(0) (that’s actually a bug whose Feedback tells it’s “reviewed”, IIRC) and the main screen is always known, I know both the source and destination screens. When that bug will be addressed, well, I’ll put my windows on “main screen” from the IDE. But both screens having probably a different size, I don’t know how to compute the destination coordinates.

Thanks.

[quote=476403:@Emile Schwarz]x,y in Screen(0) = x = 10% (from the left) and y = 15% (from the top).

So, display the window on the other screen at x = 10% (from the left) and y = 15% (from the top).related to that screen.

Of course, width and height are shrinked for the smaller external screen (if it is 640 x 480). Same size if the Screen(1) is larger than Screen(0).

Is this meaningful? (have sense?)[/quote]
Up to a given point, yes. One problem here is what you’ve discovered: since both screens have different sizes, width and height are also proportional. For a centered window, if you just keep the left and top proportionally, the window may fairly well appear non longer centered on the new screen.
I’m starting to think I should reference the point in the center of the window and proportionally compute it to the new screen. Would I take the title bar into account?
I find it weird to having to do all that computation for such a basic task…

Yes, of course. Unless in rare circumstances where you want to prevent the user to move the window (there’s a Cocoa declare for that). As previously stated, I want to show the windows on the screen the user is having the focus (other screens may just be turned off).
Thank you.

I added a “Collect Windows” to my app after a user managed to loose a window:

[code]Function Action(m as NotifyingMenuItem) Handles Action as Boolean

if ScreenCount = 1 then Return True

for currentWindow as Integer = 0 to App.WindowCount
dim ScreenID as Integer = getScreenID(App.Window(currentWindow))
if ScreenID = -1 then
Continue
ElseIf ScreenID > 0 then
'center window on middle of screen 1
App.Window(currentWindow).Left = (Screen(0).AvailableWidth - App.Window(currentWindow).Width) / 2
App.Window(currentWindow).Top = (Screen(0).AvailableHeight - App.Window(currentWindow).Height) / 2
end if

next

Return True

End Function

Private Function getScreenID(theWindow as Window) as Integer

'set the rect
if theWindow = Nil or not theWindow.Visible then Return -1
dim theRect as Realbasic.Rect
dim ToolbarHeight as Integer = theWindow.Bounds.Height - theWindow.height + 1
if ToolbarHeight = 1 then Return -1’going FullScreen
theRect = new REALbasic.Rect(theWindow.Left, theWindow.Top, theWindow.Width, theWindow.Height)

'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

Return screenID

End Function[/code]

Thank you. I’ll use this code when the original window is centered (to put it again centered on the other screen).

The remaining problem is knowing whether the original window is centered or not. If it’s not centered in the original screen, it’s probably a document or any other window that should stay near the corner (or wherever it is in the original screen).