Skip to content
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

ImageButtonHandler and Handler Re-usability #2352

Merged
merged 23 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<views:BasePage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Pages.ImageButtonPage"
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base"
Title="ImageButton">
<views:BasePage.Content>
<VerticalStackLayout Margin="12" Spacing="6">
<Label
Text="AspectFit"
Style="{StaticResource Headline}"/>
<ImageButton HorizontalOptions="Center" WidthRequest="200" HeightRequest="100" Clicked="OnImageButtonClicked" Aspect="AspectFit" Background="Green" Source="settings.png" Padding="16, 9, 16, 9" />
<Label
Text="AspectFill"
Style="{StaticResource Headline}"/>
<ImageButton HorizontalOptions="Center" WidthRequest="200" HeightRequest="100" Clicked="OnImageButtonClicked" Aspect="AspectFill" Background="Green" Source="settings.png" Padding="16, 9, 16, 9" />
<Label
Text="Fill"
Style="{StaticResource Headline}"/>
<ImageButton HorizontalOptions="Center" WidthRequest="200" HeightRequest="100" Clicked="OnImageButtonClicked" Aspect="Fill" Background="Green" Source="settings.png" Padding="16, 9, 16, 9" />
<Label
x:Name="InfoLabel"
Text="0 ImageButton clicks"
FontSize="16"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</VerticalStackLayout>
</views:BasePage.Content>
</views:BasePage>
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using Microsoft.Maui.Controls;

