Skip to content

Commit

Permalink
Ported pending fixes from Xamarin.Forms (#5510)
Browse files Browse the repository at this point in the history
* Ported pending fixes from Xamarin.Forms

* Fix build errors

* - fix static get of accent color

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
  • Loading branch information
jsuarezruiz and PureWeen authored Mar 26, 2022
1 parent f8936a5 commit 41bb8c9
Show file tree
Hide file tree
Showing 22 changed files with 172 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ internal void Recycle()
RemoveView(Content.View);
}

Content?.Dispose();
Content = null;
_size = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@ void OnDataChanged()
// we need to reset the ListView's adapter to reflect the changes on page B
// If there header and footer are present at the reset time of the adapter
// they will be DOUBLE added to the ViewGround (the ListView) causing indexes to be off by one.

if (_realListView.IsDisposed())
return;

_realListView.RemoveHeaderView(HeaderView);
_realListView.RemoveFooterView(FooterView);
_realListView.Adapter = _realListView.Adapter;
Expand Down
9 changes: 7 additions & 2 deletions src/Compatibility/Core/src/Android/VisualElementTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,13 @@ void UpdateScale()
VisualElement view = _renderer.Element;
AView aview = _renderer.View;

aview.ScaleX = (float)view.Scale * (float)view.ScaleX;
aview.ScaleY = (float)view.Scale * (float)view.ScaleY;
var scale = view.Scale;

if (double.IsNaN(scale))
return;

aview.ScaleX = (float)scale * (float)view.ScaleX;
aview.ScaleY = (float)scale * (float)view.ScaleY;
}

[PortHandler]
Expand Down
23 changes: 5 additions & 18 deletions src/Compatibility/Core/src/Windows/CellControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,10 @@ void SetCell(object newContext)
// If there is a ListView, load the Cell content from the ItemTemplate.
// Otherwise, the given Cell is already a templated Cell from a TableView.
ListView lv = _listView.Value;

if (lv != null)
{
// 🚀 If there is an old cell, check if it was a group header
// we need this later to know whether we can recycle this cell
bool? wasGroupHeader = null;
var oldCell = Cell;
if (oldCell != null)
{
wasGroupHeader = oldCell.GetIsGroupHeader<ItemsView<Cell>, Cell>();
}

Cell oldCell = Cell;
bool isGroupHeader = IsGroupHeader;
DataTemplate template = isGroupHeader ? lv.GroupHeaderTemplate : lv.ItemTemplate;
object bindingContext = newContext;
Expand All @@ -362,15 +355,8 @@ void SetCell(object newContext)
sameTemplate = oldTemplate == template;
}
}
// 🚀 if there is no datatemplateselector, we now verify if the old cell
// was a groupheader and whether the new one is as well.
// Again, this is only to verify we can reuse this cell
else if (wasGroupHeader.HasValue)
{
sameTemplate = wasGroupHeader == isGroupHeader;
}

// reuse cell
// Reuse cell
var canReuseCell = Cell != null && sameTemplate;

// 🚀 If we can reuse the cell, just reuse it...
Expand Down Expand Up @@ -407,7 +393,8 @@ void SetCell(object newContext)

if (Cell != cell)
Cell = cell;
// 🚀 even if the cell did not change, we **must** call SendDisappearing() and SendAppearing()

// 🚀 Even if the cell did not change, we **must** call SendDisappearing() and SendAppearing()
// because frameworks such as Reactive UI rely on this! (this.WhenActivated())
else if (Cell != null)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Compatibility/Core/src/Windows/FontImageSourceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public sealed class FontImageSourceHandler : IImageSourceHandler, IIconElementHa
{
FontFamily = GetFontSource(fontsource),
FontSize = (float)fontsource.Size,
HorizontalAlignment = CanvasHorizontalAlignment.Center,
HorizontalAlignment = CanvasHorizontalAlignment.Left,
VerticalAlignment = CanvasVerticalAlignment.Center,
Options = CanvasDrawTextOptions.Default
};
Expand All @@ -51,9 +51,7 @@ public sealed class FontImageSourceHandler : IImageSourceHandler, IIconElementHa
var iconcolor = (fontsource.Color != null ? fontsource.Color : Colors.White).ToWindowsColor();

// offset by 1 as we added a 1 inset
var x = (float)layout.DrawBounds.X * -1;

ds.DrawTextLayout(layout, x, 1f, iconcolor);
ds.DrawTextLayout(layout, 1f, 1f, iconcolor);
}

