MainWindow.axaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using IntegrationTestApp.Models;
  6. using IntegrationTestApp.Pages;
  7. using IntegrationTestApp.ViewModels;
  8. namespace IntegrationTestApp
  9. {
  10. public partial class MainWindow : Window
  11. {
  12. public MainWindow()
  13. {
  14. // Set name in code behind, so source generator will ignore it.
  15. Name = "MainWindow";
  16. InitializeComponent();
  17. var viewModel = new MainWindowViewModel(CreatePages());
  18. InitializeViewMenu(viewModel.Pages);
  19. DataContext = viewModel;
  20. AppOverlayPopups.Text = Program.OverlayPopups ? "Overlay Popups" : "Native Popups";
  21. PositionChanged += OnPositionChanged;
  22. }
  23. private MainWindowViewModel? ViewModel => (MainWindowViewModel?)DataContext;
  24. private void InitializeViewMenu(IEnumerable<Page> pages)
  25. {
  26. var viewMenu = (NativeMenuItem?)NativeMenu.GetMenu(this)?.Items[1];
  27. foreach (var page in pages)
  28. {
  29. var menuItem = new NativeMenuItem
  30. {
  31. Header = (string?)page.Name,
  32. ToolTip = $"Tip:{(string?)page.Name}",
  33. ToggleType = NativeMenuItemToggleType.Radio,
  34. };
  35. menuItem.Click += (_, _) =>
  36. {
  37. if (ViewModel is { } viewModel)
  38. viewModel.SelectedPage = page;
  39. };
  40. viewMenu?.Menu?.Items.Add(menuItem);
  41. }
  42. }
  43. private void Pager_SelectionChanged(object? sender, SelectionChangedEventArgs e)
  44. {
  45. if (Pager.SelectedItem is Page page)
  46. PagerContent.Child = page.CreateContent();
  47. }
  48. private void OnPositionChanged(object? sender, PixelPointEventArgs e)
  49. {
  50. // HACK: Toggling the window decorations can cause the window to be moved off screen,
  51. // causing test failures. Until this bug is fixed, detect this and move the window
  52. // to the screen origin. See #11411.
  53. if (Screens.ScreenFromWindow(this) is { } screen)
  54. {
  55. var bounds = new PixelRect(
  56. e.Point,
  57. PixelSize.FromSize(ClientSize, DesktopScaling));
  58. if (!screen.WorkingArea.Contains(bounds))
  59. Position = screen.WorkingArea.Position;
  60. }
  61. }
  62. private static IEnumerable<Page> CreatePages()
  63. {
  64. return
  65. [
  66. new("Automation", () => new AutomationPage()),
  67. new("Button", () => new ButtonPage()),
  68. new("CheckBox", () => new CheckBoxPage()),
  69. new("ComboBox", () => new ComboBoxPage()),
  70. new("ContextMenu", () => new ContextMenuPage()),
  71. new("DesktopPage", () => new DesktopPage()),
  72. new("Embedding", () => new EmbeddingPage()),
  73. new("Gestures", () => new GesturesPage()),
  74. new("ListBox", () => new ListBoxPage()),
  75. new("Menu", () => new MenuPage()),
  76. new("Pointer", () => new PointerPage()),
  77. new("RadioButton", () => new RadioButtonPage()),
  78. new("Screens", () => new ScreensPage()),
  79. new("ScrollBar", () => new ScrollBarPage()),
  80. new("Slider", () => new SliderPage()),
  81. new("Window Decorations", () => new WindowDecorationsPage()),
  82. new("Window", () => new WindowPage()),
  83. ];
  84. }
  85. }
  86. }