feat: implement title bar and caption buttons.
This commit is contained in:
91
src/Ursa/Controls/TitleBar/TitleBar.cs
Normal file
91
src/Ursa/Controls/TitleBar/TitleBar.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Platform;
|
||||
using Irihi.Avalonia.Shared.Helpers;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class TitleBar: ContentControl
|
||||
{
|
||||
private CaptionButtons? _captionButtons;
|
||||
private InputElement? _background;
|
||||
private Window? _visualRoot;
|
||||
|
||||
public static readonly StyledProperty<object?> LeftContentProperty = AvaloniaProperty.Register<TitleBar, object?>(
|
||||
nameof(LeftContent));
|
||||
|
||||
public object? LeftContent
|
||||
{
|
||||
get => GetValue(LeftContentProperty);
|
||||
set => SetValue(LeftContentProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<object?> RightContentProperty = AvaloniaProperty.Register<TitleBar, object?>(
|
||||
nameof(RightContent));
|
||||
|
||||
public object? RightContent
|
||||
{
|
||||
get => GetValue(RightContentProperty);
|
||||
set => SetValue(RightContentProperty, value);
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
this._captionButtons?.Detach();
|
||||
this._captionButtons = e.NameScope.Get<CaptionButtons>("PART_CaptionButtons");
|
||||
this._background = e.NameScope.Get<InputElement>("PART_Background");
|
||||
if (!(this.VisualRoot is Window visualRoot))
|
||||
return;
|
||||
_visualRoot = visualRoot;
|
||||
DoubleTappedEvent.AddHandler(OnDoubleTapped, _background);
|
||||
PointerPressedEvent.AddHandler(OnPointerPressed, _background);
|
||||
this._captionButtons?.Attach(visualRoot);
|
||||
// this.UpdateSize(visualRoot);
|
||||
}
|
||||
|
||||
private void OnPointerPressed(object sender, PointerPressedEventArgs e)
|
||||
{
|
||||
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
|
||||
{
|
||||
_visualRoot?.BeginMoveDrag(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDoubleTapped(object sender, TappedEventArgs e)
|
||||
{
|
||||
if (_visualRoot is not null)
|
||||
{
|
||||
if (_visualRoot.WindowState == WindowState.Maximized)
|
||||
{
|
||||
_visualRoot.WindowState = WindowState.Normal;
|
||||
}
|
||||
else
|
||||
{
|
||||
_visualRoot.WindowState = WindowState.Maximized;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateSize(Window window)
|
||||
{
|
||||
Thickness offScreenMargin = window.OffScreenMargin;
|
||||
var left = offScreenMargin.Left;
|
||||
offScreenMargin = window.OffScreenMargin;
|
||||
double top = offScreenMargin.Top;
|
||||
offScreenMargin = window.OffScreenMargin;
|
||||
double right = offScreenMargin.Right;
|
||||
offScreenMargin = window.OffScreenMargin;
|
||||
double bottom = offScreenMargin.Bottom;
|
||||
this.Margin = new Thickness(left, top, right, bottom);
|
||||
if (window.WindowState != WindowState.FullScreen)
|
||||
{
|
||||
this.Height = window.WindowDecorationMargin.Top;
|
||||
if (this._captionButtons != null)
|
||||
this._captionButtons.Height = this.Height;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user