namespace Maui.Controls.Sample.Pages
{
public partial class ImageButtonPage
{

int _clickTotal;

public ImageButtonPage()
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/samples/Controls.Sample/XamlApp.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public XamlApp(IServiceProvider services, ITextService textService)

Debug.WriteLine($"The injected text service had a message: '{textService.GetText()}'");

MainPage = Services.GetRequiredService<Page>();
MainPage = new Pages.ImageButtonPage(); //Services.GetRequiredService<Page>();

RequestedThemeChanged += (sender, args) =>
{
Expand Down
32 changes: 26 additions & 6 deletions src/Controls/src/Core/HandlerImpl/Button/Button.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

namespace Microsoft.Maui.Controls
{
public partial class Button : IButton
public partial class Button : IButton, IText
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed IText from IButton because Button might not be a Text based button.

{
public new static void RemapForControls()
{
// IButton does not include the ContentType property, so we map it here to handle Image Positioning

IPropertyMapper<IButton, ButtonHandler> ControlsButtonMapper = new PropertyMapper<Button, ButtonHandler>(ButtonHandler.ButtonMapper)
IPropertyMapper<IButton, ButtonHandler> ControlsButtonMapper = new PropertyMapper<Button, ButtonHandler>(ButtonHandler.Mapper)
{
[nameof(ContentLayout)] = MapContentLayout,
#if __IOS__
[nameof(Padding)] = MapPadding,
#endif
};

ButtonHandler.ButtonMapper = ControlsButtonMapper;
ButtonHandler.Mapper = ControlsButtonMapper;
}

public static void MapContentLayout(ButtonHandler handler, Button button)
Expand Down Expand Up @@ -46,8 +46,28 @@ void IButton.ImageSourceLoaded()
Handler?.UpdateValue(nameof(ContentLayout));
}

IImageSource IButton.ImageSource => ImageSource;

Font ITextStyle.Font => (Font)GetValue(FontElement.FontProperty);

ButtonImageSourcePart _buttonImageSourcePart;
IImageSourcePart IButton.ImageSource => _buttonImageSourcePart ??= new ButtonImageSourcePart(this);

class ButtonImageSourcePart : IImageSourcePart
{
readonly Button _button;

public IImageSource Source => _button.ImageSource;

public bool IsAnimationPlaying => false;

public void UpdateIsLoading(bool isLoading)
{

}

public ButtonImageSourcePart(Button button)
{
_button = button;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Maui.Controls
{
public partial class ImageButton : IImageButton
{
void IImageSourcePart.UpdateIsLoading(bool isLoading) { }

bool IImageSourcePart.IsAnimationPlaying => false;

IImageSource IImageSourcePart.Source => Source;

void IButton.Clicked()
{
(this as IButtonController).SendClicked();
}

void IButton.Pressed()
{
(this as IButtonController).SendPressed();
}

void IButton.Released()
{
(this as IButtonController).SendReleased();
}

void IButton.ImageSourceLoaded()
{
}

IImageSourcePart IButton.ImageSource => this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static partial class AppHostBuilderExtensions
{ typeof(Shapes.Polyline), typeof(ShapeViewHandler) },
{ typeof(Shapes.Rectangle), typeof(ShapeViewHandler) },
{ typeof(Window), typeof(WindowHandler) },
{ typeof(ImageButton), typeof(ImageButtonHandler) },
#if __ANDROID__ || __IOS__
{ typeof(RefreshView), typeof(RefreshViewHandler) },
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/src/Core/ImageButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.Maui.Controls
{
public class ImageButton : View, IImageController, IElementConfiguration<ImageButton>, IBorderElement, IButtonController, IViewController, IPaddingElement, IButtonElement, IImageElement
public partial class ImageButton : View, IImageController, IElementConfiguration<ImageButton>, IBorderElement, IButtonController, IViewController, IPaddingElement, IButtonElement, IImageElement
{
const int DefaultCornerRadius = -1;

Expand Down
13 changes: 1 addition & 12 deletions src/Controls/src/Core/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Microsoft.Maui.Controls
{
public class MenuItem : BaseMenuItem, IMenuItemController, IStyleSelectable, IImageSourcePart
public class MenuItem : BaseMenuItem, IMenuItemController, IStyleSelectable
Copy link
Member Author

@PureWeen PureWeen Sep 21, 2021

Choose a reason for hiding this comment

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

The new ImageSourceExtensions.cs approach made this interface unnecessary

{
public static readonly BindableProperty AcceleratorProperty = BindableProperty.CreateAttached(nameof(Accelerator), typeof(Accelerator), typeof(MenuItem), null);

Expand Down Expand Up @@ -141,16 +141,5 @@ void OnCommandParameterChanged()

IsEnabledCore = Command.CanExecute(CommandParameter);
}


IImageSource IImageSourcePart.Source => this.IconImageSource;

bool _isLoading;
bool IImageSourcePart.IsAnimationPlaying => false;

void IImageSourcePart.UpdateIsLoading(bool isLoading)
{
_isLoading = isLoading;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,10 @@ void clickCallback(object s, EventArgs e)

image.ImageTintList = ColorStateList.ValueOf(Colors.Black.MultiplyAlpha(0.6f).ToNative());

ImageSourceLoader shellImagePart = new ImageSourceLoader()
shellContent.icon.LoadImage(mauiContext, result =>
Copy link
Member Author

Choose a reason for hiding this comment

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

All of the changes involving ImageSourceLoader is just me moving this behavior ImageViewExtensions

{
Source = shellContent.icon
};


var services = mauiContext.Services;
var provider = services.GetRequiredService<IImageSourceServiceProvider>();
image.UpdateSourceAsync(new ImageSourceLoader() { Source = shellContent.icon }, provider)
.FireAndForget(e => Internals.Log.Warning("MenuItem", $"{e}"));
image.SetImageDrawable(result.Value);
});

innerLayout.AddView(image);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void UpdateTitleIcon()
_imageSource = source;
_titleIconView.SetImageResource(global::Android.Resource.Color.Transparent);

ImageSourceLoader.LoadImage(source, MauiContext, (result) =>
source.LoadImage(MauiContext, (result) =>
{
_titleIconView.SetImageDrawable(result.Value);
AutomationPropertiesProvider.AccessibilitySettingsChanged(_titleIconView, source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static void UpdateMenuItem(AToolbar toolbar,

internal static void UpdateMenuItemIcon(IMauiContext mauiContext, IMenuItem menuItem, ToolbarItem toolBarItem, Color tintColor)
{
ImageSourceLoader.LoadImage(toolBarItem, mauiContext, result =>
toolBarItem.IconImageSource.LoadImage(mauiContext, result =>
{
var baseDrawable = result.Value;
if (menuItem == null || !menuItem.IsAlive())
Expand Down
51 changes: 0 additions & 51 deletions src/Controls/src/Core/Platform/Android/ImageSourceLoader.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class ShellFlyoutTemplatedContentView : Java.Lang.Object, IShellFlyoutCon
int _actionBarHeight;
int _flyoutHeight;
int _flyoutWidth;
ImageSourceLoader _shellFlyoutBackgroundImagePart;

protected IMauiContext MauiContext => _shellContext.Shell.Handler.MauiContext;

Expand All @@ -53,7 +52,6 @@ public class ShellFlyoutTemplatedContentView : Java.Lang.Object, IShellFlyoutCon
public ShellFlyoutTemplatedContentView(IShellContext shellContext)
{
_shellContext = shellContext;
_shellFlyoutBackgroundImagePart = new ImageSourceLoader();
LoadView(shellContext);
}

Expand Down Expand Up @@ -351,10 +349,9 @@ protected virtual void UpdateFlyoutBackground()
UpdateFlyoutBgImageAsync();
}

async void UpdateFlyoutBgImageAsync()
void UpdateFlyoutBgImageAsync()
{
var imageSource = _shellContext.Shell.FlyoutBackgroundImage;
_shellFlyoutBackgroundImagePart.Source = imageSource;

if (imageSource == null || !_shellContext.Shell.IsSet(Shell.FlyoutBackgroundImageProperty))
{
Expand All @@ -366,8 +363,11 @@ async void UpdateFlyoutBgImageAsync()
var services = MauiContext.Services;
var provider = services.GetRequiredService<IImageSourceServiceProvider>();

using (var result = await _bgImage.UpdateSourceAsync(_shellFlyoutBackgroundImagePart, provider))
_bgImage.Clear();
imageSource.LoadImage(MauiContext, result =>
{
_bgImage.SetImageDrawable(result.Value);

if (!_rootView.IsAlive())
return;

Expand Down Expand Up @@ -400,7 +400,7 @@ async void UpdateFlyoutBgImageAsync()
else
_rootView.AddView(_bgImage, 0);
}
}
});
}

protected virtual void UpdateFlyoutHeaderBehavior()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ void clickCallback(object s, EventArgs e)
var provider = services.GetRequiredService<IImageSourceServiceProvider>();
var icon = shellContent.Icon;

var imageLoad = new ImageSourceLoader() { Source = shellContent.Icon, MauiContext = MauiContext };

imageLoad.LoadImage(image,
shellContent.Icon.LoadImage(
MauiContext,
(result) =>
{
image.SetImageDrawable(result.Value);
if (result.Value != null)
{
var color = Colors.Black.MultiplyAlpha(0.6f).ToNative();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,17 @@ AImageButton CreateImageButton(Context context, BindableObject bindable, Bindabl
if (bindable.GetValue(property) is ImageSource image)
AutomationPropertiesProvider.SetContentDescription(result, image, null, null);

new ImageSourceLoader()
((ImageSource)bindable.GetValue(property)).LoadImage(MauiContext, (r) =>
{
Source = (ImageSource)bindable.GetValue(property),
MauiContext = MauiContext
}.LoadImage(result);
result.SetImageDrawable(r.Value);
});

var lp = new LinearLayout.LayoutParams((int)Context.ToPixels(22), LP.MatchParent)
{
LeftMargin = leftMargin,
RightMargin = rightMargin
};

result.LayoutParameters = lp;
lp.Dispose();
result.SetBackground(null);
Expand Down
Loading