OS.set_window_position has an offset?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mentuhetep

I am using this to set the game window to the top left corner of the screen:

OS.set_window_position(Vector2 (0,0))

However, the window appears a few pixels to the right. Playing around with it, I have found that a Vector2 (-7,0) will stick the window to the top left corner. Is there an offset? and if so, is it because the game is running in windowed mode?

I do not want to use fullscreen mode or maximize the window, though. Maybe there is a better way/method to achieve this?

Thanks for your attention!

I have been trying more stuff since I posted this question.

In borderless mode there are no issues whatsoever. Both OS.get_window_size() and OS.get_real_window_size() return the same value (as expected), and OS.set_window_position(Vector2(0,0)) sets the window at the top left corner perfectly.

In windowed mode, however, OS.get_window_size() and OS.get_real_window_size() return a difference of 18x47, that I assume corresponds to the frame of the window, and might vary in each OS and computer. However, in my case, the left and right frames of the window are only 1 pixel wide, which means there is a gap of 8 pixels when trying to position the window at (0,0).

My question will be, is this something intended by Godot or calculated by the OS? Does this extra framing gap is fixed, or changes with a different OS / different computer?

Mentuhetep | 2019-08-05 07:19

:bust_in_silhouette: Reply From: Nanotross

I just had this issue as well, and I searched through the source code to find the cause.

The problem appears to be that when the window is in windowed mode, the window decor isn’t taken into account to correct the position of the window.

OS.get_real_window_size() - OS.get_window_size() will give the total decor on each axis, so you must divide by 2 to get the amount per side.

Because the window has a title bar, the amount of decor on the top is much greater than on the bottom (the bottom only has the border and shadow), so dividing by 2 doesn’t work. There you’d need to get the height of the title bar and subtract it from the y value of the calculated decor.

The problem (at least when I tested) seemed to only affect the horizontal position, so just calculating the horizontal decor and dividing by 2, then subtracting that from the origin (0,0) will give you the position for the window so that it is correctly in the top corner of the screen.

var window_decor = OS.get_real_window_size() - OS.get_window_size() var window_position = Vector2(0,0) - window_decor.x/2 OS.set_window_position(window_position)

I accidentally looked through the Godot 4.0 source code first, so I believe this issue is taken care of because they have a comment that specifically mentions that the window decor is a problem in the old approach. But, I would need to either test it out in 4.0 or read more carefully to make sure.