1
0

WindowPage.axaml.cs 6.4 KB

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