Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Tizen] Support VisualState on Items of CollectionView #12281

Merged
merged 2 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ public CarouselView(EvasObject parent) : base(parent)

public EScroller Scroll => base.Scroller;

protected override ViewHolder CreateViewHolder()
{
return new ViewHolder(this)
{
FocusedColor = ThemeConstants.CarouselView.ColorClass.DefaultFocusedColor,
SelectedColor = ThemeConstants.CarouselView.ColorClass.DefaultSelectedColor,
};
}
ESize ICollectionViewController.GetItemSize(int widthConstraint, int heightConstraint)
{
return AllocatedSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ ViewHolder ICollectionViewController.RealizeView(int index)
var content = Adaptor.CreateNativeView(index, this);
holder = CreateViewHolder();
holder.RequestSelected += OnRequestItemSelection;
holder.StateUpdated += OnItemStateChanged;
holder.Content = content;
holder.ViewCategory = Adaptor.GetViewCategory(index);
_innerLayout.PackEnd(holder);
Expand All @@ -301,15 +302,22 @@ ViewHolder ICollectionViewController.RealizeView(int index)
return holder;
}

void OnItemStateChanged(object sender, EventArgs e)
{
if (sender is ViewHolder holder && holder.Content != null)
{
Adaptor?.UpdateViewState(holder.Content, holder.State);
}
}

