Skip to content

Commit

Permalink
fix: [Android] Window's visible bounds are incorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Oct 6, 2021
1 parent 9743762 commit 39ea361
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/Uno.UI/Extensions/InsetsExtensions.Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using Windows.UI.Xaml;

namespace Uno.UI.Extensions
{
internal static class InsetsExtensions
{
public static Thickness ToThickness(this Android.Graphics.Insets insets)
=> new Thickness(insets.Left, insets.Top, insets.Right, insets.Bottom);
}
}
6 changes: 5 additions & 1 deletion src/Uno.UI/Extensions/ThicknessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ internal static class ThicknessExtensions
/// <summary>
/// Is this <see cref="Thickness"/> uniform (all sides are equal)?
/// </summary>
public static bool IsUniform(this Thickness thickness) => thickness.Left == thickness.Top && thickness.Left == thickness.Right && thickness.Left == thickness.Bottom;
public static bool IsUniform(this Thickness thickness)
=> thickness.Left == thickness.Top && thickness.Left == thickness.Right && thickness.Left == thickness.Bottom;

public static Thickness Minus(this Thickness x, Thickness y) // Minus ==> There is a (not implemented) Substract on struct in mono!
=> new Thickness(x.Left - y.Left, x.Top - y.Top, x.Right - y.Right, x.Bottom - y.Bottom);
}
}
36 changes: 35 additions & 1 deletion src/Uno.UI/UI/Xaml/Window.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using Windows.UI.ViewManagement;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Uno.Extensions.ValueType;
using Uno.UI.Extensions;

namespace Windows.UI.Xaml
{
Expand Down Expand Up @@ -76,6 +78,34 @@ private static Window InternalGetCurrentWindow()

internal void RaiseNativeSizeChanged()
{
#if __ANDROID_30__
var metrics = (ContextHelper.Current as Activity)?.WindowManager?.CurrentWindowMetrics;

var insetsTypes = WindowInsets.Type.SystemBars(); // == WindowInsets.Type.StatusBars() | WindowInsets.Type.NavigationBars() | WindowInsets.Type.CaptionBar();
var solidInsetsTypes = insetsTypes;
if (IsStatusBarTranslucent())
{
solidInsetsTypes &= ~WindowInsets.Type.StatusBars();
}
if (IsNavigationBarTranslucent())
{
solidInsetsTypes &= ~WindowInsets.Type.NavigationBars();
}

var insets = metrics.WindowInsets.GetInsets(insetsTypes).ToThickness();
var solidInsets = metrics.WindowInsets.GetInsets(solidInsetsTypes).ToThickness();

// The 'metric.Bounds' does not include any insets, so we remove the "solid" insets under which we cannot draw anything
var windowBounds = new Rect(default, ((Rect)metrics.Bounds).DeflateBy(solidInsets).Size).PhysicalToLogicalPixels();
var newBounds = windowBounds;

// The visible bounds is the windows bounds on which we remove also
// [translucent only inset](all_insets - solid_insets_that_has_already_been_removed_from_all_insets)
var visibleBounds = windowBounds.DeflateBy(insets.Minus(solidInsets)).PhysicalToLogicalPixels();

// The true visible bounds ... is unknown for now!
var trueVisibleBounds = visibleBounds;
#else
using var display = (ContextHelper.Current as Activity)?.WindowManager?.DefaultDisplay;
using var fullScreenMetrics = new DisplayMetrics();

Expand Down Expand Up @@ -140,6 +170,8 @@ Rect CalculateVisibleBounds(double excludedStatusBarHeight)

var visibleBounds = CalculateVisibleBounds(statusBarSizeExcluded);
var trueVisibleBounds = CalculateVisibleBounds(statusBarSize);
#endif

ApplicationView.GetForCurrentView()?.SetVisibleBounds(visibleBounds);
ApplicationView.GetForCurrentView()?.SetTrueVisibleBounds(trueVisibleBounds);

Expand Down Expand Up @@ -198,6 +230,7 @@ internal void UpdateInsetsWithVisibilities()
Insets = newInsets;
}

#if !__ANDROID_30__
private double GetLogicalStatusBarSize()
{
var logicalStatusBarHeight = 0d;
Expand Down Expand Up @@ -228,6 +261,7 @@ private double GetLogicalNavigationBarSizeExcluded()
? ViewHelper.PhysicalToLogicalPixels(navigationBarSize)
: 0;
}
#endif

internal void DisplayFullscreen(UIElement element)
{
Expand Down Expand Up @@ -268,7 +302,7 @@ public bool IsStatusBarTranslucent()
}

return activity.Window.Attributes.Flags.HasFlag(WindowManagerFlags.TranslucentStatus)
|| activity.Window.Attributes.Flags.HasFlag(WindowManagerFlags.LayoutNoLimits); ;
|| activity.Window.Attributes.Flags.HasFlag(WindowManagerFlags.LayoutNoLimits);
}
#endregion

Expand Down

0 comments on commit 39ea361

Please sign in to comment.