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. foreach (var tabItem in mainTabs.Items.Cast<TabItem>())
  41. {
  42. var menuItem = new NativeMenuItem
  43. {
  44. Header = (string?)tabItem.Header,
  45. ToolTip = (string?)tabItem.Header,
  46. IsChecked = tabItem.IsSelected,
  47. ToggleType = NativeMenuItemToggleType.Radio,
  48. };
  49. menuItem.Click += (_, _) => tabItem.IsSelected = true;
  50. viewMenu?.Menu?.Items.Add(menuItem);
  51. }
  52. }
  53. private void ShowWindow()
  54. {
  55. var sizeTextBox = this.GetControl<TextBox>("ShowWindowSize");
  56. var modeComboBox = this.GetControl<ComboBox>("ShowWindowMode");
  57. var locationComboBox = this.GetControl<ComboBox>("ShowWindowLocation");
  58. var stateComboBox = this.GetControl<ComboBox>("ShowWindowState");
  59. var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null;
  60. var systemDecorations = this.GetControl<ComboBox>("ShowWindowSystemDecorations");
  61. var extendClientArea = this.GetControl<CheckBox>("ShowWindowExtendClientAreaToDecorationsHint");
  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 ?? false,
  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.ExtendClientAreaToDecorationsHint = extendClientArea.IsChecked ?? false;
  86. window.SystemDecorations = (SystemDecorations)systemDecorations.SelectedIndex;
  87. window.WindowState = (WindowState)stateComboBox.SelectedIndex;
  88. switch (modeComboBox.SelectedIndex)
  89. {
  90. case 0:
  91. window.Show();
  92. break;
  93. case 1:
  94. window.Show(owner);
  95. break;
  96. case 2:
  97. window.ShowDialog(owner);
  98. break;
  99. }
  100. }
  101. private void ShowTransparentWindow()
  102. {
  103. // Show a background window to make sure the color behind the transparent window is
  104. // a known color (green).
  105. var backgroundWindow = new Window
  106. {
  107. Title = "Transparent Window Background",
  108. Name = "TransparentWindowBackground",
  109. Width = 300,
  110. Height = 300,
  111. Background = Brushes.Green,
  112. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  113. };
  114. // This is the transparent window with a red circle.
  115. var window = new Window
  116. {
  117. Title = "Transparent Window",
  118. Name = "TransparentWindow",
  119. SystemDecorations = SystemDecorations.None,
  120. Background = Brushes.Transparent,
  121. TransparencyLevelHint = new[] { WindowTransparencyLevel.Transparent },
  122. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  123. Width = 200,
  124. Height = 200,
  125. Content = new Border
  126. {
  127. Background = Brushes.Red,
  128. CornerRadius = new CornerRadius(100),
  129. }
  130. };
  131. window.PointerPressed += (_, _) =>
  132. {
  133. window.Close();
  134. backgroundWindow.Close();
  135. };
  136. backgroundWindow.Show(this);
  137. window.Show(backgroundWindow);
  138. }
  139. private void ShowTransparentPopup()
  140. {
  141. var popup = new Popup
  142. {
  143. WindowManagerAddShadowHint = false,
  144. Placement = PlacementMode.AnchorAndGravity,
  145. PlacementAnchor = PopupAnchor.Top,
  146. PlacementGravity = PopupGravity.Bottom,
  147. Width= 200,
  148. Height= 200,
  149. Child = new Border
  150. {
  151. Background = Brushes.Red,
  152. CornerRadius = new CornerRadius(100),
  153. }
  154. };
  155. // Show a background window to make sure the color behind the transparent window is
  156. // a known color (green).
  157. var backgroundWindow = new Window
  158. {
  159. Title = "Transparent Popup Background",
  160. Name = "TransparentPopupBackground",
  161. Width = 200,
  162. Height = 200,
  163. Background = Brushes.Green,
  164. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  165. Content = new Border
  166. {
  167. Name = "PopupContainer",
  168. Child = popup,
  169. [AutomationProperties.AccessibilityViewProperty] = AccessibilityView.Content,
  170. }
  171. };
  172. backgroundWindow.PointerPressed += (_, _) => backgroundWindow.Close();
  173. backgroundWindow.Show(this);
  174. popup.Open();
  175. }
  176. private void SendToBack()
  177. {
  178. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  179. foreach (var window in lifetime.Windows.ToArray())
  180. {
  181. window.Activate();
  182. }
  183. }
  184. private void RestoreAll()
  185. {
  186. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  187. foreach (var window in lifetime.Windows.ToArray())
  188. {
  189. window.Show();
  190. if (window.WindowState == WindowState.Minimized)
  191. window.WindowState = WindowState.Normal;
  192. }
  193. }
  194. private void InitializeGesturesTab()
  195. {
  196. var gestureBorder = this.GetControl<Border>("GestureBorder");
  197. var gestureBorder2 = this.GetControl<Border>("GestureBorder2");
  198. var lastGesture = this.GetControl<TextBlock>("LastGesture");
  199. var resetGestures = this.GetControl<Button>("ResetGestures");
  200. gestureBorder.Tapped += (_, _) => lastGesture.Text = "Tapped";
  201. gestureBorder.DoubleTapped += (_, _) =>
  202. {
  203. lastGesture.Text = "DoubleTapped";
  204. // Testing #8733
  205. gestureBorder.IsVisible = false;
  206. gestureBorder2.IsVisible = true;
  207. };
  208. gestureBorder2.DoubleTapped += (_, _) =>
  209. {
  210. lastGesture.Text = "DoubleTapped2";
  211. };
  212. Gestures.AddRightTappedHandler(gestureBorder, (_, _) => lastGesture.Text = "RightTapped");
  213. resetGestures.Click += (_, _) =>
  214. {
  215. lastGesture.Text = string.Empty;
  216. gestureBorder.IsVisible = true;
  217. gestureBorder2.IsVisible = false;
  218. };
  219. }
  220. private void MenuClicked(object? sender, RoutedEventArgs e)
  221. {
  222. var clickedMenuItemTextBlock = this.Get<TextBlock>("ClickedMenuItem");
  223. clickedMenuItemTextBlock.Text = (sender as MenuItem)?.Header?.ToString();
  224. }
  225. private void OnButtonClick(object? sender, RoutedEventArgs e)
  226. {
  227. var source = e.Source as Button;
  228. if (source?.Name == "ComboBoxSelectionClear")
  229. this.Get<ComboBox>("BasicComboBox").SelectedIndex = -1;
  230. if (source?.Name == "ComboBoxSelectFirst")
  231. this.Get<ComboBox>("BasicComboBox").SelectedIndex = 0;
  232. if (source?.Name == "ListBoxSelectionClear")
  233. this.Get<ListBox>("BasicListBox").SelectedIndex = -1;
  234. if (source?.Name == "MenuClickedMenuItemReset")
  235. this.Get<TextBlock>("ClickedMenuItem").Text = "None";
  236. if (source?.Name == "ResetSliders")
  237. this.Get<Slider>("HorizontalSlider").Value = 50;
  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. }