-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathSearchView.Appearance.cs
209 lines (185 loc) · 11.9 KB
/
SearchView.Appearance.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// /*******************************************************************************
// * Copyright 2012-2018 Esri
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// ******************************************************************************/
using System.ComponentModel;
namespace Esri.ArcGISRuntime.Toolkit.Maui;
public partial class SearchView : TemplatedView, INotifyPropertyChanged
{
#pragma warning disable SA1306, SA1310, SX1309 // Field names should begin with lower-case letter
private Entry? PART_Entry;
private ImageButton? PART_CancelButton;
private ImageButton? PART_SearchButton;
private ImageButton? PART_SourceSelectButton;
private Label? PART_ResultLabel;
private CollectionView? PART_SuggestionsView;
private CollectionView? PART_ResultView;
private CollectionView? PART_SourcesView;
private Button? PART_RepeatButton;
private Grid? PART_ResultContainer;
private Grid? PART_RepeatButtonContainer;
#pragma warning restore SA1306, SA1310, SX1309 // Field names should begin with lower-case letter
private const string FOREGROUND_LIGHT = "#151515";
private const string FOREGROUND_DARK = "#FFF";
private const string BACKGROUND_LIGHT = "#FFF";
private const string BACKGROUND_DARK = "#2B2B2B";
private static readonly DataTemplate DefaultResultTemplate;
private static readonly DataTemplate DefaultSuggestionTemplate;
private static readonly DataTemplate DefaultSuggestionGroupHeaderTemplate;
private static readonly ControlTemplate DefaultControlTemplate;
private static readonly ByteArrayToImageSourceConverter ImageSourceConverter;
private static readonly BoolToCollectionIconImageConverter CollectionIconConverter;
private static readonly EmptyStringToBoolConverter EmptyStringConverter;
static SearchView()
{
ImageSourceConverter = new ByteArrayToImageSourceConverter();
CollectionIconConverter = new BoolToCollectionIconImageConverter();
EmptyStringConverter = new EmptyStringToBoolConverter();
DefaultSuggestionGroupHeaderTemplate = new DataTemplate(() =>
{
Grid containingGrid = new Grid();
containingGrid.SetAppThemeColor(Grid.BackgroundColorProperty, Color.FromArgb("#4e4e4e"), Color.FromArgb("#151515"));
Label textLabel = new Label();
textLabel.SetBinding(Label.TextProperty, static (IGrouping<ISearchSource, SearchSuggestion> group) => group.Key.DisplayName);
textLabel.Margin = new Thickness(4);
textLabel.TextColor = Colors.White;
textLabel.FontSize = 14;
textLabel.VerticalTextAlignment = TextAlignment.Center;
containingGrid.Children.Add(textLabel);
return containingGrid;
});
DefaultSuggestionTemplate = new DataTemplate(() =>
{
Grid containingGrid = new Grid();
containingGrid.SetAppThemeColor(Grid.BackgroundColorProperty, Color.FromArgb(BACKGROUND_LIGHT), Color.FromArgb(BACKGROUND_DARK));
containingGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
containingGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Star });
containingGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
Grid textStack = new Grid();
textStack.BackgroundColor = Colors.Transparent;
textStack.VerticalOptions = LayoutOptions.Center;
textStack.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
textStack.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
Image imageView = new Image();
imageView.SetBinding(Image.SourceProperty, static (SearchSuggestion suggestion) => suggestion.IsCollection, converter: CollectionIconConverter);
imageView.WidthRequest = 16;
imageView.HeightRequest = 16;
imageView.Margin = new Thickness(4);
imageView.VerticalOptions = LayoutOptions.Center;
Label titleLabel = new Label();
titleLabel.SetBinding(Label.TextProperty, static (SearchSuggestion suggestion) => suggestion.DisplayTitle);
titleLabel.VerticalOptions = LayoutOptions.End;
titleLabel.VerticalTextAlignment = TextAlignment.End;
titleLabel.SetAppThemeColor(Label.TextColorProperty, Color.FromArgb(FOREGROUND_LIGHT), Color.FromArgb(FOREGROUND_DARK));
Label subtitleLabel = new Label();
subtitleLabel.SetBinding(Label.TextProperty, static (SearchSuggestion suggestion) => suggestion.DisplaySubtitle);
subtitleLabel.SetBinding(Label.IsVisibleProperty, static (SearchSuggestion suggestion) => suggestion.DisplaySubtitle, converter: EmptyStringConverter);
subtitleLabel.VerticalOptions = LayoutOptions.Start;
subtitleLabel.VerticalTextAlignment = TextAlignment.Start;
subtitleLabel.SetAppThemeColor(Label.TextColorProperty, Color.FromArgb(FOREGROUND_LIGHT), Color.FromArgb(FOREGROUND_DARK));
textStack.Children.Add(titleLabel);
textStack.Children.Add(subtitleLabel);
Grid.SetRow(titleLabel, 0);
Grid.SetRow(subtitleLabel, 1);
containingGrid.Children.Add(imageView);
containingGrid.Children.Add(textStack);
Grid.SetColumn(textStack, 1);
Grid.SetColumn(imageView, 0);
return containingGrid;
});
DefaultResultTemplate = new DataTemplate(() =>
{
Grid containingGrid = new Grid();
containingGrid.Padding = new Thickness(2, 4, 2, 4);
containingGrid.SetAppThemeColor(Grid.BackgroundColorProperty, Color.FromArgb(BACKGROUND_LIGHT), Color.FromArgb(BACKGROUND_DARK));
containingGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
containingGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Star });
containingGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
Grid textStack = new Grid();
textStack.BackgroundColor = Colors.Transparent;
textStack.VerticalOptions = LayoutOptions.Center;
textStack.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
textStack.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
Image imageView = new Image();
imageView.SetBinding(Image.SourceProperty, static (SearchResult result) => result.MarkerImageData, converter: ImageSourceConverter);
imageView.WidthRequest = 24;
imageView.HeightRequest = 24;
imageView.Margin = new Thickness(4);
imageView.VerticalOptions = LayoutOptions.Center;
Label titleLabel = new Label();
titleLabel.SetBinding(Label.TextProperty, static (SearchResult result) => result.DisplayTitle);
titleLabel.FontAttributes = FontAttributes.Bold;
titleLabel.VerticalOptions = LayoutOptions.End;
titleLabel.VerticalTextAlignment = TextAlignment.End;
titleLabel.SetAppThemeColor(Label.TextColorProperty, Color.FromArgb(FOREGROUND_LIGHT), Color.FromArgb(FOREGROUND_DARK));
Label subtitleLabel = new Label();
subtitleLabel.SetBinding(Label.TextProperty, static (SearchResult result) => result.DisplaySubtitle);
subtitleLabel.SetBinding(Label.IsVisibleProperty, static (SearchResult result) => result.DisplaySubtitle, converter: EmptyStringConverter);
subtitleLabel.VerticalOptions = LayoutOptions.Start;
subtitleLabel.VerticalTextAlignment = TextAlignment.Start;
subtitleLabel.SetAppThemeColor(Label.TextColorProperty, Color.FromArgb(FOREGROUND_LIGHT), Color.FromArgb(FOREGROUND_DARK));
textStack.Children.Add(titleLabel);
textStack.Children.Add(subtitleLabel);
Grid.SetRow(titleLabel, 0);
Grid.SetRow(subtitleLabel, 1);
containingGrid.Children.Add(imageView);
containingGrid.Children.Add(textStack);
Grid.SetColumn(textStack, 1);
Grid.SetColumn(imageView, 0);
return containingGrid;
});
string template =
$@"<ControlTemplate xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
xmlns:esriTK=""clr-namespace:Esri.ArcGISRuntime.Toolkit.Maui"">
<Grid RowSpacing=""0"" ColumnSpacing=""0"">
<Grid.Resources>
<Style TargetType=""Grid"" x:Key=""SVDefaultGridStyle"">
<Setter Property=""Background"" Value=""{{AppThemeBinding Dark={BACKGROUND_DARK},Light={BACKGROUND_LIGHT}}}"" />
</Style>
<Style TargetType=""CollectionView"">
<Setter Property=""Background"" Value=""{{AppThemeBinding Dark={BACKGROUND_DARK},Light={BACKGROUND_LIGHT}}}"" />
</Style>
<Style TargetType=""Entry"">
<Setter Property=""Background"" Value=""{{AppThemeBinding Dark={BACKGROUND_DARK},Light={BACKGROUND_LIGHT}}}"" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""Auto"" />
<ColumnDefinition Width=""*"" />
<ColumnDefinition Width=""32"" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=""Auto"" />
<RowDefinition Height=""Auto"" />
<RowDefinition Height=""Auto"" />
</Grid.RowDefinitions>
<Grid Grid.Row=""0"" Grid.ColumnSpan=""3"" Style=""{{StaticResource SVDefaultGridStyle}}""/>
<ImageButton x:Name=""{nameof(PART_SourceSelectButton)}"" Grid.Column=""0"" WidthRequest=""32"" HeightRequest=""32"" Padding=""4"" BackgroundColor=""Transparent"" Margin=""0"" />
<Entry x:Name=""{nameof(PART_Entry)}"" Grid.Column=""1"" Grid.Row=""0"" TextColor=""{{AppThemeBinding Light={FOREGROUND_LIGHT}, Dark={FOREGROUND_DARK}}}"" />
<ImageButton x:Name=""{nameof(PART_CancelButton)}"" Grid.Column=""1"" HorizontalOptions=""End"" WidthRequest=""32"" HeightRequest=""32"" Padding=""4"" BackgroundColor=""Transparent"" />
<ImageButton x:Name=""{nameof(PART_SearchButton)}"" Grid.Column=""2"" WidthRequest=""32"" HeightRequest=""32"" Padding=""4"" BackgroundColor=""Transparent"" />
<Grid Grid.Column=""0"" Grid.ColumnSpan=""3"" Grid.Row=""1"" >
<CollectionView x:Name=""{nameof(PART_SuggestionsView)}"" SelectionMode=""Single"" Grid.RowSpan=""2"" HeightRequest=""175"" />
<CollectionView x:Name=""{nameof(PART_ResultView)}"" SelectionMode=""Single"" Grid.RowSpan=""1"" HeightRequest=""200"" />
<CollectionView x:Name=""{nameof(PART_SourcesView)}"" SelectionMode=""Single"" HeightRequest=""150"" />
<Grid x:Name=""{nameof(PART_ResultContainer)}"" Grid.ColumnSpan=""3"" Grid.Row=""1"" Padding=""8"" Style=""{{StaticResource SVDefaultGridStyle}}""><Label x:Name=""{nameof(PART_ResultLabel)}"" HorizontalOptions=""Center"" VerticalOptions=""Center"" FontAttributes=""Bold"" /></Grid>
</Grid>
<Grid x:Name=""{nameof(PART_RepeatButtonContainer)}"" Grid.Column=""0"" Grid.ColumnSpan=""3"" Grid.Row=""2"" Style=""{{StaticResource SVDefaultGridStyle}}"">
<Button x:Name=""{nameof(PART_RepeatButton)}"" BackgroundColor=""{{AppThemeBinding Light=#007AC2, Dark=#00619B}}"" TextColor=""White"" CornerRadius=""0"" />
</Grid>
</Grid>
</ControlTemplate>";
DefaultControlTemplate = Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(new ControlTemplate(), template);
}
}