123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Avalonia;
- using Avalonia.Automation;
- using Avalonia.Controls;
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.Input;
- using Avalonia.Interactivity;
- using Avalonia.Media;
- using Avalonia.Markup.Xaml;
- using Avalonia.VisualTree;
- using Microsoft.CodeAnalysis;
- using Avalonia.Controls.Primitives;
- using Avalonia.Threading;
- using Avalonia.Controls.Primitives.PopupPositioning;
- namespace IntegrationTestApp
- {
- public class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- InitializeViewMenu();
- InitializeGesturesTab();
- this.AttachDevTools();
- AddHandler(Button.ClickEvent, OnButtonClick);
- ListBoxItems = Enumerable.Range(0, 100).Select(x => "Item " + x).ToList();
- DataContext = this;
- }
- public List<string> ListBoxItems { get; }
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- }
- private void InitializeViewMenu()
- {
- var mainTabs = this.Get<TabControl>("MainTabs");
- var viewMenu = (NativeMenuItem)NativeMenu.GetMenu(this).Items[1];
- if (mainTabs.Items is not null)
- {
- foreach (TabItem tabItem in mainTabs.Items)
- {
- var menuItem = new NativeMenuItem
- {
- Header = (string)tabItem.Header!,
- IsChecked = tabItem.IsSelected,
- ToggleType = NativeMenuItemToggleType.Radio,
- };
- menuItem.Click += (s, e) => tabItem.IsSelected = true;
- viewMenu?.Menu?.Items.Add(menuItem);
- }
- }
- }
- private void ShowWindow()
- {
- var sizeTextBox = this.GetControl<TextBox>("ShowWindowSize");
- var modeComboBox = this.GetControl<ComboBox>("ShowWindowMode");
- var locationComboBox = this.GetControl<ComboBox>("ShowWindowLocation");
- var stateComboBox = this.GetControl<ComboBox>("ShowWindowState");
- var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null;
- var owner = (Window)this.GetVisualRoot()!;
- var window = new ShowWindowTest
- {
- WindowStartupLocation = (WindowStartupLocation)locationComboBox.SelectedIndex,
- };
- if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime)
- {
- // Make sure the windows have unique names and AutomationIds.
- var existing = lifetime.Windows.OfType<ShowWindowTest>().Count();
- if (existing > 0)
- {
- AutomationProperties.SetAutomationId(window, window.Name + (existing + 1));
- window.Title += $" {existing + 1}";
- }
- }
-
- if (size.HasValue)
- {
- window.Width = size.Value.Width;
- window.Height = size.Value.Height;
- }
- sizeTextBox.Text = string.Empty;
- window.WindowState = (WindowState)stateComboBox.SelectedIndex;
- switch (modeComboBox.SelectedIndex)
- {
- case 0:
- window.Show();
- break;
- case 1:
- window.Show(owner);
- break;
- case 2:
- window.ShowDialog(owner);
- break;
- }
- }
- private void ShowTransparentWindow()
- {
- // Show a background window to make sure the color behind the transparent window is
- // a known color (green).
- var backgroundWindow = new Window
- {
- Title = "Transparent Window Background",
- Name = "TransparentWindowBackground",
- Width = 300,
- Height = 300,
- Background = Brushes.Green,
- WindowStartupLocation = WindowStartupLocation.CenterOwner,
- };
- // This is the transparent window with a red circle.
- var window = new Window
- {
- Title = "Transparent Window",
- Name = "TransparentWindow",
- SystemDecorations = SystemDecorations.None,
- Background = Brushes.Transparent,
- TransparencyLevelHint = WindowTransparencyLevel.Transparent,
- WindowStartupLocation = WindowStartupLocation.CenterOwner,
- Width = 200,
- Height = 200,
- Content = new Border
- {
- Background = Brushes.Red,
- CornerRadius = new CornerRadius(100),
- }
- };
- window.PointerPressed += (_, _) =>
- {
- window.Close();
- backgroundWindow.Close();
- };
- backgroundWindow.Show(this);
- window.Show(backgroundWindow);
- }
- private void ShowTransparentPopup()
- {
- var popup = new Popup
- {
- WindowManagerAddShadowHint = false,
- PlacementMode = PlacementMode.AnchorAndGravity,
- PlacementAnchor = PopupAnchor.Top,
- PlacementGravity = PopupGravity.Bottom,
- Width= 200,
- Height= 200,
- Child = new Border
- {
- Background = Brushes.Red,
- CornerRadius = new CornerRadius(100),
- }
- };
- // Show a background window to make sure the color behind the transparent window is
- // a known color (green).
- var backgroundWindow = new Window
- {
- Title = "Transparent Popup Background",
- Name = "TransparentPopupBackground",
- Width = 200,
- Height = 200,
- Background = Brushes.Green,
- WindowStartupLocation = WindowStartupLocation.CenterOwner,
- Content = new Border
- {
- Name = "PopupContainer",
- Child = popup,
- [AutomationProperties.AccessibilityViewProperty] = AccessibilityView.Content,
- }
- };
- backgroundWindow.PointerPressed += (_, _) => backgroundWindow.Close();
- backgroundWindow.Show(this);
- popup.Open();
- }
- private void SendToBack()
- {
- var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
- foreach (var window in lifetime.Windows)
- {
- window.Activate();
- }
- }
- private void RestoreAll()
- {
- var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
- foreach (var window in lifetime.Windows)
- {
- window.Show();
- if (window.WindowState == WindowState.Minimized)
- window.WindowState = WindowState.Normal;
- }
- }
- private void InitializeGesturesTab()
- {
- var gestureBorder = this.GetControl<Border>("GestureBorder");
- var gestureBorder2 = this.GetControl<Border>("GestureBorder2");
- var lastGesture = this.GetControl<TextBlock>("LastGesture");
- var resetGestures = this.GetControl<Button>("ResetGestures");
- gestureBorder.Tapped += (s, e) => lastGesture.Text = "Tapped";
-
- gestureBorder.DoubleTapped += (s, e) =>
- {
- lastGesture.Text = "DoubleTapped";
- // Testing #8733
- gestureBorder.IsVisible = false;
- gestureBorder2.IsVisible = true;
- };
- gestureBorder2.DoubleTapped += (s, e) =>
- {
- lastGesture.Text = "DoubleTapped2";
- };
- Gestures.AddRightTappedHandler(gestureBorder, (s, e) => lastGesture.Text = "RightTapped");
-
- resetGestures.Click += (s, e) =>
- {
- lastGesture.Text = string.Empty;
- gestureBorder.IsVisible = true;
- gestureBorder2.IsVisible = false;
- };
- }
- private void MenuClicked(object? sender, RoutedEventArgs e)
- {
- var clickedMenuItemTextBlock = this.Get<TextBlock>("ClickedMenuItem");
- clickedMenuItemTextBlock.Text = (sender as MenuItem)?.Header?.ToString();
- }
- private void OnButtonClick(object? sender, RoutedEventArgs e)
- {
- var source = e.Source as Button;
- if (source?.Name == "ComboBoxSelectionClear")
- this.Get<ComboBox>("BasicComboBox").SelectedIndex = -1;
- if (source?.Name == "ComboBoxSelectFirst")
- this.Get<ComboBox>("BasicComboBox").SelectedIndex = 0;
- if (source?.Name == "ListBoxSelectionClear")
- this.Get<ListBox>("BasicListBox").SelectedIndex = -1;
- if (source?.Name == "MenuClickedMenuItemReset")
- this.Get<TextBlock>("ClickedMenuItem").Text = "None";
- if (source?.Name == "ShowTransparentWindow")
- ShowTransparentWindow();
- if (source?.Name == "ShowTransparentPopup")
- ShowTransparentPopup();
- if (source?.Name == "ShowWindow")
- ShowWindow();
- if (source?.Name == "SendToBack")
- SendToBack();
- if (source?.Name == "EnterFullscreen")
- WindowState = WindowState.FullScreen;
- if (source?.Name == "ExitFullscreen")
- WindowState = WindowState.Normal;
- if (source?.Name == "RestoreAll")
- RestoreAll();
- }
- }
- }
|