Skip to content

Commit 98f17d4

Browse files
committed
fix(SystemPadding, Keyboard, iOS): HasSystemPadding, HasKeyboard and SystemPaddings didn't work properly
1 parent 7484384 commit 98f17d4

File tree

6 files changed

+81
-45
lines changed

6 files changed

+81
-45
lines changed

Rg.Plugins.Popup/Pages/PopupPage.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public IPopupAnimation Animation
7979
public Thickness SystemPadding
8080
{
8181
get { return (Thickness)GetValue(SystemPaddingProperty); }
82-
internal set { SetValue(SystemPaddingProperty, value); }
82+
private set { SetValue(SystemPaddingProperty, value); }
8383
}
8484

8585
public static readonly BindableProperty SystemPaddingSidesProperty = BindableProperty.Create(nameof(SystemPaddingSides), typeof(PaddingSide), typeof(PopupPage), PaddingSide.All);
@@ -164,15 +164,14 @@ protected override void OnPropertyChanged(string? propertyName = null)
164164
case nameof(HasSystemPadding):
165165
case nameof(HasKeyboardOffset):
166166
case nameof(SystemPaddingSides):
167-
case nameof(SystemPadding):
168-
ForceLayout();
169-
break;
167+
ForceLayout();
168+
break;
170169
case nameof(IsAnimating):
171-
IsAnimationEnabled = IsAnimating;
172-
break;
170+
IsAnimationEnabled = IsAnimating;
171+
break;
173172
case nameof(IsAnimationEnabled):
174-
IsAnimating = IsAnimationEnabled;
175-
break;
173+
IsAnimating = IsAnimationEnabled;
174+
break;
176175
}
177176
}
178177

Rg.Plugins.Popup/Platforms/Android/Renderers/PopupPageRenderer.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,23 @@ protected override void Dispose(bool disposing)
6060
protected override void OnLayout(bool changed, int l, int t, int r, int b)
6161
{
6262
var activity = (Activity?)Context;
63+
var element = CurrentElement;
64+
65+
if (element == null)
66+
return;
6367

6468
Thickness systemPadding;
6569
var keyboardOffset = 0d;
6670

6771
var decoreView = activity?.Window?.DecorView;
6872

69-
var visibleRect = new Android.Graphics.Rect();
73+
var visibleRect = new Rect();
7074

7175
decoreView?.GetWindowVisibleDisplayFrame(visibleRect);
7276

7377
if (Build.VERSION.SdkInt >= BuildVersionCodes.M && RootWindowInsets != null)
7478
{
75-
var h = b-t;
79+
var h = b - t;
7680

7781
var windowInsets = RootWindowInsets;
7882
var bottomPadding = Math.Min(windowInsets.StableInsetBottom, windowInsets.SystemWindowInsetBottom);
@@ -119,13 +123,17 @@ protected override void OnLayout(bool changed, int l, int t, int r, int b)
119123
systemPadding = new Thickness();
120124
}
121125

122-
CurrentElement.SetValue(PopupPage.SystemPaddingProperty, systemPadding);
123-
CurrentElement.SetValue(PopupPage.KeyboardOffsetProperty, keyboardOffset);
126+
var needForceLayout =
127+
(element.HasSystemPadding && element.SystemPadding != systemPadding)
128+
|| (element.HasKeyboardOffset && element.KeyboardOffset != keyboardOffset);
129+
130+
element.SetValueFromRenderer(PopupPage.SystemPaddingProperty, systemPadding);
131+
element.SetValueFromRenderer(PopupPage.KeyboardOffsetProperty, keyboardOffset);
124132

125133
if (changed)
126-
CurrentElement.Layout(new Rectangle(Context.FromPixels(l), Context.FromPixels(t), Context.FromPixels(r), Context.FromPixels(b)));
127-
else
128-
CurrentElement.ForceLayout();
134+
element.Layout(new Rectangle(Context.FromPixels(l), Context.FromPixels(t), Context.FromPixels(r), Context.FromPixels(b)));
135+
else if(needForceLayout)
136+
element.ForceLayout();
129137

130138
base.OnLayout(changed, l, t, r, b);
131139
}

Rg.Plugins.Popup/Platforms/Ios/Extensions/PlatformExtension.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Linq;
22

