MainWindow.xaml.cs 15 KB

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