return Task.FromResult((UI.Xaml.Media.ImageSource)imageSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class ItemsViewController<TItemsView> : UICollectionViewControll
public TItemsView ItemsView { get; }
protected ItemsViewLayout ItemsViewLayout { get; set; }
bool _initialized;
bool _isEmpty;
bool _isEmpty = true;
bool _emptyViewDisplayed;
bool _disposed;

Expand Down Expand Up @@ -212,6 +212,7 @@ public virtual void UpdateItemsSource()
{
_measurementCells.Clear();
ItemsViewLayout?.ClearCellSizeCache();
ItemsSource?.Dispose();
ItemsSource = CreateItemsViewSource();
CollectionView.ReloadData();
CollectionView.CollectionViewLayout.InvalidateLayout();
Expand Down Expand Up @@ -470,7 +471,14 @@ void AlignEmptyView()
return;
}

if (CollectionView.EffectiveUserInterfaceLayoutDirection == UIUserInterfaceLayoutDirection.RightToLeft)
bool isRtl;

if (PlatformVersion.IsAtLeast(10))
isRtl = CollectionView.EffectiveUserInterfaceLayoutDirection == UIUserInterfaceLayoutDirection.RightToLeft;
else
isRtl = CollectionView.SemanticContentAttribute == UISemanticContentAttribute.ForceRightToLeft;

if (isRtl)
{
if (_emptyUIView.Transform.A == -1)
{
Expand Down Expand Up @@ -621,6 +629,7 @@ internal protected virtual void UpdateVisibility()
{
if (CollectionView.Hidden)
{
CollectionView.ReloadData();
CollectionView.Hidden = false;
Layout.InvalidateLayout();
CollectionView.LayoutIfNeeded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
void CollectionChanged(NotifyCollectionChangedEventArgs args)
{
// Force UICollectionView to get the internal accounting straight
CollectionView.NumberOfItemsInSection(_section);
if (!CollectionView.Hidden)
CollectionView.NumberOfItemsInSection(_section);

switch (args.Action)
{
Expand Down
23 changes: 15 additions & 8 deletions src/Compatibility/Core/src/iOS/CollectionView/TemplatedCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ void SetRenderer(IVisualElementRenderer renderer)

InitializeContentConstraints(nativeView);

UpdateVisualStates();

renderer.Element.MeasureInvalidated += MeasureInvalidated;
}

Expand Down Expand Up @@ -236,14 +238,7 @@ public override bool Selected
{
base.Selected = value;

var element = VisualElementRenderer?.Element;

if (element != null)
{
VisualStateManager.GoToState(element, value
? VisualStateManager.CommonStates.Selected
: VisualStateManager.CommonStates.Normal);
}
UpdateVisualStates();
}
}

Expand Down Expand Up @@ -293,5 +288,17 @@ bool SizesAreSame(CGSize preferredSize, Size elementSize)

return true;
}

void UpdateVisualStates()
{
var element = VisualElementRenderer?.Element;

if (element != null)
{
VisualStateManager.GoToState(element, Selected
? VisualStateManager.CommonStates.Selected
: VisualStateManager.CommonStates.Normal);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -52,9 +53,9 @@ public void AddFrameData(int index, CGImageSource imageSource)
using (var delayTimeValue = gifImageProperties?.ValueForKey(ImageIO.CGImageProperties.GIFDelayTime))
{
if (unclampedDelayTimeValue != null)
double.TryParse(unclampedDelayTimeValue.ToString(), out delayTime);
double.TryParse(unclampedDelayTimeValue.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out delayTime);
else if (delayTimeValue != null)
double.TryParse(delayTimeValue.ToString(), out delayTime);
double.TryParse(delayTimeValue.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out delayTime);

// Frame delay compability adjustment.
if (delayTime <= 0.02f)
Expand Down
3 changes: 3 additions & 0 deletions src/Compatibility/Core/src/iOS/Renderers/ListViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
}
_tableViewController = new FormsUITableViewController(e.NewElement, _usingLargeTitles);
SetNativeControl(_tableViewController.TableView);

if (Forms.IsiOS15OrNewer)
_tableViewController.TableView.SectionHeaderTopPadding = new nfloat(0);

_backgroundUIView = _tableViewController.TableView.BackgroundView;

Expand Down
34 changes: 31 additions & 3 deletions src/Controls/src/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Application() : this(true)
}

internal Application(bool setCurrentApplication)
{
{
if (setCurrentApplication)
SetCurrentApplication(this);

Expand Down Expand Up @@ -101,7 +101,7 @@ public Page? MainPage
OnPropertyChanging();

_singleWindowMainPage = value;

if (Windows.Count == 1)
{
Windows[0].Page = value;
Expand Down Expand Up @@ -179,8 +179,36 @@ public AppTheme UserAppTheme
/// <include file="../../docs/Microsoft.Maui.Controls/Application.xml" path="//Member[@MemberName='RequestedTheme']/Docs" />
public AppTheme RequestedTheme => UserAppTheme != AppTheme.Unspecified ? UserAppTheme : PlatformAppTheme;

static Color? _accentColor;
/// <include file="../../docs/Microsoft.Maui.Controls/Application.xml" path="//Member[@MemberName='AccentColor']/Docs" />
public static Color? AccentColor { get; set; }
public static Color? AccentColor
{
get => _accentColor ??= GetAccentColor();
set => _accentColor = value;
}


static Color? GetAccentColor()
{
#if WINDOWS
if (UI.Xaml.Application.Current.Resources.TryGetValue("SystemColorControlAccentBrush", out object accent) &&
accent is UI.Xaml.Media.SolidColorBrush scb)
{
return scb.ToColor();
}

return null;
#elif ANDROID
if (Current?.Windows?.Count > 0)
return Current.Windows[0].MauiContext.Context?.GetAccentColor();

return null;
#elif IOS
return ColorExtensions.AccentColor.ToColor();
#else
return Color.FromRgba(50, 79, 133, 255);
#endif
}

public event EventHandler<AppThemeChangedEventArgs> RequestedThemeChanged
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ void OnDataChanged()
// we need to reset the ListView's adapter to reflect the changes on page B
// If there header and footer are present at the reset time of the adapter
// they will be DOUBLE added to the ViewGround (the ListView) causing indexes to be off by one.

if (_realListView.IsDisposed())
return;

_realListView.RemoveHeaderView(HeaderView);
_realListView.RemoveFooterView(FooterView);
_realListView.Adapter = _realListView.Adapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,27 @@ void UpdateIsRefreshing(bool isInitialValue = false)
});
}
else
_refresh.Refreshing = isRefreshing;
_refresh.Refreshing = isRefreshing;

// Allow to disable SwipeToRefresh layout AFTER refresh is done
UpdateIsSwipeToRefreshEnabled();
}
}

void UpdateIsSwipeToRefreshEnabled()
{
if (_refresh != null)
_refresh.Enabled = Element.IsPullToRefreshEnabled && (Element as IListViewController).RefreshAllowed;
{
var isEnabled = Element.IsPullToRefreshEnabled && (Element as IListViewController).RefreshAllowed;
_refresh.Post(() =>
{
// NOTE: only disable while NOT refreshing, otherwise Command bindings CanExecute behavior will effectively
// cancel refresh animation. If not possible right now we will be called by UpdateIsRefreshing().
// For details see https://github.com/xamarin/Xamarin.Forms/issues/8384
if (isEnabled || !_refresh.Refreshing)
_refresh.Enabled = isEnabled;
});
}
}

void UpdateFastScrollEnabled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,10 @@ void SetCell(object newContext)
// If there is a ListView, load the Cell content from the ItemTemplate.
// Otherwise, the given Cell is already a templated Cell from a TableView.
ListView lv = _listView.Value;

if (lv != null)
{
// 🚀 If there is an old cell, check if it was a group header
// we need this later to know whether we can recycle this cell
bool? wasGroupHeader = null;
var oldCell = Cell;
if (oldCell != null)
{
wasGroupHeader = oldCell.GetIsGroupHeader<ItemsView<Cell>, Cell>();
}

Cell oldCell = Cell;
bool isGroupHeader = IsGroupHeader;
DataTemplate template = isGroupHeader ? lv.GroupHeaderTemplate : lv.ItemTemplate;
object bindingContext = newContext;
Expand All @@ -358,15 +351,8 @@ void SetCell(object newContext)
sameTemplate = oldTemplate == template;
}
}
// 🚀 if there is no datatemplateselector, we now verify if the old cell
// was a groupheader and whether the new one is as well.
// Again, this is only to verify we can reuse this cell
else if (wasGroupHeader.HasValue)
{
sameTemplate = wasGroupHeader == isGroupHeader;
}

// reuse cell
// Reuse cell
var canReuseCell = Cell != null && sameTemplate;

// 🚀 If we can reuse the cell, just reuse it...
Expand Down Expand Up @@ -403,7 +389,8 @@ void SetCell(object newContext)

if (Cell != cell)
Cell = cell;
// 🚀 even if the cell did not change, we **must** call SendDisappearing() and SendAppearing()

// 🚀 Even if the cell did not change, we **must** call SendDisappearing() and SendAppearing()
// because frameworks such as Reactive UI rely on this! (this.WhenActivated())
else if (Cell != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
}
_tableViewController = new FormsUITableViewController(e.NewElement, _usingLargeTitles);
SetNativeControl(_tableViewController.TableView);

if (PlatformVersion.IsAtLeast(15))
_tableViewController.TableView.SectionHeaderTopPadding = new nfloat(0);

_backgroundUIView = _tableViewController.TableView.BackgroundView;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ void Destroy()

if (_root is ViewGroup vg)
vg.RemoveView(_shellPageContainer);

_shellPageContainer.Dispose();
}

_root?.Dispose();
Expand All @@ -186,6 +188,7 @@ void Destroy()
_root = null;
_viewhandler = null;
_shellContent = null;
_shellPageContainer = null;
}

protected override void Dispose(bool disposing)
Expand Down
Loading

0 comments on commit 41bb8c9

Please sign in to comment.