Skip to content

Commit

Permalink
[NUI] Add IndicatorViewHandler (dotnet#518)
Browse files Browse the repository at this point in the history
* Add IndicatorViewHandler

* Update review comment

* Add space
  • Loading branch information
myroot committed Aug 24, 2022
1 parent fb26f6b commit 85f5b23
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#elif WINDOWS
using PlatformView = Microsoft.Maui.Platform.MauiPageControl;
#elif TIZEN
using PlatformView = Tizen.NUI.BaseComponents.View;
using PlatformView = Microsoft.Maui.Platform.MauiPageControl;
#elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID && !TIZEN)
using PlatformView = System.Object;
#endif
Expand Down
52 changes: 29 additions & 23 deletions src/Core/src/Handlers/IndicatorView/IndicatorViewHandler.Tizen.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
using System;
using NView = Tizen.NUI.BaseComponents.View;

namespace Microsoft.Maui.Handlers
namespace Microsoft.Maui.Handlers
{
public partial class IndicatorViewHandler : ViewHandler<IIndicatorView, NView>
public partial class IndicatorViewHandler : ViewHandler<IIndicatorView, MauiPageControl>
{
protected override NView CreatePlatformView() => new NView
{
BackgroundColor = Tizen.NUI.Color.Red
};
protected override MauiPageControl CreatePlatformView() => new MauiPageControl(VirtualView);

public static void MapCount(IIndicatorViewHandler handler, IIndicatorView indicator)
{
handler.PlatformView.UpdateCount();
}

public static void MapPosition(IIndicatorViewHandler handler, IIndicatorView indicator)
{
handler.PlatformView.UpdatePosition();
}

//TODO : Need to impl
[MissingMapper]
public static void MapHideSingle(IIndicatorViewHandler handler, IIndicatorView indicator) { }

[MissingMapper]
public static void MapMaximumVisible(IIndicatorViewHandler handler, IIndicatorView indicator) { }
public static void MapHideSingle(IIndicatorViewHandler handler, IIndicatorView indicator)
{
handler.PlatformView.UpdateCount();
}

[MissingMapper]
public static void MapIndicatorSize(IIndicatorViewHandler handler, IIndicatorView indicator) { }
public static void MapMaximumVisible(IIndicatorViewHandler handler, IIndicatorView indicator)
{
handler.PlatformView.UpdateCount();
}

[MissingMapper]
public static void MapIndicatorColor(IIndicatorViewHandler handler, IIndicatorView indicator) { }
public static void MapIndicatorSize(IIndicatorViewHandler handler, IIndicatorView indicator)
{
handler.PlatformView.ResetIndicators();
}

[MissingMapper]
public static void MapSelectedIndicatorColor(IIndicatorViewHandler handler, IIndicatorView indicator) { }
public static void MapIndicatorColor(IIndicatorViewHandler handler, IIndicatorView indicator)
{
handler.PlatformView.ResetIndicators();
}

[MissingMapper]
public static void MapIndicatorShape(IIndicatorViewHandler handler, IIndicatorView indicator) { }
public static void MapSelectedIndicatorColor(IIndicatorViewHandler handler, IIndicatorView indicator)
{
handler.PlatformView.ResetIndicators();
}

public static void MapIndicatorShape(IIndicatorViewHandler handler, IIndicatorView indicator)
{
handler.PlatformView.ResetIndicators();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#elif WINDOWS
using PlatformView = Microsoft.Maui.Platform.MauiPageControl;
#elif TIZEN
using PlatformView = Tizen.NUI.BaseComponents.View;
using PlatformView = Microsoft.Maui.Platform.MauiPageControl;
#elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID && !TIZEN)
using PlatformView = System.Object;
#endif
Expand Down
246 changes: 246 additions & 0 deletions src/Core/src/Platform/Tizen/MauiPageControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
using System;
using System.Collections.Generic;
using Microsoft.Maui.Graphics;
using Tizen.UIExtensions.Common;
using Tizen.UIExtensions.NUI;
using Tizen.UIExtensions.NUI.GraphicsView;
using NExtents = Tizen.NUI.Extents;
using NLayoutParamPolicies = Tizen.NUI.BaseComponents.LayoutParamPolicies;
using NLinearLayout = Tizen.NUI.LinearLayout;
using NPointStateType = Tizen.NUI.PointStateType;
using NSize = Tizen.NUI.Size;
using NView = Tizen.NUI.BaseComponents.View;
using TSize = Tizen.UIExtensions.Common.Size;

namespace Microsoft.Maui.Platform
{
public class MauiPageControl : ViewGroup, IMeasurable
{
const double DefaultMargin = 4;

IIndicatorView _indicatorView;
IPlatformViewHandler? _templatedViewHandler;
ILayout? _templatedView;
NView? _contentView;

List<Indicator> _indicators = new List<Indicator>();
int _currentPoistion = -1;

public MauiPageControl(IIndicatorView view)
{
_indicatorView = view;
LayoutUpdated += OnLayoutUpdated;
}

bool UseDefaultIndicator { get; set; }

double IndicatorSizeWithMargin => IndicatorSize + DefaultMargin * 2;

double IndicatorSize => _indicatorView.IndicatorSize;

public void ResetIndicators()
{
ClearIndicatorView();
if ((_indicatorView as ITemplatedIndicatorView)?.IndicatorsLayoutOverride == null)
{
CreateDefaultView();
}
else
{
CreateTemplatedView();
}
this.InvalidateMeasure(_indicatorView);
}

public void UpdatePosition()
{
if (!UseDefaultIndicator)
return;

UpdateIndicatorColor(_currentPoistion, _indicatorView.IndicatorColor);
_currentPoistion = _indicatorView.Position;
UpdateIndicatorColor(_currentPoistion, _indicatorView.SelectedIndicatorColor);
}

public void UpdateCount()
{
if (!UseDefaultIndicator || _contentView == null)
return;

var count = Math.Min(_indicatorView.Count, _indicatorView.MaximumVisible);
var diff = _indicators.Count - count;
var needIncrease = diff < 0;

diff = Math.Abs(diff);
for (int i = 0; i < diff; i++)
{
if (needIncrease)
IncreaseIndicator();
else
DecreaseIndicator();
}
UpdatePosition();
}

public TSize Measure(double availableWidth, double availableHeight)
{
if (UseDefaultIndicator)
{
return new TSize(IndicatorSizeWithMargin.ToScaledPixel() * _indicators.Count, IndicatorSizeWithMargin.ToScaledPixel());
}
else
{
return _templatedView?.CrossPlatformMeasure(availableWidth.ToScaledDP(), availableHeight.ToScaledDP()).ToPixel() ?? new TSize(0, 0);
}
}

void UpdateIndicatorColor(int position, Paint? color)
{
if (position < 0 || position >= _indicators.Count)
return;

_indicators[position].Drawable.Background = color;
_indicators[position].Invalidate();
}

void CreateTemplatedView()
{
UseDefaultIndicator = false;
var layout = (_indicatorView as ITemplatedIndicatorView)?.IndicatorsLayoutOverride;
if (layout == null || _indicatorView?.Handler?.MauiContext == null)
return;
_contentView = layout.ToPlatform(_indicatorView.Handler.MauiContext);
_templatedView = layout;
_templatedViewHandler = layout.Handler as IPlatformViewHandler;

_contentView.WidthSpecification = NLayoutParamPolicies.MatchParent;
_contentView.HeightSpecification = NLayoutParamPolicies.MatchParent;
Children.Add(_contentView);
}

void CreateDefaultView()
{
UseDefaultIndicator = true;
_contentView = new NView
{
WidthSpecification = NLayoutParamPolicies.MatchParent,
HeightSpecification = NLayoutParamPolicies.MatchParent,
Layout = new NLinearLayout
{
LinearOrientation = NLinearLayout.Orientation.Horizontal,
}
};
Children.Add(_contentView);

_currentPoistion = _indicatorView.Position;

if (_indicatorView.Count == 1 && _indicatorView.HideSingle)
return;

var count = Math.Min(_indicatorView.Count, _indicatorView.MaximumVisible);

for (int i = 0; i < count; i++)
{
var indicator = CreateIndicator((i == _indicatorView.Position) ? _indicatorView.SelectedIndicatorColor : _indicatorView.IndicatorColor);
_contentView.Add(indicator);
_indicators.Add(indicator);
}
}

Indicator CreateIndicator(Paint? color)
{
var indicator = new Indicator()
{
Margin = new NExtents((ushort)DefaultMargin.ToScaledPixel()),
Size = new NSize(IndicatorSize.ToScaledPixel(), IndicatorSize.ToScaledPixel())
};
indicator.Drawable.Shape = _indicatorView.IndicatorsShape;
indicator.Drawable.Background = color;
indicator.TouchEvent += OnIndicatorTouch;

return indicator;
}

void ClearIndicatorView()
{
Children.Clear();

if (_templatedViewHandler != null)
{
_templatedViewHandler.Dispose();
_templatedViewHandler = null;
_templatedView = null;
}
else
{
_contentView?.Dispose();
}
_contentView = null;

foreach (var view in _indicators)
{
view.Dispose();
}
_indicators.Clear();
}

void DecreaseIndicator()
{
if (_contentView == null)
return;

var indicator = _indicators[_indicators.Count - 1];
_contentView.Remove(indicator);
_indicators.Remove(indicator);
indicator.Dispose();
}

void IncreaseIndicator()
{
if (_contentView == null)
return;

var indicator = CreateIndicator(_indicators.Count == _indicatorView.Position ? _indicatorView.SelectedIndicatorColor : _indicatorView.IndicatorColor);
_contentView.Add(indicator);
_indicators.Add(indicator);
}

bool OnIndicatorTouch(object source, TouchEventArgs e)
{
if (e.Touch.GetState(0) == NPointStateType.Up && source is Indicator indicator)
{
var touchPosition = e.Touch.GetLocalPosition(0);
if (0 < touchPosition.X && touchPosition.X < indicator.SizeWidth
&& 0 < touchPosition.Y && touchPosition.Y < indicator.SizeHeight)
{
var position = _indicators.IndexOf(indicator);
if (position != -1)
{
_indicatorView.Position = position;
}
}
}
return true;
}

void OnLayoutUpdated(object? sender, LayoutEventArgs e)
{
if (UseDefaultIndicator || _templatedView == null)
return;

var platformGeometry = this.GetBounds().ToDP();
_templatedView.CrossPlatformMeasure(platformGeometry.Width, platformGeometry.Height);

platformGeometry.X = 0;
platformGeometry.Y = 0;
_templatedView.CrossPlatformArrange(platformGeometry);
}

class Indicator : SkiaGraphicsView
{
public Indicator() : base(new MauiDrawable()) { }

public new MauiDrawable Drawable => (MauiDrawable)base.Drawable!;
}
}
}
10 changes: 8 additions & 2 deletions src/Core/src/PublicAPI/net-tizen/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ Microsoft.Maui.Handlers.IImageHandler.PlatformView.get -> Tizen.UIExtensions.NUI
Microsoft.Maui.Handlers.IImageHandler.SourceLoader.get -> Microsoft.Maui.Platform.ImageSourcePartLoader!
Microsoft.Maui.Handlers.IImageHandler.VirtualView.get -> Microsoft.Maui.IImage!
Microsoft.Maui.Handlers.IIndicatorViewHandler
Microsoft.Maui.Handlers.IIndicatorViewHandler.PlatformView.get -> Tizen.NUI.BaseComponents.View!
Microsoft.Maui.Handlers.IIndicatorViewHandler.PlatformView.get -> Microsoft.Maui.Platform.MauiPageControl!
Microsoft.Maui.Handlers.IIndicatorViewHandler.VirtualView.get -> Microsoft.Maui.IIndicatorView!
Microsoft.Maui.Handlers.ILabelHandler
Microsoft.Maui.Handlers.ILabelHandler.PlatformView.get -> Tizen.UIExtensions.NUI.Label!
Expand Down Expand Up @@ -1492,6 +1492,12 @@ Microsoft.Maui.Platform.MauiImageSource.LoadSource(System.IO.Stream! stream) ->
Microsoft.Maui.Platform.MauiImageSource.MauiImageSource() -> void
Microsoft.Maui.Platform.MauiImageSource.ResourceUrl.get -> string?
Microsoft.Maui.Platform.MauiImageSource.ResourceUrl.set -> void
Microsoft.Maui.Platform.MauiPageControl
Microsoft.Maui.Platform.MauiPageControl.MauiPageControl(Microsoft.Maui.IIndicatorView! view) -> void
Microsoft.Maui.Platform.MauiPageControl.Measure(double availableWidth, double availableHeight) -> Tizen.UIExtensions.Common.Size
Microsoft.Maui.Platform.MauiPageControl.ResetIndicators() -> void
Microsoft.Maui.Platform.MauiPageControl.UpdateCount() -> void
Microsoft.Maui.Platform.MauiPageControl.UpdatePosition() -> void
Microsoft.Maui.Platform.MauiSearchBar
Microsoft.Maui.Platform.MauiSearchBar.Entry.get -> Tizen.UIExtensions.NUI.Entry!
Microsoft.Maui.Platform.MauiSearchBar.MauiSearchBar() -> void
Expand Down Expand Up @@ -1882,7 +1888,7 @@ override Microsoft.Maui.Handlers.ImageButtonHandler.NeedsContainer.get -> bool
override Microsoft.Maui.Handlers.ImageHandler.CreatePlatformView() -> Tizen.UIExtensions.NUI.Image!
override Microsoft.Maui.Handlers.ImageHandler.DisconnectHandler(Tizen.UIExtensions.NUI.Image! platformView) -> void
override Microsoft.Maui.Handlers.ImageHandler.NeedsContainer.get -> bool
override Microsoft.Maui.Handlers.IndicatorViewHandler.CreatePlatformView() -> Tizen.NUI.BaseComponents.View!
override Microsoft.Maui.Handlers.IndicatorViewHandler.CreatePlatformView() -> Microsoft.Maui.Platform.MauiPageControl!
override Microsoft.Maui.Handlers.LabelHandler.CreatePlatformView() -> Tizen.UIExtensions.NUI.Label!
override Microsoft.Maui.Handlers.LayoutHandler.CreatePlatformView() -> Microsoft.Maui.Platform.LayoutViewGroup!
override Microsoft.Maui.Handlers.LayoutHandler.Dispose(bool disposing) -> void
Expand Down

0 comments on commit 85f5b23

Please sign in to comment.