feat: implement layer change.
This commit is contained in:
@@ -26,11 +26,6 @@
|
|||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
Grid.ColumnSpan="2"
|
Grid.ColumnSpan="2"
|
||||||
Background="Transparent" >
|
Background="Transparent" >
|
||||||
<Panel.ContextFlyout>
|
|
||||||
<MenuFlyout>
|
|
||||||
<MenuItem Header="Bring to Top"></MenuItem>
|
|
||||||
</MenuFlyout>
|
|
||||||
</Panel.ContextFlyout>
|
|
||||||
</Panel>
|
</Panel>
|
||||||
<TextBlock
|
<TextBlock
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
@@ -55,6 +50,16 @@
|
|||||||
<Setter Property="Grid.Row" Value="0" />
|
<Setter Property="Grid.Row" Value="0" />
|
||||||
<Setter Property="Grid.RowSpan" Value="3" />
|
<Setter Property="Grid.RowSpan" Value="3" />
|
||||||
</Style>
|
</Style>
|
||||||
|
<Style Selector="^:not(:modal) /template/ Panel#PART_TitleArea">
|
||||||
|
<Setter Property="ContextFlyout">
|
||||||
|
<MenuFlyout>
|
||||||
|
<MenuItem Header="Bring Forward" Command="{Binding $parent[u:DialogControl].BringForward}"></MenuItem>
|
||||||
|
<MenuItem Header="Send Backward" Command="{Binding $parent[u:DialogControl].SendBackward}" ></MenuItem>
|
||||||
|
<MenuItem Header="Bring To Front" Command="{Binding $parent[u:DialogControl].BringToFront}"></MenuItem>
|
||||||
|
<MenuItem Header="Send To Back" Command="{Binding $parent[u:DialogControl].SendToBack}"></MenuItem>
|
||||||
|
</MenuFlyout>
|
||||||
|
</Setter>
|
||||||
|
</Style>
|
||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
|
|
||||||
<ControlTheme x:Key="{x:Type u:DialogWindow}" TargetType="u:DialogWindow">
|
<ControlTheme x:Key="{x:Type u:DialogWindow}" TargetType="u:DialogWindow">
|
||||||
|
|||||||
@@ -97,6 +97,8 @@ public static class DialogBox
|
|||||||
{
|
{
|
||||||
Content = new TView() { DataContext = vm },
|
Content = new TView() { DataContext = vm },
|
||||||
DataContext = vm,
|
DataContext = vm,
|
||||||
|
ExtendToClientArea = options.ExtendToClientArea,
|
||||||
|
Title = options.Title,
|
||||||
};
|
};
|
||||||
var host = OverlayDialogManager.GetOverlayDialogHost(hostId);
|
var host = OverlayDialogManager.GetOverlayDialogHost(hostId);
|
||||||
host?.AddModalDialog(t);
|
host?.AddModalDialog(t);
|
||||||
|
|||||||
@@ -15,10 +15,12 @@ public class DialogControl: ContentControl
|
|||||||
public const string PART_CloseButton = "PART_CloseButton";
|
public const string PART_CloseButton = "PART_CloseButton";
|
||||||
public const string PART_TitleArea = "PART_TitleArea";
|
public const string PART_TitleArea = "PART_TitleArea";
|
||||||
public const string PC_ExtendClientArea = ":extend";
|
public const string PC_ExtendClientArea = ":extend";
|
||||||
|
public const string PC_Modal = ":modal";
|
||||||
|
|
||||||
private Button? _closeButton;
|
private Button? _closeButton;
|
||||||
private Panel? _titleArea;
|
private Panel? _titleArea;
|
||||||
public event EventHandler<object?>? OnClose;
|
public event EventHandler<object?>? OnClose;
|
||||||
|
public event EventHandler<DialogLayerChangeEventArgs>? OnLayerChange;
|
||||||
|
|
||||||
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DialogControl, string?>(
|
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DialogControl, string?>(
|
||||||
nameof(Title));
|
nameof(Title));
|
||||||
@@ -93,7 +95,7 @@ public class DialogControl: ContentControl
|
|||||||
public Task<T> ShowAsync<T>()
|
public Task<T> ShowAsync<T>()
|
||||||
{
|
{
|
||||||
var tcs = new TaskCompletionSource<T>();
|
var tcs = new TaskCompletionSource<T>();
|
||||||
|
PseudoClasses.Set(PC_Modal, true);
|
||||||
void OnCloseHandler(object sender, object? args)
|
void OnCloseHandler(object sender, object? args)
|
||||||
{
|
{
|
||||||
if (args is T result)
|
if (args is T result)
|
||||||
@@ -124,4 +126,24 @@ public class DialogControl: ContentControl
|
|||||||
OnClose?.Invoke(this, null);
|
OnClose?.Invoke(this, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void BringForward()
|
||||||
|
{
|
||||||
|
OnLayerChange?.Invoke(this, new DialogLayerChangeEventArgs(DialogLayerChangeType.BringForward));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SendBackward()
|
||||||
|
{
|
||||||
|
OnLayerChange?.Invoke(this, new DialogLayerChangeEventArgs(DialogLayerChangeType.SendBackward));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void BringToFront()
|
||||||
|
{
|
||||||
|
OnLayerChange?.Invoke(this, new DialogLayerChangeEventArgs(DialogLayerChangeType.BringToFront));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SendToBack()
|
||||||
|
{
|
||||||
|
OnLayerChange?.Invoke(this, new DialogLayerChangeEventArgs(DialogLayerChangeType.SendToBack));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
18
src/Ursa/Controls/Dialog/DialogLayerChangeEventArgs.cs
Normal file
18
src/Ursa/Controls/Dialog/DialogLayerChangeEventArgs.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
|
public class DialogLayerChangeEventArgs
|
||||||
|
{
|
||||||
|
public DialogLayerChangeType ChangeType { get; }
|
||||||
|
public DialogLayerChangeEventArgs(DialogLayerChangeType type)
|
||||||
|
{
|
||||||
|
ChangeType = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum DialogLayerChangeType
|
||||||
|
{
|
||||||
|
BringForward,
|
||||||
|
SendBackward,
|
||||||
|
BringToFront,
|
||||||
|
SendToBack
|
||||||
|
}
|
||||||
@@ -9,37 +9,38 @@ using Avalonia.Media;
|
|||||||
using Avalonia.Utilities;
|
using Avalonia.Utilities;
|
||||||
using Avalonia.VisualTree;
|
using Avalonia.VisualTree;
|
||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
public class OverlayDialogHost: Canvas
|
public class OverlayDialogHost : Canvas
|
||||||
{
|
{
|
||||||
private readonly List<DialogControl> _dialogs = new();
|
private readonly List<DialogControl> _dialogs = new();
|
||||||
private readonly List<DialogControl> _modalDialogs = new();
|
private readonly List<DialogControl> _modalDialogs = new();
|
||||||
private readonly List<Border> _masks = new();
|
private readonly List<Border> _masks = new();
|
||||||
|
|
||||||
public string? HostId { get; set; }
|
public string? HostId { get; set; }
|
||||||
|
|
||||||
private Point _lastPoint;
|
private Point _lastPoint;
|
||||||
|
|
||||||
public static readonly StyledProperty<IBrush?> OverlayMaskBrushProperty = AvaloniaProperty.Register<OverlayDialogHost, IBrush?>(
|
public static readonly StyledProperty<IBrush?> OverlayMaskBrushProperty =
|
||||||
nameof(OverlayMaskBrush));
|
AvaloniaProperty.Register<OverlayDialogHost, IBrush?>(
|
||||||
|
nameof(OverlayMaskBrush));
|
||||||
|
|
||||||
public IBrush? OverlayMaskBrush
|
public IBrush? OverlayMaskBrush
|
||||||
{
|
{
|
||||||
get => GetValue(OverlayMaskBrushProperty);
|
get => GetValue(OverlayMaskBrushProperty);
|
||||||
set => SetValue(OverlayMaskBrushProperty, value);
|
set => SetValue(OverlayMaskBrushProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Border CreateOverlayMask() => new()
|
private Border CreateOverlayMask() => new()
|
||||||
{
|
{
|
||||||
HorizontalAlignment = HorizontalAlignment.Stretch,
|
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||||
VerticalAlignment = VerticalAlignment.Stretch,
|
VerticalAlignment = VerticalAlignment.Stretch,
|
||||||
Width = this.Bounds.Width,
|
Width = this.Bounds.Width,
|
||||||
Height = this.Bounds.Height,
|
Height = this.Bounds.Height,
|
||||||
[!BackgroundProperty] = this[!OverlayMaskBrushProperty],
|
[!BackgroundProperty] = this[!OverlayMaskBrushProperty],
|
||||||
IsVisible = true,
|
IsVisible = true,
|
||||||
};
|
};
|
||||||
|
|
||||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnAttachedToVisualTree(e);
|
base.OnAttachedToVisualTree(e);
|
||||||
@@ -70,7 +71,7 @@ public class OverlayDialogHost: Canvas
|
|||||||
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
|
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
|
||||||
{
|
{
|
||||||
var p = e.GetPosition(this);
|
var p = e.GetPosition(this);
|
||||||
var left= p.X - _lastPoint.X;
|
var left = p.X - _lastPoint.X;
|
||||||
var top = p.Y - _lastPoint.Y;
|
var top = p.Y - _lastPoint.Y;
|
||||||
left = MathUtilities.Clamp(left, 0, Bounds.Width - item.Bounds.Width);
|
left = MathUtilities.Clamp(left, 0, Bounds.Width - item.Bounds.Width);
|
||||||
top = MathUtilities.Clamp(top, 0, Bounds.Height - item.Bounds.Height);
|
top = MathUtilities.Clamp(top, 0, Bounds.Height - item.Bounds.Height);
|
||||||
@@ -93,7 +94,9 @@ public class OverlayDialogHost: Canvas
|
|||||||
{
|
{
|
||||||
this.Children.Add(control);
|
this.Children.Add(control);
|
||||||
_dialogs.Add(control);
|
_dialogs.Add(control);
|
||||||
|
control.ZIndex = Children.Last().ZIndex + 1;
|
||||||
control.OnClose += OnDialogClose;
|
control.OnClose += OnDialogClose;
|
||||||
|
control.OnLayerChange += OnDialogLayerChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDialogClose(object sender, object? e)
|
private void OnDialogClose(object sender, object? e)
|
||||||
@@ -102,11 +105,12 @@ public class OverlayDialogHost: Canvas
|
|||||||
{
|
{
|
||||||
this.Children.Remove(control);
|
this.Children.Remove(control);
|
||||||
control.OnClose -= OnDialogClose;
|
control.OnClose -= OnDialogClose;
|
||||||
|
control.OnLayerChange -= OnDialogLayerChange;
|
||||||
if (_dialogs.Contains(control))
|
if (_dialogs.Contains(control))
|
||||||
{
|
{
|
||||||
_dialogs.Remove(control);
|
_dialogs.Remove(control);
|
||||||
}
|
}
|
||||||
else if(_modalDialogs.Contains(control))
|
else if (_modalDialogs.Contains(control))
|
||||||
{
|
{
|
||||||
_modalDialogs.Remove(control);
|
_modalDialogs.Remove(control);
|
||||||
if (_masks.Count > 0)
|
if (_masks.Count > 0)
|
||||||
@@ -116,7 +120,7 @@ public class OverlayDialogHost: Canvas
|
|||||||
_masks.Remove(last);
|
_masks.Remove(last);
|
||||||
if (_masks.Count > 0)
|
if (_masks.Count > 0)
|
||||||
{
|
{
|
||||||
_masks.Last().IsVisible= true;
|
_masks.Last().IsVisible = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,7 +137,41 @@ public class OverlayDialogHost: Canvas
|
|||||||
{
|
{
|
||||||
_masks[i].IsVisible = false;
|
_masks[i].IsVisible = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_masks.Add(mask);
|
_masks.Add(mask);
|
||||||
control.OnClose += OnDialogClose;
|
control.OnClose += OnDialogClose;
|
||||||
|
control.OnLayerChange += OnDialogLayerChange;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDialogLayerChange(object sender, DialogLayerChangeEventArgs e)
|
||||||
|
{
|
||||||
|
if (sender is not DialogControl control)
|
||||||
|
return;
|
||||||
|
if (!_dialogs.Contains(control))
|
||||||
|
return;
|
||||||
|
int index = _dialogs.IndexOf(control);
|
||||||
|
_dialogs.Remove(control);
|
||||||
|
int newIndex = index;
|
||||||
|
switch (e.ChangeType)
|
||||||
|
{
|
||||||
|
case DialogLayerChangeType.BringForward:
|
||||||
|
newIndex = MathUtilities.Clamp(index + 1, 0, _dialogs.Count);
|
||||||
|
break;
|
||||||
|
case DialogLayerChangeType.SendBackward:
|
||||||
|
newIndex = MathUtilities.Clamp(index - 1, 0, _dialogs.Count);
|
||||||
|
break;
|
||||||
|
case DialogLayerChangeType.BringToFront:
|
||||||
|
newIndex = _dialogs.Count;
|
||||||
|
break;
|
||||||
|
case DialogLayerChangeType.SendToBack:
|
||||||
|
newIndex = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
_dialogs.Insert(newIndex, control);
|
||||||
|
for (int i = 0; i < _dialogs.Count; i++)
|
||||||
|
{
|
||||||
|
_dialogs[i].ZIndex = i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user