From 4f9f46ce38ee71b42c91c57881a7c3e75f005044 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Thu, 26 Apr 2018 16:55:23 -0700 Subject: [PATCH] Make an Element for each cell type --- Ooui.Forms/Cells/CellElement.cs | 44 +++++++++++ Ooui.Forms/Cells/CellRenderer.cs | 46 ++++++----- Ooui.Forms/Cells/CellView.cs | 84 -------------------- Ooui.Forms/Cells/EntryCellElement.cs | 81 +++++++++++++++++++ Ooui.Forms/Cells/EntryCellRenderer.cs | 99 +----------------------- Ooui.Forms/Cells/ImageCellElement.cs | 85 ++++++++++++++++++++ Ooui.Forms/Cells/ImageCellRenderer.cs | 54 +------------ Ooui.Forms/Cells/SwitchCellElement.cs | 69 +++++++++++++++++ Ooui.Forms/Cells/SwitchCellRenderer.cs | 70 +---------------- Ooui.Forms/Cells/TextCellElement.cs | 55 +++++++++++++ Ooui.Forms/Cells/TextCellRenderer.cs | 38 +-------- Ooui.Forms/Cells/ViewCellElement.cs | 47 +++++++++++ Ooui.Forms/Cells/ViewCellRenderer.cs | 17 +--- Ooui.Forms/Cells/ViewCellView.cs | 63 --------------- Ooui.Forms/OouiTheme.cs | 3 + Ooui.Forms/Renderers/ListViewRenderer.cs | 6 +- Ooui.Forms/Renderers/SwitchRenderer.cs | 13 ++-- Ooui.sln | 28 +++---- 18 files changed, 443 insertions(+), 459 deletions(-) create mode 100644 Ooui.Forms/Cells/CellElement.cs delete mode 100644 Ooui.Forms/Cells/CellView.cs create mode 100644 Ooui.Forms/Cells/EntryCellElement.cs create mode 100644 Ooui.Forms/Cells/ImageCellElement.cs create mode 100644 Ooui.Forms/Cells/SwitchCellElement.cs create mode 100644 Ooui.Forms/Cells/TextCellElement.cs create mode 100644 Ooui.Forms/Cells/ViewCellElement.cs delete mode 100644 Ooui.Forms/Cells/ViewCellView.cs diff --git a/Ooui.Forms/Cells/CellElement.cs b/Ooui.Forms/Cells/CellElement.cs new file mode 100644 index 0000000..98f8f69 --- /dev/null +++ b/Ooui.Forms/Cells/CellElement.cs @@ -0,0 +1,44 @@ +using System; +using System.ComponentModel; +using Xamarin.Forms; + +namespace Ooui.Forms.Cells +{ + public class CellElement : Div, INativeElementView + { + Cell cell; + + public Cell Cell { + get => cell; + set { + if (cell == value) + return; + + if (cell != null) + UnbindCell (); + + cell = value; + + if (cell != null) + BindCell (); + } + } + + public virtual Xamarin.Forms.Element Element => Cell; + + public CellElement () + { + Style.Width = "100%"; + } + + protected virtual void UnbindCell () + { + Device.BeginInvokeOnMainThread (Cell.SendDisappearing); + } + + protected virtual void BindCell () + { + Device.BeginInvokeOnMainThread (cell.SendAppearing); + } + } +} diff --git a/Ooui.Forms/Cells/CellRenderer.cs b/Ooui.Forms/Cells/CellRenderer.cs index ef7865a..665da7c 100644 --- a/Ooui.Forms/Cells/CellRenderer.cs +++ b/Ooui.Forms/Cells/CellRenderer.cs @@ -6,61 +6,59 @@ namespace Ooui.Forms.Cells { public class CellRenderer : IRegisterable { - private EventHandler _onForceUpdateSizeRequested; + EventHandler _onForceUpdateSizeRequested; static readonly BindableProperty RealCellProperty = - BindableProperty.CreateAttached("RealCell", typeof(Div), - typeof(Cell), null); + BindableProperty.CreateAttached ("RealCell", typeof (CellElement), typeof (Cell), null); - public virtual CellView GetCell(Cell item, CellView reusableView, List listView) + public virtual CellElement GetCellElement (Cell cell, CellElement reusableElement, List listView) { - var nativeCell = reusableView ?? GetCellInstance (item); + var cellElement = reusableElement ?? CreateCellElement (cell); - nativeCell.Cell = item; + cellElement.Cell = cell; - WireUpForceUpdateSizeRequested(item, nativeCell); - UpdateBackground(nativeCell, item); + WireUpForceUpdateSizeRequested (cell, cellElement); + UpdateBackground (cellElement, cell); - return nativeCell; + return cellElement; } - internal static CellView GetRealCell(BindableObject cell) + protected static CellElement GetRealCell (BindableObject cell) { - return (CellView)cell.GetValue(RealCellProperty); + return (CellElement)cell.GetValue (RealCellProperty); } - internal static void SetRealCell(BindableObject cell, CellView renderer) + protected static void SetRealCell (BindableObject cell, CellElement renderer) { - cell.SetValue(RealCellProperty, renderer); + cell.SetValue (RealCellProperty, renderer); } - protected virtual CellView GetCellInstance(Cell item) + protected virtual CellElement CreateCellElement (Cell cell) { - return new CellView(); + return new CellElement (); } - protected virtual void OnForceUpdateSizeRequest(Cell cell, CellView nativeCell) + protected virtual void OnForceUpdateSizeRequest (Cell cell, CellElement cellElement) { - nativeCell.Style.Height = (int)cell.RenderHeight; + cellElement.Style.Height = (int)cell.RenderHeight; } - protected void UpdateBackground(CellView tableViewCell, Cell cell) + protected void UpdateBackground (CellElement cellElement, Cell cell) { var backgroundColor = Xamarin.Forms.Color.Default; if (backgroundColor == Xamarin.Forms.Color.Default && cell.RealParent is VisualElement element) backgroundColor = element.BackgroundColor; - - tableViewCell.Style.BackgroundColor = backgroundColor.ToOouiColor (Xamarin.Forms.Color.White); + + cellElement.Style.BackgroundColor = backgroundColor.ToOouiColor (Xamarin.Forms.Color.White); } - protected void WireUpForceUpdateSizeRequested(Cell cell, CellView nativeCell) + protected void WireUpForceUpdateSizeRequested (Cell cell, CellElement cellElement) { cell.ForceUpdateSizeRequested -= _onForceUpdateSizeRequested; - _onForceUpdateSizeRequested = (sender, e) => - { - OnForceUpdateSizeRequest(cell, nativeCell); + _onForceUpdateSizeRequested = (sender, e) => { + OnForceUpdateSizeRequest (cell, cellElement); }; cell.ForceUpdateSizeRequested += _onForceUpdateSizeRequested; diff --git a/Ooui.Forms/Cells/CellView.cs b/Ooui.Forms/Cells/CellView.cs deleted file mode 100644 index 8034ccb..0000000 --- a/Ooui.Forms/Cells/CellView.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.ComponentModel; -using Xamarin.Forms; - -namespace Ooui.Forms.Cells -{ - public class CellView : Div, INativeElementView - { - private Cell _cell; - - public Action ForwardPropertyChanged; - - public CellView() - { - CreateUI(); - } - - public Cell Cell - { - get { return _cell; } - set - { - if (_cell == value) - return; - - if (_cell != null) - Device.BeginInvokeOnMainThread(_cell.SendDisappearing); - - _cell = value; - - if (_cell != null) - Device.BeginInvokeOnMainThread(_cell.SendAppearing); - } - } - - public Div FirstCol { get; private set; } - - public Div SecondCol { get; private set; } - - public Div ThirdCol { get; private set; } - - public Label TextLabel { get; private set; } - - public Label DetailTextLabel { get; private set; } - - public Image ImageView { get; private set; } - - public Div CustomView { get; private set; } - - public virtual Xamarin.Forms.Element Element => Cell; - - public void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) - { - ForwardPropertyChanged?.Invoke(this, e); - } - - private void CreateUI() - { - Style.Width = "100%"; - Style.Display = "flex"; - - FirstCol = new Div(); - AppendChild(FirstCol); - - SecondCol = new Div(); - AppendChild(SecondCol); - - ThirdCol = new Div(); - AppendChild(ThirdCol); - - ImageView = new Image(); - FirstCol.AppendChild(ImageView); - - TextLabel = new Label(); - SecondCol.AppendChild(TextLabel); - - DetailTextLabel = new Label(); - SecondCol.AppendChild(DetailTextLabel); - - CustomView = new Div(); - ThirdCol.AppendChild(CustomView); - } - } -} diff --git a/Ooui.Forms/Cells/EntryCellElement.cs b/Ooui.Forms/Cells/EntryCellElement.cs new file mode 100644 index 0000000..c9038ea --- /dev/null +++ b/Ooui.Forms/Cells/EntryCellElement.cs @@ -0,0 +1,81 @@ +using System; +using System.ComponentModel; +using Ooui.Forms.Extensions; +using Xamarin.Forms; + +namespace Ooui.Forms.Cells +{ + public class EntryCellElement : CellElement + { + public Label TextLabel { get; } = new Label (); + public TextInput TextInput { get; } = new TextInput (); + + public EntryCellElement () + { + AppendChild (TextLabel); + AppendChild (TextInput); + + TextInput.Change += TextInput_Change; + } + + protected override void BindCell () + { + Cell.PropertyChanged += Cell_PropertyChanged; + + if (Cell is EntryCell cell) { + UpdateLabel (cell); + UpdateText (cell); + UpdatePlaceholder (cell); + UpdateLabelColor (cell); + } + + base.BindCell (); + } + + protected override void UnbindCell () + { + Cell.PropertyChanged -= Cell_PropertyChanged; + base.UnbindCell (); + } + + void Cell_PropertyChanged (object sender, PropertyChangedEventArgs e) + { + var entryCell = (EntryCell)sender; + + if (e.PropertyName == EntryCell.LabelProperty.PropertyName) + UpdateLabel (entryCell); + else if (e.PropertyName == EntryCell.TextProperty.PropertyName) + UpdateText (entryCell); + else if (e.PropertyName == EntryCell.PlaceholderProperty.PropertyName) + UpdatePlaceholder (entryCell); + else if (e.PropertyName == EntryCell.LabelColorProperty.PropertyName) + UpdateLabelColor (entryCell); + } + + void UpdateLabel (EntryCell entryCell) + { + TextLabel.Text = entryCell.Label ?? string.Empty; + } + + void UpdateLabelColor (EntryCell entryCell) + { + TextLabel.Style.Color = entryCell.LabelColor.ToOouiColor (OouiTheme.TextColor); + } + + void UpdatePlaceholder (EntryCell entryCell) + { + TextInput.Placeholder = entryCell.Placeholder ?? string.Empty; + } + + void UpdateText (EntryCell entryCell) + { + TextInput.Value = entryCell.Text ?? string.Empty; + } + + void TextInput_Change (object sender, EventArgs e) + { + if (Cell is EntryCell cell) + cell.Text = TextInput.Text; + } + } +} diff --git a/Ooui.Forms/Cells/EntryCellRenderer.cs b/Ooui.Forms/Cells/EntryCellRenderer.cs index ca3f38a..dab3f11 100644 --- a/Ooui.Forms/Cells/EntryCellRenderer.cs +++ b/Ooui.Forms/Cells/EntryCellRenderer.cs @@ -1,104 +1,13 @@ -using Ooui.Forms.Extensions; -using System; -using System.ComponentModel; +using System; using Xamarin.Forms; namespace Ooui.Forms.Cells { - public class EntryCellRenderer : CellRenderer + public class EntryCellRenderer : TextCellRenderer { - private static Cell _cell; - - public override CellView GetCell(Cell item, CellView reusableView, List listView) + protected override CellElement CreateCellElement (Cell item) { - TextInput nativeEntry = null; - - var nativeEntryCell = base.GetCell(item, reusableView, listView); - - if (nativeEntryCell == null) - nativeEntryCell = new CellView(); - else - { - nativeEntryCell.Cell.PropertyChanged -= OnCellPropertyChanged; - - nativeEntry = nativeEntryCell.CustomView.FirstChild as TextInput; - if (nativeEntry != null) - { - nativeEntryCell.CustomView.RemoveChild(nativeEntry); - nativeEntry.Change -= OnTextChanged; - } - } - - SetRealCell(item, nativeEntryCell); - - if (nativeEntry == null) - nativeEntryCell.CustomView.AppendChild(nativeEntry = new TextInput()); - - var entryCell = (EntryCell)item; - - nativeEntryCell.Cell = item; - nativeEntryCell.SecondCol.Style.Width = "25%"; - _cell = nativeEntryCell.Cell; - - nativeEntryCell.Cell.PropertyChanged += OnCellPropertyChanged; - nativeEntry.Change += OnTextChanged; - - WireUpForceUpdateSizeRequested(item, nativeEntryCell); - - UpdateBackground(nativeEntryCell, entryCell); - UpdateLabel(nativeEntryCell, entryCell); - UpdateText(nativeEntryCell, entryCell); - UpdatePlaceholder(nativeEntryCell, entryCell); - UpdateLabelColor(nativeEntryCell, entryCell); - - return nativeEntryCell; - } - - private static void OnCellPropertyChanged(object sender, PropertyChangedEventArgs e) - { - var entryCell = (EntryCell)sender; - var realCell = (CellView)GetRealCell(entryCell); - - if (e.PropertyName == EntryCell.LabelProperty.PropertyName) - UpdateLabel(realCell, entryCell); - else if (e.PropertyName == EntryCell.TextProperty.PropertyName) - UpdateText(realCell, entryCell); - else if (e.PropertyName == EntryCell.PlaceholderProperty.PropertyName) - UpdatePlaceholder(realCell, entryCell); - else if (e.PropertyName == EntryCell.LabelColorProperty.PropertyName) - UpdateLabelColor(realCell, entryCell); - } - - private static void UpdateLabel(CellView cell, EntryCell entryCell) - { - cell.TextLabel.Text = entryCell.Label ?? string.Empty; - } - - private static void UpdateLabelColor(CellView cell, EntryCell entryCell) - { - cell.TextLabel.Style.Color = entryCell.LabelColor.ToOouiColor(OouiTheme.TextColor); - } - - private static void UpdatePlaceholder(CellView cell, EntryCell entryCell) - { - if (cell.CustomView.FirstChild is TextInput textInput) - textInput.Placeholder = entryCell.Placeholder ?? string.Empty; - } - - private static void UpdateText(CellView cell, EntryCell entryCell) - { - if (cell.CustomView.FirstChild is TextInput textInput) - textInput.Text = entryCell.Text ?? string.Empty; - } - - private static void OnTextChanged(object sender, EventArgs eventArgs) - { - var textInput = (TextInput)sender; - - CellView realCell = GetRealCell(_cell); - - if (realCell != null) - ((EntryCell)realCell.Cell).Text = textInput.Text; + return new EntryCellElement (); } } } diff --git a/Ooui.Forms/Cells/ImageCellElement.cs b/Ooui.Forms/Cells/ImageCellElement.cs new file mode 100644 index 0000000..6c9b62d --- /dev/null +++ b/Ooui.Forms/Cells/ImageCellElement.cs @@ -0,0 +1,85 @@ +using System; +using System.ComponentModel; +using System.Threading.Tasks; +using Ooui.Forms.Extensions; +using Ooui.Forms.Renderers; +using Xamarin.Forms; +using Xamarin.Forms.Internals; + +namespace Ooui.Forms.Cells +{ + public class ImageCellElement : CellElement + { + public Image ImageView { get; } = new Image (); + + public Label TextLabel { get; } = new Label (); + + public Label DetailTextLabel { get; } = new Label (); + + public ImageCellElement () + { + AppendChild (ImageView); + AppendChild (TextLabel); + AppendChild (DetailTextLabel); + } + + protected override void BindCell () + { + Cell.PropertyChanged += Cell_PropertyChanged; + + if (Cell is ImageCell cell) { + TextLabel.Text = cell.Text ?? string.Empty; + DetailTextLabel.Text = cell.Detail ?? string.Empty; + TextLabel.Style.Color = cell.TextColor.ToOouiColor (OouiTheme.TextColor); + DetailTextLabel.Style.Color = cell.DetailColor.ToOouiColor (OouiTheme.SecondaryTextColor); + } + + base.BindCell (); + } + + protected override void UnbindCell () + { + Cell.PropertyChanged -= Cell_PropertyChanged; + + base.UnbindCell (); + } + + async void Cell_PropertyChanged (object sender, PropertyChangedEventArgs args) + { + if (!(Cell is ImageCell cell)) + return; + + if (args.PropertyName == TextCell.TextProperty.PropertyName) + TextLabel.Text = cell.Text ?? string.Empty; + else if (args.PropertyName == TextCell.DetailProperty.PropertyName) + DetailTextLabel.Text = cell.Detail ?? string.Empty; + else if (args.PropertyName == TextCell.TextColorProperty.PropertyName) + TextLabel.Style.Color = cell.TextColor.ToOouiColor (OouiTheme.TextColor); + else if (args.PropertyName == TextCell.DetailColorProperty.PropertyName) + DetailTextLabel.Style.Color = cell.DetailColor.ToOouiColor (OouiTheme.SecondaryTextColor); + else if (args.PropertyName == ImageCell.ImageSourceProperty.PropertyName) + await SetImage (cell.ImageSource).ConfigureAwait (false); + } + + async Task SetImage (ImageSource source) + { + ImageView.Source = null; + + IImageSourceHandler handler; + + if (source != null && (handler = Registrar.Registered.GetHandlerForObject (source)) != null) { + string image; + try { + image = await handler.LoadImageAsync (source).ConfigureAwait (false); + } + catch (TaskCanceledException) { + image = null; + } + ImageView.Source = image; + } + else { + ImageView.Source = null; + } + } + } +} diff --git a/Ooui.Forms/Cells/ImageCellRenderer.cs b/Ooui.Forms/Cells/ImageCellRenderer.cs index 4746c9e..55ee38d 100644 --- a/Ooui.Forms/Cells/ImageCellRenderer.cs +++ b/Ooui.Forms/Cells/ImageCellRenderer.cs @@ -8,59 +8,9 @@ namespace Ooui.Forms.Cells { public class ImageCellRenderer : TextCellRenderer { - public override CellView GetCell(Cell item, CellView reusableView, List listView) + protected override CellElement CreateCellElement (Cell cell) { - var nativeImageCell = reusableView as CellView ?? new CellView(); - - var result = (CellView)base.GetCell(item, nativeImageCell, listView); - - var imageCell = (ImageCell)item; - - WireUpForceUpdateSizeRequested(item, result); - -#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed - SetImage(imageCell, result); -#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed - - return result; - } - - protected override async void HandlePropertyChanged(object sender, PropertyChangedEventArgs args) - { - var tvc = (CellView)sender; - var imageCell = (ImageCell)tvc.Cell; - - base.HandlePropertyChanged(sender, args); - - if (args.PropertyName == ImageCell.ImageSourceProperty.PropertyName) - await SetImage(imageCell, tvc); - } - - static async Task SetImage(ImageCell cell, CellView target) - { - var source = cell.ImageSource; - - target.ImageView.Source = null; - - IImageSourceHandler handler; - - if (source != null && (handler = - Registrar.Registered.GetHandler(source.GetType())) != null) - { - string image; - try - { - image = await handler.LoadImageAsync(source).ConfigureAwait(false); - } - catch (TaskCanceledException) - { - image = null; - } - - target.ImageView.Source = image; - } - else - target.ImageView.Source = null; + return new ImageCellElement (); } } } diff --git a/Ooui.Forms/Cells/SwitchCellElement.cs b/Ooui.Forms/Cells/SwitchCellElement.cs new file mode 100644 index 0000000..0f86729 --- /dev/null +++ b/Ooui.Forms/Cells/SwitchCellElement.cs @@ -0,0 +1,69 @@ +using System; +using System.ComponentModel; +using Ooui.Forms.Extensions; +using Ooui.Forms.Renderers; +using Xamarin.Forms; + +namespace Ooui.Forms.Cells +{ + public class SwitchCellElement : CellElement + { + public Label TextLabel { get; } = new Label (); + public SwitchRenderer.SwitchElement Switch { get; } = new SwitchRenderer.SwitchElement (); + + public SwitchCellElement () + { + AppendChild (TextLabel); + AppendChild (Switch); + + Switch.Style.Display = "inline-block"; + + Switch.Change += Switch_Change; + } + + protected override void BindCell () + { + Cell.PropertyChanged += Cell_PropertyChanged; + + if (Cell is SwitchCell cell) { + UpdateText (cell); + UpdateOn (cell); + } + + base.BindCell (); + } + + protected override void UnbindCell () + { + Cell.PropertyChanged -= Cell_PropertyChanged; + + base.UnbindCell (); + } + + void Cell_PropertyChanged (object sender, PropertyChangedEventArgs e) + { + var cell = (SwitchCell)sender; + + if (e.PropertyName == SwitchCell.TextProperty.PropertyName) + UpdateText (cell); + else if (e.PropertyName == SwitchCell.OnProperty.PropertyName) + UpdateOn (cell); + } + + void UpdateText (SwitchCell cell) + { + TextLabel.Text = cell.Text ?? string.Empty; + } + + void UpdateOn (SwitchCell cell) + { + Switch.IsChecked = cell.On; + } + + void Switch_Change (object sender, EventArgs e) + { + if (Cell is SwitchCell cell) + cell.On = Switch.IsChecked; + } + } +} diff --git a/Ooui.Forms/Cells/SwitchCellRenderer.cs b/Ooui.Forms/Cells/SwitchCellRenderer.cs index e6e1536..8471618 100644 --- a/Ooui.Forms/Cells/SwitchCellRenderer.cs +++ b/Ooui.Forms/Cells/SwitchCellRenderer.cs @@ -1,79 +1,13 @@ using System; -using System.ComponentModel; using Xamarin.Forms; namespace Ooui.Forms.Cells { public class SwitchCellRenderer : CellRenderer { - private static Cell _cell; - - public override CellView GetCell(Cell item, CellView reusableView, List listView) + protected override CellElement CreateCellElement (Cell cell) { - var nativeSwitchCell = reusableView as CellView; - Input oouiSwitch = null; - - if (nativeSwitchCell == null) - nativeSwitchCell = new CellView(); - else - { - oouiSwitch = nativeSwitchCell.CustomView.FirstChild as Input; - - if (oouiSwitch != null) - { - nativeSwitchCell.CustomView.RemoveChild(oouiSwitch); - oouiSwitch.Click -= OnSwitchClick; - } - nativeSwitchCell.Cell.PropertyChanged -= OnCellPropertyChanged; - } - - SetRealCell(item, nativeSwitchCell); - - if (oouiSwitch == null) - { - oouiSwitch = new Input(InputType.Checkbox); - oouiSwitch.SetAttribute("data-toggle", "toggle"); - } - - var switchCell = (SwitchCell)item; - - nativeSwitchCell.Cell = item; - nativeSwitchCell.SecondCol.Style.Width = "25%"; - _cell = nativeSwitchCell.Cell; - - nativeSwitchCell.Cell.PropertyChanged += OnCellPropertyChanged; - nativeSwitchCell.CustomView.AppendChild(oouiSwitch); - nativeSwitchCell.TextLabel.Text = switchCell.Text ?? string.Empty; - - oouiSwitch.IsChecked = switchCell.On; - oouiSwitch.Click += OnSwitchClick; - - WireUpForceUpdateSizeRequested(item, nativeSwitchCell); - - UpdateBackground(nativeSwitchCell, item); - - return nativeSwitchCell; - } - - private void OnCellPropertyChanged(object sender, PropertyChangedEventArgs e) - { - var switchCell = (SwitchCell)sender; - var nativeSwitchCell = (CellView)GetRealCell(switchCell); - - if (e.PropertyName == SwitchCell.OnProperty.PropertyName) - ((Input)nativeSwitchCell.CustomView.FirstChild).IsChecked = switchCell.On; - else if (e.PropertyName == SwitchCell.TextProperty.PropertyName) - nativeSwitchCell.TextLabel.Text = switchCell.Text ?? string.Empty; - } - - private void OnSwitchClick(object sender, EventArgs eventArgs) - { - var switchInput = (Input)sender; - - CellView realCell = GetRealCell(_cell); - - if (realCell != null) - ((SwitchCell)realCell.Cell).On = switchInput.IsChecked; + return new SwitchCellElement (); } } } diff --git a/Ooui.Forms/Cells/TextCellElement.cs b/Ooui.Forms/Cells/TextCellElement.cs new file mode 100644 index 0000000..0ada969 --- /dev/null +++ b/Ooui.Forms/Cells/TextCellElement.cs @@ -0,0 +1,55 @@ +using System; +using System.ComponentModel; +using Ooui.Forms.Extensions; +using Xamarin.Forms; + +namespace Ooui.Forms.Cells +{ + public class TextCellElement : CellElement + { + public Label TextLabel { get; } = new Label (); + + public Label DetailTextLabel { get; } = new Label (); + + public TextCellElement () + { + AppendChild (TextLabel); + AppendChild (DetailTextLabel); + } + + protected override void BindCell () + { + Cell.PropertyChanged += Cell_PropertyChanged; + + if (Cell is TextCell textCell) { + TextLabel.Text = textCell.Text ?? string.Empty; + DetailTextLabel.Text = textCell.Detail ?? string.Empty; + TextLabel.Style.Color = textCell.TextColor.ToOouiColor (OouiTheme.TextColor); + DetailTextLabel.Style.Color = textCell.DetailColor.ToOouiColor (OouiTheme.SecondaryTextColor); + } + + base.BindCell (); + } + + protected override void UnbindCell () + { + Cell.PropertyChanged -= Cell_PropertyChanged; + base.UnbindCell (); + } + + void Cell_PropertyChanged (object sender, PropertyChangedEventArgs args) + { + if (!(Cell is TextCell textCell)) + return; + + if (args.PropertyName == TextCell.TextProperty.PropertyName) + TextLabel.Text = textCell.Text ?? string.Empty; + else if (args.PropertyName == TextCell.DetailProperty.PropertyName) + DetailTextLabel.Text = textCell.Detail ?? string.Empty; + else if (args.PropertyName == TextCell.TextColorProperty.PropertyName) + TextLabel.Style.Color = textCell.TextColor.ToOouiColor (OouiTheme.TextColor); + else if (args.PropertyName == TextCell.DetailColorProperty.PropertyName) + DetailTextLabel.Style.Color = textCell.DetailColor.ToOouiColor (OouiTheme.SecondaryTextColor); + } + } +} diff --git a/Ooui.Forms/Cells/TextCellRenderer.cs b/Ooui.Forms/Cells/TextCellRenderer.cs index 56126aa..b507364 100644 --- a/Ooui.Forms/Cells/TextCellRenderer.cs +++ b/Ooui.Forms/Cells/TextCellRenderer.cs @@ -6,43 +6,9 @@ namespace Ooui.Forms.Cells { public class TextCellRenderer : CellRenderer { - public override CellView GetCell(Cell item, CellView reusableView, List listView) + protected override CellElement CreateCellElement (Cell item) { - var nativeTextCell = base.GetCell(item, reusableView, listView); - var textCell = (TextCell)item; - - if (nativeTextCell.Cell != null) - nativeTextCell.Cell.PropertyChanged -= nativeTextCell.HandlePropertyChanged; - - nativeTextCell.Cell = textCell; - textCell.PropertyChanged += nativeTextCell.HandlePropertyChanged; - nativeTextCell.ForwardPropertyChanged = HandlePropertyChanged; - - nativeTextCell.TextLabel.Text = textCell.Text ?? string.Empty; - nativeTextCell.DetailTextLabel.Text = textCell.Detail ?? string.Empty; - nativeTextCell.TextLabel.Style.Color = textCell.TextColor.ToOouiColor(OouiTheme.TextColor); - nativeTextCell.DetailTextLabel.Style.Color = textCell.DetailColor.ToOouiColor(OouiTheme.SecondaryTextColor); - - WireUpForceUpdateSizeRequested(item, nativeTextCell); - - UpdateBackground(nativeTextCell, item); - - return nativeTextCell; - } - - protected virtual void HandlePropertyChanged(object sender, PropertyChangedEventArgs args) - { - var tvc = (CellView)sender; - var textCell = (TextCell)tvc.Cell; - - if (args.PropertyName == TextCell.TextProperty.PropertyName) - tvc.TextLabel.Text = textCell.Text ?? string.Empty; - else if (args.PropertyName == TextCell.DetailProperty.PropertyName) - tvc.DetailTextLabel.Text = textCell.Detail ?? string.Empty; - else if (args.PropertyName == TextCell.TextColorProperty.PropertyName) - tvc.TextLabel.Style.Color = textCell.TextColor.ToOouiColor(OouiTheme.TextColor); - else if (args.PropertyName == TextCell.DetailColorProperty.PropertyName) - tvc.DetailTextLabel.Style.Color = textCell.DetailColor.ToOouiColor(OouiTheme.SecondaryTextColor); + return new TextCellElement (); } } } diff --git a/Ooui.Forms/Cells/ViewCellElement.cs b/Ooui.Forms/Cells/ViewCellElement.cs new file mode 100644 index 0000000..f8eab0e --- /dev/null +++ b/Ooui.Forms/Cells/ViewCellElement.cs @@ -0,0 +1,47 @@ +using Ooui.Forms.Renderers; +using System; +using Xamarin.Forms; + +namespace Ooui.Forms.Cells +{ + public class ViewCellElement : CellElement + { + WeakReference _rendererRef; + + protected override void BindCell () + { + var cell = (ViewCell)Cell; + + IVisualElementRenderer renderer; + if (_rendererRef == null || !_rendererRef.TryGetTarget (out renderer)) + renderer = GetNewRenderer (cell); + else { + if (renderer.Element != null && renderer == Platform.GetRenderer (renderer.Element)) + renderer.Element.ClearValue (Platform.RendererProperty); + + var type = Xamarin.Forms.Internals.Registrar.Registered.GetHandlerTypeForObject (cell.View); + var reflectableType = renderer as System.Reflection.IReflectableType; + var rendererType = reflectableType != null ? reflectableType.GetTypeInfo ().AsType () : renderer.GetType (); + if (rendererType == type || (renderer is DefaultRenderer && type == null)) { + renderer.SetElement (cell.View); + } + else { + renderer = GetNewRenderer (cell); + } + } + + Platform.SetRenderer (cell.View, renderer); + + base.BindCell (); + } + + IVisualElementRenderer GetNewRenderer (ViewCell cell) + { + var newRenderer = Platform.CreateRenderer (cell.View); + _rendererRef = new WeakReference (newRenderer); + AppendChild (newRenderer.NativeView); + + return newRenderer; + } + } +} diff --git a/Ooui.Forms/Cells/ViewCellRenderer.cs b/Ooui.Forms/Cells/ViewCellRenderer.cs index 5fffef1..4542b62 100644 --- a/Ooui.Forms/Cells/ViewCellRenderer.cs +++ b/Ooui.Forms/Cells/ViewCellRenderer.cs @@ -4,22 +4,9 @@ namespace Ooui.Forms.Cells { public class ViewCellRenderer : CellRenderer { - public override CellView GetCell(Cell item, CellView reusableView, List listView) + protected override CellElement CreateCellElement (Cell cell) { - var viewCell = (ViewCell)item; - - var nativeViewCell = reusableView as ViewCellView; - - if (nativeViewCell == null) - nativeViewCell = new ViewCellView(); - - nativeViewCell.ViewCell = viewCell; - - SetRealCell(item, nativeViewCell); - - WireUpForceUpdateSizeRequested(item, nativeViewCell); - - return nativeViewCell; + return new ViewCellElement (); } } } diff --git a/Ooui.Forms/Cells/ViewCellView.cs b/Ooui.Forms/Cells/ViewCellView.cs deleted file mode 100644 index 6bec165..0000000 --- a/Ooui.Forms/Cells/ViewCellView.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Ooui.Forms.Renderers; -using System; -using Xamarin.Forms; - -namespace Ooui.Forms.Cells -{ - public class ViewCellView : CellView - { - private WeakReference _rendererRef; - private ViewCell _viewCell; - - public ViewCell ViewCell - { - get { return _viewCell; } - set - { - if (_viewCell == value) - return; - UpdateCell(value); - } - } - - private void UpdateCell(ViewCell cell) - { - if (_viewCell != null) - Device.BeginInvokeOnMainThread(_viewCell.SendDisappearing); - - _viewCell = cell; - - Device.BeginInvokeOnMainThread(_viewCell.SendAppearing); - - IVisualElementRenderer renderer; - if (_rendererRef == null || !_rendererRef.TryGetTarget(out renderer)) - renderer = GetNewRenderer(); - else - { - if (renderer.Element != null && renderer == Platform.GetRenderer(renderer.Element)) - renderer.Element.ClearValue(Platform.RendererProperty); - - var type = Xamarin.Forms.Internals.Registrar.Registered.GetHandlerType(_viewCell.View.GetType()); - var reflectableType = renderer as System.Reflection.IReflectableType; - var rendererType = reflectableType != null ? reflectableType.GetTypeInfo().AsType() : renderer.GetType(); - if (rendererType == type || (renderer is DefaultRenderer && type == null)) - renderer.SetElement(_viewCell.View); - else - { - renderer = GetNewRenderer(); - } - } - - Platform.SetRenderer(_viewCell.View, renderer); - } - - private IVisualElementRenderer GetNewRenderer() - { - var newRenderer = Platform.CreateRenderer(_viewCell.View); - _rendererRef = new WeakReference(newRenderer); - AppendChild(newRenderer.NativeView); - - return newRenderer; - } - } -} diff --git a/Ooui.Forms/OouiTheme.cs b/Ooui.Forms/OouiTheme.cs index caa95df..4fd4f8c 100644 --- a/Ooui.Forms/OouiTheme.cs +++ b/Ooui.Forms/OouiTheme.cs @@ -1,4 +1,5 @@ using System; +using Xamarin.Forms; namespace Ooui.Forms { public static class OouiTheme @@ -10,5 +11,7 @@ namespace Ooui.Forms public static readonly Color ButtonBackgroundColor = new Color (51, 122, 183, 0xFF); public static readonly Color ButtonBorderColor = new Color (46, 109, 164, 0xFF); public static readonly Color ButtonTextColor = new Color (0xFF, 0xFF, 0xFF, 0xFF); + + public static readonly Size SwitchSize = new Size (54, 38); } } diff --git a/Ooui.Forms/Renderers/ListViewRenderer.cs b/Ooui.Forms/Renderers/ListViewRenderer.cs index b958f4c..98cf5a3 100644 --- a/Ooui.Forms/Renderers/ListViewRenderer.cs +++ b/Ooui.Forms/Renderers/ListViewRenderer.cs @@ -124,7 +124,7 @@ namespace Ooui.Forms.Renderers foreach (var item in items) { var li = listItems[i]; var children = li.Children; - var rv = children.Count > 0 ? children[0] as CellView : null; + var rv = children.Count > 0 ? children[0] as CellElement : null; var cell = GetCell (item, rv); if (rv == null) { li.AppendChild (cell); @@ -171,11 +171,11 @@ namespace Ooui.Forms.Renderers Control.Style.BackgroundColor = backgroundColor; } - CellView GetCell (Cell cell, CellView reusableView) + CellElement GetCell (Cell cell, CellElement reusableView) { var renderer = (Cells.CellRenderer)Registrar.Registered.GetHandlerForObject (cell); - var realCell = renderer.GetCell (cell, reusableView, Control); + var realCell = renderer.GetCellElement (cell, reusableView, Control); return realCell; } diff --git a/Ooui.Forms/Renderers/SwitchRenderer.cs b/Ooui.Forms/Renderers/SwitchRenderer.cs index 771e737..281360f 100644 --- a/Ooui.Forms/Renderers/SwitchRenderer.cs +++ b/Ooui.Forms/Renderers/SwitchRenderer.cs @@ -7,7 +7,7 @@ namespace Ooui.Forms.Renderers { public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint) { - var size = new Size (54, 38); + var size = OouiTheme.SwitchSize; return new SizeRequest (size, size); } @@ -62,11 +62,10 @@ namespace Ooui.Forms.Renderers } public SwitchElement () { - AppendChild (knob); knob.Style.Position = "absolute"; knob.Style.BorderRadius = "10px"; knob.Style.Cursor = "pointer"; - knob.Style.Top = "2px"; + knob.Style.Top = "0px"; knob.Style.Width = "18px"; knob.Style.Height = "34px"; @@ -74,10 +73,14 @@ namespace Ooui.Forms.Renderers Style.Cursor = "pointer"; Style.BorderStyle = "solid"; Style.BorderWidth = "2px"; + Style.Width = OouiTheme.SwitchSize.Width; + Style.Height = OouiTheme.SwitchSize.Height; + Style.Position = "relative"; Click += (s, e) => { IsChecked = !IsChecked; Change?.Invoke (this, EventArgs.Empty); }; + AppendChild (knob); UpdateUI (); } @@ -87,10 +90,10 @@ namespace Ooui.Forms.Renderers Style.BorderColor = Style.BackgroundColor; knob.Style.BackgroundColor = isChecked ? "#FFF" : "#EEE"; if (isChecked) { - knob.Style.Left = "34px"; + knob.Style.Left = "32px"; } else { - knob.Style.Left = "2px"; + knob.Style.Left = "0px"; } } } diff --git a/Ooui.sln b/Ooui.sln index 26cd07d..7985e6f 100644 --- a/Ooui.sln +++ b/Ooui.sln @@ -16,10 +16,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PlatformSamples", "Platform EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCoreMvc", "PlatformSamples\AspNetCoreMvc\AspNetCoreMvc.csproj", "{7C6D477C-3378-4A86-9C31-AAD51204120B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OouiTemplates", "Templates\OouiTemplates\OouiTemplates.csproj", "{1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ooui.Wasm.Build.Tasks", "Ooui.Wasm.Build.Tasks\Ooui.Wasm.Build.Tasks.csproj", "{6E9C9582-0DA8-4496-BAE0-23EFAF4A10C2}" EndProject +Project("{9344BDBB-3E7F-41FC-A0DD-8665D75EE146}") = "OouiTemplates", "Templates\OouiTemplates\OouiTemplates.csproj", "{7D3B32CF-38A3-40DE-8123-2E7A0D87104D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -102,18 +102,6 @@ Global {7C6D477C-3378-4A86-9C31-AAD51204120B}.Release|x64.Build.0 = Release|Any CPU {7C6D477C-3378-4A86-9C31-AAD51204120B}.Release|x86.ActiveCfg = Release|Any CPU {7C6D477C-3378-4A86-9C31-AAD51204120B}.Release|x86.Build.0 = Release|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Debug|x64.ActiveCfg = Debug|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Debug|x64.Build.0 = Debug|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Debug|x86.ActiveCfg = Debug|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Debug|x86.Build.0 = Debug|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Release|Any CPU.Build.0 = Release|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Release|x64.ActiveCfg = Release|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Release|x64.Build.0 = Release|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Release|x86.ActiveCfg = Release|Any CPU - {1DA10AAB-EB41-49CF-9441-B4D28D0A7F96}.Release|x86.Build.0 = Release|Any CPU {6E9C9582-0DA8-4496-BAE0-23EFAF4A10C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {6E9C9582-0DA8-4496-BAE0-23EFAF4A10C2}.Debug|Any CPU.Build.0 = Debug|Any CPU {6E9C9582-0DA8-4496-BAE0-23EFAF4A10C2}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -126,6 +114,18 @@ Global {6E9C9582-0DA8-4496-BAE0-23EFAF4A10C2}.Release|x64.Build.0 = Release|Any CPU {6E9C9582-0DA8-4496-BAE0-23EFAF4A10C2}.Release|x86.ActiveCfg = Release|Any CPU {6E9C9582-0DA8-4496-BAE0-23EFAF4A10C2}.Release|x86.Build.0 = Release|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Debug|x64.ActiveCfg = Debug|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Debug|x64.Build.0 = Debug|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Debug|x86.ActiveCfg = Debug|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Debug|x86.Build.0 = Debug|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Release|Any CPU.Build.0 = Release|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Release|x64.ActiveCfg = Release|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Release|x64.Build.0 = Release|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Release|x86.ActiveCfg = Release|Any CPU + {7D3B32CF-38A3-40DE-8123-2E7A0D87104D}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE