BaseWindow.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Input;
  5. namespace MediaBrowser.UI.Controls
  6. {
  7. /// <summary>
  8. /// Provides a base class for all Windows
  9. /// </summary>
  10. public abstract class BaseWindow : Window, INotifyPropertyChanged
  11. {
  12. /// <summary>
  13. /// Occurs when [property changed].
  14. /// </summary>
  15. public event PropertyChangedEventHandler PropertyChanged;
  16. /// <summary>
  17. /// Called when [property changed].
  18. /// </summary>
  19. /// <param name="info">The info.</param>
  20. public void OnPropertyChanged(String info)
  21. {
  22. if (PropertyChanged != null)
  23. {
  24. PropertyChanged(this, new PropertyChangedEventArgs(info));
  25. }
  26. }
  27. /// <summary>
  28. /// The _content scale
  29. /// </summary>
  30. private double _contentScale = 1;
  31. /// <summary>
  32. /// Gets the content scale.
  33. /// </summary>
  34. /// <value>The content scale.</value>
  35. public double ContentScale
  36. {
  37. get { return _contentScale; }
  38. private set
  39. {
  40. _contentScale = value;
  41. OnPropertyChanged("ContentScale");
  42. }
  43. }
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="BaseWindow" /> class.
  46. /// </summary>
  47. protected BaseWindow()
  48. : base()
  49. {
  50. SizeChanged += MainWindow_SizeChanged;
  51. Loaded += BaseWindowLoaded;
  52. }
  53. /// <summary>
  54. /// Bases the window loaded.
  55. /// </summary>
  56. /// <param name="sender">The sender.</param>
  57. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  58. void BaseWindowLoaded(object sender, RoutedEventArgs e)
  59. {
  60. OnLoaded();
  61. }
  62. /// <summary>
  63. /// Called when [loaded].
  64. /// </summary>
  65. protected virtual void OnLoaded()
  66. {
  67. MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
  68. }
  69. /// <summary>
  70. /// Handles the SizeChanged event of the MainWindow control.
  71. /// </summary>
  72. /// <param name="sender">The source of the event.</param>
  73. /// <param name="e">The <see cref="SizeChangedEventArgs" /> instance containing the event data.</param>
  74. void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
  75. {
  76. ContentScale = e.NewSize.Height / 1080;
  77. }
  78. /// <summary>
  79. /// Called when [browser back].
  80. /// </summary>
  81. protected virtual void OnBrowserBack()
  82. {
  83. }
  84. /// <summary>
  85. /// Called when [browser forward].
  86. /// </summary>
  87. protected virtual void OnBrowserForward()
  88. {
  89. }
  90. /// <summary>
  91. /// Invoked when an unhandled <see cref="E:System.Windows.Input.Keyboard.PreviewKeyDown" /> attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
  92. /// </summary>
  93. /// <param name="e">The <see cref="T:System.Windows.Input.KeyEventArgs" /> that contains the event data.</param>
  94. protected override void OnPreviewKeyDown(KeyEventArgs e)
  95. {
  96. if (IsBackPress(e))
  97. {
  98. e.Handled = true;
  99. if (!e.IsRepeat)
  100. {
  101. OnBrowserBack();
  102. }
  103. }
  104. else if (IsForwardPress(e))
  105. {
  106. e.Handled = true;
  107. if (!e.IsRepeat)
  108. {
  109. OnBrowserForward();
  110. }
  111. }
  112. base.OnPreviewKeyDown(e);
  113. }
  114. /// <summary>
  115. /// Determines if a keypress should be treated as a backward press
  116. /// </summary>
  117. /// <param name="e">The <see cref="KeyEventArgs" /> instance containing the event data.</param>
  118. /// <returns><c>true</c> if [is back press] [the specified e]; otherwise, <c>false</c>.</returns>
  119. private bool IsBackPress(KeyEventArgs e)
  120. {
  121. if (e.Key == Key.Escape)
  122. {
  123. return true;
  124. }
  125. if (e.Key == Key.BrowserBack || e.Key == Key.Back)
  126. {
  127. return true;
  128. }
  129. if (e.SystemKey == Key.Left && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt))
  130. {
  131. return true;
  132. }
  133. return false;
  134. }
  135. /// <summary>
  136. /// Determines if a keypress should be treated as a forward press
  137. /// </summary>
  138. /// <param name="e">The <see cref="KeyEventArgs" /> instance containing the event data.</param>
  139. /// <returns><c>true</c> if [is forward press] [the specified e]; otherwise, <c>false</c>.</returns>
  140. private bool IsForwardPress(KeyEventArgs e)
  141. {
  142. if (e.Key == Key.BrowserForward)
  143. {
  144. return true;
  145. }
  146. if (e.SystemKey == Key.RightAlt && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt))
  147. {
  148. return true;
  149. }
  150. return false;
  151. }
  152. }
  153. }