aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.UI/MainWindow.xaml.cs
blob: 07e8e9433190197785f8456d18a2dbda4dea06ff (plain)
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
using MediaBrowser.Model.DTO;
using MediaBrowser.UI.Controller;
using MediaBrowser.UI.Controls;
using System;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace MediaBrowser.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private Timer MouseIdleTimer { get; set; }
        private Timer BackdropTimer { get; set; }
        private Image BackdropImage { get; set; }
        private string[] CurrentBackdrops { get; set; }
        private int CurrentBackdropIndex { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            BackButton.Click += BtnApplicationBackClick;
            ExitButton.Click += ExitButtonClick;
            ForwardButton.Click += ForwardButtonClick;
            DragBar.MouseDown += DragableGridMouseDown;
            Loaded += MainWindowLoaded;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        private bool _isMouseIdle = true;
        public bool IsMouseIdle
        {
            get { return _isMouseIdle; }
            set
            {
                _isMouseIdle = value;
                OnPropertyChanged("IsMouseIdle");
            }
        }

        void MainWindowLoaded(object sender, RoutedEventArgs e)
        {
            DataContext = App.Instance;

            if (App.Instance.ServerConfiguration == null)
            {
                App.Instance.PropertyChanged += ApplicationPropertyChanged;
            }
            else
            {
                LoadInitialPage();
            }
        }

        void ForwardButtonClick(object sender, RoutedEventArgs e)
        {
            NavigateForward();
        }

        void ExitButtonClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        void ApplicationPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("ServerConfiguration"))
            {
                App.Instance.PropertyChanged -= ApplicationPropertyChanged;
                LoadInitialPage();
            }
        }

        private async void LoadInitialPage()
        {
            await App.Instance.LogoutUser().ConfigureAwait(false);
        }

        private void DragableGridMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
            }
            else if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        void BtnApplicationBackClick(object sender, RoutedEventArgs e)
        {
            NavigateBack();
        }

        private Frame PageFrame
        {
            get
            {
                // Finding the grid that is generated by the ControlTemplate of the Button
                return TreeHelper.FindChild<Frame>(PageContent, "PageFrame");
            }
        }

        public void Navigate(Uri uri)
        {
            PageFrame.Navigate(uri);
        }

        /// <summary>
        /// Sets the backdrop based on an ApiBaseItemWrapper
        /// </summary>
        public void SetBackdrops(DtoBaseItem item)
        {
            SetBackdrops(UIKernel.Instance.ApiClient.GetBackdropImageUrls(item, null, null, 1920, 1080));
        }

        /// <summary>
        /// Sets the backdrop based on a list of image files
        /// </summary>
        public async void SetBackdrops(string[] backdrops)
        {
            // Don't reload the same backdrops
            if (CurrentBackdrops != null && backdrops.SequenceEqual(CurrentBackdrops))
            {
                return;
            }

            if (BackdropTimer != null)
            {
                BackdropTimer.Dispose();
            }

            BackdropGrid.Children.Clear();

            if (backdrops.Length == 0)
            {
                CurrentBackdrops = null;
                return;
            }

            CurrentBackdropIndex = GetFirstBackdropIndex();

            Image image = await App.Instance.GetImage(backdrops.ElementAt(CurrentBackdropIndex));
            image.SetResourceReference(Image.StyleProperty, "BackdropImage");

            BackdropGrid.Children.Add(image);

            CurrentBackdrops = backdrops;
            BackdropImage = image;

            const int backdropRotationTime = 7000;

            if (backdrops.Count() > 1)
            {
                BackdropTimer = new Timer(BackdropTimerCallback, null, backdropRotationTime, backdropRotationTime);
            }
        }

        public void ClearBackdrops()
        {
            if (BackdropTimer != null)
            {
                BackdropTimer.Dispose();
            }

            BackdropGrid.Children.Clear();

            CurrentBackdrops = null;
        }

        private void BackdropTimerCallback(object stateInfo)
        {
            // Need to do this on the UI thread
            Application.Current.Dispatcher.InvokeAsync(() =>
            {
                var animFadeOut = new Storyboard();
                animFadeOut.Completed += AnimFadeOutCompleted;

                var fadeOut = new DoubleAnimation();
                fadeOut.From = 1.0;
                fadeOut.To = 0.5;
                fadeOut.Duration = new Duration(TimeSpan.FromSeconds(1));

                animFadeOut.Children.Add(fadeOut);
                Storyboard.SetTarget(fadeOut, BackdropImage);
                Storyboard.SetTargetProperty(fadeOut, new PropertyPath(Image.OpacityProperty));

                animFadeOut.Begin(this);
            });
        }

        async void AnimFadeOutCompleted(object sender, System.EventArgs e)
        {
            if (CurrentBackdrops == null)
            {
                return;
            }

            int backdropIndex = GetNextBackdropIndex();

            BitmapImage image = await App.Instance.GetBitmapImage(CurrentBackdrops[backdropIndex]);
            CurrentBackdropIndex = backdropIndex;

            // Need to do this on the UI thread
            BackdropImage.Source = image;
            Storyboard imageFadeIn = new Storyboard();

            DoubleAnimation fadeIn = new DoubleAnimation();

            fadeIn.From = 0.25;
            fadeIn.To = 1.0;
            fadeIn.Duration = new Duration(TimeSpan.FromSeconds(1));

            imageFadeIn.Children.Add(fadeIn);
            Storyboard.SetTarget(fadeIn, BackdropImage);
            Storyboard.SetTargetProperty(fadeIn, new PropertyPath(Image.OpacityProperty));
            imageFadeIn.Begin(this);
        }

        private int GetFirstBackdropIndex()
        {
            return 0;
        }

        private int GetNextBackdropIndex()
        {
            if (CurrentBackdropIndex < CurrentBackdrops.Length - 1)
            {
                return CurrentBackdropIndex + 1;
            }

            return 0;
        }

        public void NavigateBack()
        {
            if (PageFrame.NavigationService.CanGoBack)
            {
                PageFrame.NavigationService.GoBack();
            }
        }

        public void NavigateForward()
        {
            if (PageFrame.NavigationService.CanGoForward)
            {
                PageFrame.NavigationService.GoForward();
            }
        }

        /// <summary>
        /// Shows the control bar then starts a timer to hide it
        /// </summary>
        private void StartMouseIdleTimer()
        {
            IsMouseIdle = false;

            const int duration = 10000;

            // Start the timer if it's null, otherwise reset it
            if (MouseIdleTimer == null)
            {
                MouseIdleTimer = new Timer(MouseIdleTimerCallback, null, duration, Timeout.Infinite);
            }
            else
            {
                MouseIdleTimer.Change(duration, Timeout.Infinite);
            }
        }

        /// <summary>
        /// This is the Timer callback method to hide the control bar
        /// </summary>
        private void MouseIdleTimerCallback(object stateInfo)
        {
            IsMouseIdle = true;
            
            if (MouseIdleTimer != null)
            {
                MouseIdleTimer.Dispose();
                MouseIdleTimer = null;
            }
        }

        /// <summary>
        /// Handles OnMouseMove to show the control box
        /// </summary>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            StartMouseIdleTimer();
        }

        /// <summary>
        /// Handles OnKeyUp to provide keyboard based navigation
        /// </summary>
        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);

            if (IsBackPress(e))
            {
                NavigateBack();
            }

            else if (IsForwardPress(e))
            {
                NavigateForward();
            }
        }

        /// <summary>
        /// Determines if a keypress should be treated as a backward press
        /// </summary>
        private bool IsBackPress(KeyEventArgs e)
        {
            if (e.Key == Key.BrowserBack || e.Key == Key.Back)
            {
                return true;
            }

            if (e.SystemKey == Key.Left && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Determines if a keypress should be treated as a forward press
        /// </summary>
        private bool IsForwardPress(KeyEventArgs e)
        {
            if (e.Key == Key.BrowserForward)
            {
                return true;
            }

            if (e.SystemKey == Key.RightAlt && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt))
            {
                return true;
            }

            return false;
        }
    }
}