MainWindow.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Model.Dto;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Model.Net;
  5. using MediaBrowser.UI.Controller;
  6. using MediaBrowser.UI.Controls;
  7. using System;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. using MediaBrowser.UI.Extensions;
  15. namespace MediaBrowser.UI
  16. {
  17. /// <summary>
  18. /// Interaction logic for MainWindow.xaml
  19. /// </summary>
  20. public partial class MainWindow : BaseWindow, IDisposable
  21. {
  22. /// <summary>
  23. /// Gets or sets the mouse idle timer.
  24. /// </summary>
  25. /// <value>The mouse idle timer.</value>
  26. private Timer MouseIdleTimer { get; set; }
  27. /// <summary>
  28. /// Gets or sets the backdrop timer.
  29. /// </summary>
  30. /// <value>The backdrop timer.</value>
  31. private Timer BackdropTimer { get; set; }
  32. /// <summary>
  33. /// Gets or sets the current backdrops.
  34. /// </summary>
  35. /// <value>The current backdrops.</value>
  36. private string[] CurrentBackdrops { get; set; }
  37. /// <summary>
  38. /// The _current backdrop index
  39. /// </summary>
  40. private int _currentBackdropIndex;
  41. /// <summary>
  42. /// Gets or sets the index of the current backdrop.
  43. /// </summary>
  44. /// <value>The index of the current backdrop.</value>
  45. public int CurrentBackdropIndex
  46. {
  47. get { return _currentBackdropIndex; }
  48. set
  49. {
  50. _currentBackdropIndex = value;
  51. OnPropertyChanged("CurrentBackdropIndex");
  52. Dispatcher.InvokeAsync(OnBackdropIndexChanged);
  53. }
  54. }
  55. /// <summary>
  56. /// The _is mouse idle
  57. /// </summary>
  58. private bool _isMouseIdle = true;
  59. /// <summary>
  60. /// Gets or sets a value indicating whether this instance is mouse idle.
  61. /// </summary>
  62. /// <value><c>true</c> if this instance is mouse idle; otherwise, <c>false</c>.</value>
  63. public bool IsMouseIdle
  64. {
  65. get { return _isMouseIdle; }
  66. set
  67. {
  68. _isMouseIdle = value;
  69. Dispatcher.InvokeAsync(() => Cursor = value ? Cursors.None : Cursors.Arrow);
  70. OnPropertyChanged("IsMouseIdle");
  71. }
  72. }
  73. private readonly ILogger _logger;
  74. /// <summary>
  75. /// Initializes a new instance of the <see cref="MainWindow" /> class.
  76. /// </summary>
  77. public MainWindow(ILogger logger)
  78. : base()
  79. {
  80. _logger = logger;
  81. InitializeComponent();
  82. }
  83. /// <summary>
  84. /// Called when [loaded].
  85. /// </summary>
  86. protected override void OnLoaded()
  87. {
  88. base.OnLoaded();
  89. DragBar.MouseDown += DragableGridMouseDown;
  90. DataContext = App.Instance;
  91. }
  92. /// <summary>
  93. /// Loads the initial UI.
  94. /// </summary>
  95. /// <returns>Task.</returns>
  96. internal Task LoadInitialUI()
  97. {
  98. return LoadInitialPage();
  99. }
  100. /// <summary>
  101. /// Called when [backdrop index changed].
  102. /// </summary>
  103. private async void OnBackdropIndexChanged()
  104. {
  105. var currentBackdropIndex = CurrentBackdropIndex;
  106. if (currentBackdropIndex == -1 )
  107. {
  108. // Setting this to null doesn't seem to clear out the content
  109. // Have to check it for null or get startup errors
  110. if (BackdropContainer.Content != null)
  111. {
  112. BackdropContainer.Content = new FrameworkElement();
  113. }
  114. return;
  115. }
  116. try
  117. {
  118. var bitmap = await App.Instance.GetRemoteBitmapAsync(CurrentBackdrops[currentBackdropIndex]);
  119. var img = new Image
  120. {
  121. Source = bitmap
  122. };
  123. img.SetResourceReference(StyleProperty, "BackdropImage");
  124. BackdropContainer.Content = img;
  125. }
  126. catch (HttpException)
  127. {
  128. if (currentBackdropIndex == 0)
  129. {
  130. BackdropContainer.Content = new FrameworkElement();
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// Loads the initial page.
  136. /// </summary>
  137. /// <returns>Task.</returns>
  138. private Task LoadInitialPage()
  139. {
  140. return App.Instance.LogoutUser();
  141. }
  142. /// <summary>
  143. /// Dragables the grid mouse down.
  144. /// </summary>
  145. /// <param name="sender">The sender.</param>
  146. /// <param name="e">The <see cref="MouseButtonEventArgs" /> instance containing the event data.</param>
  147. private void DragableGridMouseDown(object sender, MouseButtonEventArgs e)
  148. {
  149. if (e.ClickCount == 2)
  150. {
  151. WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
  152. }
  153. else if (e.LeftButton == MouseButtonState.Pressed)
  154. {
  155. DragMove();
  156. }
  157. }
  158. /// <summary>
  159. /// Gets the page frame.
  160. /// </summary>
  161. /// <value>The page frame.</value>
  162. private TransitionFrame PageFrame
  163. {
  164. get
  165. {
  166. // Finding the grid that is generated by the ControlTemplate of the Button
  167. return TreeHelper.FindChild<TransitionFrame>(PageContent, "PageFrame");
  168. }
  169. }
  170. /// <summary>
  171. /// Navigates the specified page.
  172. /// </summary>
  173. /// <param name="page">The page.</param>
  174. internal void Navigate(Page page)
  175. {
  176. _logger.Info("Navigating to " + page.GetType().Name);
  177. Dispatcher.InvokeAsync(() => PageFrame.NavigateWithTransition(page));
  178. }
  179. /// <summary>
  180. /// Sets the backdrop based on a BaseItemDto
  181. /// </summary>
  182. /// <param name="item">The item.</param>
  183. public void SetBackdrops(BaseItemDto item)
  184. {
  185. var urls = App.Instance.ApiClient.GetBackdropImageUrls(item, new ImageOptions
  186. {
  187. MaxWidth = Convert.ToInt32(SystemParameters.VirtualScreenWidth),
  188. MaxHeight = Convert.ToInt32(SystemParameters.VirtualScreenHeight)
  189. });
  190. SetBackdrops(urls);
  191. }
  192. /// <summary>
  193. /// Sets the backdrop based on a list of image files
  194. /// </summary>
  195. /// <param name="backdrops">The backdrops.</param>
  196. public void SetBackdrops(string[] backdrops)
  197. {
  198. // Don't reload the same backdrops
  199. if (CurrentBackdrops != null && backdrops.SequenceEqual(CurrentBackdrops))
  200. {
  201. return;
  202. }
  203. DisposeBackdropTimer();
  204. CurrentBackdrops = backdrops;
  205. if (backdrops == null || backdrops.Length == 0)
  206. {
  207. CurrentBackdropIndex = -1;
  208. // Setting this to null doesn't seem to clear out the content
  209. // Have to check it for null or get startup errors
  210. if (BackdropContainer.Content != null)
  211. {
  212. BackdropContainer.Content = new FrameworkElement();
  213. }
  214. return;
  215. }
  216. CurrentBackdropIndex = 0;
  217. // We only need the timer if there's more than one backdrop
  218. if (backdrops != null && backdrops.Length > 1)
  219. {
  220. BackdropTimer = new Timer(state =>
  221. {
  222. // Don't display backdrops during video playback
  223. if (UIKernel.Instance.PlaybackManager.ActivePlayers.Any(p => p.CurrentMedia.IsVideo))
  224. {
  225. return;
  226. }
  227. var index = CurrentBackdropIndex + 1;
  228. if (index >= backdrops.Length)
  229. {
  230. index = 0;
  231. }
  232. CurrentBackdropIndex = index;
  233. }, null, 5000, 5000);
  234. }
  235. }
  236. /// <summary>
  237. /// Disposes the backdrop timer.
  238. /// </summary>
  239. public void DisposeBackdropTimer()
  240. {
  241. if (BackdropTimer != null)
  242. {
  243. BackdropTimer.Dispose();
  244. }
  245. }
  246. /// <summary>
  247. /// Disposes the mouse idle timer.
  248. /// </summary>
  249. public void DisposeMouseIdleTimer()
  250. {
  251. if (MouseIdleTimer != null)
  252. {
  253. MouseIdleTimer.Dispose();
  254. }
  255. }
  256. /// <summary>
  257. /// Clears the backdrops.
  258. /// </summary>
  259. public void ClearBackdrops()
  260. {
  261. SetBackdrops(new string[] { });
  262. }
  263. /// <summary>
  264. /// Navigates the back.
  265. /// </summary>
  266. public void NavigateBack()
  267. {
  268. Dispatcher.InvokeAsync(() =>
  269. {
  270. if (PageFrame.NavigationService.CanGoBack)
  271. {
  272. PageFrame.GoBackWithTransition();
  273. }
  274. });
  275. }
  276. /// <summary>
  277. /// Navigates the forward.
  278. /// </summary>
  279. public void NavigateForward()
  280. {
  281. Dispatcher.InvokeAsync(() =>
  282. {
  283. if (PageFrame.NavigationService.CanGoForward)
  284. {
  285. PageFrame.GoForwardWithTransition();
  286. }
  287. });
  288. }
  289. /// <summary>
  290. /// Called when [browser back].
  291. /// </summary>
  292. protected override void OnBrowserBack()
  293. {
  294. base.OnBrowserBack();
  295. NavigateBack();
  296. }
  297. /// <summary>
  298. /// Called when [browser forward].
  299. /// </summary>
  300. protected override void OnBrowserForward()
  301. {
  302. base.OnBrowserForward();
  303. NavigateForward();
  304. }
  305. /// <summary>
  306. /// Shows the control bar then starts a timer to hide it
  307. /// </summary>
  308. private void StartMouseIdleTimer()
  309. {
  310. IsMouseIdle = false;
  311. const int duration = 4000;
  312. // Start the timer if it's null, otherwise reset it
  313. if (MouseIdleTimer == null)
  314. {
  315. MouseIdleTimer = new Timer(MouseIdleTimerCallback, null, duration, Timeout.Infinite);
  316. }
  317. else
  318. {
  319. MouseIdleTimer.Change(duration, Timeout.Infinite);
  320. }
  321. }
  322. /// <summary>
  323. /// This is the Timer callback method to hide the control bar
  324. /// </summary>
  325. /// <param name="stateInfo">The state info.</param>
  326. private void MouseIdleTimerCallback(object stateInfo)
  327. {
  328. IsMouseIdle = true;
  329. if (MouseIdleTimer != null)
  330. {
  331. MouseIdleTimer.Dispose();
  332. MouseIdleTimer = null;
  333. }
  334. }
  335. /// <summary>
  336. /// The _last mouse move point
  337. /// </summary>
  338. private Point _lastMouseMovePoint;
  339. /// <summary>
  340. /// Handles OnMouseMove to show the control box
  341. /// </summary>
  342. /// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs" /> that contains the event data.</param>
  343. protected override void OnMouseMove(MouseEventArgs e)
  344. {
  345. base.OnMouseMove(e);
  346. // Store the last position for comparison purposes
  347. // Even if the mouse is not moving this event will fire as elements are showing and hiding
  348. var pos = e.GetPosition(this);
  349. if (pos == _lastMouseMovePoint)
  350. {
  351. return;
  352. }
  353. _lastMouseMovePoint = pos;
  354. StartMouseIdleTimer();
  355. }
  356. /// <summary>
  357. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  358. /// </summary>
  359. public void Dispose()
  360. {
  361. DisposeBackdropTimer();
  362. DisposeMouseIdleTimer();
  363. }
  364. /// <summary>
  365. /// Shows a notification message that will disappear on it's own
  366. /// </summary>
  367. /// <param name="text">The text.</param>
  368. /// <param name="caption">The caption.</param>
  369. /// <param name="icon">The icon.</param>
  370. public void ShowNotificationMessage(string text, string caption = null, MessageBoxIcon icon = MessageBoxIcon.None)
  371. {
  372. var control = new NotificationMessage
  373. {
  374. Caption = caption,
  375. Text = text,
  376. MessageBoxImage = icon
  377. };
  378. mainGrid.Children.Add(control);
  379. Dispatcher.InvokeWithDelay(() => mainGrid.Children.Remove(control), 5000);
  380. }
  381. /// <summary>
  382. /// Shows a notification message that will disappear on it's own
  383. /// </summary>
  384. /// <param name="text">The text.</param>
  385. /// <param name="caption">The caption.</param>
  386. /// <param name="icon">The icon.</param>
  387. public void ShowNotificationMessage(UIElement text, string caption = null, MessageBoxIcon icon = MessageBoxIcon.None)
  388. {
  389. var control = new NotificationMessage
  390. {
  391. Caption = caption,
  392. TextContent = text,
  393. MessageBoxImage = icon
  394. };
  395. mainGrid.Children.Add(control);
  396. Dispatcher.InvokeWithDelay(() => mainGrid.Children.Remove(control), 5000);
  397. }
  398. /// <summary>
  399. /// Shows a modal message box and asynchronously returns a MessageBoxResult
  400. /// </summary>
  401. /// <param name="text">The text.</param>
  402. /// <param name="caption">The caption.</param>
  403. /// <param name="button">The button.</param>
  404. /// <param name="icon">The icon.</param>
  405. /// <returns>MessageBoxResult.</returns>
  406. public MessageBoxResult ShowModalMessage(string text, string caption = null, MessageBoxButton button = MessageBoxButton.OK, MessageBoxIcon icon = MessageBoxIcon.None)
  407. {
  408. var win = new ModalWindow
  409. {
  410. Caption = caption,
  411. Button = button,
  412. MessageBoxImage = icon,
  413. Text = text
  414. };
  415. win.ShowModal(this);
  416. return win.MessageBoxResult;
  417. }
  418. /// <summary>
  419. /// Shows a modal message box and asynchronously returns a MessageBoxResult
  420. /// </summary>
  421. /// <param name="text">The text.</param>
  422. /// <param name="caption">The caption.</param>
  423. /// <param name="button">The button.</param>
  424. /// <param name="icon">The icon.</param>
  425. /// <returns>MessageBoxResult.</returns>
  426. public MessageBoxResult ShowModalMessage(UIElement text, string caption = null, MessageBoxButton button = MessageBoxButton.OK, MessageBoxIcon icon = MessageBoxIcon.None)
  427. {
  428. var win = new ModalWindow
  429. {
  430. Caption = caption,
  431. Button = button,
  432. MessageBoxImage = icon,
  433. TextContent = text
  434. };
  435. win.ShowModal(this);
  436. return win.MessageBoxResult;
  437. }
  438. }
  439. }