Adjust size of dimmed image on subpanel button
[profile/tv/apps/dotnet/home.git] / TVHome / TVHome / Views / SubPanel.xaml.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd
3  *
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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  */
16
17 using System.ComponentModel;
18 using TVHome.Controls;
19 using LibTVRefCommonPortable.DataModels;
20 using Xamarin.Forms;
21 using System.Threading.Tasks;
22 using System.Windows.Input;
23 using System.Collections.Generic;
24 using LibTVRefCommonPortable.Utils;
25 using System;
26
27 namespace TVHome.Views
28 {
29     /// <summary>
30     /// Sub Panel in Main Page
31     /// </summary>
32     public partial class SubPanel : Panel
33     {
34         /// <summary>
35         /// A list of PanelButtons.
36         /// </summary>
37         private List<PanelButton> ButtonList;
38
39         /// <summary>
40         /// A list of Button's Views.
41         /// </summary>
42         private List<View> ButtonViewList;
43
44         /// <summary>
45         /// A Command will be executed the button is moved.
46         /// </summary>
47         public ICommand OnMoveCommand { get; set; }
48
49         /// <summary>
50         /// SubPanel icon's transition height value when it focused.
51         /// </summary>
52         private int selectTransitionHeight = SizeUtils.GetHeightSize(146);
53
54         public IList<View> GetSubPanelButtons()
55         {
56             return PanelButtonStack.Children;
57         }
58
59         /// <summary>
60         /// Constructor
61         /// </summary>
62         public SubPanel()
63         {
64             InitializeComponent();
65             isFocused = false;
66             isMoveMode = false;
67
68             ButtonList = new List<PanelButton>();
69             ButtonViewList = new List<View>();
70             PropertyChanged += OnItemsSourcePropertyChanged;
71
72             MessagingCenter.Subscribe<App>(this, "MenuKeyPressed", (sender) =>
73             {
74                 if (isFocused)
75                 {
76                     foreach (var item in ButtonList)
77                     {
78                         if (item.isFocused)
79                         {
80                             item.ShowContextPopup();
81                         }
82                     }
83                 }
84             });
85         }
86         /// <summary>
87         /// A event handler for handling property changed event
88         /// </summary>
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)
92         {
93             if (e.PropertyName != "ItemsSource")
94             {
95                 return;
96             }
97
98             PanelButtonStack.Children.Clear();
99             ButtonViewList.Clear();
100             ButtonList.Clear();
101             foreach (ShortcutInfo item in ItemsSource)
102             {
103                 PanelButton button;
104
105                 if (item is AppShortcutInfo)
106                 {
107                     if (item.StateDescriptions["default"].Label.Equals("All apps")
108                         || item.StateDescriptions["default"].Label.Equals("Add pin"))
109                     {
110                         button = new SubPanelAllAppsButton();
111                         ButtonList.Add(button);
112                     }
113                     else if (item.StateDescriptions["default"].Label.Equals("Media Hub"))
114                     {
115                         button = new SubPanelReservedButton();
116                         ButtonList.Add(button);
117                     }
118                     else
119                     {
120                         button = new SubPanelButton();
121                         button.OnMoveCommand = new Command<string>((direction) =>
122                         {
123                             int index = ButtonViewList.IndexOf(button.View);
124                             if (direction.Equals("Right"))
125                             {
126                                 MoveItemToRight(index);
127                             }
128                             else if (direction.Equals("Left"))
129                             {
130                                 MoveItemToLeft(index);
131                             }
132                         });
133
134                         button.OnMoveFinishedCommand = new Command(() =>
135                         {
136                             // TODO : This is triggered by LongTap event.
137                             isMoveMode = !isMoveMode;
138
139                             OnMoveCommand.Execute(isMoveMode);
140
141                             ChangeLayoutButtons(isMoveMode);
142                             ChangeIsEnabledProperty(button.View);
143                             button.ChangeMoveMode(isMoveMode, false);
144
145                             if (!isMoveMode)
146                             {
147                                 foreach (var viewItem in ButtonViewList)
148                                 {
149                                     viewItem.TranslateTo(SizeUtils.GetWidthSize((int)viewItem.TranslationX), 0, 0);
150                                 }
151                                 OnMoveVMCommand.Execute(ButtonViewList);
152                             }
153                         });
154                     }
155
156                     ButtonList.Add(button);
157                 }
158                 else
159                 {
160                     button = new SubPanelSettingButton();
161                 }
162
163                 button.View.BindingContext = item;
164                 button.OnFocusedCommand = new Command(() =>
165                 {
166                     FocusPanel();
167                 });
168                 button.OnClickedCommand = new Command(() =>
169                 {
170                     item.DoAction();
171                 });
172                 button.OnUnpinCommand = new Command(() =>
173                 {
174                     AppShortcutInfo shortcut = (AppShortcutInfo)button.View.BindingContext;
175                     OnUnpinVMCommand.Execute(shortcut.AppID);
176                 });
177
178                 PanelButtonStack.Children.Add(button.View);
179                 ButtonViewList.Add(button.View);
180             }
181
182             OnItemSourceChanged(EventArgs.Empty);
183
184             if (!isFocused)
185             {
186                 ShowPanel();
187             }
188             else
189             {
190                 isFocused = false;
191                 FocusPanel();
192             }
193         }
194
195         /// <summary>
196         /// A method is called when apps subpanel is changed to move mode and change item's IsEnabled property.
197         /// </summary>
198         /// <param name="selectedBtn">A selected button view to move</param>
199         private void ChangeIsEnabledProperty(View selectedBtn)
200         {
201             foreach (var item in PanelButtonStack.Children)
202             {
203                 if (item != selectedBtn)
204                 {
205                     item.IsEnabled = !isMoveMode;
206                 }
207             }
208         }
209
210         /// <summary>
211         /// A method for changing button's LayoutOptions in SubPanel according to parameter
212         /// </summary>
213         /// <param name="isMoveMode">A flag indicates whether the SubPanel is MoveMode or not</param>
214         private void ChangeLayoutButtons(bool isMoveMode)
215         {
216             foreach (var item in ButtonList)
217             {
218                 item.ChangeLayoutOptions(isMoveMode);
219             }
220         }
221
222         /// <summary>
223         /// A method for hiding the panel
224         /// </summary>
225         public override async void HidePanel()
226         {
227             isFocused = false;
228             foreach (var item in PanelButtonStack.Children)
229             {
230                 item.IsEnabled = false;
231             }
232
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);
238         }
239
240         /// <summary>
241         /// A method for showing the panel
242         /// </summary>
243         public override async void ShowPanel()
244         {
245             isFocused = false;
246             foreach (var item in PanelButtonStack.Children)
247             {
248                 item.IsEnabled = true;
249                 item.FindByName<Image>("ButtonDimmedImage").Opacity = 0.99;
250                 item.FindByName<Image>("ButtonDimmedImage").ScaleTo(1.0, 300);
251             }
252
253 #pragma warning disable CS4014
254             this.TranslateTo(0, 0, 300);
255 #pragma warning restore CS4014
256             await this.FadeTo(0.3, 300);
257         }
258
259         /// <summary>
260         /// A method for handling panel focused event
261         /// </summary>
262         public override async void FocusPanel()
263         {
264             if (isFocused)
265             {
266                 if (!isMoveMode)
267                 {
268                     OnFocusedCommand.Execute("");
269                 }
270
271                 return;
272             }
273
274             OnFocusedCommand.Execute("");
275
276             isFocused = true;
277             var button = PanelButtonStack.Children[1];
278             button.FindByName<Button>("ButtonFocusArea").Focus();
279
280             foreach (var item in PanelButtonStack.Children)
281             {
282                 var panelButton = item.FindByName<Button>("ButtonFocusArea");
283                 if (panelButton.IsFocused == true)
284                 {
285                     item.FindByName<Image>("ButtonDimmedImage").Scale = 1.3;
286                 }
287                 else
288                 {
289                     item.FindByName<Image>("ButtonDimmedImage").Scale = 1.0;
290                 }
291
292                 item.FindByName<Image>("ButtonDimmedImage").Opacity = 0;
293             }
294
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);
300         }
301
302         /// <summary>
303         /// A method is called when back button is pressed in move mode
304         /// </summary>
305         public void ChangeToDefaultMode()
306         {
307             if (isMoveMode)
308             {
309                 isMoveMode = !isMoveMode;
310
311                 foreach (var item in ButtonList)
312                 {
313                     if (item.isMoveMode)
314                     {
315                         ChangeIsEnabledProperty(item.View);
316                         item.ChangeMoveMode(isMoveMode, true);
317                     }
318                     else
319                     {
320                         item.View.TranslateTo(0, 0, 0);
321                     }
322                 }
323
324                 OnMoveCommand.Execute(isMoveMode);
325                 ChangeLayoutButtons(isMoveMode);
326
327                 ButtonViewList.Clear();
328                 foreach (var stackItem in PanelButtonStack.Children)
329                 {
330                     ButtonViewList.Add(stackItem);
331                 }
332             }
333         }
334
335         /// <summary>
336         /// A method for moving the selected item to right
337         /// </summary>
338         /// <param name="index">A index of item to be moved</param>
339         private void MoveItemToRight(int index)
340         {
341             View originItemView = ButtonViewList[index];
342             int nextIndex = index + 1;
343             if (nextIndex >= PanelButtonStack.Children.Count - 1)
344             {
345                 return;
346             }
347
348             View nextItemView = ButtonViewList[nextIndex];
349
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);
355
356             ButtonViewList[index] = nextItemView;
357             ButtonViewList[nextIndex] = originItemView;
358         }
359
360         /// <summary>
361         /// A method for moving the selected item to left
362         /// </summary>
363         /// <param name="index">A index of item to be moved</param>
364         private void MoveItemToLeft(int index)
365         {
366             View originItemView = ButtonViewList[index];
367             int prevIndex = index - 1;
368
369             if (prevIndex < 2)
370             {
371                 return;
372             }
373
374             View prevItemView = ButtonViewList[prevIndex];
375
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);
381
382             ButtonViewList[index] = prevItemView;
383             ButtonViewList[prevIndex] = originItemView;
384         }
385     }
386 }