Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an OS.is_window_focused() getter #33967

Merged
merged 1 commit into from
Jan 6, 2020

Conversation

Calinou
Copy link
Member

@Calinou Calinou commented Nov 28, 2019

This makes it possible to know whether the window is focused at a given time, without having to track the focus state manually using NOTIFICATION_WM_FOCUS_IN and NOTIFICATION_WM_FOCUS_OUT.

PS: I noticed Android makes use of those constants, but does it make sense to implement OS.is_window_focused() there? Can you run code in the game loop while the app is minimized?

This partially addresses #33928.

platform/osx/os_osx.mm Outdated Show resolved Hide resolved
@capnm
Copy link
Contributor

capnm commented Nov 28, 2019

PS: I noticed Android makes use of those constants, but does it make sense to implement OS.is_window_focused() there? Can you run code in the game loop while the app is minimized?

This is after switching from app to android desktop and back:

InputEventMouseButton pressed: True[InputEventMouseButton:2947]SceneButtons
InputEventMouseButton pressed: False[InputEventMouseButton:3013]SceneButtons
I: NOTIFICATION_WM_FOCUS_OUT

I: NOTIFICATION_APP_PAUSED
I: NOTIFICATION_WM_FOCUS_IN
I: NOTIFICATION_APP_RESUMED
InputEventMouseButton pressed: True[InputEventMouseButton:3019]SceneButtons
InputEventMouseButton pressed: False[InputEventMouseButton:3097]SceneButtons
I: NOTIFICATION_WM_FOCUS_OUT

I: NOTIFICATION_APP_PAUSED
I: NOTIFICATION_WM_FOCUS_IN
I: NOTIFICATION_APP_RESUMED
func _notification(what):
	match what:
		NOTIFICATION_WM_FOCUS_IN:
			g.info("NOTIFICATION_WM_FOCUS_IN")
		NOTIFICATION_WM_FOCUS_OUT:
			g.info("NOTIFICATION_WM_FOCUS_OUT\n")
		NOTIFICATION_WM_QUIT_REQUEST:
			g.info("NOTIFICATION_WM_QUIT_REQUEST")

		NOTIFICATION_WM_MOUSE_ENTER:
			g.info("NOTIFICATION_WM_MOUSE_ENTER")
		NOTIFICATION_WM_MOUSE_EXIT:
			g.info("NOTIFICATION_WM_MOUSE_EXIT")

		# Android
		NOTIFICATION_APP_PAUSED :
			g.info("NOTIFICATION_APP_PAUSED")
		NOTIFICATION_APP_RESUMED :
			g.info("NOTIFICATION_APP_RESUMED")
		NOTIFICATION_WM_GO_BACK_REQUEST:
			g.info("NOTIFICATION_WM_GO_BACK_REQUEST")

This makes it possible to know whether the window is focused
at a given time, without having to track the focus state manually
using `NOTIFICATION_WM_FOCUS_IN` and `NOTIFICATION_WM_FOCUS_OUT`.

This partially addresses godotengine#33928.
@Calinou Calinou force-pushed the add-os-is-window-focused branch from b43923b to 21a3923 Compare November 28, 2019 15:43
@Calinou
Copy link
Member Author

Calinou commented Nov 28, 2019

@capnm I see, it might not be very useful to implement it on Android then (unless I'm mistaken).

@capnm
Copy link
Contributor

capnm commented Nov 28, 2019

@capnm I see, it might not be very useful to implement it on Android then (unless I'm mistaken).

I would agree. There doesn't seem to be any Godot support for the Andoid wake lock.
If yes, please tell me ❓

I use this patch already without any issues on Linux. It's nice shortcut for focus dependent processing or in this case #16832 managing the game controller input.

I: tick 6300
I: tick 6400
InputEventMouseButton pressed: True[InputEventMouseButton:1945]SceneButtons
I: font size = 99
InputEventMouseButton pressed: False[InputEventMouseButton:2059]SceneButtons
I: NOTIFICATION_WM_FOCUS_OUT

I: NOTIFICATION_APP_PAUSED

The idle and physics loop is disabled when the app is out of focus.

I: NOTIFICATION_WM_FOCUS_IN
I: NOTIFICATION_APP_RESUMED
I: tick 6500
var fr: int = 0
func _process(delta: float):
	fr += 1
	if fr % 100 == 0:
		print("tick ", fr)

@akien-mga akien-mga added this to the 4.0 milestone Nov 29, 2019
@akien-mga akien-mga modified the milestones: 4.0, 3.2 Jan 6, 2020
@akien-mga akien-mga merged commit 8454804 into godotengine:master Jan 6, 2020
@akien-mga
Copy link
Member

Thanks!

@Calinou Calinou deleted the add-os-is-window-focused branch January 27, 2020 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants