Moved maximized window restores to primary monitor

Hello,

I’m trying to support creating a maximized window that ‘starts’ on a non-primary monitor. I’m going about it by creating the maximized window in the primary monitor, and then moving it to the other monitor. Of course, the target monitor may be to the left, right, above, or below the primary monitor. I’ve written code to account for this. Below is the relevant code, after the window is created.

		int x, y;
		auto info = mMonitorInfos[targetMonitorIndex]; // The target monitor.
		mPosX = info.mPosX; // Target monitor's x offset.
		mPosY = info.mPosY; // Target monitor's y offset.
		glfwGetWindowPos(mWindow, &x, &y); // Current window position, in primary monitor. We want the 'y' offset.

		// The target monitor may be positioned 'above' or 'below' the standard monitor(s).
                // Account for the titlebar size; y is based on the client-area.
		if (mPosY != 0) {
			y += mPosY;
		}
		glfwSetWindowPos(mWindow, mPosX, y);
		glfwGetWindowPos(mWindow, &mPosX, &mPosY);

The code above is fulfilling my requirement, but I’m running into an issue.

When I restore the window, via the window’s restore button, the window restores to the main monitor, not the monitor that it’s on.

I have verified that monitor is moved to the correct location in the target monitor. I have even hard coded some positions, for testing purposes. I cannot figure out why this is happening, and thus can’t fix it.

Note that, once the issue happens, if I manually drag the window to the target monitor, the restoration works correctly; it stays on the placed monitor.

Any ideas what could be the problem?

This is a simplified version of the code above.

		int x, y;
		auto info = mMonitorInfos[mold.mMonitor]; // The target monitor.
		mPosX = info.mPosX; // Target monitor's x offset.
		mPosY = info.mPosY; // Target monitor's y offset.
		glfwGetWindowPos(mWindow, &x, &y); // Current window position, in primary monitor. We want the 'y' offset.

		// The target monitor may be positioned 'above' or 'below' the standard monitor(s).
		// Account for the titlebar size; y is based on the client-area.
		mPosY += y;

		glfwSetWindowPos(mWindow, mPosX, mPosY);

A curious thing I noticed is that, when I initially restore the window (which goes to the primary monitor), the titlebar’s top is slightly above the desktop’s y:0 coordinate, by around 5-10 pixels. Not sure if this is relevant.

Have you tried moving (or creating) the window on the other monitor and then maximizing it there?

Also note that the window hint GLFW_MAXIMIZED can be set before creation so that the window is created maximized - there is however a potential bug with this at the moment.

Have you tried moving (or creating) the window on the other monitor and then maximizing it there?

My understanding is that it’s only possible to create the window in the main monitor, unless the window is fullscreen.

I was creating the window maximized in the primary monitor, and then moving it to the secondary. That was having the restore issue. I ended working around the issue by creating it non-maximized in the primary monitor, moving it to the secondary, and the maximizing it there. Basically, like your suggestion.

I did notice an issue, while testing, that if the window is created maximized in the primary monitor, and then restored, the resized window will be placed towards the top-left of the monitor, where it will have part of its titlebar passed the top of the monitor, thus partly cut off. That’s not a big deal to me, but it’s likely not correct behavior.

Thanks for the help.

Yes the create API doesn’t take position coordinates, but you can create it invisible using the GLFW_VISIBLE window hint, set it’s position then show it.

I’ll have a look at that restore behaviour once I get time.

Both issue 1806 and the bug mentioned in this thread where the correct restored window position was lost for a window created with GLFW_MAXIMIZED should both be fixed now in the main branch.