void OnRequestItemSelection(object sender, EventArgs e)
{
if (SelectionMode == CollectionViewSelectionMode.None)
return;


if (_lastSelectedViewHolder != null)
{
_lastSelectedViewHolder.State = ViewHolderState.Normal;
_lastSelectedViewHolder.ResetState();
}

_lastSelectedViewHolder = sender as ViewHolder;
Expand Down Expand Up @@ -377,7 +385,7 @@ void UpdateSelectionMode()
{
if (_lastSelectedViewHolder != null)
{
_lastSelectedViewHolder.State = ViewHolderState.Normal;
_lastSelectedViewHolder.ResetState();
_lastSelectedViewHolder = null;
}
_selectedItemIndex = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public virtual void SendItemSelected(int index)
ItemSelected?.Invoke(this, new SelectedItemChangedEventArgs(this[index], index));
}

public virtual void UpdateViewState(EvasObject view, ViewHolderState state)
{
}

public void RequestItemSelected(object item)
{
if (CollectionView != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ public class ItemTemplateAdaptor : ItemAdaptor
protected View _headerCache;
protected View _footerCache;

public ItemTemplateAdaptor(ItemsView itemsView) : base(itemsView.ItemsSource)
{
ItemTemplate = itemsView.ItemTemplate;
_itemsView = itemsView;
}
bool IsSelectable { get; }


public ItemTemplateAdaptor(ItemsView itemsView) : this(itemsView, itemsView.ItemsSource, itemsView.ItemTemplate) { }

protected ItemTemplateAdaptor(ItemsView itemsView, IEnumerable items, DataTemplate template) : base(items)
{
ItemTemplate = template;
_itemsView = itemsView;
IsSelectable = itemsView is SelectableItemsView;
}

protected DataTemplate ItemTemplate { get; set; }
Expand Down Expand Up @@ -212,6 +212,27 @@ public override ESize MeasureFooter(int widthConstraint, int heightConstraint)
return _footerCache?.Measure(Forms.ConvertToScaledDP(widthConstraint), Forms.ConvertToScaledDP(heightConstraint)).Request.ToPixel() ?? new ESize(0, 0);
}

public override void UpdateViewState(EvasObject view, ViewHolderState state)
{
base.UpdateViewState(view, state);
if (_nativeFormsTable.TryGetValue(view, out View formsView))
{
switch (state)
{
case ViewHolderState.Focused:
VisualStateManager.GoToState(formsView, VisualStateManager.CommonStates.Focused);
break;
case ViewHolderState.Normal:
VisualStateManager.GoToState(formsView, VisualStateManager.CommonStates.Normal);
break;
case ViewHolderState.Selected:
if (IsSelectable)
VisualStateManager.GoToState(formsView, VisualStateManager.CommonStates.Selected);
break;
}
}
}

protected virtual View CreateHeaderView()
{
if (_itemsView is StructuredItemsView structuredItemsView)
Expand Down
63 changes: 17 additions & 46 deletions Xamarin.Forms.Platform.Tizen/Native/CollectionView/ViewHolder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using ElmSharp;
using ERectangle = ElmSharp.Rectangle;
using EColor = ElmSharp.Color;


Expand All @@ -11,40 +10,28 @@ public enum ViewHolderState
{
Normal,
Selected,
Focused,
}

public class ViewHolder : Box
{
ERectangle _background;
Button _focusArea;
EvasObject _content;
ViewHolderState _state;
bool _isSelected;

public ViewHolder(EvasObject parent) : base(parent)
{
Initialize(parent);
}

public object ViewCategory { get; set; }
public EColor FocusedColor { get; set; }
public EColor SelectedColor { get; set; }

EColor EffectiveFocusedColor => FocusedColor == EColor.Default ? ThemeConstants.CollectionView.ColorClass.DefaultFocusedColor : FocusedColor;
EColor EffectiveSelectedColor => SelectedColor == EColor.Default ? ThemeConstants.CollectionView.ColorClass.DefaultSelectedColor : SelectedColor;
[Obsolete("FocusedColor is obsolete. Use VisualStateManager")]
public EColor FocusedColor { get; set; }

EColor FocusSelectedColor
{
get
{
var color1 = EffectiveFocusedColor;
var color2 = EffectiveSelectedColor;
return new EColor(
(color1.R + color2.R) / 2,
(color1.G + color2.G) / 2,
(color1.B + color2.B) / 2,
(color1.A + color2.A) / 2);
}
}
[Obsolete("SelectedColor is obsolete. Use VisualStateManager")]
public EColor SelectedColor { get; set; }

public EvasObject Content
{
Expand All @@ -61,7 +48,7 @@ public EvasObject Content
_content = value;
if (_content != null)
{
PackAfter(_content, _background);
PackEnd(_content);
_content.StackBelow(_focusArea);
}
}
Expand All @@ -77,30 +64,19 @@ public ViewHolderState State
}
}

public event EventHandler Selected;
public event EventHandler RequestSelected;

public event EventHandler StateUpdated;

public void ResetState()
{
State = ViewHolderState.Normal;
_background.Color = EColor.Transparent;
}

protected void SendSelected()
{
Selected?.Invoke(this, EventArgs.Empty);
}

protected void Initialize(EvasObject parent)
{
SetLayoutCallback(OnLayout);

_background = new ERectangle(parent)
{
Color = EColor.Transparent
};
_background.Show();

_focusArea = new Button(parent);
_focusArea.Color = EColor.Transparent;
_focusArea.BackgroundColor = EColor.Transparent;
Expand All @@ -112,21 +88,19 @@ protected void Initialize(EvasObject parent)
_focusArea.RepeatEvents = true;
_focusArea.Show();

PackEnd(_background);
PackEnd(_focusArea);
FocusedColor = EColor.Default;
Show();
}

protected virtual void OnFocused(object sender, EventArgs e)
{
if (_focusArea.IsFocused)
{
_background.Color = State == ViewHolderState.Selected ? FocusSelectedColor : EffectiveFocusedColor;
State = ViewHolderState.Focused;
}
else
{
_background.Color = State == ViewHolderState.Selected ? EffectiveSelectedColor : EColor.Transparent;
State = _isSelected ? ViewHolderState.Selected : ViewHolderState.Normal;
}
}

Expand All @@ -137,7 +111,6 @@ protected virtual void OnClicked(object sender, EventArgs e)

protected virtual void OnLayout()
{
_background.Geometry = Geometry;
_focusArea.Geometry = Geometry;
if (_content != null)
{
Expand All @@ -147,14 +120,12 @@ protected virtual void OnLayout()

protected virtual void UpdateState()
{
if (State == ViewHolderState.Normal)
{
_background.Color = _focusArea.IsFocused ? EffectiveFocusedColor : EColor.Transparent;
} else
{
_background.Color = _focusArea.IsFocused ? FocusSelectedColor : SelectedColor;
SendSelected();
}
if (State == ViewHolderState.Selected)
_isSelected = true;
else if (State == ViewHolderState.Normal)
_isSelected = false;

StateUpdated?.Invoke(this, EventArgs.Empty);
}


Expand Down
11 changes: 0 additions & 11 deletions Xamarin.Forms.Platform.Tizen/ThemeConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,17 +580,6 @@ public class Watch
}
#endregion

#region CollectionView
public class CollectionView
{
public class ColorClass
{
public static readonly EColor DefaultFocusedColor = EColor.FromRgba(244, 244, 244, 200);
public static readonly EColor DefaultSelectedColor = EColor.FromRgba(227, 242, 253, 200);
}
}
#endregion

#region CarouselView
public class CarouselView
{
Expand Down