PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Okra.Core/UI/FlyoutPane.cs

http://okra.codeplex.com
C# | 211 lines | 132 code | 55 blank | 24 comment | 10 complexity | 1fb180fa2bc8ead3d8e1fc25b316f9a8 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Windows.Foundation;
  7. using Windows.UI.ApplicationSettings;
  8. using Windows.UI.Core;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Controls;
  11. using Windows.UI.Xaml.Controls.Primitives;
  12. using Windows.UI.Xaml.Media.Animation;
  13. namespace Okra.UI
  14. {
  15. public class FlyoutPane
  16. {
  17. // *** Fields ***
  18. private Popup popup;
  19. private ContentControl contentControl;
  20. private readonly FlyoutEdge flyoutEdge;
  21. private readonly bool isLightDismissEnabled;
  22. // *** Events ***
  23. public event EventHandler Closed;
  24. public event EventHandler Opened;
  25. // *** Constructors ***
  26. public FlyoutPane(FlyoutEdge flyoutEdge = FlyoutEdge.Right, bool isLightDismissEnabled = false)
  27. {
  28. this.flyoutEdge = flyoutEdge;
  29. this.isLightDismissEnabled = isLightDismissEnabled;
  30. }
  31. // *** Properties ***
  32. public FlyoutEdge FlyoutEdge
  33. {
  34. get
  35. {
  36. return flyoutEdge;
  37. }
  38. }
  39. public bool IsLightDismissEnabled
  40. {
  41. get
  42. {
  43. return isLightDismissEnabled;
  44. }
  45. }
  46. public bool IsOpen
  47. {
  48. get
  49. {
  50. if (popup == null)
  51. return false;
  52. else
  53. return popup.IsOpen;
  54. }
  55. }
  56. // *** Methods ***
  57. public void Close()
  58. {
  59. // Hide the popup
  60. popup.IsOpen = false;
  61. }
  62. public void Show(object content)
  63. {
  64. // Create the popup if required
  65. if (popup == null)
  66. CreatePopup();
  67. // Update the content control
  68. contentControl.Content = content;
  69. contentControl.Measure(new Size(double.PositiveInfinity, contentControl.Height));
  70. // Position the flyout on the screen
  71. SetFlyoutPosition();
  72. // Ensure that the popup is displayed
  73. popup.IsOpen = true;
  74. }
  75. // *** Protected Methods ***
  76. protected virtual void OnClosed()
  77. {
  78. EventHandler eventHandler = Closed;
  79. if (eventHandler != null)
  80. eventHandler(this, EventArgs.Empty);
  81. }
  82. protected virtual void OnOpened()
  83. {
  84. EventHandler eventHandler = Opened;
  85. if (eventHandler != null)
  86. eventHandler(this, EventArgs.Empty);
  87. }
  88. // *** Private Methods ***
  89. private void CreatePopup()
  90. {
  91. // Create the transitions
  92. TransitionCollection childTransitions = new TransitionCollection();
  93. childTransitions.Add(new PaneThemeTransition() { Edge = GetPopupTransitionEdge() });
  94. // Create a new Popup to display the content
  95. popup = new Popup()
  96. {
  97. IsLightDismissEnabled = isLightDismissEnabled,
  98. ChildTransitions = childTransitions
  99. };
  100. // Register for the Opened and Closed events
  101. popup.Opened += Popup_Opened;
  102. popup.Closed += Popup_Closed;
  103. // Create a content control to display the content
  104. contentControl = new ContentControl()
  105. {
  106. Height = Window.Current.Bounds.Height,
  107. HorizontalContentAlignment = HorizontalAlignment.Stretch,
  108. VerticalContentAlignment = VerticalAlignment.Stretch
  109. };
  110. popup.Child = contentControl;
  111. }
  112. private EdgeTransitionLocation GetPopupTransitionEdge()
  113. {
  114. switch (flyoutEdge)
  115. {
  116. case FlyoutEdge.Left:
  117. return EdgeTransitionLocation.Left;
  118. case FlyoutEdge.Right:
  119. return EdgeTransitionLocation.Right;
  120. default:
  121. throw new InvalidOperationException();
  122. }
  123. }
  124. private void SetFlyoutPosition()
  125. {
  126. // Position the flyout on the screen
  127. double width = contentControl.DesiredSize.Width;
  128. double left = flyoutEdge == FlyoutEdge.Left ? 0.0 : Window.Current.Bounds.Width - width;
  129. Canvas.SetTop(popup, 0);
  130. Canvas.SetLeft(popup, left);
  131. // Make sure that the flyout is always the height of the screen
  132. contentControl.Height = Window.Current.Bounds.Height;
  133. }
  134. private void Window_SizeChanged(object sender, WindowSizeChangedEventArgs e)
  135. {
  136. // Reset the flyout position every time the window size changes
  137. SetFlyoutPosition();
  138. }
  139. private void Popup_Closed(object sender, object e)
  140. {
  141. // Unsubscribe from the Window.SizeChanged event
  142. Window.Current.SizeChanged -= Window_SizeChanged;
  143. // Clear the content control (so the garbage collector can dispose of it)
  144. contentControl.Content = null;
  145. // Raise the 'Closed' event
  146. OnClosed();
  147. }
  148. private void Popup_Opened(object sender, object e)
  149. {
  150. // Register to the Window.SizeChanged event
  151. Window.Current.SizeChanged += Window_SizeChanged;
  152. // Raise the 'Opened' event
  153. OnOpened();
  154. }
  155. }
  156. }