Skip to content

Commit

Permalink
feat: Support icon elements for NativeCommandBarPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Jun 23, 2023
1 parent 98c0424 commit 2dcad9b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 73 deletions.
23 changes: 6 additions & 17 deletions src/Uno.UI/Controls/CommandBar/AppBarButtonRenderer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,19 @@ protected override void Render()
native.SetIcon(null);
native.SetTitle(element.Label);
}
else if (element.Icon != null)
else
{
switch (element.Icon)
var iconOrContent = element.Icon ?? element.Content;

switch (iconOrContent)
{
case BitmapIcon bitmap:
var drawable = DrawableHelper.FromUri(bitmap.UriSource);
native.SetIcon(drawable);
native.SetActionView(null);
native.SetTitle(null);
break;

case FontIcon font: // not supported
case PathIcon path: // not supported
case SymbolIcon symbol: // not supported
default:
this.Log().Warn($"{GetType().Name ?? "FontIcon, PathIcon and SymbolIcon"} are not supported. Use BitmapIcon instead with UriSource.");
native.SetIcon(null);
break;
}
native.SetActionView(null);
native.SetTitle(null);
}
else
{
switch (element.Content)
{
case string text:
native.SetIcon(null);
native.SetActionView(null);
Expand Down
93 changes: 37 additions & 56 deletions src/Uno.UI/Controls/CommandBar/AppBarButtonRenderer.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,63 +86,44 @@ protected override void Render()
var native = Native;
var element = Element;

if (element.Icon != null)
var iconOrContent = element.Icon ?? element.Content;
switch (element.Icon)
{
switch (element.Icon)
{
case BitmapIcon bitmap:
native.Image = UIImageHelper.FromUri(bitmap.UriSource);
native.ClearCustomView();
native.Title = null;
break;

case FontIcon font: // not supported
case PathIcon path: // not supported
case SymbolIcon symbol: // not supported
default:
this.Log().Warn($"{GetType().Name ?? "FontIcon, PathIcon and SymbolIcon"} are not supported. Use BitmapIcon instead with UriSource.");
native.Image = null;
native.ClearCustomView();
// iOS doesn't add the UIBarButtonItem to the native logical tree unless it has an Image or Title set.
// We default to an empty string to ensure it is added.
native.Title = string.Empty;
break;
}
}
else
{
switch (element.Content)
{
case string text:
native.Image = null;
native.ClearCustomView();
native.Title = text;
break;

case FrameworkElement fe:
var currentParent = element.GetParent();
_appBarButtonWrapper.Child = element;

//Restore the original parent if any, as we
// want the DataContext to flow properly from the
// CommandBar.
element.SetParent(currentParent);

native.Image = null;
native.CustomView = fe.Visibility == Visibility.Visible ? _appBarButtonWrapper : null;
// iOS doesn't add the UIBarButtonItem to the native logical tree unless it has an Image or Title set.
// We default to an empty string to ensure it is added, in order to support late-bound Content.
native.Title = string.Empty;
break;

default:
native.Image = null;
native.ClearCustomView();
// iOS doesn't add the UIBarButtonItem to the native logical tree unless it has an Image or Title set.
// We default to an empty string to ensure it is added.
native.Title = string.Empty;
break;
}
case BitmapIcon bitmap:
native.Image = UIImageHelper.FromUri(bitmap.UriSource);
native.ClearCustomView();
native.Title = null;
break;

case string text:
native.Image = null;
native.ClearCustomView();
native.Title = text;
break;

case FrameworkElement fe:
var currentParent = element.GetParent();
_appBarButtonWrapper.Child = element;

//Restore the original parent if any, as we
// want the DataContext to flow properly from the
// CommandBar.
element.SetParent(currentParent);

native.Image = null;
native.CustomView = fe.Visibility == Visibility.Visible ? _appBarButtonWrapper : null;
// iOS doesn't add the UIBarButtonItem to the native logical tree unless it has an Image or Title set.
// We default to an empty string to ensure it is added, in order to support late-bound Content.
native.Title = string.Empty;
break;

default:
native.Image = null;
native.ClearCustomView();
// iOS doesn't add the UIBarButtonItem to the native logical tree unless it has an Image or Title set.
// We default to an empty string to ensure it is added.
native.Title = string.Empty;
break;
}

// Label
Expand Down

0 comments on commit 2dcad9b

Please sign in to comment.