Jelajahi Sumber

adjust view size when osk is shown or hidden

Emmanuel Hansen 2 tahun lalu
induk
melakukan
e685659173

+ 4 - 0
src/Android/Avalonia.Android/AndroidInputMethod.cs

@@ -95,6 +95,10 @@ namespace Avalonia.Android
 
                 _imm.UpdateSelection(_host, surroundingText.AnchorOffset, surroundingText.CursorOffset, surroundingText.AnchorOffset, surroundingText.CursorOffset);
             }
+            else
+            {
+                _imm.HideSoftInputFromWindow(_host.WindowToken, HideSoftInputFlags.ImplicitOnly);
+            }
         }
 
         private void SurroundingTextChanged(object sender, EventArgs e)

+ 25 - 0
src/Android/Avalonia.Android/AvaloniaMainActivity.cs

@@ -4,9 +4,12 @@ using Android.Content;
 using Android.Content.Res;
 using Android.OS;
 using Android.Runtime;
+using Android.Views;
 using AndroidX.AppCompat.App;
 using AndroidX.Lifecycle;
 
+using AndroidRect = Android.Graphics.Rect;
+
 namespace Avalonia.Android
 {
     public abstract class AvaloniaMainActivity : AppCompatActivity, IActivityResultHandler
@@ -15,6 +18,7 @@ namespace Avalonia.Android
 
         public Action<int, Result, Intent> ActivityResult { get; set; }
         internal AvaloniaView View;
+        private GlobalLayoutListener _listener;
 
         protected override void OnCreate(Bundle savedInstanceState)
         {
@@ -32,6 +36,10 @@ namespace Avalonia.Android
             base.OnCreate(savedInstanceState);
 
             SetContentView(View);
+
+            _listener = new GlobalLayoutListener(View);
+
+            View.ViewTreeObserver?.AddOnGlobalLayoutListener(_listener);
         }
 
         public object Content
@@ -57,6 +65,8 @@ namespace Avalonia.Android
         {
             View.Content = null;
 
+            View.ViewTreeObserver?.RemoveOnGlobalLayoutListener(_listener);
+
             base.OnDestroy();
         }
 
@@ -66,5 +76,20 @@ namespace Avalonia.Android
 
             ActivityResult?.Invoke(requestCode, resultCode, data);
         }
+
+        class GlobalLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener
+        {
+            private AvaloniaView _view;
+
+            public GlobalLayoutListener(AvaloniaView view)
+            {
+                _view = view;
+            }
+
+            public void OnGlobalLayout()
+            {
+                _view.TopLevelImpl?.Resize(_view.TopLevelImpl.ClientSize);
+            }
+        }
     }
 }

+ 21 - 1
src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs

@@ -26,6 +26,7 @@ using Avalonia.Rendering;
 using Avalonia.Rendering.Composition;
 using Java.Lang;
 using Math = System.Math;
+using AndroidRect = Android.Graphics.Rect;
 
 namespace Avalonia.Android.Platform.SkiaPlatform
 {
@@ -63,7 +64,21 @@ namespace Avalonia.Android.Platform.SkiaPlatform
 
         public IInputRoot InputRoot { get; private set; }
 
-        public virtual Size ClientSize => Size.ToSize(RenderScaling);
+        public virtual Size ClientSize
+        {
+            get
+            {
+                AndroidRect rect = new AndroidRect();
+                AndroidRect intersection = new AndroidRect();
+
+                _view.GetWindowVisibleDisplayFrame(intersection);
+                _view.GetGlobalVisibleRect(rect);
+
+                intersection.Intersect(rect);
+
+                return new PixelSize(intersection.Right - intersection.Left, intersection.Bottom - intersection.Top).ToSize(RenderScaling);
+            }
+        }
 
         public Size? FrameSize => null;
 
@@ -149,6 +164,11 @@ namespace Avalonia.Android.Platform.SkiaPlatform
             Resized?.Invoke(size, PlatformResizeReason.Unspecified);
         }
 
+        internal void Resize(Size size)
+        {
+            Resized?.Invoke(size, PlatformResizeReason.Layout);
+        }
+
         class ViewImpl : InvalidationAwareSurfaceView, ISurfaceHolderCallback, IInitEditorInfo
         {
             private readonly TopLevelImpl _tl;