Skip to content
Merged
Show file tree
Hide file tree
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 @@ -4,6 +4,7 @@
using CoreGraphics;
using Foundation;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Graphics.Platform;
using Microsoft.Maui.Platform;
using ObjCRuntime;
using UIKit;
Expand Down Expand Up @@ -57,6 +58,35 @@ internal static SecondarySubToolbarItem ToSecondarySubToolbarItem(this ToolbarIt
return new SecondarySubToolbarItem(item, action);
}

static UIImage ScaleImageToSystemDefaults(ImageSource imageSource, UIImage uIImage)
{
var icon = uIImage;

var originalImageSize = icon?.Size ?? CGSize.Empty;

// The largest height you can use for navigation bar icons in iOS.
// Per Apple's Human Interface Guidelines, the navigation bar height is 44 points,
// so using the full height ensures maximum visual clarity and maintains consistency
// with iOS design standards. This allows icons to utilize the entire available
// vertical space within the navigation bar container.
var defaultIconHeight = 44f;
Copy link

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 44f should be extracted to a named constant to improve maintainability and make the iOS navigation bar height constraint explicit.

Suggested change
var defaultIconHeight = 44f;
var defaultIconHeight = NavigationBarHeight;

Copilot uses AI. Check for mistakes.
var buffer = 0.1;
Copy link

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 0.1 should be extracted to a named constant with a descriptive name explaining its purpose as a tolerance buffer for size comparison.

Suggested change
var buffer = 0.1;
var buffer = IconHeightComparisonTolerance;

Copilot uses AI. Check for mistakes.
// We only check height because the navigation bar constrains vertical space (44pt height),
// but allows horizontal flexibility. Width can vary based on icon design and content,
// while height must fit within the fixed navigation bar bounds to avoid clipping.

// if the image is bigger than the default available size, resize it
if (icon is not null && originalImageSize.Height - defaultIconHeight > buffer)
{
if (imageSource is not FontImageSource fontImageSource || !fontImageSource.IsSet(FontImageSource.SizeProperty))
{
icon = icon.ResizeImageSource(originalImageSize.Width, defaultIconHeight, originalImageSize);
Copy link

Copilot AI Aug 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ResizeImageSource call passes originalImageSize.Width for the new width but also passes originalImageSize as the third parameter. This appears redundant and could be confusing - consider clarifying the intent or simplifying the parameters.

Suggested change
icon = icon.ResizeImageSource(originalImageSize.Width, defaultIconHeight, originalImageSize);
icon = icon.ResizeImageSource(originalImageSize.Width, defaultIconHeight);

Copilot uses AI. Check for mistakes.
}
}

return icon;
}

sealed class PrimaryToolbarItem : UIBarButtonItem
{
readonly bool _forceName;
Expand Down Expand Up @@ -302,11 +332,11 @@ void OnPropertyChanged(object sender, PropertyChangedEventArgs e)

void UpdateIcon(ToolbarItem item)
{
if (item.IconImageSource != null && !item.IconImageSource.IsEmpty)
if (item.IconImageSource is not null && !item.IconImageSource.IsEmpty)
{
item.IconImageSource.LoadImage(item.FindMauiContext(), result =>
{
((SecondaryToolbarItemContent)CustomView).Image = result?.Value;
((SecondaryToolbarItemContent)CustomView).Image = ScaleImageToSystemDefaults(item.IconImageSource, result?.Value);
});
}
else
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading