MainWindow.axaml.cs 9.9 KB

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