WindowDecorationsPage.axaml.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Media;
  4. using Avalonia.Platform;
  5. namespace IntegrationTestApp.Pages;
  6. public partial class WindowDecorationsPage : UserControl
  7. {
  8. public WindowDecorationsPage()
  9. {
  10. InitializeComponent();
  11. }
  12. private void SetWindowDecorations(Window window)
  13. {
  14. window.ExtendClientAreaToDecorationsHint = WindowExtendClientAreaToDecorationsHint.IsChecked!.Value;
  15. window.ExtendClientAreaTitleBarHeightHint =
  16. int.TryParse(WindowTitleBarHeightHint.Text, out var val) ? val / window.DesktopScaling : -1;
  17. window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome
  18. | (WindowForceSystemChrome.IsChecked == true ? ExtendClientAreaChromeHints.SystemChrome : 0)
  19. | (WindowPreferSystemChrome.IsChecked == true ? ExtendClientAreaChromeHints.PreferSystemChrome : 0)
  20. | (WindowMacThickSystemChrome.IsChecked == true ? ExtendClientAreaChromeHints.OSXThickTitleBar : 0);
  21. if (window is ShowWindowTest showWindowTest && WindowShowTitleAreaControl.IsChecked == true)
  22. {
  23. showWindowTest.ShowTitleAreaControl();
  24. }
  25. AdjustOffsets(window);
  26. window.Background = Brushes.Transparent;
  27. window.PropertyChanged += WindowOnPropertyChanged;
  28. void WindowOnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
  29. {
  30. var window = (Window)sender!;
  31. if (e.Property == Window.OffScreenMarginProperty || e.Property == Window.WindowDecorationMarginProperty)
  32. {
  33. AdjustOffsets(window);
  34. }
  35. }
  36. void AdjustOffsets(Window window)
  37. {
  38. var scaling = window.DesktopScaling;
  39. window.Padding = window.OffScreenMargin;
  40. ((Control)window.Content!).Margin = window.WindowDecorationMargin;
  41. WindowDecorationProperties.Text =
  42. $"{window.OffScreenMargin.Top * scaling} {window.WindowDecorationMargin.Top * scaling} {window.IsExtendedIntoWindowDecorations}";
  43. }
  44. }
  45. private void ApplyWindowDecorations_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  46. {
  47. var window = TopLevel.GetTopLevel(this) as Window ??
  48. throw new AvaloniaInternalException("WindowDecorationsPage is not attached to a Window.");
  49. SetWindowDecorations(window);
  50. }
  51. private void ShowNewWindowDecorations_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  52. {
  53. var window = new ShowWindowTest();
  54. SetWindowDecorations(window);
  55. window.Show();
  56. }
  57. }