MainWindow.axaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.Input;
  8. using Avalonia.Interactivity;
  9. using Avalonia.Markup.Xaml;
  10. using Avalonia.VisualTree;
  11. using Microsoft.CodeAnalysis;
  12. namespace IntegrationTestApp
  13. {
  14. public class MainWindow : Window
  15. {
  16. public MainWindow()
  17. {
  18. InitializeComponent();
  19. InitializeViewMenu();
  20. InitializeGesturesTab();
  21. this.AttachDevTools();
  22. AddHandler(Button.ClickEvent, OnButtonClick);
  23. ListBoxItems = Enumerable.Range(0, 100).Select(x => "Item " + x).ToList();
  24. DataContext = this;
  25. }
  26. public List<string> ListBoxItems { get; }
  27. private void InitializeComponent()
  28. {
  29. AvaloniaXamlLoader.Load(this);
  30. }
  31. private void InitializeViewMenu()
  32. {
  33. var mainTabs = this.Get<TabControl>("MainTabs");
  34. var viewMenu = (NativeMenuItem)NativeMenu.GetMenu(this).Items[1];
  35. if (mainTabs.Items is not null)
  36. {
  37. foreach (TabItem tabItem in mainTabs.Items)
  38. {
  39. var menuItem = new NativeMenuItem
  40. {
  41. Header = (string)tabItem.Header!,
  42. IsChecked = tabItem.IsSelected,
  43. ToggleType = NativeMenuItemToggleType.Radio,
  44. };
  45. menuItem.Click += (s, e) => tabItem.IsSelected = true;
  46. viewMenu?.Menu?.Items.Add(menuItem);
  47. }
  48. }
  49. }
  50. private void ShowWindow()
  51. {
  52. var sizeTextBox = this.GetControl<TextBox>("ShowWindowSize");
  53. var modeComboBox = this.GetControl<ComboBox>("ShowWindowMode");
  54. var locationComboBox = this.GetControl<ComboBox>("ShowWindowLocation");
  55. var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null;
  56. var owner = (Window)this.GetVisualRoot()!;
  57. var window = new ShowWindowTest
  58. {
  59. WindowStartupLocation = (WindowStartupLocation)locationComboBox.SelectedIndex,
  60. };
  61. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime)
  62. {
  63. // Make sure the windows have unique names and AutomationIds.
  64. var existing = lifetime.Windows.OfType<ShowWindowTest>().Count();
  65. if (existing > 0)
  66. {
  67. AutomationProperties.SetAutomationId(window, window.Name + (existing + 1));
  68. window.Title += $" {existing + 1}";
  69. }
  70. }
  71. if (size.HasValue)
  72. {
  73. window.Width = size.Value.Width;
  74. window.Height = size.Value.Height;
  75. }
  76. sizeTextBox.Text = string.Empty;
  77. switch (modeComboBox.SelectedIndex)
  78. {
  79. case 0:
  80. window.Show();
  81. break;
  82. case 1:
  83. window.Show(owner);
  84. break;
  85. case 2:
  86. window.ShowDialog(owner);
  87. break;
  88. }
  89. }
  90. private void SendToBack()
  91. {
  92. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  93. foreach (var window in lifetime.Windows)
  94. {
  95. window.Activate();
  96. }
  97. }
  98. private void RestoreAll()
  99. {
  100. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  101. foreach (var window in lifetime.Windows)
  102. {
  103. window.Show();
  104. if (window.WindowState == WindowState.Minimized)
  105. window.WindowState = WindowState.Normal;
  106. }
  107. }
  108. private void InitializeGesturesTab()
  109. {
  110. var gestureBorder = this.GetControl<Border>("GestureBorder");
  111. var gestureBorder2 = this.GetControl<Border>("GestureBorder2");
  112. var lastGesture = this.GetControl<TextBlock>("LastGesture");
  113. var resetGestures = this.GetControl<Button>("ResetGestures");
  114. gestureBorder.Tapped += (s, e) => lastGesture.Text = "Tapped";
  115. gestureBorder.DoubleTapped += (s, e) =>
  116. {
  117. lastGesture.Text = "DoubleTapped";
  118. // Testing #8733
  119. gestureBorder.IsVisible = false;
  120. gestureBorder2.IsVisible = true;
  121. };
  122. gestureBorder2.DoubleTapped += (s, e) =>
  123. {
  124. lastGesture.Text = "DoubleTapped2";
  125. };
  126. Gestures.AddRightTappedHandler(gestureBorder, (s, e) => lastGesture.Text = "RightTapped");
  127. resetGestures.Click += (s, e) =>
  128. {
  129. lastGesture.Text = string.Empty;
  130. gestureBorder.IsVisible = true;
  131. gestureBorder2.IsVisible = false;
  132. };
  133. }
  134. private void MenuClicked(object? sender, RoutedEventArgs e)
  135. {
  136. var clickedMenuItemTextBlock = this.Get<TextBlock>("ClickedMenuItem");
  137. clickedMenuItemTextBlock.Text = (sender as MenuItem)?.Header?.ToString();
  138. }
  139. private void OnButtonClick(object? sender, RoutedEventArgs e)
  140. {
  141. var source = e.Source as Button;
  142. if (source?.Name == "ComboBoxSelectionClear")
  143. this.Get<ComboBox>("BasicComboBox").SelectedIndex = -1;
  144. if (source?.Name == "ComboBoxSelectFirst")
  145. this.Get<ComboBox>("BasicComboBox").SelectedIndex = 0;
  146. if (source?.Name == "ListBoxSelectionClear")
  147. this.Get<ListBox>("BasicListBox").SelectedIndex = -1;
  148. if (source?.Name == "MenuClickedMenuItemReset")
  149. this.Get<TextBlock>("ClickedMenuItem").Text = "None";
  150. if (source?.Name == "ShowWindow")
  151. ShowWindow();
  152. if (source?.Name == "SendToBack")
  153. SendToBack();
  154. if (source?.Name == "ExitFullscreen")
  155. WindowState = WindowState.Normal;
  156. if (source?.Name == "RestoreAll")
  157. RestoreAll();
  158. }
  159. }
  160. }