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

[Tizen] CollectionView optimize for TV profile #12781

Merged
merged 1 commit into from
Nov 9, 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
@@ -0,0 +1,31 @@
namespace Xamarin.Forms.PlatformConfiguration.TizenSpecific
{
using FormsElement = Forms.ItemsView;

public static class ItemsView
{
public static readonly BindableProperty FocusedItemScrollPositionProperty = BindableProperty.Create("FocusedItemScrollPosition", typeof(ScrollToPosition), typeof(FormsElement), ScrollToPosition.MakeVisible);


public static ScrollToPosition GetFocusedItemScrollPosition(BindableObject element)
{
return (ScrollToPosition)element.GetValue(FocusedItemScrollPositionProperty);
}

public static void SetFocusedItemScrollPosition(BindableObject element, ScrollToPosition position)
{
element.SetValue(FocusedItemScrollPositionProperty, position);
}

public static ScrollToPosition GetFocusedItemScrollPosition(this IPlatformElementConfiguration<Tizen, FormsElement> config)
{
return GetFocusedItemScrollPosition(config.Element);
}

public static IPlatformElementConfiguration<Tizen, FormsElement> SetFocusedItemScrollPosition(this IPlatformElementConfiguration<Tizen, FormsElement> config, ScrollToPosition position)
{
SetFocusedItemScrollPosition(config.Element, position);
return config;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public ScrollBarVisiblePolicy HorizontalScrollBarVisiblePolicy
set => Scroller.HorizontalScrollBarVisiblePolicy = value;
}

public ScrollToPosition FocusedItemScrollPosition { get; set; }

int ICollectionViewController.Count
{
get
Expand Down Expand Up @@ -304,10 +306,22 @@ ViewHolder ICollectionViewController.RealizeView(int index)

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

if (holder.State == ViewHolderState.Focused && FocusedItemScrollPosition != ScrollToPosition.MakeVisible)
{
Device.BeginInvokeOnMainThread(() =>
{
if (holder.State == ViewHolderState.Focused && _viewHolderIndexTable.TryGetValue(holder, out int itemIndex))
{
ScrollTo(itemIndex, FocusedItemScrollPosition, true);
}
});
}
}

void OnRequestItemSelection(object sender, EventArgs e)
Expand Down Expand Up @@ -572,10 +586,13 @@ void OnLayout()
if (_adaptor != null && _layoutManager != null)
{
_layoutManager?.SizeAllocated(Geometry.Size);

_layoutManager?.LayoutItems(ViewPort);
_layoutManager?.SetHeader(_headerView, Adaptor.MeasureHeader(AllocatedSize.Width, AllocatedSize.Height));
_layoutManager?.SetFooter(_footerView, Adaptor.MeasureFooter(AllocatedSize.Width, AllocatedSize.Height));

Scroller.ScrollBlock = LayoutManager.IsHorizontal ? ScrollBlock.Vertical : ScrollBlock.Horizontal;
Scroller.HorizontalStepSize = _layoutManager.GetScrollBlockSize();
Scroller.VerticalStepSize = _layoutManager.GetScrollBlockSize();
UpdateSnapPointsType(SnapPointsType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,17 @@ public int GetVisibleItemIndex(int x, int y)
if (_scrollCanvasSize.Width < x || _scrollCanvasSize.Height < y)
return CollectionView.Count - 1;

int first = (IsHorizontal ? x : y) / (BaseItemSize + ItemSpacing);
if (_hasUnevenRows)
int first = 0;
if (!_hasUnevenRows)
{
first = Math.Min(Math.Max(0, ((IsHorizontal ? x : y) - ItemStartPoint) / (BaseItemSize + ItemSpacing)), ((CollectionView.Count - 1) / Span));
}
else
{
first = _accumulatedItemSizes.FindIndex(current => (IsHorizontal ? x : y) <= current);
if (first == -1)
first = (CollectionView.Count - 1) / Span;
}

int second = (IsHorizontal ? y : x) / (ColumnSize + ColumnSpacing);
if (second == Span)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public override EvasObject GetFooterView(EvasObject parent)
{
_footerCache.Parent = _itemsView;
var renderer = Platform.GetOrCreateRenderer(_footerCache);
(renderer as LayoutRenderer).RegisterOnLayoutUpdated();
(renderer as LayoutRenderer)?.RegisterOnLayoutUpdated();
return renderer.NativeView;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,16 @@ public int GetVisibleItemIndex(int x, int y)
return CollectionView.Count - 1;

if (!_hasUnevenRows)
return coordinate / (BaseItemSize + ItemSpacing);
{
return Math.Min(Math.Max(0, (coordinate - ItemStartPoint) / (BaseItemSize + ItemSpacing)), CollectionView.Count - 1);
}
else
return _accumulatedItemSizes.FindIndex(current => coordinate <= current);
{
var index = _accumulatedItemSizes.FindIndex(current => coordinate <= current);
if (index == -1)
index = CollectionView.Count - 1;
return index;
}
}

public int GetScrollBlockSize()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using ElmSharp;
using EColor = ElmSharp.Color;
using ERectangle = ElmSharp.Rectangle;


namespace Xamarin.Forms.Platform.Tizen.Native
{
Expand Down Expand Up @@ -125,11 +123,12 @@ protected virtual void UpdateState()
_isSelected = true;
else if (State == ViewHolderState.Normal)
_isSelected = false;
else if (State == ViewHolderState.Focused)
RaiseTop();

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


void OnKeyUp(object sender, EvasKeyEventArgs e)
{
if (e.KeyName == "Enter" && _focusArea.IsFocused)
Expand Down
10 changes: 10 additions & 0 deletions Xamarin.Forms.Platform.Tizen/Renderers/ItemsViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Xamarin.Forms.Platform.Tizen.Native;

using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.ItemsView;

namespace Xamarin.Forms.Platform.Tizen
{
public abstract class ItemsViewRenderer<TItemsView, TNative> : ViewRenderer<TItemsView, TNative>
Expand All @@ -19,6 +21,7 @@ public ItemsViewRenderer()
RegisterPropertyHandler(ItemsView.ItemTemplateProperty, UpdateAdaptor);
RegisterPropertyHandler(ItemsView.HorizontalScrollBarVisibilityProperty, UpdateHorizontalScrollBarVisibility);
RegisterPropertyHandler(ItemsView.VerticalScrollBarVisibilityProperty, UpdateVerticalScrollBarVisibility);
RegisterPropertyHandler(Specific.FocusedItemScrollPositionProperty, UpdateFocusedItemScrollPosition);
}

protected abstract TNative CreateNativeControl(ElmSharp.EvasObject parent);
Expand Down Expand Up @@ -176,6 +179,13 @@ protected virtual void UpdateVerticalScrollBarVisibility()
{
Control.VerticalScrollBarVisiblePolicy = Element.VerticalScrollBarVisibility.ToNative();
}

void UpdateFocusedItemScrollPosition(bool init)
{
if (init && Specific.GetFocusedItemScrollPosition(Element) == ScrollToPosition.MakeVisible)
return;
Control.FocusedItemScrollPosition = Specific.GetFocusedItemScrollPosition(Element);
}
}

static class ItemsLayoutExtension
Expand Down