Skip to content

Commit

Permalink
- additional
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Sep 21, 2021
1 parent 4bf6b9d commit ec4a587
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/Controls/samples/Controls.Sample/Pages/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
RowDefinitions="150, *, Auto"
RowSpacing="0">
<!-- HEADER -->
<Image
<ImageButton
Grid.Row="0"
Aspect="AspectFill"
Source="header_background"/>
Expand Down
47 changes: 30 additions & 17 deletions src/Controls/src/Core/HandlerImpl/Button/Button.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,26 @@

namespace Microsoft.Maui.Controls
{
public partial class Button : IButton, IText, IImageSourcePart
public partial class Button : IButton, IText
{
public new static void RemapForControls()
{
// IButton does not include the ContentType property, so we map it here to handle Image Positioning

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

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

public static void MapContentLayout(IButtonHandler handler, Button button)
public static void MapContentLayout(ButtonHandler handler, Button button)
{
#if __IOS__
(handler.NativeView as UIKit.UIButton)?.UpdateContentLayout(button);
#elif __ANDROID__
(handler.NativeView as Google.Android.Material.Button.MaterialButton)?.UpdateContentLayout(button);
#endif
handler.NativeView.UpdateContentLayout(button);
}

void IButton.Clicked()
Expand All @@ -45,16 +41,33 @@ void IButton.Released()
(this as IButtonController).SendReleased();
}

IImageSource IImageSourcePart.Source => ImageSource;

void IImageSourcePart.UpdateIsLoading(bool isLoading)
void IButton.ImageSourceLoaded()
{
if (!isLoading)
Handler?.UpdateValue(nameof(ContentLayout));
Handler?.UpdateValue(nameof(ContentLayout));
}

bool IImageSourcePart.IsAnimationPlaying => false;

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
Expand Up @@ -6,12 +6,12 @@ namespace Microsoft.Maui.Controls
{
public partial class ImageButton : IImageButton
{
IImageSource IImageSourcePart.Source => Source;

void IImageSourcePart.UpdateIsLoading(bool isLoading) { }

bool IImageSourcePart.IsAnimationPlaying => false;

IImageSource IImageSourcePart.Source => Source;

void IButton.Clicked()
{
(this as IButtonController).SendClicked();
Expand All @@ -26,5 +26,11 @@ void IButton.Released()
{
(this as IButtonController).SendReleased();
}

void IButton.ImageSourceLoaded()
{
}

IImageSourcePart IButton.ImageSource => this;
}
}
2 changes: 1 addition & 1 deletion src/Core/src/Core/IButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IButton : IView, IPadding
/// <summary>
/// Allows you to display a bitmap image on the Button.
/// </summary>
IImageSource? ImageSource { get; }
IImageSourcePart? ImageSource { get; }


void ImageSourceLoaded();
Expand Down
6 changes: 1 addition & 5 deletions src/Core/src/Handlers/Button/ButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ public partial class ButtonHandler : IButtonHandler
{
ImageSourcePartLoader? _imageSourcePartLoader;
public ImageSourcePartLoader ImageSourceLoader =>
_imageSourcePartLoader ??= new ImageSourcePartLoader(this,
() => VirtualView.ImageSource,
() => false,
(isLoading) => (VirtualView as IImage)?.UpdateIsLoading(isLoading),
OnSetImageSource);
_imageSourcePartLoader ??= new ImageSourcePartLoader(this, () => VirtualView?.ImageSource, OnSetImageSource);

public static IPropertyMapper<ITextStyle, IButtonHandler> TextStyleMapper = new PropertyMapper<ITextStyle, IButtonHandler>(ViewHandler.ViewMapper)
{
Expand Down
6 changes: 1 addition & 5 deletions src/Core/src/Handlers/Image/ImageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public partial class ImageHandler : IImageHandler

ImageSourcePartLoader? _imageSourcePartLoader;
public ImageSourcePartLoader SourceLoader =>
_imageSourcePartLoader ??= new ImageSourcePartLoader(this,
() => VirtualView.Source,
() => VirtualView.IsAnimationPlaying,
(isLoading) => (VirtualView as IImage)?.UpdateIsLoading(isLoading),
OnSetImageSource);
_imageSourcePartLoader ??= new ImageSourcePartLoader(this, () => VirtualView, OnSetImageSource);

public ImageHandler() : base(Mapper)
{
Expand Down
8 changes: 3 additions & 5 deletions src/Core/src/Platform/Android/ImageSourcePartWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@

namespace Microsoft.Maui
{
public partial class ImageSourcePartLoader : IImageSourcePart
public partial class ImageSourcePartLoader
{
Action<Drawable?>? SetImage { get; }

View? NativeView => Handler.NativeView as View;

public ImageSourcePartLoader(
IElementHandler handler,
Func<IImageSource?> getSource,
Func<bool>? getIsAnimationPlaying,
Action<bool>? setIsLoading,
Action<Drawable?> setDrawable) : this(handler, getSource, getIsAnimationPlaying, setIsLoading)
Func<IImageSourcePart?> imageSourcePart,
Action<Drawable?> setDrawable) : this(handler, imageSourcePart)
{
SetImage = setDrawable;
}
Expand Down
46 changes: 26 additions & 20 deletions src/Core/src/Platform/ImageSourcePartWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,58 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Handlers;
#if __IOS__ || MACCATALYST
using NativeImage = UIKit.UIImage;
#elif MONOANDROID
using NativeImage = Android.Graphics.Drawables.Drawable;
#elif WINDOWS
using NativeImage = Microsoft.UI.Xaml.Media.ImageSource;
#elif NETSTANDARD || (NET6_0 && !IOS && !ANDROID)
using NativeImage = System.Object;
#endif

namespace Microsoft.Maui
{
public partial class ImageSourcePartLoader : IImageSourcePart
public partial class ImageSourcePartLoader
{
public ImageSourceServiceResultManager SourceManager { get; } = new ImageSourceServiceResultManager();

Func<IImageSource?>? GetSource { get; }

Func<bool>? GetIsAnimationPlaying { get; }
readonly Func<IImageSourcePart?> _imageSourcePart;

Action<bool>? SetIsLoading { get; }
public ImageSourceServiceResultManager SourceManager { get; } = new ImageSourceServiceResultManager();

IElementHandler Handler { get; }

internal ImageSourcePartLoader(
IElementHandler handler,
Func<IImageSource?> getSource,
Func<bool>? getIsAnimationPlaying,
Action<bool>? setIsLoading)
Func<IImageSourcePart?> imageSourcePart)
{
Handler = handler;
_imageSourcePart = imageSourcePart;
}

public void Reset()
{
SourceManager.Reset();
}

IImageSource? IImageSourcePart.Source => GetSource?.Invoke();

bool IImageSourcePart.IsAnimationPlaying => GetIsAnimationPlaying?.Invoke() ?? false;

void IImageSourcePart.UpdateIsLoading(bool isLoading) => SetIsLoading?.Invoke(isLoading);

public async Task UpdateImageSourceAsync()
{
#if __IOS__ || __ANDROID__ || WINDOWS
if (NativeView != null)
{
var token = this.SourceManager.BeginLoad();
var provider = Handler.GetRequiredService<IImageSourceServiceProvider>();
var result = await this.UpdateSourceAsync(NativeView, provider, SetImage!, token);
SourceManager.CompleteLoad(result);
var imageSource = _imageSourcePart();

if (imageSource != null)
{
var result = await imageSource.UpdateSourceAsync(NativeView, provider, SetImage!, token);
SourceManager.CompleteLoad(result);
}
else
{
SetImage?.Invoke(null);
SourceManager.CompleteLoad(null);
}
}
#else
await Task.CompletedTask;
Expand Down
6 changes: 2 additions & 4 deletions src/Core/src/Platform/Standard/ImageSourcePartWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ public partial class ImageSourcePartLoader
{
public ImageSourcePartLoader(
IElementHandler handler,
Func<IImageSource?> getSource,
Func<bool>? getIsAnimationPlaying,
Action<bool>? setIsLoading,
Func<IImageSourcePart?> imageSourcePart,
Action<object?> setImage)
: this(handler, getSource, getIsAnimationPlaying, setIsLoading)
: this(handler, imageSourcePart)
{
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/Core/src/Platform/Windows/ImageSourcePartWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ public partial class ImageSourcePartLoader

public ImageSourcePartLoader(
IElementHandler handler,
Func<IImageSource?> getSource,
Func<bool>? getIsAnimationPlaying,
Action<bool>? setIsLoading,
Func<IImageSourcePart?> imageSourcePart,
Action<ImageSource?> setImage)
: this(handler, getSource, getIsAnimationPlaying, setIsLoading)
: this(handler, imageSourcePart)
{
SetImage = setImage;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Core/src/Platform/iOS/ImageSourcePartWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public partial class ImageSourcePartLoader

public ImageSourcePartLoader(
IElementHandler handler,
Func<IImageSource?> getSource,
Func<bool>? getIsAnimationPlaying,
Action<bool>? setIsLoading,
Func<IImageSourcePart?> imageSourcePart,
Action<UIImage?> setImage)
: this(handler, getSource, getIsAnimationPlaying, setIsLoading)
: this(handler, imageSourcePart)
{
SetImage = setImage;
}
Expand Down

0 comments on commit ec4a587

Please sign in to comment.