aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.UI/MainWindow.xaml.cs
blob: d3307e9132e97206ba0b89a207780b6e24524aa5 (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
using MediaBrowser.Common.Extensions;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using MediaBrowser.UI.Controller;
using MediaBrowser.UI.Controls;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MediaBrowser.UI.Extensions;

namespace MediaBrowser.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : BaseWindow, IDisposable
    {
        /// <summary>
        /// Gets or sets the mouse idle timer.
        /// </summary>
        /// <value>The mouse idle timer.</value>
        private Timer MouseIdleTimer { get; set; }
        /// <summary>
        /// Gets or sets the backdrop timer.
        /// </summary>
        /// <value>The backdrop timer.</value>
        private Timer BackdropTimer { get; set; }
        /// <summary>
        /// Gets or sets the current backdrops.
        /// </summary>
        /// <value>The current backdrops.</value>
        private string[] CurrentBackdrops { get; set; }

        /// <summary>
        /// The _current backdrop index
        /// </summary>
        private int _currentBackdropIndex;
        /// <summary>
        /// Gets or sets the index of the current backdrop.
        /// </summary>
        /// <value>The index of the current backdrop.</value>
        public int CurrentBackdropIndex
        {
            get { return _currentBackdropIndex; }
            set
            {
                _currentBackdropIndex = value;
                OnPropertyChanged("CurrentBackdropIndex");
                Dispatcher.InvokeAsync(OnBackdropIndexChanged);
            }
        }

        /// <summary>
        /// The _is mouse idle
        /// </summary>
        private bool _isMouseIdle = true;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is mouse idle.
        /// </summary>
        /// <value><c>true</c> if this instance is mouse idle; otherwise, <c>false</c>.</value>
        public bool IsMouseIdle
        {
            get { return _isMouseIdle; }
            set
            {
                _isMouseIdle = value;

                Dispatcher.InvokeAsync(() => Cursor = value ? Cursors.None : Cursors.Arrow);

                OnPropertyChanged("IsMouseIdle");
            }
        }

        private readonly ILogger _logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow" /> class.
        /// </summary>
        public MainWindow(ILogger logger)
            : base()
        {
            _logger = logger;

            InitializeComponent();
        }

        /// <summary>
        /// Called when [loaded].
        /// </summary>
        protected override void OnLoaded()
        {
            base.OnLoaded();

            DragBar.MouseDown += DragableGridMouseDown;

            DataContext = App.Instance;
        }

        /// <summary>
        /// Loads the initial UI.
        /// </summary>
        /// <returns>Task.</returns>
        internal Task LoadInitialUI()
        {
            return LoadInitialPage();
        }

        /// <summary>
        /// Called when [backdrop index changed].
        /// </summary>
        private async void OnBackdropIndexChanged()
        {
            var currentBackdropIndex = CurrentBackdropIndex;

            if (currentBackdropIndex == -1  )
            {
                // Setting this to null doesn't seem to clear out the content
                // Have to check it for null or get startup errors
                if (BackdropContainer.Content != null)
                {
                    BackdropContainer.Content = new FrameworkElement();
                }
                return;
            }

            try
            {
                var bitmap = await App.Instance.GetRemoteBitmapAsync(CurrentBackdrops[currentBackdropIndex]);

                var img = new Image
                {
                    Source = bitmap
                };

                img.SetResourceReference(StyleProperty, "BackdropImage");

                BackdropContainer.Content = img;
            }
            catch (HttpException)
            {
                if (currentBackdropIndex == 0)
                {
                    BackdropContainer.Content = new FrameworkElement();
                }
            }
        }

        /// <summary>
        /// Loads the initial page.
        /// </summary>
        /// <returns>Task.</returns>
        private Task LoadInitialPage()
        {
            return App.Instance.LogoutUser();
        }

        /// <summary>
        /// Dragables the grid mouse down.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="MouseButtonEventArgs" /> instance containing the event data.</param>
        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();
            }
        }

        /// <summary>
        /// Gets the page frame.
        /// </summary>
        /// <value>The page frame.</value>
        private TransitionFrame PageFrame
        {
            get
            {
                // Finding the grid that is generated by the ControlTemplate of the Button
                return TreeHelper.FindChild<TransitionFrame>(PageContent, "PageFrame");
            }
        }

        /// <summary>
        /// Navigates the specified page.
        /// </summary>
        /// <param name="page">The page.</param>
        internal void Navigate(Page page)
        {
            _logger.Info("Navigating to " + page.GetType().Name);
            
            Dispatcher.InvokeAsync(() => PageFrame.NavigateWithTransition(page));
        }

        /// <summary>
        /// Sets the backdrop based on a BaseItemDto
        /// </summary>
        /// <param name="item">The item.</param>
        public void SetBackdrops(BaseItemDto item)
        {
            var urls = App.Instance.ApiClient.GetBackdropImageUrls(item, new ImageOptions
            {
                MaxWidth = Convert.ToInt32(SystemParameters.VirtualScreenWidth),
                MaxHeight = Convert.ToInt32(SystemParameters.VirtualScreenHeight)
            });

            SetBackdrops(urls);
        }

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

            DisposeBackdropTimer();
            CurrentBackdrops = backdrops;

            if (backdrops == null || backdrops.Length == 0)
            {
                CurrentBackdropIndex = -1;

                // Setting this to null doesn't seem to clear out the content
                // Have to check it for null or get startup errors
                if (BackdropContainer.Content != null)
                {
                    BackdropContainer.Content = new FrameworkElement();
                }
                return;
            }

            CurrentBackdropIndex = 0;

            // We only need the timer if there's more than one backdrop
            if (backdrops != null && backdrops.Length > 1)
            {
                BackdropTimer = new Timer(state =>
                {
                    // Don't display backdrops during video playback
                    if (UIKernel.Instance.PlaybackManager.ActivePlayers.Any(p => p.CurrentMedia.IsVideo))
                    {
                        return;
                    }

                    var index = CurrentBackdropIndex + 1;

                    if (index >= backdrops.Length)
                    {
                        index = 0;
                    }

                    CurrentBackdropIndex = index;

                }, null, 5000, 5000);
            }
        }

        /// <summary>
        /// Disposes the backdrop timer.
        /// </summary>
        public void DisposeBackdropTimer()
        {
            if (BackdropTimer != null)
            {
                BackdropTimer.Dispose();
            }
        }

        /// <summary>
        /// Disposes the mouse idle timer.
        /// </summary>
        public void DisposeMouseIdleTimer()
        {
            if (MouseIdleTimer != null)
            {
                MouseIdleTimer.Dispose();
            }
        }

        /// <summary>
        /// Clears the backdrops.
        /// </summary>
        public void ClearBackdrops()
        {
            SetBackdrops(new string[] { });
        }

        /// <summary>
        /// Navigates the back.
        /// </summary>
        public void NavigateBack()
        {
            Dispatcher.InvokeAsync(() =>
            {
                if (PageFrame.NavigationService.CanGoBack)
                {
                    PageFrame.GoBackWithTransition();
                }
            });
        }

        /// <summary>
        /// Navigates the forward.
        /// </summary>
        public void NavigateForward()
        {
            Dispatcher.InvokeAsync(() =>
            {
                if (PageFrame.NavigationService.CanGoForward)
                {
                    PageFrame.GoForwardWithTransition();
                }
            });
        }

        /// <summary>
        /// Called when [browser back].
        /// </summary>
        protected override void OnBrowserBack()
        {
            base.OnBrowserBack();

            NavigateBack();
        }

        /// <summary>
        /// Called when [browser forward].
        /// </summary>
        protected override void OnBrowserForward()
        {
            base.OnBrowserForward();

            NavigateForward();
        }

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

            const int duration = 4000;

            // 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>
        /// <param name="stateInfo">The state info.</param>
        private void MouseIdleTimerCallback(object stateInfo)
        {
            IsMouseIdle = true;

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

        /// <summary>
        /// The _last mouse move point
        /// </summary>
        private Point _lastMouseMovePoint;

        /// <summary>
        /// Handles OnMouseMove to show the control box
        /// </summary>
        /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs" /> that contains the event data.</param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            // Store the last position for comparison purposes
            // Even if the mouse is not moving this event will fire as elements are showing and hiding
            var pos = e.GetPosition(this);

            if (pos == _lastMouseMovePoint)
            {
                return;
            }

            _lastMouseMovePoint = pos;

            StartMouseIdleTimer();
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            DisposeBackdropTimer();
            DisposeMouseIdleTimer();
        }

        /// <summary>
        /// Shows a notification message that will disappear on it's own
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="icon">The icon.</param>
        public void ShowNotificationMessage(string text, string caption = null, MessageBoxIcon icon = MessageBoxIcon.None)
        {
            var control = new NotificationMessage
            {
                Caption = caption,
                Text = text,
                MessageBoxImage = icon
            };

            mainGrid.Children.Add(control);

            Dispatcher.InvokeWithDelay(() => mainGrid.Children.Remove(control), 5000);
        }

        /// <summary>
        /// Shows a notification message that will disappear on it's own
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="icon">The icon.</param>
        public void ShowNotificationMessage(UIElement text, string caption = null, MessageBoxIcon icon = MessageBoxIcon.None)
        {
            var control = new NotificationMessage
            {
                Caption = caption,
                TextContent = text,
                MessageBoxImage = icon
            };

            mainGrid.Children.Add(control);

            Dispatcher.InvokeWithDelay(() => mainGrid.Children.Remove(control), 5000);
        }

        /// <summary>
        /// Shows a modal message box and asynchronously returns a MessageBoxResult
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="button">The button.</param>
        /// <param name="icon">The icon.</param>
        /// <returns>MessageBoxResult.</returns>
        public MessageBoxResult ShowModalMessage(string text, string caption = null, MessageBoxButton button = MessageBoxButton.OK, MessageBoxIcon icon = MessageBoxIcon.None)
        {
            var win = new ModalWindow
            {
                Caption = caption,
                Button = button,
                MessageBoxImage = icon,
                Text = text
            };

            win.ShowModal(this);

            return win.MessageBoxResult;
        }

        /// <summary>
        /// Shows a modal message box and asynchronously returns a MessageBoxResult
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="button">The button.</param>
        /// <param name="icon">The icon.</param>
        /// <returns>MessageBoxResult.</returns>
        public MessageBoxResult ShowModalMessage(UIElement text, string caption = null, MessageBoxButton button = MessageBoxButton.OK, MessageBoxIcon icon = MessageBoxIcon.None)
        {
            var win = new ModalWindow
            {
                Caption = caption,
                Button = button,
                MessageBoxImage = icon,
                TextContent = text
            };

            win.ShowModal(this);

            return win.MessageBoxResult;
        }
    }
}