Any methods to check if I am focusing the 3d scene viewport?

Godot Version

4.2.1

Question

As the title said, I am writing editor tools and have some logic in the _process function, but I don’t want the _process running when I’m not focusing the 3d viewport, like coding in the script tab or open the project settings etc.

Finally, I found a solution which doesn’t look good.
Write below code in your plugin’s _enter_tree():

	var viewport = EditorInterface.get_editor_viewport_3d()
	var main_window = viewport.get_window()
	main_window.gui_focus_changed.connect(_main_window_focus_changed)

And below code in your plugins’s _exit_tree():

	var viewport = EditorInterface.get_editor_viewport_3d()
	var main_window = viewport.get_window()
	main_window.gui_focus_changed.disconnect(_main_window_focus_changed)

The _main_window_focus_changed function entered each time the main window’s focus is changed, record the focusing control.

func _main_window_focus_changed(control):
	_focused = control

Then check if the _focused is something in the viewport’s hierarchy:

	if _context:
		var viewport = EditorInterface.get_editor_viewport_3d()
		if _focused == viewport.get_parent().get_parent().get_child(1):
			_context.set_process(true)
		else:
			_context.set_process(false)

Well, I don’t find other pretty solution though. If you have any better solution, reply me plz. Thanks.