MainWindow.axaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 canResizeCheckBox = this.GetControl<CheckBox>("ShowWindowCanResize");
  63. var owner = (Window)this.GetVisualRoot()!;
  64. var window = new ShowWindowTest
  65. {
  66. WindowStartupLocation = (WindowStartupLocation)locationComboBox.SelectedIndex,
  67. CanResize = canResizeCheckBox.IsChecked.Value,
  68. };
  69. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime)
  70. {
  71. // Make sure the windows have unique names and AutomationIds.
  72. var existing = lifetime.Windows.OfType<ShowWindowTest>().Count();
  73. if (existing > 0)
  74. {
  75. AutomationProperties.SetAutomationId(window, window.Name + (existing + 1));
  76. window.Title += $" {existing + 1}";
  77. }
  78. }
  79. if (size.HasValue)
  80. {
  81. window.Width = size.Value.Width;
  82. window.Height = size.Value.Height;
  83. }
  84. sizeTextBox.Text = string.Empty;
  85. window.WindowState = (WindowState)stateComboBox.SelectedIndex;
  86. switch (modeComboBox.SelectedIndex)
  87. {
  88. case 0:
  89. window.Show();
  90. break;
  91. case 1:
  92. window.Show(owner);
  93. break;
  94. case 2:
  95. window.ShowDialog(owner);
  96. break;
  97. }
  98. }
  99. private void ShowTransparentWindow()
  100. {
  101. // Show a background window to make sure the color behind the transparent window is
  102. // a known color (green).
  103. var backgroundWindow = new Window
  104. {
  105. Title = "Transparent Window Background",
  106. Name = "TransparentWindowBackground",
  107. Width = 300,
  108. Height = 300,
  109. Background = Brushes.Green,
  110. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  111. };
  112. // This is the transparent window with a red circle.
  113. var window = new Window
  114. {
  115. Title = "Transparent Window",
  116. Name = "TransparentWindow",
  117. SystemDecorations = SystemDecorations.None,
  118. Background = Brushes.Transparent,
  119. TransparencyLevelHint = WindowTransparencyLevel.Transparent,
  120. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  121. Width = 200,
  122. Height = 200,
  123. Content = new Border
  124. {
  125. Background = Brushes.Red,
  126. CornerRadius = new CornerRadius(100),
  127. }
  128. };
  129. window.PointerPressed += (_, _) =>
  130. {
  131. window.Close();
  132. backgroundWindow.Close();
  133. };
  134. backgroundWindow.Show(this);
  135. window.Show(backgroundWindow);
  136. }
  137. private void ShowTransparentPopup()
  138. {
  139. var popup = new Popup
  140. {
  141. WindowManagerAddShadowHint = false,
  142. PlacementMode = PlacementMode.AnchorAndGravity,
  143. PlacementAnchor = PopupAnchor.Top,
  144. PlacementGravity = PopupGravity.Bottom,
  145. Width= 200,
  146. Height= 200,
  147. Child = new Border
  148. {
  149. Background = Brushes.Red,
  150. CornerRadius = new CornerRadius(100),
  151. }
  152. };
  153. // Show a background window to make sure the color behind the transparent window is
  154. // a known color (green).
  155. var backgroundWindow = new Window
  156. {
  157. Title = "Transparent Popup Background",
  158. Name = "TransparentPopupBackground",
  159. Width = 200,
  160. Height = 200,
  161. Background = Brushes.Green,
  162. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  163. Content = new Border
  164. {
  165. Name = "PopupContainer",
  166. Child = popup,
  167. [AutomationProperties.AccessibilityViewProperty] = AccessibilityView.Content,
  168. }
  169. };
  170. backgroundWindow.PointerPressed += (_, _) => backgroundWindow.Close();
  171. backgroundWindow.Show(this);
  172. popup.Open();
  173. }
  174. private void SendToBack()
  175. {
  176. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  177. foreach (var window in lifetime.Windows)
  178. {
  179. window.Activate();
  180. }
  181. }
  182. private void RestoreAll()
  183. {
  184. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  185. foreach (var window in lifetime.Windows)
  186. {
  187. window.Show();
  188. if (window.WindowState == WindowState.Minimized)
  189. window.WindowState = WindowState.Normal;
  190. }
  191. }
  192. private void InitializeGesturesTab()
  193. {
  194. var gestureBorder = this.GetControl<Border>("GestureBorder");
  195. var gestureBorder2 = this.GetControl<Border>("GestureBorder2");
  196. var lastGesture = this.GetControl<TextBlock>("LastGesture");
  197. var resetGestures = this.GetControl<Button>("ResetGestures");
  198. gestureBorder.Tapped += (s, e) => lastGesture.Text = "Tapped";
  199. gestureBorder.DoubleTapped += (s, e) =>
  200. {
  201. lastGesture.Text = "DoubleTapped";
  202. // Testing #8733
  203. gestureBorder.IsVisible = false;
  204. gestureBorder2.IsVisible = true;
  205. };
  206. gestureBorder2.DoubleTapped += (s, e) =>
  207. {
  208. lastGesture.Text = "DoubleTapped2";
  209. };
  210. Gestures.AddRightTappedHandler(gestureBorder, (s, e) => lastGesture.Text = "RightTapped");
  211. resetGestures.Click += (s, e) =>
  212. {
  213. lastGesture.Text = string.Empty;
  214. gestureBorder.IsVisible = true;
  215. gestureBorder2.IsVisible = false;
  216. };
  217. }
  218. private void MenuClicked(object? sender, RoutedEventArgs e)
  219. {
  220. var clickedMenuItemTextBlock = this.Get<TextBlock>("ClickedMenuItem");
  221. clickedMenuItemTextBlock.Text = (sender as MenuItem)?.Header?.ToString();
  222. }
  223. private void OnButtonClick(object? sender, RoutedEventArgs e)
  224. {
  225. var source = e.Source as Button;
  226. if (source?.Name == "ComboBoxSelectionClear")
  227. this.Get<ComboBox>("BasicComboBox").SelectedIndex = -1;
  228. if (source?.Name == "ComboBoxSelectFirst")
  229. this.Get<ComboBox>("BasicComboBox").SelectedIndex = 0;
  230. if (source?.Name == "ListBoxSelectionClear")
  231. this.Get<ListBox>("BasicListBox").SelectedIndex = -1;
  232. if (source?.Name == "MenuClickedMenuItemReset")
  233. this.Get<TextBlock>("ClickedMenuItem").Text = "None";
  234. if (source?.Name == "ShowTransparentWindow")
  235. ShowTransparentWindow();
  236. if (source?.Name == "ShowTransparentPopup")
  237. ShowTransparentPopup();
  238. if (source?.Name == "ShowWindow")
  239. ShowWindow();
  240. if (source?.Name == "SendToBack")
  241. SendToBack();
  242. if (source?.Name == "EnterFullscreen")
  243. WindowState = WindowState.FullScreen;
  244. if (source?.Name == "ExitFullscreen")
  245. WindowState = WindowState.Normal;
  246. if (source?.Name == "RestoreAll")
  247. RestoreAll();
  248. }
  249. }
  250. }