diff options
Diffstat (limited to 'Xamarin.Forms.Platform.Tizen/Cells/EntryCellRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Tizen/Cells/EntryCellRenderer.cs | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Tizen/Cells/EntryCellRenderer.cs b/Xamarin.Forms.Platform.Tizen/Cells/EntryCellRenderer.cs new file mode 100644 index 00000000..c07985f8 --- /dev/null +++ b/Xamarin.Forms.Platform.Tizen/Cells/EntryCellRenderer.cs @@ -0,0 +1,163 @@ +using ElmSharp; +using EColor = ElmSharp.Color; +using EBox = ElmSharp.Box; +using System.Collections.Generic; + +namespace Xamarin.Forms.Platform.Tizen +{ + public class EntryCellRenderer : ViewCellRenderer + { + const int _defaultHeight = 120; + readonly Dictionary<EvasObject, NativeEntryComponent> _realizedComponent = new Dictionary<EvasObject, NativeEntryComponent>(); + + public EntryCellRenderer() + { + } + + protected override EvasObject OnGetContent(Cell cell, string part) + { + if (part == MainContentPart) + { + var entryCell = cell as EntryCell; + int height = (int)entryCell.RenderHeight; + height = height <= 0 ? FindCellContentHeight(entryCell) : height; + height = height <= 0 ? _defaultHeight : height; + + // TODO + // Need to use Forms.Core element instead of using Elmsharp. + // if we use Forms element, easily binding data to view elements + var box = new EBox(Forms.Context.MainWindow) + { + IsHorizontal = true, + MinimumHeight = height, + }; + box.SetAlignment(-1.0, -1.0); // fill + box.SetWeight(1.0, 1.0); // expand + + var label = new Native.Label(box) + { + Text = entryCell.Label, + TextColor = GetLabelColor(entryCell), + }; + label.SetAlignment(0.0, 0.5); + label.SetWeight(0.0, 1.0); + + var entry = new Native.Entry(box) + { + IsSingleLine = true, + Text = entryCell.Text, + TextColor = s_defaultTextColor, + Placeholder = entryCell.Placeholder, + PlaceholderColor = s_defaultTextColor, + Keyboard = entryCell.Keyboard.ToNative(), + HorizontalTextAlignment = entryCell.HorizontalTextAlignment.ToNative(), + }; + entry.SetAlignment(-1.0, 0.5); + entry.SetWeight(1.0, 1.0); + + box.PackEnd(label); + box.PackEnd(entry); + + label.Show(); + entry.Show(); + + entry.TextChanged += (sender, e) => + { + entryCell.Text = e.NewTextValue; + }; + entry.Activated += (sender, e) => + { + (cell as IEntryCellController).SendCompleted(); + }; + NativeEntryComponent component = new NativeEntryComponent() + { + Entry = entry, + Label = label, + }; + _realizedComponent[box] = component; + box.Deleted += (sender, e) => + { + _realizedComponent.Remove(box); + }; + return box; + } + return null; + } + + protected override bool OnCellPropertyChanged(Cell cell, string property, Dictionary<string, EvasObject> realizedView) + { + if (!realizedView.ContainsKey(MainContentPart) || !_realizedComponent.ContainsKey(realizedView[MainContentPart])) + { + return base.OnCellPropertyChanged(cell, property, realizedView); + } + + NativeEntryComponent realizedCompoenet = _realizedComponent[realizedView[MainContentPart]]; + EntryCell entryCell = (EntryCell)cell; + + if (property == EntryCell.HorizontalTextAlignmentProperty.PropertyName) + { + var entry = realizedCompoenet.Entry; + if (entry != null) + { + entry.HorizontalTextAlignment = entryCell.HorizontalTextAlignment.ToNative(); + } + } + else if (property == EntryCell.KeyboardProperty.PropertyName) + { + var entry = realizedCompoenet.Entry; + if (entry != null) + { + entry.Keyboard = entryCell.Keyboard.ToNative(); + } + } + else if (property == EntryCell.PlaceholderProperty.PropertyName) + { + var entry = realizedCompoenet.Entry; + if (entry != null) + { + entry.Placeholder = entryCell.Placeholder; + } + } + else if (property == EntryCell.TextProperty.PropertyName) + { + var entry = realizedCompoenet.Entry; + if (entry != null) + { + entry.Text = entryCell.Text; + } + } + else if (property == EntryCell.LabelProperty.PropertyName) + { + var label = realizedCompoenet.Label; + if (label != null) + { + label.Text = entryCell.Label; + } + } + else if (property == EntryCell.LabelColorProperty.PropertyName) + { + var label = realizedCompoenet.Label; + if (label != null) + { + label.TextColor = GetLabelColor(entryCell); + } + } + else + { + return base.OnCellPropertyChanged(cell, property, realizedView); + } + return false; + } + + EColor GetLabelColor(EntryCell cell) + { + return cell.LabelColor.IsDefault ? s_defaultTextColor : cell.LabelColor.ToNative(); + } + + class NativeEntryComponent + { + public Native.Entry Entry { get; set; } + public Native.Label Label { get; set; } + } + } +} |