Skip to content

Commit

Permalink
[Tizen] Add ImageButton handler (dotnet#261)
Browse files Browse the repository at this point in the history
* [Tizen] Add ImageButton handler

* Add comment
  • Loading branch information
shyunMin authored and rookiejava committed Jan 19, 2022
1 parent fc24e40 commit 2ecd407
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 10 deletions.
52 changes: 43 additions & 9 deletions src/Core/src/Handlers/ImageButton/ImageButtonHandler.Tizen.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
using System;
using System.Collections.Generic;
using System.Text;
using ElmSharp;
using TImage = Tizen.UIExtensions.ElmSharp.Image;

namespace Microsoft.Maui.Handlers
{
//TODO: Need to impl
public partial class ImageButtonHandler : ViewHandler<IImageButton, TImage>
public partial class ImageButtonHandler : ViewHandler<IImageButton, MauiImageButton>
{
protected override TImage CreateNativeView() => throw new NotImplementedException();

void OnSetImageSource(TImage? obj)
protected override MauiImageButton CreateNativeView()
{
throw new NotImplementedException();
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} must be set to create a ImageButton");
_ = NativeParent ?? throw new InvalidOperationException($"{nameof(NativeParent)} cannot be null");

var view = new MauiImageButton(NativeParent);
return view;
}

protected override void ConnectHandler(MauiImageButton nativeView)
{
nativeView.Clicked += OnClicked;
nativeView.Pressed += OnPressed;
nativeView.Released += OnReleased;
base.ConnectHandler(nativeView);
}

protected override void DisconnectHandler(MauiImageButton nativeView)
{
nativeView.Clicked -= OnClicked;
nativeView.Pressed -= OnPressed;
nativeView.Released -= OnReleased;
base.DisconnectHandler(nativeView);
}

private void OnReleased(object? sender, EventArgs e)
{
VirtualView?.Released();
}

private void OnPressed(object? sender, EventArgs e)
{
VirtualView?.Pressed();
}

private void OnClicked(object? sender, EventArgs e)
{
VirtualView?.Clicked();
}

void OnSetImageSource(TImage? img)
{
//Empty on purpose
}
}
}
3 changes: 2 additions & 1 deletion src/Core/src/Handlers/ImageButton/ImageButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public ImageButtonHandler(IPropertyMapper mapper) : base(mapper ?? Mapper)
IImage IImageHandler.TypedVirtualView => VirtualView;

NativeImageView IImageHandler.TypedNativeView =>
#if __IOS__
#if __IOS__ || TIZEN
NativeView.ImageView;
#elif WINDOWS

NativeView.GetContent<NativeImageView>() ?? throw new InvalidOperationException("ImageButton did not contain an Image element.");
#else
NativeView;
Expand Down
65 changes: 65 additions & 0 deletions src/Core/src/Platform/Tizen/MauiImageButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using ElmSharp;
using Tizen.UIExtensions.Common;
using Tizen.UIExtensions.ElmSharp;
using TButton = Tizen.UIExtensions.ElmSharp.Button;
using TImage = Tizen.UIExtensions.ElmSharp.Image;

namespace Microsoft.Maui
{
public class MauiImageButton : Canvas
{
TImage _image;
TButton _button;

public MauiImageButton(EvasObject parent) : base(parent)
{
_image = new TImage(parent);
_button = new TButton(parent);

_button.Clicked += OnClicked;
_button.Pressed += OnPressed;
_button.Released += OnReleased;
_button.SetTransparentStyle();

Children.Add(_image);
_image.RaiseTop();

Children.Add(_button);
_button.SetTransparentStyle();
_button.RaiseTop();

LayoutUpdated += OnLayout;
}

public TImage ImageView
{
get => _image;
}

public event EventHandler? Clicked;
public event EventHandler? Pressed;
public event EventHandler? Released;

void OnReleased(object? sender, EventArgs e)
{
Released?.Invoke(this, EventArgs.Empty);
}

void OnPressed(object? sender, EventArgs e)
{
Pressed?.Invoke(this, EventArgs.Empty);
}

void OnClicked(object? sender, EventArgs e)
{
Clicked?.Invoke(this, EventArgs.Empty);
}

void OnLayout(object? sender, LayoutEventArgs e)
{
_button.Geometry = Geometry;
_image.Geometry = Geometry;
}
}
}

0 comments on commit 2ecd407

Please sign in to comment.