WindowDecorationsPage.axaml.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. AdjustOffsets(window);
  22. window.Background = Brushes.Transparent;
  23. window.PropertyChanged += WindowOnPropertyChanged;
  24. void WindowOnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
  25. {
  26. var window = (Window)sender!;
  27. if (e.Property == Window.OffScreenMarginProperty || e.Property == Window.WindowDecorationMarginProperty)
  28. {
  29. AdjustOffsets(window);
  30. }
  31. }
  32. void AdjustOffsets(Window window)
  33. {
  34. var scaling = window.DesktopScaling;
  35. window.Padding = window.OffScreenMargin;
  36. ((Control)window.Content!).Margin = window.WindowDecorationMargin;
  37. WindowDecorationProperties.Text =
  38. $"{window.OffScreenMargin.Top * scaling} {window.WindowDecorationMargin.Top * scaling} {window.IsExtendedIntoWindowDecorations}";
  39. }
  40. }
  41. private void ApplyWindowDecorations_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  42. {
  43. var window = TopLevel.GetTopLevel(this) as Window ??
  44. throw new AvaloniaInternalException("WindowDecorationsPage is not attached to a Window.");
  45. SetWindowDecorations(window);
  46. }
  47. private void ShowNewWindowDecorations_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  48. {
  49. var window = new ShowWindowTest();
  50. SetWindowDecorations(window);
  51. window.Show();
  52. }
  53. }