Skip to content

Adjust LayoutProvider for Skia Android #19894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
using AndroidX.Core.View;
using Uno.Disposables;
using Uno.UI.Extensions;
using Windows.Devices.Sensors;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Microsoft.UI.Xaml;
using Rect = Android.Graphics.Rect;

Expand All @@ -36,11 +33,14 @@

private readonly Activity _activity;
private readonly GlobalLayoutProvider _adjustNothingLayoutProvider, _adjustResizeLayoutProvider;
private double _cachedDensity;

Check warning on line 36 in src/Uno.UI.Runtime.Skia.Android/Microsoft/UI/Xaml/LayoutProvider.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.Runtime.Skia.Android/Microsoft/UI/Xaml/LayoutProvider.cs#L36

Make '_cachedDensity' 'readonly'.

public LayoutProvider(Activity activity)
{
this._activity = activity;

_cachedDensity = GetScale();

_adjustNothingLayoutProvider = new GlobalLayoutProvider(activity, null!, null!)
{
SoftInputMode = SoftInput.AdjustNothing | SoftInput.StateUnchanged,
Expand Down Expand Up @@ -148,11 +148,37 @@

private void MeasureInsets(PopupWindow sender, WindowInsetsCompat insets)
{
//var systemInsets = insets.GetInsets(WindowInsetsCompat.Type.SystemBars())
// .ToThickness()
// .PhysicalToLogicalPixels();
var size = insets.GetInsets(WindowInsetsCompat.Type.SystemBars())
.ToThickness();

var systemInsets = PhysicalToLogicalPixels(size);

InsetsChanged?.Invoke(systemInsets);
}

private Thickness PhysicalToLogicalPixels(Thickness size)
{
return new Thickness(
top: PhysicalToLogicalPixels(size.Top),
left: PhysicalToLogicalPixels(size.Left),
right: PhysicalToLogicalPixels(size.Right),
bottom: PhysicalToLogicalPixels(size.Bottom)
);
}

private double PhysicalToLogicalPixels(double value) => value / _cachedDensity;

private float GetScale()
{
float cachedDensity = 0;

if (Android.App.Application.Context.Resources is { } resources && resources.DisplayMetrics is not null)
{
using Android.Util.DisplayMetrics displayMetrics = resources.DisplayMetrics;
cachedDensity = displayMetrics.Density;
}

InsetsChanged?.Invoke(default);
return cachedDensity;
}

private class GlobalLayoutProvider : PopupWindow, ViewTreeObserver.IOnGlobalLayoutListener, AndroidX.Core.View.IOnApplyWindowInsetsListener
Expand Down
Loading