Skip to content

Commit

Permalink
Fix Flyout and TitleView Offset Measurements (#12480)
Browse files Browse the repository at this point in the history
* - Simplify and fix flyout offset measurements

* - fix merge

* Update PublicAPI.Unshipped.txt

* Update PublicAPI.Unshipped.txt

* Update ShellTests.cs

* - fix up null checks and formatting

* - fix merge

* Auto-format source code

* -sample

* Auto-format source code

* - fix safe area mistake

* - fix titleview lifecycles on shell

* Auto-format source code

* - fix titlebar android

* - remove samples

* Update UIContainerView.cs

* - PR comments

* - formatting

---------

Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
Co-authored-by: Rui Marinho <me@ruimarinho.net>
  • Loading branch information
3 people authored Mar 14, 2023
1 parent 40dfb10 commit ebc990b
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 201 deletions.
6 changes: 3 additions & 3 deletions src/Compatibility/Core/src/iOS/Renderers/UIContainerView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal double? Width

internal bool MeasureIfNeeded()
{
if (View == null)
if (View is null)
return false;

if (double.IsNaN(MeasuredHeight) || Frame.Width != View.Width)
Expand All @@ -67,7 +67,7 @@ public virtual Thickness Margin

void ReMeasure()
{
if (Height != null && MatchHeight)
if (Height is not null && MatchHeight)
{
MeasuredHeight = Height.Value;
}
Expand Down Expand Up @@ -104,7 +104,7 @@ protected override void Dispose(bool disposing)

if (disposing)
{
if (_view != null)
if (_view is not null)
_view.MeasureInvalidated -= OnMeasureInvalidated;

_renderer?.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ void OnShellNavigated(object sender, ShellNavigatedEventArgs e)
Page == ShellContext?.Shell?.GetCurrentShellPage())
{
UpdateLeftBarButtonItem();
UpdateTitleView();
}
}

Expand Down Expand Up @@ -588,6 +589,8 @@ protected virtual void UpdatePageTitle(AToolbar toolbar, Page page)

protected virtual void UpdateTitleView(Context context, AToolbar toolbar, View titleView)
{
if (_toolbar != null && ShellContext?.Shell?.GetCurrentShellPage() == Page)
_toolbar.Handler?.UpdateValue(nameof(Toolbar.TitleView));
}

protected virtual void UpdateToolbarItems(AToolbar toolbar, Page page)
Expand Down Expand Up @@ -677,7 +680,9 @@ void UpdateLeftBarButtonItem()

void UpdateTitleView()
{
UpdateTitleView(ShellContext.AndroidContext, _platformToolbar, Shell.GetTitleView(Page));
UpdateTitleView(ShellContext.AndroidContext, _platformToolbar,
(_toolbar as Toolbar).TitleView as View ??
Shell.GetTitleView(Page));
}

void UpdateToolbarItems()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ShellFlyoutContentRenderer : UIViewController, IShellFlyoutContentR
UIImageView _bgImage;
readonly IShellContext _shellContext;
UIContainerView _headerView;
UIView _footerView;
UIContainerView _footerView;
View _footer;
ShellTableViewController _tableViewController;
ShellFlyoutLayoutManager _shellFlyoutContentManager;
Expand Down Expand Up @@ -89,14 +89,14 @@ void UpdateFlyoutHeader()
return;

int previousIndex = GetPreviousIndex(_headerView);
if (_headerView != null)
if (_headerView is not null)
{
_tableViewController.HeaderView = null;
_headerView.RemoveFromSuperview();
_headerView.Dispose();
}

if (header != null)
if (header is not null)
_headerView = new ShellFlyoutHeaderContainer(((IShellController)_shellContext.Shell).FlyoutHeader);
else
_headerView = null;
Expand All @@ -117,27 +117,25 @@ void UpdateFlyoutFooter(View view)
return;

int previousIndex = GetPreviousIndex(_footerView);
if (_footer != null)
if (_footer is not null)
{
var oldRenderer = (IPlatformViewHandler)_footer.Handler;
var oldFooterView = _footerView;
_tableViewController.FooterView = null;
_footerView?.Disconnect();
_footerView = null;
_uIViews[FooterIndex] = null;
oldFooterView?.RemoveFromSuperview();
if (_footer != null)
_footer.MeasureInvalidated -= OnFooterMeasureInvalidated;

_footer.Handler = null;
oldRenderer?.DisconnectHandler();
}

_footer = view;

if (_footer != null)
if (_footer is not null)
{
var renderer = (IPlatformViewHandler)_footer.ToHandler(_shellContext.Shell.FindMauiContext());
_footerView = renderer.PlatformView;
_footerView = new UIContainerView(_footer);
_uIViews[FooterIndex] = _footerView;
AddViewInCorrectOrder(_footerView, previousIndex);

Expand All @@ -151,15 +149,15 @@ void UpdateFlyoutFooter(View view)

int GetPreviousIndex(UIView oldView)
{
if (oldView == null)
if (oldView is null)
return -1;

return Array.IndexOf(View.Subviews, oldView);
}

void AddViewInCorrectOrder(UIView newView, int previousIndex)
{
if (newView == null)
if (newView is null)
return;

if (Array.IndexOf(View.Subviews, newView) >= 0)
Expand All @@ -175,7 +173,7 @@ void AddViewInCorrectOrder(UIView newView, int previousIndex)
for (int i = startingIndex - 1; i >= 0; i--)
{
var topView = _uIViews[i];
if (topView == null)
if (topView is null)
continue;

if (Array.IndexOf(View.Subviews, topView) >= 0)
Expand All @@ -195,24 +193,25 @@ void OnFooterMeasureInvalidated(object sender, System.EventArgs e)

void ReMeasureFooter()
{
_footer?.LayoutToMeasuredSize(View.Frame.Width, double.PositiveInfinity);
UpdateFooterPosition(_footerView.Frame.Height);
var size = _footerView?.SizeThatFits(new CGSize(View.Frame.Width, double.PositiveInfinity));
if (size is not null)
UpdateFooterPosition(size.Value.Height);
}

void UpdateFooterPosition()
{
if (_footerView == null)
if (_footerView is null)
return;

if (_footerView.Frame.Height == 0)
if (double.IsNaN(_footerView.MeasuredHeight))
ReMeasureFooter();
else
UpdateFooterPosition(_footerView.Frame.Height);
UpdateFooterPosition((nfloat)_footerView.MeasuredHeight);
}

void UpdateFooterPosition(nfloat footerHeight)
{
if (_footerView == null)
if (_footerView is null && !nfloat.IsNaN(footerHeight))
return;

var footerWidth = View.Frame.Width;
Expand All @@ -235,15 +234,15 @@ protected virtual void UpdateBackground()
var brush = _shellContext.Shell.FlyoutBackground;
int previousIndex = GetPreviousIndex(_blurView);
var backgroundImage = View.GetBackgroundImage(brush);
View.BackgroundColor = backgroundImage != null ? UIColor.FromPatternImage(backgroundImage) : color?.ToPlatform() ?? Maui.Platform.ColorExtensions.BackgroundColor;
View.BackgroundColor = backgroundImage is not null ? UIColor.FromPatternImage(backgroundImage) : color?.ToPlatform() ?? Maui.Platform.ColorExtensions.BackgroundColor;

if (View.BackgroundColor.CGColor.Alpha < 1)
{
AddViewInCorrectOrder(_blurView, previousIndex);
}
else
{
if (_blurView.Superview != null)
if (_blurView.Superview is not null)
_blurView.RemoveFromSuperview();
}

Expand All @@ -254,7 +253,7 @@ void UpdateFlyoutBgImageAsync()
{
// Image
var imageSource = _shellContext.Shell.FlyoutBackgroundImage;
if (imageSource == null || !_shellContext.Shell.IsSet(Shell.FlyoutBackgroundImageProperty))
if (imageSource is null || !_shellContext.Shell.IsSet(Shell.FlyoutBackgroundImageProperty))
{
_bgImage.RemoveFromSuperview();
_bgImage.Image?.Dispose();
Expand All @@ -263,18 +262,18 @@ void UpdateFlyoutBgImageAsync()
}

var mauiContext = _shellContext.Shell.FindMauiContext();
if (mauiContext == null)
if (mauiContext is null)
return;

imageSource.LoadImage(mauiContext, result =>
{
var nativeImage = result?.Value;

if (View == null || nativeImage == null)
if (View is null || nativeImage is null)
return;

int previousIndex = GetPreviousIndex(_bgImage);
if (nativeImage == null ||
if (nativeImage is null ||
_shellContext.Shell.FlyoutBackgroundImage != imageSource)
{
_bgImage?.RemoveFromSuperview();
Expand Down Expand Up @@ -347,7 +346,7 @@ void UpdateFlyoutContent()
var view = (_shellContext.Shell as IShellController).FlyoutContent;

var previousIndex = GetPreviousIndex(_shellFlyoutContentManager.ContentView);
if (view != null)
if (view is not null)
{
_shellFlyoutContentManager.SetCustomContent(view);
}
Expand All @@ -358,7 +357,7 @@ void UpdateFlyoutContent()

_uIViews[ContentIndex] = _shellFlyoutContentManager.ContentView;
AddViewInCorrectOrder(_uIViews[ContentIndex], previousIndex);
_shellFlyoutContentManager.LayoutParallax();
_shellFlyoutContentManager.UpdateHeaderSize();
}

public override void ViewWillAppear(bool animated)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,70 @@
#nullable disable
using System;
using CoreGraphics;
using Microsoft.Maui.Platform;
using UIKit;

namespace Microsoft.Maui.Controls.Platform.Compatibility
{
internal class ShellFlyoutHeaderContainer : UIContainerView
{
Thickness _safearea = Thickness.Zero;
public ShellFlyoutHeaderContainer(View view) : base(view)
{
UpdateSafeAreaMargin();
}

public override Thickness Margin
{
get
{
if (!View.IsSet(View.MarginProperty))
{
var newMargin = new Thickness(0, (float)UIApplication.SharedApplication.GetSafeAreaInsetsForWindow().Top, 0, 0);
if (View.IsSet(View.MarginProperty))
return View.Margin;

if (newMargin != View.Margin)
{
View.Margin = newMargin;
}
}
var safeArea = UIApplication.SharedApplication.GetSafeAreaInsetsForWindow();

return View.Margin;
return new Thickness(
safeArea.Left,
safeArea.Top,
safeArea.Right,
safeArea.Left);
}
}

public override void LayoutSubviews()
{
if (!UpdateSafeAreaMargin())
base.LayoutSubviews();
}

public override void SafeAreaInsetsDidChange()
{
UpdateSafeAreaMargin();
base.SafeAreaInsetsDidChange();
}

bool UpdateSafeAreaMargin()
{
var safeArea = UIApplication.SharedApplication.GetSafeAreaInsetsForWindow();

if (safeArea.Top != _safearea.Top ||
safeArea.Bottom != _safearea.Bottom ||
safeArea.Right != _safearea.Right ||
safeArea.Left != _safearea.Left)
{
_safearea =
new Thickness(
safeArea.Left,
safeArea.Top,
safeArea.Right,
safeArea.Bottom);

OnHeaderSizeChanged();
return true;
}

return false;

}
}
}
Loading

0 comments on commit ebc990b

Please sign in to comment.