/
SearchBarHandler.Android.cs
126 lines (97 loc) · 3.41 KB
/
SearchBarHandler.Android.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using Android.Content.Res;
using Android.Graphics.Drawables;
using Android.Widget;
using static AndroidX.AppCompat.Widget.SearchView;
using SearchView = AndroidX.AppCompat.Widget.SearchView;
namespace Microsoft.Maui.Handlers
{
public partial class SearchBarHandler : ViewHandler<ISearchBar, SearchView>
{
QueryTextListener QueryListener { get; } = new QueryTextListener();
static Drawable? DefaultBackground;
static ColorStateList? DefaultPlaceholderTextColors { get; set; }
EditText? _editText;
public EditText? QueryEditor => _editText;
protected override SearchView CreateNativeView()
{
var searchView = new SearchView(Context);
searchView.SetIconifiedByDefault(false);
_editText = searchView.GetFirstChildOfType<EditText>();
return searchView;
}
protected override void ConnectHandler(SearchView nativeView)
{
QueryListener.Handler = this;
nativeView.SetOnQueryTextListener(QueryListener);
}
protected override void DisconnectHandler(SearchView nativeView)
{
nativeView.SetOnQueryTextListener(null);
QueryListener.Handler = null;
}
void SetupDefaults(SearchView nativeView)
{
DefaultBackground = nativeView.Background;
}
// This is a Android-specific mapping
public static void MapBackground(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdateBackground(searchBar, DefaultBackground);
}
public static void MapText(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdateText(searchBar);
}
public static void MapPlaceholder(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdatePlaceholder(searchBar);
}
public static void MapPlaceholderColor(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdatePlaceholderColor(searchBar, DefaultPlaceholderTextColors, handler._editText);
}
public static void MapFont(SearchBarHandler handler, ISearchBar searchBar)
{
var fontManager = handler.GetRequiredService<IFontManager>();
handler.NativeView?.UpdateFont(searchBar, fontManager, handler._editText);
}
public static void MapHorizontalTextAlignment(SearchBarHandler handler, ISearchBar searchBar)
{
handler.QueryEditor?.UpdateHorizontalTextAlignment(searchBar);
}
public static void MapCharacterSpacing(SearchBarHandler handler, ISearchBar searchBar)
{
handler.QueryEditor?.UpdateCharacterSpacing(searchBar);
}
public static void MapTextColor(SearchBarHandler handler, ISearchBar searchBar)
{
handler.QueryEditor?.UpdateTextColor(searchBar);
}
[MissingMapper]
public static void MapIsTextPredictionEnabled(IViewHandler handler, ISearchBar searchBar) { }
public static void MapMaxLength(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdateMaxLength(searchBar, handler.QueryEditor);
}
[MissingMapper]
public static void MapIsReadOnly(IViewHandler handler, ISearchBar searchBar) { }
public static void MapCancelButtonColor(SearchBarHandler handler, ISearchBar searchBar)
{
handler.NativeView?.UpdateCancelButtonColor(searchBar);
}
public class QueryTextListener : Java.Lang.Object, IOnQueryTextListener
{
public SearchBarHandler? Handler { get; set; }
public bool OnQueryTextChange(string newText)
{
return true;
}
public bool OnQueryTextSubmit(string newText)
{
Handler?.VirtualView?.SearchButtonPressed();
// TODO: Clear focus
return true;
}
}
}
}