diff options
Diffstat (limited to 'Xamarin.Forms.Platform.Tizen/Cells/CellRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Tizen/Cells/CellRenderer.cs | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Tizen/Cells/CellRenderer.cs b/Xamarin.Forms.Platform.Tizen/Cells/CellRenderer.cs new file mode 100644 index 00000000..1cda74c2 --- /dev/null +++ b/Xamarin.Forms.Platform.Tizen/Cells/CellRenderer.cs @@ -0,0 +1,142 @@ +using ElmSharp; +using System.Collections.Generic; +using EColor = ElmSharp.Color; + +namespace Xamarin.Forms.Platform.Tizen +{ + public abstract class CellRenderer : IRegisterable + { + const string _heightProperty = "Height"; + protected static readonly EColor s_defaultTextColor = EColor.Black; + protected static readonly EColor s_defaultBackgroundColor = EColor.White; + readonly Dictionary<Cell, Dictionary<string, EvasObject>> _realizedNativeViews = new Dictionary<Cell, Dictionary<string, EvasObject>>(); + + protected CellRenderer(string style) + { + Class = new GenItemClass(style) + { + GetTextHandler = GetText, + GetContentHandler = GetContent, + DeleteHandler = ItemDeleted, + }; + } + + public GenItemClass Class + { + get; + private set; + } + + protected virtual bool OnCellPropertyChanged(Cell cell, string property, Dictionary<string, EvasObject> realizedView) + { + if (property == _heightProperty) + { + return true; + } + return false; + } + + protected virtual Span OnGetText(Cell cell, string part) + { + return null; + } + protected virtual EvasObject OnGetContent(Cell cell, string part) + { + return null; + } + protected virtual void OnDeleted(Cell cell) + { + } + + protected virtual void OnUnrealizedCell(Cell cell) + { + } + + protected int FindCellContentHeight(Cell cell) + { + ViewCell viewCell = cell as ViewCell; + if (viewCell != null) + { + var parentWidth = (cell.Parent as VisualElement).Width; + var view = viewCell.View; + return (int)view.Measure(parentWidth, double.PositiveInfinity).Request.Height; + } + else + return -1; + } + + static Native.Span ToNative(Span span) + { + var nativeSpan = new Native.Span(); + nativeSpan.Text = span.Text; + nativeSpan.ForegroundColor = span.ForegroundColor.IsDefault ? s_defaultTextColor : span.ForegroundColor.ToNative(); + nativeSpan.FontAttributes = span.FontAttributes; + nativeSpan.BackgroundColor = span.BackgroundColor.IsDefault ? s_defaultBackgroundColor : span.BackgroundColor.ToNative(); + nativeSpan.FontSize = span.FontSize; + nativeSpan.FontFamily = span.FontFamily; + return nativeSpan; + } + + internal void SendCellPropertyChanged(Cell cell, GenListItem item, string property) + { + Dictionary<string, EvasObject> realizedView = null; + _realizedNativeViews.TryGetValue(cell, out realizedView); + + // just to prevent null reference exception in OnCellPropertyChanged + realizedView = realizedView ?? new Dictionary<string, EvasObject>(); + + if (property == Cell.IsEnabledProperty.PropertyName) + { + item.IsEnabled = cell.IsEnabled; + } + // if true was returned, item was updated + // if it's possible to update the cell property without Update(), return false + else if (OnCellPropertyChanged(cell, property, realizedView)) + { + item.Update(); + } + } + + internal void SendUnrealizedCell(Cell cell) + { + _realizedNativeViews.Remove(cell); + OnUnrealizedCell(cell); + } + + string GetText(object data, string part) + { + var span = OnGetText((data as Native.ListView.ItemContext).Cell, part); + return span != null ? ToNative(span).GetDecoratedText() : null; + } + + EvasObject GetContent(object data, string part) + { + var cell = (data as Native.ListView.ItemContext).Cell; + EvasObject nativeView = OnGetContent(cell, part); + if (part != null && nativeView != null) + { + Dictionary<string, EvasObject> realizedView = null; + _realizedNativeViews.TryGetValue(cell, out realizedView); + if (realizedView == null) + { + realizedView = new Dictionary<string, EvasObject>(); + _realizedNativeViews[cell] = realizedView; + } + realizedView[part] = nativeView; + + nativeView.Deleted += (sender, e) => + { + realizedView.Remove(part); + }; + } + return nativeView; + } + + void ItemDeleted(object data) + { + var cell = (data as Native.ListView.ItemContext).Cell; + _realizedNativeViews.Remove(cell); + OnDeleted(cell); + } + } +} |