2 using System.Reflection;
4 using Xamarin.Forms.Platform.Tizen.Native;
5 using EColor = ElmSharp.Color;
6 using EButton = ElmSharp.Button;
7 using EImage = ElmSharp.Image;
9 namespace Xamarin.Forms.Platform.Tizen
11 public class ShellNavBar : Native.Box
14 EButton _menuButton = null;
15 Native.Label _title = null;
16 Native.SearchBar _nativeSearchHandler = null;
17 EvasObject _nativeTitleView = null;
19 SearchHandler _searchHandler = null;
20 View _titleView = null;
23 IFlyoutController _flyoutController = null;
25 EColor _backgroudColor = ShellRenderer.DefaultBackgroundColor.ToNative();
26 EColor _foregroudColor = ShellRenderer.DefaultForegroundColor.ToNative();
28 // The source of icon resources is https://materialdesignicons.com/
29 const string _menuIcon = "XSF.Resources.menu.png";
30 const string _backIcon = "XSF.Resources.arrow_left.png";
32 bool _hasBackButton = false;
34 public ShellNavBar(IFlyoutController flyoutController) : base(Forms.NativeParent)
36 _flyoutController = flyoutController;
38 _menuButton = new EButton(Forms.NativeParent);
39 _menuButton.Clicked += OnMenuClicked;
40 _menu = new EImage(Forms.NativeParent);
45 _menuButton.SetPartContent("icon", _menu);
47 _title = new Native.Label(Forms.NativeParent)
49 FontSize = Device.Idiom == TargetIdiom.TV ? 60 : 23,
50 VerticalTextAlignment = Native.TextAlignment.Center,
51 TextColor = _backgroudColor,
52 FontAttributes = FontAttributes.Bold,
56 BackgroundColor = _backgroudColor;
57 _menuButton.BackgroundColor = _backgroudColor;
60 LayoutUpdated += OnLayoutUpdated;
63 public bool HasBackButton
67 return _hasBackButton;
71 _hasBackButton = value;
76 public SearchHandler SearchHandler
80 return _searchHandler;
84 _searchHandler = value;
85 UpdateSearchHandler(_searchHandler);
99 UpdateTitleView(_titleView);
112 if (string.IsNullOrEmpty(value))
123 public override EColor BackgroundColor
127 return _backgroudColor;
131 _backgroudColor = value;
132 _menuButton.BackgroundColor = _backgroudColor;
133 base.BackgroundColor = _backgroudColor;
137 public EColor ForegroundColor
141 return _foregroudColor;
145 _foregroudColor = value;
146 _menuButton.Color = value;
150 public EColor TitleColor
154 return _title.TextColor;
158 _title.TextColor = value;
162 internal Page CurrentPage
174 void UpdateMenuIcon()
176 string file = _hasBackButton ? _backIcon : _menuIcon;
177 var path = Assembly.GetExecutingAssembly().GetManifestResourceStream(file);
181 void OnMenuClicked(object sender, EventArgs e)
183 var backButtonHandler = Shell.GetBackButtonBehavior(_page);
184 if (backButtonHandler?.Command != null)
186 backButtonHandler.Command.Execute(backButtonHandler.CommandParameter);
188 else if (_hasBackButton)
190 Shell.Current.CurrentItem.Navigation.PopAsync();
194 _flyoutController.Open();
198 void UpdateTitleView(View titleView)
200 _nativeTitleView?.Unrealize();
201 _nativeTitleView = null;
203 if (titleView != null)
205 var renderer = Platform.GetOrCreateRenderer(titleView);
206 (renderer as LayoutRenderer)?.RegisterOnLayoutUpdated();
207 _nativeTitleView = renderer.NativeView;
208 PackEnd(_nativeTitleView);
212 void UpdateSearchHandler(SearchHandler handler)
214 _nativeSearchHandler?.Unrealize();
215 _nativeSearchHandler = null;
219 _nativeSearchHandler = new Native.SearchBar(Forms.NativeParent);
220 _nativeSearchHandler.IsSingleLine = true;
221 _nativeSearchHandler.BackgroundColor = ElmSharp.Color.White;
222 _nativeSearchHandler.Placeholder = handler.Placeholder;
223 _nativeSearchHandler.Show();
224 PackEnd(_nativeSearchHandler);
228 Native.Image GetSearchHandlerIcon(ImageSource source)
230 Native.Image _icon = new Native.Image(Forms.NativeParent);
233 var task = _icon.LoadFromImageSourceAsync(source);
238 void UpdateChildren()
240 if (_searchHandler != null)
242 _nativeSearchHandler.Show();
244 _nativeTitleView?.Hide();
246 else if (_titleView != null)
248 _nativeTitleView.Show();
250 _nativeSearchHandler?.Hide();
255 _nativeTitleView?.Hide();
256 _nativeSearchHandler?.Hide();
261 void OnLayoutUpdated(object sender, LayoutEventArgs e)
265 int titleLeftMargin = 40;
266 int titleViewTopMargin = 40;
268 _menuButton.Move(e.Geometry.X + menuMargin, e.Geometry.Y + (e.Geometry.Height - menuSize) / 2);
269 _menuButton.Resize(menuSize, menuSize);
271 if (_searchHandler != null)
273 _nativeSearchHandler.Move(e.Geometry.X + (menuSize + menuMargin * 2) + titleLeftMargin, e.Geometry.Y + (titleViewTopMargin / 2));
274 _nativeSearchHandler.Resize(e.Geometry.Width - (menuSize + menuMargin * 2) - titleLeftMargin - (titleViewTopMargin / 2), e.Geometry.Height - titleViewTopMargin);
276 else if (_titleView != null)
278 _nativeTitleView.Move(e.Geometry.X + (menuSize + menuMargin * 2) + titleLeftMargin, e.Geometry.Y + (titleViewTopMargin / 2));
279 _nativeTitleView.Resize(e.Geometry.Width - (menuSize + menuMargin * 2) - titleLeftMargin - (titleViewTopMargin / 2), e.Geometry.Height - titleViewTopMargin);
283 _title.Move(e.Geometry.X + (menuSize + menuMargin * 2) + titleLeftMargin, e.Geometry.Y);
284 _title.Resize(e.Geometry.Width - (menuSize + menuMargin) - titleLeftMargin, e.Geometry.Height);
288 void UpdatPageLayout()
290 OnLayoutUpdated(this, new LayoutEventArgs() { Geometry = Geometry });