using System; using EColor = ElmSharp.Color; namespace Xamarin.Forms.Platform.Tizen { public class SearchBarRenderer : ViewRenderer { //TODO need to add internationalization support const string DefaultPlaceholderText = "Search"; static readonly EColor s_defaultCancelButtonColor = EColor.Aqua; //TODO: read default platform color static readonly EColor s_defaultPlaceholderColor = EColor.Gray; static readonly EColor s_defaultTextColor = EColor.Black; /// /// Creates a new instance of the class. /// Registers handlers for various properties of the SearchBar widget. /// public SearchBarRenderer() { RegisterPropertyHandler(SearchBar.CancelButtonColorProperty, CancelButtonColorPropertyHandler); RegisterPropertyHandler(SearchBar.FontAttributesProperty, FontAttributesPropertyHandler); RegisterPropertyHandler(SearchBar.FontFamilyProperty, FontFamilyPropertyHandler); RegisterPropertyHandler(SearchBar.FontSizeProperty, FontSizePropertyHandler); RegisterPropertyHandler(SearchBar.HorizontalTextAlignmentProperty, HorizontalTextAlignmentPropertyHandler); RegisterPropertyHandler(SearchBar.PlaceholderProperty, PlaceholderPropertyHandler); RegisterPropertyHandler(SearchBar.PlaceholderColorProperty, PlaceholderColorPropertyHandler); RegisterPropertyHandler(SearchBar.TextProperty, TextPropertyHandler); RegisterPropertyHandler(SearchBar.TextColorProperty, TextColorPropertyHandler); } /// /// A method called whenever the associated element has changed. /// protected override void OnElementChanged(ElementChangedEventArgs e) { if (Control == null) { var searchBar = new Native.SearchBar(Forms.Context.MainWindow); SetNativeControl(searchBar); } if (e.OldElement != null) { Control.TextChanged -= SearchBarTextChangedHandler; Control.SearchButtonPressed -= SearchButtonPressedHandler; } if (e.NewElement != null) { Control.TextChanged += SearchBarTextChangedHandler; Control.SearchButtonPressed += SearchButtonPressedHandler; Control.BatchBegin(); } base.OnElementChanged(e); } protected override Size MinimumSize() { return new Size(136, 65); } protected override void OnElementReady() { Control?.BatchCommit(); } /// /// Called upon changing of Xamarin widget's cancel button color property. /// Converts current Color to ElmSharp.Color instance and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native widget. /// void CancelButtonColorPropertyHandler() { Control.CancelButtonColor = Element.CancelButtonColor.IsDefault ? s_defaultCancelButtonColor : Element.CancelButtonColor.ToNative(); } /// /// Called upon changing of Xamarin widget's font attributes property. /// Converts current FontAttributes to ElmSharp.FontAttributes instance /// and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget. /// void FontAttributesPropertyHandler() { Control.FontAttributes = Element.FontAttributes; } /// /// Called upon changing of Xamarin widget's font family property. /// Sets current value of FontFamily property to the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget. /// void FontFamilyPropertyHandler() { Control.FontFamily = Element.FontFamily; } /// /// Called upon changing of Xamarin widget's font size property. /// Sets current value of FontSize property to the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget. /// void FontSizePropertyHandler() { Control.FontSize = Element.FontSize; } /// /// Called upon changing of Xamarin widget's horizontal text alignment property. /// Converts current HorizontalTextAlignment property's value to Xamarin.Forms.Platform.Tizen.Native.TextAlignment instance /// and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget. /// void HorizontalTextAlignmentPropertyHandler() { Control.HorizontalTextAlignment = Element.HorizontalTextAlignment.ToNative(); } /// /// Called upon changing of Xamarin widget's placeholder color property. /// Converts current PlaceholderColor property value to ElmSharp.Color instance /// and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget. /// void PlaceholderColorPropertyHandler() { Control.PlaceholderColor = Element.PlaceholderColor.IsDefault ? s_defaultPlaceholderColor : Element.PlaceholderColor.ToNative(); } /// /// Called upon changing of Xamarin widget's placeholder text property. /// void PlaceholderPropertyHandler() { Control.Placeholder = Element.Placeholder == null ? DefaultPlaceholderText : Element.Placeholder; } /// /// Called on every change of underlying SearchBar's Text property. /// Rewrites current underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar's Text contents to its Xamarin counterpart. /// /// Sender. void SearchBarTextChangedHandler(object sender, EventArgs e) { Element.Text = Control.Text; } /// /// Called when the user clicks the Search button. /// /// Sender. /// Event arguments. void SearchButtonPressedHandler(object sender, EventArgs e) { (Element as ISearchBarController).OnSearchButtonPressed(); } /// /// Called upon changing of Xamarin widget's text color property. /// Converts current TextColor property value to ElmSharp.Color instance /// and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget. /// void TextColorPropertyHandler() { Control.TextColor = Element.TextColor.IsDefault ? s_defaultTextColor : Element.TextColor.ToNative(); } /// /// Called upon changing of Xamarin widget's text property. /// void TextPropertyHandler() { Control.Text = Element.Text; } } }