33
using Rg.Plugins.Popup.IOS.Renderers;
4-
4+
using Rg.Plugins.Popup.Pages;
55
using UIKit;
66

77
using Xamarin.Forms;
@@ -57,23 +57,44 @@ public static void UpdateSize(this PopupPageRenderer renderer)
5757

5858
var superviewFrame = renderer.View.Superview.Frame;
5959
var applicationFrame = UIScreen.MainScreen.ApplicationFrame;
60+
var keyboardOffset = renderer.KeyboardBounds.Height;
61+
62+
Thickness systemPadding;
6063

61-
var systemPadding = new Thickness
64+
if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
6265
{
63-
Left = applicationFrame.Left,
64-
Top = applicationFrame.Top,
65-
Right = applicationFrame.Right - applicationFrame.Width - applicationFrame.Left,
66-
Bottom = applicationFrame.Bottom - applicationFrame.Height - applicationFrame.Top + renderer.KeyboardBounds.Height
67-
};
68-
69-
if ((renderer.Element.Width != superviewFrame.Width && renderer.Element.Height != superviewFrame.Height)
70-
|| currentElement.SystemPadding.Bottom != systemPadding.Bottom)
66+
var safeAreaInsets = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets;
67+
68+
systemPadding = new Thickness(
69+
safeAreaInsets.Left,
70+
safeAreaInsets.Top,
71+
safeAreaInsets.Right,
72+
safeAreaInsets.Bottom);
73+
}
74+
else
7175
{
72-
currentElement.BatchBegin();
73-
currentElement.SystemPadding = systemPadding;
74-
renderer.SetElementSize(new Size(superviewFrame.Width, superviewFrame.Height));
75-
currentElement.BatchCommit();
76+
systemPadding = new Thickness
77+
{
78+
Left = applicationFrame.Left,
79+
Top = applicationFrame.Top,
80+
Right = applicationFrame.Right - applicationFrame.Width - applicationFrame.Left,
81+
Bottom = applicationFrame.Bottom - applicationFrame.Height - applicationFrame.Top
82+
};
7683
}
84+
85+
var needForceLayout =
86+
(currentElement.HasSystemPadding && currentElement.SystemPadding != systemPadding)
87+
|| (currentElement.HasKeyboardOffset && currentElement.KeyboardOffset != keyboardOffset);
88+
89+
currentElement.SetValueFromRenderer(PopupPage.SystemPaddingProperty, systemPadding);
90+
currentElement.SetValueFromRenderer(PopupPage.KeyboardOffsetProperty, keyboardOffset);
91+
92+
var elementSize = new Size(superviewFrame.Width, superviewFrame.Height);
93+
94+
if (currentElement.Bounds.Size != elementSize)
95+
renderer.SetElementSize(elementSize);
96+
else if (needForceLayout)
97+
currentElement.ForceLayout();
7798
}
7899
}
79100
}

Rg.Plugins.Popup/Platforms/Mac/Extensions/PlatformExtension.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,8 @@ public static void UpdateSize(this PopupPageRenderer renderer)
5656
return;
5757

5858
var superviewFrame = renderer.View.Superview.Frame;
59-
var applactionFrame = NSScreen.MainScreen.Frame;
6059

61-
62-
63-
var systemPadding = new Thickness
64-
{
65-
Left = applactionFrame.Left,
66-
Top = applactionFrame.Top,
67-
Right = applactionFrame.Right - applactionFrame.Width - applactionFrame.Left,
68-
Bottom = applactionFrame.Bottom - applactionFrame.Height - applactionFrame.Top
69-
};
70-
71-
currentElement.BatchBegin();
72-
73-
currentElement.SystemPadding = systemPadding;
7460
renderer.SetElementSize(new Size(superviewFrame.Width, superviewFrame.Height));
75-
76-
currentElement.BatchCommit();
7761
}
7862
}
7963
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<ContentPage
3+
xmlns="http://xamarin.com/schemas/2014/forms"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
x:Class="Demo.Pages.KeyboardTestPage">
6+
<ContentPage.Content>
7+
8+
</ContentPage.Content>
9+
</ContentPage>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using Xamarin.Forms;
5+
6+
namespace Demo.Pages
7+
{
8+
public partial class KeyboardTestPage : ContentPage
9+
{
10+
public KeyboardTestPage()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)