forked from dotnet/maui
/
ShellNavBar.cs
388 lines (343 loc) · 8.28 KB
/
ShellNavBar.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using System;
using System.Reflection;
using ElmSharp;
using Microsoft.Extensions.DependencyInjection;
using Tizen.UIExtensions.ElmSharp;
using EBox = ElmSharp.Box;
using EColor = ElmSharp.Color;
using TButton = Tizen.UIExtensions.ElmSharp.Button;
using TImage = Tizen.UIExtensions.ElmSharp.Image;
using TLabel = Tizen.UIExtensions.ElmSharp.Label;
using TThemeConstants = Tizen.UIExtensions.ElmSharp.ThemeConstants;
namespace Microsoft.Maui.Controls.Platform
{
public class ShellNavBar : EBox, IFlyoutBehaviorObserver, IDisposable
{
TImage _menuIcon = null;
TButton _menuButton = null;
TLabel _title = null;
ShellSearchView _searchView = null;
EvasObject _nativeTitleView = null;
SearchHandler _searchHandler = null;
View _titleView = null;
Page _page = null;
FlyoutBehavior _flyoutBehavior = FlyoutBehavior.Flyout;
EColor _backgroudColor = ShellView.DefaultBackgroundColor;
EColor _foregroudColor = ShellView.DefaultForegroundColor;
EColor _titleColor = ShellView.DefaultTitleColor;
// The source of icon resources is https://materialdesignicons.com/
const string _menuIconRes = TThemeConstants.Shell.Resources.MenuIcon;
const string _backIconRes = TThemeConstants.Shell.Resources.BackIcon;
bool _hasBackButton = false;
private bool disposedValue;
bool _isTV = Device.Idiom == TargetIdiom.TV;
public ShellNavBar(IMauiContext context) : base(context?.Context?.BaseLayout)
{
MauiContext = context;
SetLayoutCallback(OnLayout);
_menuButton = new TButton(NativeParent);
_menuButton.Clicked += OnMenuClicked;
_menuIcon = new TImage(NativeParent);
UpdateMenuIcon();
_title = new TLabel(NativeParent)
{
FontSize = this.GetDefaultTitleFontSize(),
VerticalTextAlignment = (global::Tizen.UIExtensions.Common.TextAlignment)TextAlignment.Center,
TextColor = _titleColor.ToCommon(),
FontAttributes = Tizen.UIExtensions.Common.FontAttributes.Bold,
};
_title.Show();
BackgroundColor = _backgroudColor;
_menuButton.BackgroundColor = _backgroudColor;
PackEnd(_menuButton);
PackEnd(_title);
}
~ShellNavBar()
{
Dispose(false);
}
protected IMauiContext MauiContext { get; private set; }
protected EvasObject NativeParent
{
get => MauiContext?.Context?.BaseLayout;
}
public IShellController ShellController => Shell.Current;
public bool HasBackButton
{
get
{
return _hasBackButton;
}
set
{
_hasBackButton = value;
UpdateMenuIcon();
}
}
public FlyoutBehavior FlyoutBehavior
{
get => _flyoutBehavior;
set
{
if (_flyoutBehavior != value)
{
_flyoutBehavior = value;
UpdateMenuIcon();
}
}
}
public SearchHandler SearchHandler
{
get
{
return _searchHandler;
}
set
{
_searchHandler = value;
UpdateSearchHandler(_searchHandler);
UpdateChildren();
}
}
public View TitleView
{
get
{
return _titleView;
}
set
{
_titleView = value;
UpdateTitleView(_titleView);
UpdateChildren();
}
}
public string Title
{
get
{
return _title?.Text;
}
set
{
_title.Text = value;
}
}
public override EColor BackgroundColor
{
get
{
return _backgroudColor;
}
set
{
_backgroudColor = value;
_menuButton.BackgroundColor = _backgroudColor;
base.BackgroundColor = _backgroudColor;
}
}
public EColor ForegroundColor
{
get
{
return _foregroudColor;
}
set
{
_foregroudColor = value;
}
}
public EColor TitleColor
{
get
{
return _titleColor;
}
set
{
_titleColor = value;
_title.TextColor = value.ToCommon();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void SetPage(Page page)
{
_page = page;
Title = page.Title;
SearchHandler = Shell.GetSearchHandler(page);
TitleView = Shell.GetTitleView(page);
UpdateMenuIcon();
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Unrealize();
}
disposedValue = true;
}
}
async void UpdateMenuIcon()
{
ImageSource source = null;
if (HasBackButton)
{
if (_isTV)
{
_menuButton.Style = ThemeConstants.Button.Styles.Default;
_menuButton.Text = ThemeConstants.Shell.Resources.TV.BackIconCode;
_menuIcon = null;
}
else
{
var assembly = typeof(ShellNavBar).GetTypeInfo().Assembly;
var assemblyName = assembly.GetName().Name;
source = ImageSource.FromResource(assemblyName + "." + _backIconRes, assembly);
}
}
else if (_flyoutBehavior != FlyoutBehavior.Flyout)
{
_menuButton.Hide();
}
else if (ShellController.FlyoutIcon != null)
{
if (_isTV)
{
_menuButton.Style = ThemeConstants.Button.Styles.Circle;
_menuIcon = new TImage(NativeParent);
}
source = Shell.Current.FlyoutIcon;
}
else
{
if (_isTV)
{
_menuButton.Style = ThemeConstants.Button.Styles.Default;
_menuButton.Text = ThemeConstants.Shell.Resources.TV.MenuIconCode;
_menuIcon = null;
}
else
{
var assembly = typeof(ShellNavBar).GetTypeInfo().Assembly;
var assemblyName = assembly.GetName().Name;
source = ImageSource.FromResource(assemblyName + "." + _menuIconRes, assembly);
}
}
if (source != null && _menuIcon != null)
{
_menuIcon.Show();
var provider = MauiContext.Services.GetRequiredService<IImageSourceServiceProvider>();
var service = provider.GetRequiredImageSourceService(source);
await service.LoadImageAsync(source, _menuIcon);
}
_menuButton.SetIconPart(_menuIcon);
_menuButton.Show();
}
void OnMenuClicked(object sender, EventArgs e)
{
var backButtonHandler = Shell.GetBackButtonBehavior(_page);
if (backButtonHandler?.Command != null)
{
backButtonHandler.Command.Execute(backButtonHandler.CommandParameter);
}
else if (_hasBackButton)
{
Shell.Current.CurrentItem.Navigation.PopAsync();
}
else if (_flyoutBehavior == FlyoutBehavior.Flyout)
{
Shell.Current.FlyoutIsPresented = !Shell.Current.FlyoutIsPresented;
}
}
void UpdateTitleView(View titleView)
{
_nativeTitleView?.Unrealize();
_nativeTitleView = null;
if (titleView != null)
{
var _nativeTitleView = titleView.ToNative(MauiContext);
_nativeTitleView.Show();
PackEnd(_nativeTitleView);
}
}
void UpdateSearchHandler(SearchHandler handler)
{
if (_searchView != null)
{
UnPack(_searchView.NativeView);
_searchView.Dispose();
_searchView = null;
}
if (handler != null)
{
_searchView = new ShellSearchView(handler, MauiContext);
_searchView.NativeView.Show();
PackEnd(_searchView.NativeView);
}
}
void UpdateChildren()
{
if (_searchHandler != null)
{
_searchView.NativeView.Show();
_title?.Hide();
_nativeTitleView?.Hide();
}
else if (_titleView != null)
{
_nativeTitleView.Show();
_title?.Hide();
_searchView?.NativeView?.Hide();
}
else
{
_title.Show();
_nativeTitleView?.Hide();
_searchView?.NativeView?.Hide();
}
}
void OnLayout()
{
if (Geometry.Width == 0 || Geometry.Height == 0)
return;
int menuSize = DPExtensions.ConvertToScaledPixel(this.GetDefaultMenuSize());
int menuMargin = DPExtensions.ConvertToScaledPixel(this.GetDefaultMargin());
int titleHMargin = DPExtensions.ConvertToScaledPixel(this.GetDefaultMargin());
int titleVMargin = DPExtensions.ConvertToScaledPixel(this.GetDefaultTitleVMargin());
var bound = Geometry;
var menuBound = bound;
menuBound.X += menuMargin;
menuBound.Y += (menuBound.Height - menuSize) / 2;
menuBound.Width = menuSize;
menuBound.Height = menuSize;
_menuButton.Geometry = menuBound;
var contentBound = Geometry;
contentBound.X = menuBound.Right + titleHMargin;
contentBound.Y += titleVMargin;
contentBound.Width -= (menuBound.Width + menuMargin + titleHMargin * 2);
contentBound.Height -= titleVMargin * 2;
if (_searchView != null)
{
_searchView.NativeView.Geometry = contentBound;
}
else if (_titleView != null)
{
_nativeTitleView.Geometry = contentBound;
}
else
{
_title.Geometry = contentBound;
}
}
void IFlyoutBehaviorObserver.OnFlyoutBehaviorChanged(FlyoutBehavior behavior)
{
FlyoutBehavior = behavior;
}
}
}