WindowPage.axaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Linq;
  2. using Avalonia;
  3. using Avalonia.Automation;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.ApplicationLifetimes;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Primitives.PopupPositioning;
  8. using Avalonia.Interactivity;
  9. using Avalonia.Media;
  10. namespace IntegrationTestApp.Pages;
  11. public partial class WindowPage : UserControl
  12. {
  13. public WindowPage()
  14. {
  15. InitializeComponent();
  16. }
  17. private Window Window => TopLevel.GetTopLevel(this) as Window ??
  18. throw new AvaloniaInternalException("WindowPage is not attached to a Window.");
  19. private void ShowWindow_Click(object? sender, RoutedEventArgs e)
  20. {
  21. var size = !string.IsNullOrWhiteSpace(ShowWindowSize.Text) ? Size.Parse(ShowWindowSize.Text) : (Size?)null;
  22. var canResize = ShowWindowCanResize.IsChecked ?? false;
  23. var window = new ShowWindowTest
  24. {
  25. WindowStartupLocation = (WindowStartupLocation)ShowWindowLocation.SelectedIndex,
  26. CanResize = canResize,
  27. CanMinimize = ShowWindowCanMinimize.IsChecked ?? false,
  28. CanMaximize = canResize && (ShowWindowCanMaximize.IsChecked ?? false)
  29. };
  30. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime)
  31. {
  32. // Make sure the windows have unique names and AutomationIds.
  33. var existing = lifetime.Windows.OfType<ShowWindowTest>().Count();
  34. if (existing > 0)
  35. {
  36. AutomationProperties.SetAutomationId(window, window.Name + (existing + 1));
  37. window.Title += $" {existing + 1}";
  38. }
  39. }
  40. if (size.HasValue)
  41. {
  42. window.Width = size.Value.Width;
  43. window.Height = size.Value.Height;
  44. }
  45. ShowWindowSize.Text = string.Empty;
  46. window.ExtendClientAreaToDecorationsHint = ShowWindowExtendClientAreaToDecorationsHint.IsChecked ?? false;
  47. window.SystemDecorations = (SystemDecorations)ShowWindowSystemDecorations.SelectedIndex;
  48. window.WindowState = (WindowState)ShowWindowState.SelectedIndex;
  49. switch (ShowWindowMode.SelectedIndex)
  50. {
  51. case 0:
  52. window.Show();
  53. break;
  54. case 1:
  55. window.Show(Window);
  56. break;
  57. case 2:
  58. window.ShowDialog(Window);
  59. break;
  60. }
  61. }
  62. private void ShowTransparentWindow_Click(object? sender, RoutedEventArgs e)
  63. {
  64. // Show a background window to make sure the color behind the transparent window is
  65. // a known color (green).
  66. var backgroundWindow = new Window
  67. {
  68. Title = "Transparent Window Background",
  69. Name = "TransparentWindowBackground",
  70. Width = 300,
  71. Height = 300,
  72. Background = Brushes.Green,
  73. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  74. };
  75. // This is the transparent window with a red circle.
  76. var window = new Window
  77. {
  78. Title = "Transparent Window",
  79. Name = "TransparentWindow",
  80. SystemDecorations = SystemDecorations.None,
  81. Background = Brushes.Transparent,
  82. TransparencyLevelHint = new[] { WindowTransparencyLevel.Transparent },
  83. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  84. Width = 200,
  85. Height = 200,
  86. Content = new Border
  87. {
  88. Background = Brushes.Red,
  89. CornerRadius = new CornerRadius(100),
  90. }
  91. };
  92. window.PointerPressed += (_, _) =>
  93. {
  94. window.Close();
  95. backgroundWindow.Close();
  96. };
  97. backgroundWindow.Show(Window);
  98. window.Show(backgroundWindow);
  99. }
  100. private void ShowTransparentPopup_Click(object? sender, RoutedEventArgs e)
  101. {
  102. var popup = new Popup
  103. {
  104. WindowManagerAddShadowHint = false,
  105. Placement = PlacementMode.AnchorAndGravity,
  106. PlacementAnchor = PopupAnchor.Top,
  107. PlacementGravity = PopupGravity.Bottom,
  108. Width = 200,
  109. Height = 200,
  110. Child = new Border
  111. {
  112. Background = Brushes.Red,
  113. CornerRadius = new CornerRadius(100),
  114. }
  115. };
  116. // Show a background window to make sure the color behind the transparent window is
  117. // a known color (green).
  118. var backgroundWindow = new Window
  119. {
  120. Title = "Transparent Popup Background",
  121. Name = "TransparentPopupBackground",
  122. Width = 200,
  123. Height = 200,
  124. Background = Brushes.Green,
  125. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  126. Content = new Border
  127. {
  128. Name = "PopupContainer",
  129. Child = popup,
  130. [AutomationProperties.AccessibilityViewProperty] = AccessibilityView.Content,
  131. }
  132. };
  133. backgroundWindow.PointerPressed += (_, _) => backgroundWindow.Close();
  134. backgroundWindow.Show(Window);
  135. popup.Open();
  136. }
  137. private void SendToBack_Click(object? sender, RoutedEventArgs e)
  138. {
  139. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  140. foreach (var window in lifetime.Windows.ToArray())
  141. {
  142. window.Activate();
  143. }
  144. }
  145. private void EnterFullscreen_Click(object? sender, RoutedEventArgs e)
  146. {
  147. Window.WindowState = WindowState.FullScreen;
  148. }
  149. private void ExitFullscreen_Click(object? sender, RoutedEventArgs e)
  150. {
  151. Window.WindowState = WindowState.Normal;
  152. }
  153. private void RestoreAll_Click(object? sender, RoutedEventArgs e)
  154. {
  155. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  156. foreach (var window in lifetime.Windows.ToArray())
  157. {
  158. window.Show();
  159. if (window.WindowState == WindowState.Minimized)
  160. window.WindowState = WindowState.Normal;
  161. }
  162. }
  163. private void ShowTopmostWindow_Click(object? sender, RoutedEventArgs e)
  164. {
  165. var mainWindow = new TopmostWindowTest("OwnerWindow")
  166. {
  167. Topmost = true,
  168. Title = "Owner Window"
  169. };
  170. var ownedWindow = new TopmostWindowTest("OwnedWindow")
  171. {
  172. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  173. Title = "Owned Window"
  174. };
  175. mainWindow.Show();
  176. ownedWindow.Show(mainWindow);
  177. }
  178. }