From 22202be3946b5cb65511059e717af449168812f3 Mon Sep 17 00:00:00 2001
From: TSRBerry <20988865+TSRBerry@users.noreply.github.com>
Date: Sun, 14 May 2023 16:34:31 +0200
Subject: [PATCH] [GUI] Fix always hide cursor mode not hiding the cursor until
 it was moved (#4927)

* gtk: Add missing isMouseInClient check for hide-cursor

* ava: Add missing events and default isCursorInRenderer to true

This is necessary because we don't receive a initial PointerEnter event for some reason.
---
 src/Ryujinx.Ava/AppHost.cs           | 18 ++++++++++++----
 src/Ryujinx/Ui/RendererWidgetBase.cs | 32 ++++++++++++++--------------
 2 files changed, 30 insertions(+), 20 deletions(-)

diff --git a/src/Ryujinx.Ava/AppHost.cs b/src/Ryujinx.Ava/AppHost.cs
index 795c3f7a..f3e90ef1 100644
--- a/src/Ryujinx.Ava/AppHost.cs
+++ b/src/Ryujinx.Ava/AppHost.cs
@@ -86,7 +86,7 @@ namespace Ryujinx.Ava
         private KeyboardHotkeyState         _prevHotkeyState;
 
         private long _lastCursorMoveTime;
-        private bool _isCursorInRenderer;
+        private bool _isCursorInRenderer = true;
 
         private bool _isStopped;
         private bool _isActive;
@@ -160,7 +160,9 @@ namespace Ryujinx.Ava
 
             ConfigurationState.Instance.HideCursor.Event += HideCursorState_Changed;
 
-            _topLevel.PointerMoved += TopLevel_PointerMoved;
+            _topLevel.PointerMoved += TopLevel_PointerEnterOrMoved;
+            _topLevel.PointerEnter += TopLevel_PointerEnterOrMoved;
+            _topLevel.PointerLeave += TopLevel_PointerLeave;
 
             if (OperatingSystem.IsWindows())
             {
@@ -183,7 +185,7 @@ namespace Ryujinx.Ava
             _gpuCancellationTokenSource = new CancellationTokenSource();
         }
 
-        private void TopLevel_PointerMoved(object sender, PointerEventArgs e)
+        private void TopLevel_PointerEnterOrMoved(object sender, PointerEventArgs e)
         {
             if (sender is MainWindow window)
             {
@@ -201,6 +203,12 @@ namespace Ryujinx.Ava
                 }
             }
         }
+
+        private void TopLevel_PointerLeave(object sender, PointerEventArgs e)
+        {
+            _isCursorInRenderer = false;
+        }
+
         private void UpdateScalingFilterLevel(object sender, ReactiveEventArgs<int> e)
         {
             _renderer.Window?.SetScalingFilter((Graphics.GAL.ScalingFilter)ConfigurationState.Instance.Graphics.ScalingFilter.Value);
@@ -446,7 +454,9 @@ namespace Ryujinx.Ava
             ConfigurationState.Instance.Graphics.ScalingFilterLevel.Event  -= UpdateScalingFilterLevel;
             ConfigurationState.Instance.Graphics.AntiAliasing.Event        -= UpdateAntiAliasing;
 
-            _topLevel.PointerMoved -= TopLevel_PointerMoved;
+            _topLevel.PointerMoved -= TopLevel_PointerEnterOrMoved;
+            _topLevel.PointerEnter -= TopLevel_PointerEnterOrMoved;
+            _topLevel.PointerLeave -= TopLevel_PointerLeave;
 
             _gpuCancellationTokenSource.Cancel();
             _gpuCancellationTokenSource.Dispose();
diff --git a/src/Ryujinx/Ui/RendererWidgetBase.cs b/src/Ryujinx/Ui/RendererWidgetBase.cs
index 0fa7240b..573b69b3 100644
--- a/src/Ryujinx/Ui/RendererWidgetBase.cs
+++ b/src/Ryujinx/Ui/RendererWidgetBase.cs
@@ -321,27 +321,27 @@ namespace Ryujinx.Ui
 
             _toggleDockedMode = toggleDockedMode;
 
-            if (ConfigurationState.Instance.Hid.EnableMouse.Value)
+            if (_isMouseInClient)
             {
-                if (_isMouseInClient)
+                if (ConfigurationState.Instance.Hid.EnableMouse.Value)
                 {
                     Window.Cursor = _invisibleCursor;
                 }
-            }
-            else
-            {
-                switch (_hideCursorMode)
+                else
                 {
-                    case HideCursorMode.OnIdle:
-                        long cursorMoveDelta = Stopwatch.GetTimestamp() - _lastCursorMoveTime;
-                        Window.Cursor = (cursorMoveDelta >= CursorHideIdleTime * Stopwatch.Frequency) ? _invisibleCursor : null;
-                        break;
-                    case HideCursorMode.Always:
-                        Window.Cursor = _invisibleCursor;
-                        break;
-                    case HideCursorMode.Never:
-                        Window.Cursor = null;
-                        break;
+                    switch (_hideCursorMode)
+                    {
+                        case HideCursorMode.OnIdle:
+                            long cursorMoveDelta = Stopwatch.GetTimestamp() - _lastCursorMoveTime;
+                            Window.Cursor = (cursorMoveDelta >= CursorHideIdleTime * Stopwatch.Frequency) ? _invisibleCursor : null;
+                            break;
+                        case HideCursorMode.Always:
+                            Window.Cursor = _invisibleCursor;
+                            break;
+                        case HideCursorMode.Never:
+                            Window.Cursor = null;
+                            break;
+                    }
                 }
             }
         }