2016-03-22 23:02:25 +03:00
|
|
|
using System;
|
2017-04-11 21:02:06 +03:00
|
|
|
using System.ComponentModel;
|
2016-03-22 23:02:25 +03:00
|
|
|
using System.Windows.Input;
|
2017-03-07 22:56:24 +03:00
|
|
|
using Xamarin.Forms.Internals;
|
2016-03-22 23:02:25 +03:00
|
|
|
using Xamarin.Forms.Platform;
|
|
|
|
|
|
|
|
namespace Xamarin.Forms
|
|
|
|
{
|
|
|
|
[RenderWith(typeof(_SearchBarRenderer))]
|
2018-08-31 21:35:56 +03:00
|
|
|
public class SearchBar : View, IFontElement, IPlaceholderElement, ITextElement, ITextAlignmentElement, ISearchBarController, IElementConfiguration<SearchBar>
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
public static readonly BindableProperty SearchCommandProperty = BindableProperty.Create("SearchCommand", typeof(ICommand), typeof(SearchBar), null, propertyChanged: OnCommandChanged);
|
|
|
|
|
|
|
|
public static readonly BindableProperty SearchCommandParameterProperty = BindableProperty.Create("SearchCommandParameter", typeof(object), typeof(SearchBar), null);
|
|
|
|
|
|
|
|
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(SearchBar), default(string), BindingMode.TwoWay,
|
|
|
|
propertyChanged: (bindable, oldValue, newValue) =>
|
|
|
|
{
|
|
|
|
var searchBar = (SearchBar)bindable;
|
|
|
|
EventHandler<TextChangedEventArgs> eh = searchBar.TextChanged;
|
|
|
|
if (eh != null)
|
|
|
|
eh(searchBar, new TextChangedEventArgs((string)oldValue, (string)newValue));
|
|
|
|
});
|
|
|
|
|
|
|
|
public static readonly BindableProperty CancelButtonColorProperty = BindableProperty.Create("CancelButtonColor", typeof(Color), typeof(SearchBar), default(Color));
|
|
|
|
|
2018-08-31 21:35:56 +03:00
|
|
|
public static readonly BindableProperty PlaceholderProperty = PlaceholderElement.PlaceholderProperty;
|
|
|
|
|
|
|
|
public static readonly BindableProperty PlaceholderColorProperty = PlaceholderElement.PlaceholderColorProperty;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2017-03-03 11:16:04 +03:00
|
|
|
public static readonly BindableProperty FontFamilyProperty = FontElement.FontFamilyProperty;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2017-03-03 11:16:04 +03:00
|
|
|
public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2017-03-03 11:16:04 +03:00
|
|
|
public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2017-11-14 11:37:06 +03:00
|
|
|
public static readonly BindableProperty HorizontalTextAlignmentProperty = TextAlignmentElement.HorizontalTextAlignmentProperty;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2017-03-06 12:35:17 +03:00
|
|
|
public static readonly BindableProperty TextColorProperty = TextElement.TextColorProperty;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2016-08-30 20:46:14 +03:00
|
|
|
readonly Lazy<PlatformConfigurationRegistry<SearchBar>> _platformConfigurationRegistry;
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
public Color CancelButtonColor
|
|
|
|
{
|
|
|
|
get { return (Color)GetValue(CancelButtonColorProperty); }
|
|
|
|
set { SetValue(CancelButtonColorProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public TextAlignment HorizontalTextAlignment
|
|
|
|
{
|
2017-11-14 11:37:06 +03:00
|
|
|
get { return (TextAlignment)GetValue(TextAlignmentElement.HorizontalTextAlignmentProperty); }
|
|
|
|
set { SetValue(TextAlignmentElement.HorizontalTextAlignmentProperty, value); }
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:35:56 +03:00
|
|
|
public string Placeholder {
|
|
|
|
get => (string)GetValue(PlaceholderElement.PlaceholderProperty);
|
|
|
|
set => SetValue(PlaceholderElement.PlaceholderProperty, value);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:35:56 +03:00
|
|
|
public Color PlaceholderColor {
|
|
|
|
get => (Color)GetValue(PlaceholderElement.PlaceholderColorProperty);
|
|
|
|
set => SetValue(PlaceholderElement.PlaceholderColorProperty, value);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public ICommand SearchCommand
|
|
|
|
{
|
|
|
|
get { return (ICommand)GetValue(SearchCommandProperty); }
|
|
|
|
set { SetValue(SearchCommandProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public object SearchCommandParameter
|
|
|
|
{
|
|
|
|
get { return GetValue(SearchCommandParameterProperty); }
|
|
|
|
set { SetValue(SearchCommandParameterProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Text
|
|
|
|
{
|
|
|
|
get { return (string)GetValue(TextProperty); }
|
|
|
|
set { SetValue(TextProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color TextColor
|
|
|
|
{
|
2017-03-06 12:35:17 +03:00
|
|
|
get { return (Color)GetValue(TextElement.TextColorProperty); }
|
|
|
|
set { SetValue(TextElement.TextColorProperty, value); }
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEnabledCore
|
|
|
|
{
|
|
|
|
set { SetValueCore(IsEnabledProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public FontAttributes FontAttributes
|
|
|
|
{
|
|
|
|
get { return (FontAttributes)GetValue(FontAttributesProperty); }
|
|
|
|
set { SetValue(FontAttributesProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public string FontFamily
|
|
|
|
{
|
|
|
|
get { return (string)GetValue(FontFamilyProperty); }
|
|
|
|
set { SetValue(FontFamilyProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
[TypeConverter(typeof(FontSizeConverter))]
|
|
|
|
public double FontSize
|
|
|
|
{
|
|
|
|
get { return (double)GetValue(FontSizeProperty); }
|
|
|
|
set { SetValue(FontSizeProperty, value); }
|
|
|
|
}
|
|
|
|
|
2017-03-03 11:16:04 +03:00
|
|
|
void IFontElement.OnFontFamilyChanged(string oldValue, string newValue)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void IFontElement.OnFontSizeChanged(double oldValue, double newValue)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
double IFontElement.FontSizeDefaultValueCreator() =>
|
|
|
|
Device.GetNamedSize(NamedSize.Default, (SearchBar)this);
|
|
|
|
|
|
|
|
void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-03-03 14:39:30 +03:00
|
|
|
void IFontElement.OnFontChanged(Font oldValue, Font newValue)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
public event EventHandler SearchButtonPressed;
|
|
|
|
|
|
|
|
public event EventHandler<TextChangedEventArgs> TextChanged;
|
|
|
|
|
2016-08-30 20:46:14 +03:00
|
|
|
public SearchBar()
|
|
|
|
{
|
|
|
|
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<SearchBar>>(() => new PlatformConfigurationRegistry<SearchBar>(this));
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:02:06 +03:00
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public void OnSearchButtonPressed()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
ICommand cmd = SearchCommand;
|
|
|
|
|
|
|
|
if (cmd != null && !cmd.CanExecute(SearchCommandParameter))
|
|
|
|
return;
|
|
|
|
|
|
|
|
cmd?.Execute(SearchCommandParameter);
|
|
|
|
SearchButtonPressed?.Invoke(this, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandCanExecuteChanged(object sender, EventArgs eventArgs)
|
|
|
|
{
|
|
|
|
ICommand cmd = SearchCommand;
|
|
|
|
if (cmd != null)
|
|
|
|
IsEnabledCore = cmd.CanExecute(SearchCommandParameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnCommandChanged(BindableObject bindable, object oldValue, object newValue)
|
|
|
|
{
|
|
|
|
var self = (SearchBar)bindable;
|
2016-04-07 09:09:45 +03:00
|
|
|
var newCommand = (ICommand)newValue;
|
|
|
|
var oldCommand = (ICommand)oldValue;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
if (oldCommand != null)
|
|
|
|
{
|
|
|
|
oldCommand.CanExecuteChanged -= self.CommandCanExecuteChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newCommand != null)
|
|
|
|
{
|
|
|
|
newCommand.CanExecuteChanged += self.CommandCanExecuteChanged;
|
|
|
|
self.CommandCanExecuteChanged(self, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self.IsEnabledCore = true;
|
|
|
|
}
|
|
|
|
}
|
2016-08-30 20:46:14 +03:00
|
|
|
|
|
|
|
public IPlatformElementConfiguration<T, SearchBar> On<T>() where T : IConfigPlatform
|
|
|
|
{
|
|
|
|
return _platformConfigurationRegistry.Value.On<T>();
|
|
|
|
}
|
2017-03-06 12:35:17 +03:00
|
|
|
|
|
|
|
void ITextElement.OnTextColorPropertyChanged(Color oldValue, Color newValue)
|
|
|
|
{
|
|
|
|
}
|
2017-11-14 11:37:06 +03:00
|
|
|
|
|
|
|
void ITextAlignmentElement.OnHorizontalTextAlignmentPropertyChanged(TextAlignment oldValue, TextAlignment newValue)
|
|
|
|
{
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
}
|