MainWindow.axaml.cs 6.6 KB

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