MainWindow.axaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Avalonia;
  4. using Avalonia.Automation;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.ApplicationLifetimes;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Controls.Primitives.PopupPositioning;
  9. using Avalonia.Input;
  10. using Avalonia.Interactivity;
  11. using Avalonia.Markup.Xaml;
  12. using Avalonia.Media;
  13. using Avalonia.VisualTree;
  14. using Microsoft.CodeAnalysis;
  15. namespace IntegrationTestApp
  16. {
  17. public class MainWindow : Window
  18. {
  19. public MainWindow()
  20. {
  21. InitializeComponent();
  22. InitializeViewMenu();
  23. InitializeGesturesTab();
  24. this.AttachDevTools();
  25. var overlayPopups = this.Get<TextBlock>("AppOverlayPopups");
  26. overlayPopups.Text = Program.OverlayPopups ? "Overlay Popups" : "Native Popups";
  27. AddHandler(Button.ClickEvent, OnButtonClick);
  28. ListBoxItems = Enumerable.Range(0, 100).Select(x => "Item " + x).ToList();
  29. DataContext = this;
  30. }
  31. public List<string> ListBoxItems { get; }
  32. private void InitializeComponent()
  33. {
  34. AvaloniaXamlLoader.Load(this);
  35. }
  36. private void InitializeViewMenu()
  37. {
  38. var mainTabs = this.Get<TabControl>("MainTabs");
  39. var viewMenu = (NativeMenuItem)NativeMenu.GetMenu(this).Items[1];
  40. if (mainTabs.Items is not null)
  41. {
  42. foreach (TabItem tabItem in mainTabs.Items)
  43. {
  44. var menuItem = new NativeMenuItem
  45. {
  46. Header = (string)tabItem.Header!,
  47. IsChecked = tabItem.IsSelected,
  48. ToggleType = NativeMenuItemToggleType.Radio,
  49. };
  50. menuItem.Click += (s, e) => tabItem.IsSelected = true;
  51. viewMenu?.Menu?.Items.Add(menuItem);
  52. }
  53. }
  54. }
  55. private void ShowWindow()
  56. {
  57. var sizeTextBox = this.GetControl<TextBox>("ShowWindowSize");
  58. var modeComboBox = this.GetControl<ComboBox>("ShowWindowMode");
  59. var locationComboBox = this.GetControl<ComboBox>("ShowWindowLocation");
  60. var stateComboBox = this.GetControl<ComboBox>("ShowWindowState");
  61. var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null;
  62. var systemDecorations = this.GetControl<ComboBox>("ShowWindowSystemDecorations");
  63. var extendClientArea = this.GetControl<CheckBox>("ShowWindowExtendClientAreaToDecorationsHint");
  64. var canResizeCheckBox = this.GetControl<CheckBox>("ShowWindowCanResize");
  65. var owner = (Window)this.GetVisualRoot()!;
  66. var window = new ShowWindowTest
  67. {
  68. WindowStartupLocation = (WindowStartupLocation)locationComboBox.SelectedIndex,
  69. CanResize = canResizeCheckBox.IsChecked.Value,
  70. };
  71. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime)
  72. {
  73. // Make sure the windows have unique names and AutomationIds.
  74. var existing = lifetime.Windows.OfType<ShowWindowTest>().Count();
  75. if (existing > 0)
  76. {
  77. AutomationProperties.SetAutomationId(window, window.Name + (existing + 1));
  78. window.Title += $" {existing + 1}";
  79. }
  80. }
  81. if (size.HasValue)
  82. {
  83. window.Width = size.Value.Width;
  84. window.Height = size.Value.Height;
  85. }
  86. sizeTextBox.Text = string.Empty;
  87. window.ExtendClientAreaToDecorationsHint = extendClientArea.IsChecked ?? false;
  88. window.SystemDecorations = (SystemDecorations)systemDecorations.SelectedIndex;
  89. window.WindowState = (WindowState)stateComboBox.SelectedIndex;
  90. switch (modeComboBox.SelectedIndex)
  91. {
  92. case 0:
  93. window.Show();
  94. break;
  95. case 1:
  96. window.Show(owner);
  97. break;
  98. case 2:
  99. window.ShowDialog(owner);
  100. break;
  101. }
  102. }
  103. private void ShowTransparentWindow()
  104. {
  105. // Show a background window to make sure the color behind the transparent window is
  106. // a known color (green).
  107. var backgroundWindow = new Window
  108. {
  109. Title = "Transparent Window Background",
  110. Name = "TransparentWindowBackground",
  111. Width = 300,
  112. Height = 300,
  113. Background = Brushes.Green,
  114. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  115. };
  116. // This is the transparent window with a red circle.
  117. var window = new Window
  118. {
  119. Title = "Transparent Window",
  120. Name = "TransparentWindow",
  121. SystemDecorations = SystemDecorations.None,
  122. Background = Brushes.Transparent,
  123. TransparencyLevelHint = WindowTransparencyLevel.Transparent,
  124. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  125. Width = 200,
  126. Height = 200,
  127. Content = new Border
  128. {
  129. Background = Brushes.Red,
  130. CornerRadius = new CornerRadius(100),
  131. }
  132. };
  133. window.PointerPressed += (_, _) =>
  134. {
  135. window.Close();
  136. backgroundWindow.Close();
  137. };
  138. backgroundWindow.Show(this);
  139. window.Show(backgroundWindow);
  140. }
  141. private void ShowTransparentPopup()
  142. {
  143. var popup = new Popup
  144. {
  145. WindowManagerAddShadowHint = false,
  146. Placement = PlacementMode.AnchorAndGravity,
  147. PlacementAnchor = PopupAnchor.Top,
  148. PlacementGravity = PopupGravity.Bottom,
  149. Width= 200,
  150. Height= 200,
  151. Child = new Border
  152. {
  153. Background = Brushes.Red,
  154. CornerRadius = new CornerRadius(100),
  155. }
  156. };
  157. // Show a background window to make sure the color behind the transparent window is
  158. // a known color (green).
  159. var backgroundWindow = new Window
  160. {
  161. Title = "Transparent Popup Background",
  162. Name = "TransparentPopupBackground",
  163. Width = 200,
  164. Height = 200,
  165. Background = Brushes.Green,
  166. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  167. Content = new Border
  168. {
  169. Name = "PopupContainer",
  170. Child = popup,
  171. [AutomationProperties.AccessibilityViewProperty] = AccessibilityView.Content,
  172. }
  173. };
  174. backgroundWindow.PointerPressed += (_, _) => backgroundWindow.Close();
  175. backgroundWindow.Show(this);
  176. popup.Open();
  177. }
  178. private void SendToBack()
  179. {
  180. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  181. foreach (var window in lifetime.Windows)
  182. {
  183. window.Activate();
  184. }
  185. }
  186. private void RestoreAll()
  187. {
  188. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  189. foreach (var window in lifetime.Windows)
  190. {
  191. window.Show();
  192. if (window.WindowState == WindowState.Minimized)
  193. window.WindowState = WindowState.Normal;
  194. }
  195. }
  196. private void InitializeGesturesTab()
  197. {
  198. var gestureBorder = this.GetControl<Border>("GestureBorder");
  199. var gestureBorder2 = this.GetControl<Border>("GestureBorder2");
  200. var lastGesture = this.GetControl<TextBlock>("LastGesture");
  201. var resetGestures = this.GetControl<Button>("ResetGestures");
  202. gestureBorder.Tapped += (s, e) => lastGesture.Text = "Tapped";
  203. gestureBorder.DoubleTapped += (s, e) =>
  204. {
  205. lastGesture.Text = "DoubleTapped";
  206. // Testing #8733
  207. gestureBorder.IsVisible = false;
  208. gestureBorder2.IsVisible = true;
  209. };
  210. gestureBorder2.DoubleTapped += (s, e) =>
  211. {
  212. lastGesture.Text = "DoubleTapped2";
  213. };
  214. Gestures.AddRightTappedHandler(gestureBorder, (s, e) => lastGesture.Text = "RightTapped");
  215. resetGestures.Click += (s, e) =>
  216. {
  217. lastGesture.Text = string.Empty;
  218. gestureBorder.IsVisible = true;
  219. gestureBorder2.IsVisible = false;
  220. };
  221. }
  222. private void MenuClicked(object? sender, RoutedEventArgs e)
  223. {
  224. var clickedMenuItemTextBlock = this.Get<TextBlock>("ClickedMenuItem");
  225. clickedMenuItemTextBlock.Text = (sender as MenuItem)?.Header?.ToString();
  226. }
  227. private void OnButtonClick(object? sender, RoutedEventArgs e)
  228. {
  229. var source = e.Source as Button;
  230. if (source?.Name == "ComboBoxSelectionClear")
  231. this.Get<ComboBox>("BasicComboBox").SelectedIndex = -1;
  232. if (source?.Name == "ComboBoxSelectFirst")
  233. this.Get<ComboBox>("BasicComboBox").SelectedIndex = 0;
  234. if (source?.Name == "ListBoxSelectionClear")
  235. this.Get<ListBox>("BasicListBox").SelectedIndex = -1;
  236. if (source?.Name == "MenuClickedMenuItemReset")
  237. this.Get<TextBlock>("ClickedMenuItem").Text = "None";
  238. if (source?.Name == "ShowTransparentWindow")
  239. ShowTransparentWindow();
  240. if (source?.Name == "ShowTransparentPopup")
  241. ShowTransparentPopup();
  242. if (source?.Name == "ShowWindow")
  243. ShowWindow();
  244. if (source?.Name == "SendToBack")
  245. SendToBack();
  246. if (source?.Name == "EnterFullscreen")
  247. WindowState = WindowState.FullScreen;
  248. if (source?.Name == "ExitFullscreen")
  249. WindowState = WindowState.Normal;
  250. if (source?.Name == "RestoreAll")
  251. RestoreAll();
  252. }
  253. }
  254. }