2 * Copyright (c) 2017 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System.ComponentModel;
18 using TVHome.Controls;
19 using LibTVRefCommonPortable.DataModels;
21 using System.Threading.Tasks;
22 using System.Windows.Input;
23 using System.Collections.Generic;
24 using LibTVRefCommonPortable.Utils;
27 namespace TVHome.Views
30 /// Sub Panel in Main Page
32 public partial class SubPanel : Panel
35 /// A list of PanelButtons.
37 private List<PanelButton> ButtonList;
40 /// A list of Button's Views.
42 private List<View> ButtonViewList;
45 /// A Command will be executed the button is moved.
47 public ICommand OnMoveCommand { get; set; }
50 /// SubPanel icon's transition height value when it focused.
52 private int selectTransitionHeight = SizeUtils.GetHeightSize(146);
54 public IList<View> GetSubPanelButtons()
56 return PanelButtonStack.Children;
64 InitializeComponent();
68 ButtonList = new List<PanelButton>();
69 ButtonViewList = new List<View>();
70 PropertyChanged += OnItemsSourcePropertyChanged;
72 MessagingCenter.Subscribe<App>(this, "MenuKeyPressed", (sender) =>
76 foreach (var item in ButtonList)
80 item.ShowContextPopup();
87 /// A event handler for handling property changed event
89 /// <param name="sender">A source of event</param>
90 /// <param name="e">The event that is occurred when property is changed</param>
91 private void OnItemsSourcePropertyChanged(object sender, PropertyChangedEventArgs e)
93 if (e.PropertyName != "ItemsSource")
98 PanelButtonStack.Children.Clear();
99 ButtonViewList.Clear();
101 foreach (ShortcutInfo item in ItemsSource)
105 if (item is AppShortcutInfo)
107 if (item.StateDescriptions["default"].Label.Equals("All apps")
108 || item.StateDescriptions["default"].Label.Equals("Add pin"))
110 button = new SubPanelAllAppsButton();
111 ButtonList.Add(button);
113 else if (item.StateDescriptions["default"].Label.Equals("Media Hub"))
115 button = new SubPanelReservedButton();
116 ButtonList.Add(button);
120 button = new SubPanelButton();
121 button.OnMoveCommand = new Command<string>((direction) =>
123 int index = ButtonViewList.IndexOf(button.View);
124 if (direction.Equals("Right"))
126 MoveItemToRight(index);
128 else if (direction.Equals("Left"))
130 MoveItemToLeft(index);
134 button.OnMoveFinishedCommand = new Command(() =>
136 // TODO : This is triggered by LongTap event.
137 isMoveMode = !isMoveMode;
139 OnMoveCommand.Execute(isMoveMode);
141 ChangeLayoutButtons(isMoveMode);
142 ChangeIsEnabledProperty(button.View);
143 button.ChangeMoveMode(isMoveMode, false);
147 foreach (var viewItem in ButtonViewList)
149 viewItem.TranslateTo(SizeUtils.GetWidthSize((int)viewItem.TranslationX), 0, 0);
151 OnMoveVMCommand.Execute(ButtonViewList);
156 ButtonList.Add(button);
160 button = new SubPanelSettingButton();
163 button.View.BindingContext = item;
164 button.OnFocusedCommand = new Command(() =>
168 button.OnClickedCommand = new Command(() =>
172 button.OnUnpinCommand = new Command(() =>
174 AppShortcutInfo shortcut = (AppShortcutInfo)button.View.BindingContext;
175 OnUnpinVMCommand.Execute(shortcut.AppID);
178 PanelButtonStack.Children.Add(button.View);
179 ButtonViewList.Add(button.View);
182 OnItemSourceChanged(EventArgs.Empty);
196 /// A method is called when apps subpanel is changed to move mode and change item's IsEnabled property.
198 /// <param name="selectedBtn">A selected button view to move</param>
199 private void ChangeIsEnabledProperty(View selectedBtn)
201 foreach (var item in PanelButtonStack.Children)
203 if (item != selectedBtn)
205 item.IsEnabled = !isMoveMode;
211 /// A method for changing button's LayoutOptions in SubPanel according to parameter
213 /// <param name="isMoveMode">A flag indicates whether the SubPanel is MoveMode or not</param>
214 private void ChangeLayoutButtons(bool isMoveMode)
216 foreach (var item in ButtonList)
218 item.ChangeLayoutOptions(isMoveMode);
223 /// A method for hiding the panel
225 public override async void HidePanel()
228 foreach (var item in PanelButtonStack.Children)
230 item.IsEnabled = false;
233 #pragma warning disable CS4014
234 PanelScrollView.ScrollToAsync(0, 0, true);
235 this.TranslateTo(0, selectTransitionHeight, 300);
236 #pragma warning restore CS4014
237 await this.FadeTo(0, 300);
241 /// A method for showing the panel
243 public override async void ShowPanel()
246 foreach (var item in PanelButtonStack.Children)
248 item.IsEnabled = true;
249 item.FindByName<Image>("ButtonDimmedImage").Opacity = 0.99;
250 item.FindByName<Image>("ButtonDimmedImage").ScaleTo(1.0, 300);
253 #pragma warning disable CS4014
254 this.TranslateTo(0, 0, 300);
255 #pragma warning restore CS4014
256 await this.FadeTo(0.3, 300);
260 /// A method for handling panel focused event
262 public override async void FocusPanel()
268 OnFocusedCommand.Execute("");
274 OnFocusedCommand.Execute("");
277 var button = PanelButtonStack.Children[1];
278 button.FindByName<Button>("ButtonFocusArea").Focus();
280 foreach (var item in PanelButtonStack.Children)
282 var panelButton = item.FindByName<Button>("ButtonFocusArea");
283 if (panelButton.IsFocused == true)
285 item.FindByName<Image>("ButtonDimmedImage").Scale = 1.3;
289 item.FindByName<Image>("ButtonDimmedImage").Scale = 1.0;
292 item.FindByName<Image>("ButtonDimmedImage").Opacity = 0;
295 await Task.Delay(300);
296 #pragma warning disable CS4014
297 this.TranslateTo(0, -selectTransitionHeight, 300);
298 #pragma warning restore CS4014
299 await this.FadeTo(0.99, 300);
303 /// A method is called when back button is pressed in move mode
305 public void ChangeToDefaultMode()
309 isMoveMode = !isMoveMode;
311 foreach (var item in ButtonList)
315 ChangeIsEnabledProperty(item.View);
316 item.ChangeMoveMode(isMoveMode, true);
320 item.View.TranslateTo(0, 0, 0);
324 OnMoveCommand.Execute(isMoveMode);
325 ChangeLayoutButtons(isMoveMode);
327 ButtonViewList.Clear();
328 foreach (var stackItem in PanelButtonStack.Children)
330 ButtonViewList.Add(stackItem);
336 /// A method for moving the selected item to right
338 /// <param name="index">A index of item to be moved</param>
339 private void MoveItemToRight(int index)
341 View originItemView = ButtonViewList[index];
342 int nextIndex = index + 1;
343 if (nextIndex >= PanelButtonStack.Children.Count - 1)
348 View nextItemView = ButtonViewList[nextIndex];
350 int translateX = SizeUtils.GetWidthSize((int)originItemView.TranslationX + 216);
351 int translateY = SizeUtils.GetWidthSize((int)originItemView.TranslationY);
352 originItemView.TranslateTo(translateX, translateY, 300);
353 translateX = SizeUtils.GetWidthSize((int)nextItemView.TranslationX - 216);
354 nextItemView.TranslateTo(translateX, 0, 300);
356 ButtonViewList[index] = nextItemView;
357 ButtonViewList[nextIndex] = originItemView;
361 /// A method for moving the selected item to left
363 /// <param name="index">A index of item to be moved</param>
364 private void MoveItemToLeft(int index)
366 View originItemView = ButtonViewList[index];
367 int prevIndex = index - 1;
374 View prevItemView = ButtonViewList[prevIndex];
376 int translateX = SizeUtils.GetWidthSize((int)originItemView.TranslationX - 216);
377 int translateY = SizeUtils.GetWidthSize((int)originItemView.TranslationY);
378 originItemView.TranslateTo(translateX, translateY, 300);
379 translateX = SizeUtils.GetWidthSize((int)prevItemView.TranslationX + 216);
380 prevItemView.TranslateTo(translateX, 0, 300);
382 ButtonViewList[index] = prevItemView;
383 ButtonViewList[prevIndex] = originItemView;