Ver Fonte

adjust view size when osk is shown or hidden

Emmanuel Hansen há 2 anos atrás
pai
commit
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);
                 _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)
         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.Content.Res;
 using Android.OS;
 using Android.OS;
 using Android.Runtime;
 using Android.Runtime;
+using Android.Views;
 using AndroidX.AppCompat.App;
 using AndroidX.AppCompat.App;
 using AndroidX.Lifecycle;
 using AndroidX.Lifecycle;
 
 
+using AndroidRect = Android.Graphics.Rect;
+
 namespace Avalonia.Android
 namespace Avalonia.Android
 {
 {
     public abstract class AvaloniaMainActivity : AppCompatActivity, IActivityResultHandler
     public abstract class AvaloniaMainActivity : AppCompatActivity, IActivityResultHandler
@@ -15,6 +18,7 @@ namespace Avalonia.Android
 
 
         public Action<int, Result, Intent> ActivityResult { get; set; }
         public Action<int, Result, Intent> ActivityResult { get; set; }
         internal AvaloniaView View;
         internal AvaloniaView View;
+        private GlobalLayoutListener _listener;
 
 
         protected override void OnCreate(Bundle savedInstanceState)
         protected override void OnCreate(Bundle savedInstanceState)
         {
         {
@@ -32,6 +36,10 @@ namespace Avalonia.Android
             base.OnCreate(savedInstanceState);
             base.OnCreate(savedInstanceState);
 
 
             SetContentView(View);
             SetContentView(View);
+
+            _listener = new GlobalLayoutListener(View);
+
+            View.ViewTreeObserver?.AddOnGlobalLayoutListener(_listener);
         }
         }
 
 
         public object Content
         public object Content
@@ -57,6 +65,8 @@ namespace Avalonia.Android
         {
         {
             View.Content = null;
             View.Content = null;
 
 
+            View.ViewTreeObserver?.RemoveOnGlobalLayoutListener(_listener);
+
             base.OnDestroy();
             base.OnDestroy();
         }
         }
 
 
@@ -66,5 +76,20 @@ namespace Avalonia.Android
 
 
             ActivityResult?.Invoke(requestCode, resultCode, data);
             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 Avalonia.Rendering.Composition;
 using Java.Lang;
 using Java.Lang;
 using Math = System.Math;
 using Math = System.Math;
+using AndroidRect = Android.Graphics.Rect;
 
 
 namespace Avalonia.Android.Platform.SkiaPlatform
 namespace Avalonia.Android.Platform.SkiaPlatform
 {
 {
@@ -63,7 +64,21 @@ namespace Avalonia.Android.Platform.SkiaPlatform
 
 
         public IInputRoot InputRoot { get; private set; }
         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;
         public Size? FrameSize => null;
 
 
@@ -149,6 +164,11 @@ namespace Avalonia.Android.Platform.SkiaPlatform
             Resized?.Invoke(size, PlatformResizeReason.Unspecified);
             Resized?.Invoke(size, PlatformResizeReason.Unspecified);
         }
         }
 
 
+        internal void Resize(Size size)
+        {
+            Resized?.Invoke(size, PlatformResizeReason.Layout);
+        }
+
         class ViewImpl : InvalidationAwareSurfaceView, ISurfaceHolderCallback, IInitEditorInfo
         class ViewImpl : InvalidationAwareSurfaceView, ISurfaceHolderCallback, IInitEditorInfo
         {
         {
             private readonly TopLevelImpl _tl;
             private readonly TopLevelImpl _tl;