godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.14k stars 93 forks source link

Add window mode (fullscreen state) change signal or callback window event #5884

Open lostminds opened 1 year ago

lostminds commented 1 year ago

Describe the project you are working on

A game with fullscreen and windowed mode on multiple platforms

Describe the problem or limitation you are having in your project

While I can easily set and get the fullscreen mode of the game from code, in some cases the user can also do this using system features (notably on macOS it's easy to enter and exit fullscreen via window buttons). This means the fullscreen setting I save in my settings may get out of sync with the actual state of the window, and for example not be saved for the next session.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

My suggestion would be to add signals and/or window events for window mode changes that we can use to get notified when the window mode changes.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Add a mode_changed() signal to Window that we can connect to and that is emitted on mode change and/or Add a new WindowEvent.WINDOW_EVENT_MODE_CHANGED we can check for using the window_set_window_event_callback() callback on DisplayServer

If this enhancement will not be used often, can it be worked around with a few lines of script?

The best workaround I've found currently is to subscribe to the main viewport size_changed signal and then check if the window mode has changed after this size change. Assuming fullscreen/window changes also mean changing size.

However, it's not ideal as it seems the viewport size change when moving from fullscreen to windowed happens before the mode change, so I need to set up a timer to check for the change a little while after the viewport size change to catch going to window mode. Technically I guess you could also change between fullscreen and window mode without changing the size of the viewport in some cases, which this workaround would miss.

Is there a reason why this should be core and not an add-on in the asset library?

I don't think add-ons can add new event types or callbacks on core objects like Window?

YuriSizov commented 1 year ago

It should probably be a new NOTIFICATION_WM_* notification (surprised there isn't one yet). You could probably hack something together with NOTIFICATION_WM_SIZE_CHANGED in the meantime.

dalexeev commented 1 year ago

Or Viewport.size_changed signal.

lostminds commented 1 year ago

@YuriSizov thanks for the tip, I'll check out the NOTIFICATIONWM* system, I don't think I've seen that before. I was also surprised there wasn't a signal for this already. Perhaps because it was assumed (and maybe true) before that the window mode would never be changed by external sources. @dalexeev yes, that is the workaround I use now, but see above in the works around sections for some drawbacks of this.

lostminds commented 12 months ago

Small update on this: It seems there is a Window signal titlebar_changed(), which according to the docs can be used to detect fullscreen mode changes since these also usually mean a title bar being shown/hidden. Not ideal, but for anyone looking for workarounds until a mode_changed() signal is added to Window this might be useful.