Skip to content

Commit daafb57

Browse files
committed
Updated solution to allow the test runtime to start in focus independent of the Application.isFocused property
- Since the runtime is what triggers the callback that the InputManager uses to respond to the focus change, we don't need to have the InputManager set the focusState. We can just rely on the runtime itself to set the focus state after triggering the Action. - TODO: The InputTestRuntime.InvokePlayerFocusChanged does the opposite order of NativeInputRuntime.OnFocusChanged when setting the field. However, if I update the test runtime to set the `m_FocusState` after invoking the `onPlayerFocusChanged`, the `UI_WhenAppLosesAndRegainsFocus_WhileUIButtonIsPressed_UIButtonClickBehaviorShouldDependOnIfDeviceCanRunInBackground(false)` test fails. More investigation is needed to identify if the test is not setup correctly or if the difference in order is intended.
1 parent b231937 commit daafb57

5 files changed

Lines changed: 30 additions & 23 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ internal unsafe interface IInputRuntime
7474
/// command sent to the device.</returns>
7575
long DeviceCommand(int deviceId, InputDeviceCommand* commandPtr);
7676

77+
/// <summary>
78+
/// Initialize the focus state based on current conditions of the application.
79+
/// </summary>
80+
/// <seealso cref="focusState"/>
81+
void InitializeFocusState();
82+
7783
/// <summary>
7884
/// Set delegate to be called on input updates.
7985
/// </summary>
@@ -117,7 +123,11 @@ internal unsafe interface IInputRuntime
117123
Action<bool> onPlayerFocusChanged { get; set; }
118124
#endif
119125

126+
/// <summary>
127+
/// Flags indicating various focus states for the application and editor.
128+
/// </summary>
120129
FocusFlags focusState { get; set; }
130+
121131
/// <summary>
122132
/// Is true when the player or game view has focus.
123133
/// </summary>

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ internal void OnFocusChanged(bool focus)
3333
SyncAllDevicesWhenEditorIsActivated();
3434

3535
if (!m_Runtime.isInPlayMode)
36-
{
37-
SetRuntimeFocusState(focus);
3836
return;
39-
}
4037

4138
var gameViewFocus = m_Settings.editorInputBehaviorInPlayMode;
4239
#endif
@@ -59,7 +56,6 @@ internal void OnFocusChanged(bool focus)
5956
{
6057
// If runInBackground is true, no device changes should happen, even when focus is gained. So early out.
6158
// If runInBackground is false, we still want to sync devices when focus is gained. So we need to continue further.
62-
SetRuntimeFocusState(focus);
6359
return;
6460
}
6561

@@ -120,9 +116,6 @@ internal void OnFocusChanged(bool focus)
120116
#if UNITY_EDITOR
121117
m_CurrentUpdate = InputUpdateType.None;
122118
#endif
123-
124-
// We set this *after* the block above as defaultUpdateType is influenced by the setting.
125-
SetRuntimeFocusState(focus);
126119
}
127120

128121
/// <summary>

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,7 +2267,7 @@ internal void InstallRuntime(IInputRuntime runtime)
22672267
#endif
22682268
m_Runtime.pollingFrequency = pollingFrequency;
22692269

2270-
SetRuntimeFocusState(Application.isFocused);
2270+
m_Runtime.InitializeFocusState();
22712271

22722272
// We only hook NativeInputSystem.onBeforeUpdate if necessary.
22732273
if (m_BeforeUpdateListeners.length > 0 || m_HaveDevicesWithStateCallbackReceivers)
@@ -4046,21 +4046,6 @@ private bool ShouldDropStatusEvents(InputEventBuffer eventBuffer)
40464046

40474047
#endif // UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
40484048

4049-
/// <summary>
4050-
/// Set the focus state of the runtime, converting from the given boolean to either setting or clearing the focus flag.
4051-
/// </summary>
4052-
/// <param name="applicationFocus">The application focus state.</param>
4053-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4054-
private void SetRuntimeFocusState(bool applicationFocus)
4055-
{
4056-
if (m_Runtime != null)
4057-
{
4058-
m_Runtime.focusState = applicationFocus
4059-
? m_Runtime.focusState | FocusFlags.ApplicationFocus
4060-
: m_Runtime.focusState & ~FocusFlags.ApplicationFocus;
4061-
}
4062-
}
4063-
40644049
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40654050
private void FinalizeUpdate(InputUpdateType updateType)
40664051
{

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,24 @@ private bool OnWantsToShutdown()
316316
return true;
317317
}
318318

319+
public void InitializeFocusState()
320+
{
321+
m_FocusState = Application.isFocused
322+
? m_FocusState | FocusFlags.ApplicationFocus
323+
: m_FocusState & ~FocusFlags.ApplicationFocus;
324+
}
325+
319326
#if !UNITY_INPUTSYSTEM_SUPPORTS_FOCUS_EVENTS
320327
private Action<bool> m_FocusChangedMethod;
321328

322329
private void OnFocusChanged(bool focus)
323330
{
324331
m_FocusChangedMethod(focus);
332+
333+
// We set this *after* the callback above as InputManager.defaultUpdateType is influenced by the property.
334+
m_FocusState = focus
335+
? m_FocusState | FocusFlags.ApplicationFocus
336+
: m_FocusState & ~FocusFlags.ApplicationFocus;
325337
}
326338

327339
#endif

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ public unsafe long DeviceCommand(int deviceId, InputDeviceCommand* commandPtr)
234234
}
235235
}
236236

237+
public void InitializeFocusState()
238+
{
239+
// Testing should start in focus and individual tests can change this test runtime to lose focus
240+
// by calling InvokePlayerFocusChanged via InputTestFixture.ScheduleFocusChangedEvent.
241+
m_FocusState = FocusFlags.ApplicationFocus;
242+
}
243+
237244
public void InvokePlayerFocusChanged(bool newFocusState)
238245
{
239246
m_FocusState = newFocusState

0 commit comments

Comments
 (0)