Added SearchBarRenderer and sample
This commit is contained in:
		
							parent
							
								
									a81c152720
								
							
						
					
					
						commit
						bf1b2edbbc
					
				|  | @ -14,8 +14,9 @@ using Xamarin.Forms; | ||||||
| [assembly: ExportRenderer(typeof(Image), typeof(ImageRenderer))] | [assembly: ExportRenderer(typeof(Image), typeof(ImageRenderer))] | ||||||
| [assembly: ExportRenderer(typeof(Label), typeof(LabelRenderer))] | [assembly: ExportRenderer(typeof(Label), typeof(LabelRenderer))] | ||||||
| [assembly: ExportRenderer(typeof(ProgressBar), typeof(ProgressBarRenderer))] | [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(Switch), typeof(SwitchRenderer))] | ||||||
|  | [assembly: ExportRenderer(typeof(TimePicker), typeof(TimePickerRenderer))] | ||||||
| [assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))] | [assembly: ExportImageSourceHandler(typeof(FileImageSource), typeof(FileImageSourceHandler))] | ||||||
| [assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))] | [assembly: ExportImageSourceHandler(typeof(StreamImageSource), typeof(StreamImagesourceHandler))] | ||||||
| [assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))] | [assembly: ExportImageSourceHandler(typeof(UriImageSource), typeof(ImageLoaderSourceHandler))] | ||||||
|  |  | ||||||
|  | @ -0,0 +1,130 @@ | ||||||
|  | using Ooui.Forms.Extensions; | ||||||
|  | using System; | ||||||
|  | using System.ComponentModel; | ||||||
|  | using Xamarin.Forms; | ||||||
|  | 
 | ||||||
|  | namespace Ooui.Forms.Renderers | ||||||
|  | { | ||||||
|  |     public class SearchBarRenderer : ViewRenderer<SearchBar, Div> | ||||||
|  |     { | ||||||
|  |         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<SearchBar> 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(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -32,6 +32,7 @@ namespace Samples | ||||||
|             new DisplayAlertSample ().Publish (); |             new DisplayAlertSample ().Publish (); | ||||||
|             new DotMatrixClockSample().Publish(); |             new DotMatrixClockSample().Publish(); | ||||||
|             new EditorSample().Publish(); |             new EditorSample().Publish(); | ||||||
|  |             new SearchBarSample().Publish(); | ||||||
|             new TimePickerSample().Publish(); |             new TimePickerSample().Publish(); | ||||||
|             new TipCalcSample().Publish(); |             new TipCalcSample().Publish(); | ||||||
|             new WeatherAppSample().Publish(); |             new WeatherAppSample().Publish(); | ||||||
|  |  | ||||||
|  | @ -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<Tuple<Type, Type>>(); | ||||||
|  |             _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, Type>(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<Type, Type> 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); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue