123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Controls.Shapes;
- using Avalonia.LogicalTree;
- using Avalonia.Markup.Xaml;
- using Avalonia.Media.Imaging;
- using Avalonia.Platform;
- using System;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Threading.Tasks;
- namespace fis
- {
- public class MacosTitleBar : UserControl
- {
- private Button closeButton;
- private Button minimizeButton;
- private Button zoomButton;
- private DockPanel titleBarBackground;
- private StackPanel titleAndWindowIconWrapper;
- public static readonly StyledProperty<bool> IsSeamlessProperty =
- AvaloniaProperty.Register<MacosTitleBar, bool>(nameof(IsSeamless));
- public bool IsSeamless
- {
- get { return GetValue(IsSeamlessProperty); }
- set {
- SetValue(IsSeamlessProperty, value);
- if (titleBarBackground != null && titleAndWindowIconWrapper != null)
- {
- titleBarBackground.IsVisible = IsSeamless ? false : true;
- titleAndWindowIconWrapper.IsVisible = IsSeamless ? false : true;
- }
- }
- }
- public MacosTitleBar()
- {
- this.InitializeComponent();
- if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) == false)
- {
- this.IsVisible = false;
- }
- else
- {
- minimizeButton = this.FindControl<Button>("MinimizeButton");
- zoomButton = this.FindControl<Button>("ZoomButton");
- closeButton = this.FindControl<Button>("CloseButton");
- minimizeButton.Click += MinimizeWindow;
- zoomButton.Click += MaximizeWindow;
- closeButton.Click += CloseWindow;
- titleBarBackground = this.FindControl<DockPanel>("TitleBarBackground");
- titleAndWindowIconWrapper = this.FindControl<StackPanel>("TitleAndWindowIconWrapper");
- SubscribeToWindowState();
- }
- }
- private void CloseWindow(object sender, Avalonia.Interactivity.RoutedEventArgs e)
- {
- Window hostWindow = (Window)this.VisualRoot;
- hostWindow.Close();
- }
- private void MaximizeWindow(object sender, Avalonia.Interactivity.RoutedEventArgs e)
- {
- Window hostWindow = (Window)this.VisualRoot;
- if (hostWindow.WindowState == WindowState.Normal)
- {
- hostWindow.WindowState = WindowState.Maximized;
- }
- else
- {
- hostWindow.WindowState = WindowState.Normal;
- }
- }
- private void MinimizeWindow(object sender, Avalonia.Interactivity.RoutedEventArgs e)
- {
- Window hostWindow = (Window)this.VisualRoot;
- hostWindow.WindowState = WindowState.Minimized;
- }
- private async void SubscribeToWindowState()
- {
- Window hostWindow = (Window)this.VisualRoot;
- while (hostWindow == null)
- {
- hostWindow = (Window)this.VisualRoot;
- await Task.Delay(50);
- }
- hostWindow.ExtendClientAreaTitleBarHeightHint = 44;
- hostWindow.GetObservable(Window.WindowStateProperty).Subscribe(s =>
- {
- if (s != WindowState.Maximized)
- {
- hostWindow.Padding = new Thickness(0, 0, 0, 0);
- }
- if (s == WindowState.Maximized)
- {
- hostWindow.Padding = new Thickness(7, 7, 7, 7);
- // This should be a more universal approach in both cases, but I found it to be less reliable, when for example double-clicking the title bar.
- /*hostWindow.Padding = new Thickness(
- hostWindow.OffScreenMargin.Left,
- hostWindow.OffScreenMargin.Top,
- hostWindow.OffScreenMargin.Right,
- hostWindow.OffScreenMargin.Bottom);*/
- }
- });
- }
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- }
- }
- }
|