From bf1b2edbbc8dd920e6d146e38a0880981f7dd21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Wed, 27 Dec 2017 11:48:58 +0100 Subject: [PATCH] Added SearchBarRenderer and sample --- Ooui.Forms/Exports.cs | 3 +- Ooui.Forms/Renderers/SearchBarRenderer.cs | 130 ++++++++++++++++++++++ Samples/Program.cs | 1 + Samples/SearchBarSample.cs | 114 +++++++++++++++++++ 4 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 Ooui.Forms/Renderers/SearchBarRenderer.cs create mode 100644 Samples/SearchBarSample.cs diff --git a/Ooui.Forms/Exports.cs b/Ooui.Forms/Exports.cs index f245db7..04c82e6 100644 --- a/Ooui.Forms/Exports.cs +++ b/Ooui.Forms/Exports.cs @@ -14,8 +14,9 @@ using Xamarin.Forms; [assembly: ExportRenderer(typeof(Image), typeof(ImageRenderer))] [assembly: ExportRenderer(typeof(Label), typeof(LabelRenderer))] [assembly: ExportRenderer(typeof(ProgressBar), typeof(ProgressBarRenderer))] -[assembly: ExportRenderer(typeof(TimePicker), typeof(TimePickerRenderer))] +[assembly: ExportRenderer(typeof(SearchBar), typeof(SearchBarRenderer))] [assembly: ExportRenderer(typeof(Switch), typeof(SwitchRenderer))] +[assembly: ExportRenderer(typeof(TimePicker), typeof(TimePickerRenderer))] [assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))] [assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))] [assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))] diff --git a/Ooui.Forms/Renderers/SearchBarRenderer.cs b/Ooui.Forms/Renderers/SearchBarRenderer.cs new file mode 100644 index 0000000..9e117e5 --- /dev/null +++ b/Ooui.Forms/Renderers/SearchBarRenderer.cs @@ -0,0 +1,130 @@ +using Ooui.Forms.Extensions; +using System; +using System.ComponentModel; +using Xamarin.Forms; + +namespace Ooui.Forms.Renderers +{ + public class SearchBarRenderer : ViewRenderer + { + Input _searchBar; + Button _searchButton; + bool _disposed; + + IElementController ElementController => Element as IElementController; + + public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) + { + var text = Element.Text; + if (text == null || text.Length == 0) + { + text = Element.Placeholder; + } + Size size; + if (text == null || text.Length == 0) + { + size = new Size(Element.FontSize * 0.25, Element.FontSize); + } + else + { + size = text.MeasureSize(Element.FontFamily, Element.FontSize, Element.FontAttributes); + } + size = new Size(size.Width, size.Height * 1.428 + 14); + return new SizeRequest(size, size); + } + + protected override void Dispose(bool disposing) + { + if (_disposed) + return; + + _disposed = true; + + if (disposing) + { + if (Control != null && _searchBar != null && _searchButton != null) + { + _searchBar.Change -= OnChange; + _searchButton.Click -= OnClick; + } + } + + base.Dispose(disposing); + } + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + base.OnElementChanged(e); + + if (e.NewElement == null) + return; + + if (Control == null) + { + var p = new Div { ClassName = "input-group" }; + var pb = new Span { ClassName = "input-group-btn" }; + _searchButton = new Button { ClassName = "btn btn-secondary", Text = "Search" }; + pb.AppendChild(_searchButton); + _searchBar = new Input + { + ClassName = "form-control", + Type = InputType.Text + }; + + p.AppendChild(_searchBar); + p.AppendChild(pb); + + _searchBar.Change += OnChange; + _searchButton.Click += OnClick; + + SetNativeControl(p); + } + + UpdateText(); + UpdateTextColor(); + UpdatePlaceholder(); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + base.OnElementPropertyChanged(sender, e); + + if (e.PropertyName == SearchBar.TextProperty.PropertyName) + UpdateText(); + else if (e.PropertyName == SearchBar.PlaceholderProperty.PropertyName) + UpdatePlaceholder(); + else if (e.PropertyName == SearchBar.PlaceholderColorProperty.PropertyName) + UpdatePlaceholder(); + else if(e.PropertyName == SearchBar.TextColorProperty.PropertyName) + UpdateTextColor(); + } + + void UpdateText() + { + _searchBar.Value = Element.Text; + } + + void UpdateTextColor() + { + var textColor = (Xamarin.Forms.Color)Element.GetValue(TimePicker.TextColorProperty); + + Control.Style.Color = textColor.ToOouiColor(Xamarin.Forms.Color.Black); + } + + void UpdatePlaceholder() + { + _searchBar.Placeholder = Element.Placeholder ?? string.Empty; + } + + void OnChange(object sender, EventArgs eventArgs) + { + if (_searchBar.Value != Element.Text) + ElementController.SetValueFromRenderer(SearchBar.TextProperty, _searchBar.Value); + } + + void OnClick(object sender, TargetEventArgs e) + { + Element.OnSearchButtonPressed(); + } + } +} diff --git a/Samples/Program.cs b/Samples/Program.cs index d4b4175..dc9166d 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -32,6 +32,7 @@ namespace Samples new DisplayAlertSample ().Publish (); new DotMatrixClockSample().Publish(); new EditorSample().Publish(); + new SearchBarSample().Publish(); new TimePickerSample().Publish(); new TipCalcSample().Publish(); new WeatherAppSample().Publish(); diff --git a/Samples/SearchBarSample.cs b/Samples/SearchBarSample.cs new file mode 100644 index 0000000..ffbf7c9 --- /dev/null +++ b/Samples/SearchBarSample.cs @@ -0,0 +1,114 @@ +using Ooui; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Xamarin.Forms; + +namespace Samples +{ + public class SearchBarSample : ISample + { + private Xamarin.Forms.Label _resultsLabel; + + public string Title => "Xamarin.Forms SearchBar"; + + public Ooui.Element CreateElement() + { + var panel = new StackLayout(); + + var titleLabel = new Xamarin.Forms.Label + { + Text = "SearchBar", + FontSize = 24, + FontAttributes = FontAttributes.Bold, + }; + panel.Children.Add(titleLabel); + + SearchBar searchBar = new SearchBar + { + Placeholder = "Xamarin.Forms Property", + }; + + searchBar.SearchButtonPressed += OnSearchBarButtonPressed; + + panel.Children.Add(searchBar); + + _resultsLabel = new Xamarin.Forms.Label(); + panel.Children.Add(_resultsLabel); + + var page = new ContentPage + { + Content = panel + }; + + return page.GetOouiElement(); + } + + void OnSearchBarButtonPressed(object sender, EventArgs args) + { + // Get the search text. + SearchBar searchBar = (SearchBar)sender; + string searchText = searchBar.Text; + + // Create a List and initialize the results Label. + var list = new List>(); + _resultsLabel.Text = string.Empty; + + // Get Xamarin.Forms assembly. + Assembly xamarinFormsAssembly = typeof(View).GetTypeInfo().Assembly; + + // Loop through all the types. + foreach (Type type in xamarinFormsAssembly.ExportedTypes) + { + TypeInfo typeInfo = type.GetTypeInfo(); + + // Public types only. + if (typeInfo.IsPublic) + { + // Loop through the properties. + foreach (PropertyInfo property in typeInfo.DeclaredProperties) + { + // Check for a match + if (property.Name.Equals(searchText)) + { + // Add it to the list. + list.Add(Tuple.Create(type, property.PropertyType)); + } + } + } + } + + if (list.Count == 0) + { + _resultsLabel.Text = + String.Format("No Xamarin.Forms properties with " + + "the name of {0} were found", + searchText); + } + else + { + _resultsLabel.Text = "The "; + + foreach (Tuple tuple in list) + { + _resultsLabel.Text += + String.Format("{0} type defines a property named {1} of type {2}", + tuple.Item1.Name, searchText, tuple.Item2.Name); + + if (tuple != list.Last()) + { + _resultsLabel.Text += "; and the "; + } + } + + _resultsLabel.Text += "."; + } + } + + public void Publish() + { + UI.Publish("/searchbar", CreateElement); + } + } +}