Skip to content

Commit 488a7fd

Browse files
committed
Added temporary override of applicationHasFocus while handling the focus change to restore old functionality
- Since the `applicationHasFocus` property was updated with this branch to return the runtime's value instead of having the duplicate field, it now caused the defaultUpdateType to be different during the method. The goal of this change is to restore the old timing of when that property changes until after the `OnFocusChanged` method finishes executing.
1 parent 4c668b7 commit 488a7fd

4 files changed

Lines changed: 34 additions & 7 deletions

File tree

Packages/com.unity.inputsystem/InputSystem/Runtime/InputManager.LegacyFocusHandling.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,20 @@ internal partial class InputManager
2929
{
3030
internal void OnFocusChanged(bool focus)
3131
{
32+
// We set this to temporarily override applicationHasFocus before processing the focus change
33+
// as defaultUpdateType is influenced by it. Before returning from this method, clear the
34+
// bool to stop overriding to indicate this manager has finished processing the focus change.
35+
m_IsHandlingFocusChange = true;
36+
m_ApplicationHadFocus = !focus;
37+
3238
#if UNITY_EDITOR
3339
SyncAllDevicesWhenEditorIsActivated();
3440

3541
if (!m_Runtime.isInPlayMode)
42+
{
43+
m_IsHandlingFocusChange = false;
3644
return;
45+
}
3746

3847
var gameViewFocus = m_Settings.editorInputBehaviorInPlayMode;
3948
#endif
@@ -51,11 +60,11 @@ internal void OnFocusChanged(bool focus)
5160
m_Runtime.runInBackground;
5261
#endif
5362

54-
var backgroundBehavior = m_Settings.backgroundBehavior;
55-
if (backgroundBehavior == InputSettings.BackgroundBehavior.IgnoreFocus && runInBackground)
63+
if (m_Settings.backgroundBehavior == InputSettings.BackgroundBehavior.IgnoreFocus && runInBackground)
5664
{
5765
// If runInBackground is true, no device changes should happen, even when focus is gained. So early out.
5866
// If runInBackground is false, we still want to sync devices when focus is gained. So we need to continue further.
67+
m_IsHandlingFocusChange = false;
5968
return;
6069
}
6170

@@ -116,6 +125,8 @@ internal void OnFocusChanged(bool focus)
116125
#if UNITY_EDITOR
117126
m_CurrentUpdate = InputUpdateType.None;
118127
#endif
128+
129+
m_IsHandlingFocusChange = false;
119130
}
120131

121132
/// <summary>

Packages/com.unity.inputsystem/InputSystem/Runtime/InputManager.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public InputUpdateType defaultUpdateType
241241
// The solution here would be to make update calls explicitly specify the update type and no longer use this property.
242242
if (!m_RunPlayerUpdatesInEditMode && (!gameIsPlaying || !gameHasFocus))
243243
return InputUpdateType.Editor;
244-
#endif
244+
#endif
245245

246246
return m_UpdateMask.GetUpdateTypeForPlayer();
247247
}
@@ -536,7 +536,17 @@ internal static void StopEditorEventPassthrough()
536536
#else
537537
true;
538538
#endif
539-
private bool applicationHasFocus => m_Runtime != null ? m_Runtime.isPlayerFocused : Application.isFocused;
539+
private bool applicationHasFocus
540+
{
541+
get
542+
{
543+
#if !UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
544+
if (m_IsHandlingFocusChange)
545+
return m_ApplicationHadFocus;
546+
#endif
547+
return m_Runtime != null ? m_Runtime.isPlayerFocused : Application.isFocused;
548+
}
549+
}
540550

541551
private bool gameHasFocus =>
542552
#if UNITY_EDITOR
@@ -2425,6 +2435,12 @@ internal struct AvailableDevice
24252435
#if !UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
24262436
private bool m_DiscardOutOfFocusEvents;
24272437
private double m_FocusRegainedTime;
2438+
// These two bools are used for overriding applicationHasFocus which affects gameHasFocus
2439+
// and thus defaultUpdateType. See comments in defaultUpdateType. While processing the
2440+
// focus change in the OnFocusChanged method, these are used to temporarily override
2441+
// those properties.
2442+
private bool m_IsHandlingFocusChange;
2443+
private bool m_ApplicationHadFocus;
24282444
#endif
24292445
private InputEventStream m_InputEventStream;
24302446

Packages/com.unity.inputsystem/InputSystem/Runtime/NativeInputRuntime.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,11 @@ public void InitializeFocusState()
328328

329329
private void OnFocusChanged(bool focus)
330330
{
331-
m_FocusChangedMethod(focus);
332-
333-
// We set this *after* the callback above as InputManager.defaultUpdateType is influenced by the property.
334331
m_FocusState = focus
335332
? m_FocusState | FocusFlags.ApplicationFocus
336333
: m_FocusState & ~FocusFlags.ApplicationFocus;
334+
335+
m_FocusChangedMethod(focus);
337336
}
338337

339338
#endif

Packages/com.unity.inputsystem/Tests/TestFixture/InputTestRuntime.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ public void InvokePlayerFocusChanged(bool newFocusState)
246246
m_FocusState = newFocusState
247247
? m_FocusState | FocusFlags.ApplicationFocus
248248
: m_FocusState & ~FocusFlags.ApplicationFocus;
249+
249250
onPlayerFocusChanged?.Invoke(newFocusState);
250251
}
251252

0 commit comments

Comments
 (0)