Skip to content

Commit

Permalink
Merge pull request #452 from WCKYWCKF/A-few-minor-issues
Browse files Browse the repository at this point in the history
Fixed: Closing a dialog returns TView to the user instead of occupying its parent.
  • Loading branch information
rabbitism authored Oct 31, 2024
2 parents f70e4de + a9ce9fa commit f07531a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 17 deletions.
54 changes: 37 additions & 17 deletions src/Ursa/Controls/OverlayShared/OverlayFeedbackElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public abstract class OverlayFeedbackElement : ContentControl
nameof(Closed), RoutingStrategies.Bubble);

private bool _resizeDragging;

protected Panel? ContainerPanel;
private Rect _resizeDragStartBounds;
private Point _resizeDragStartPoint;

private WindowEdge? _windowEdge;

static OverlayFeedbackElement()
Expand All @@ -35,6 +35,12 @@ static OverlayFeedbackElement()
ClosedEvent.AddClassHandler<OverlayFeedbackElement>((o, e) => o.OnClosed(e));
}

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
Content = null;
}

public bool IsClosed
{
get => GetValue(IsClosedProperty);
Expand Down Expand Up @@ -96,7 +102,7 @@ internal void BeginResizeDrag(WindowEdge windowEdge, PointerPressedEventArgs e)
_resizeDragStartBounds = Bounds;
_windowEdge = windowEdge;
}

internal void BeginMoveDrag(PointerPressedEventArgs e)
{
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return;
Expand Down Expand Up @@ -133,9 +139,11 @@ protected override void OnPointerMoved(PointerEventArgs e)
var left = Canvas.GetLeft(this);
var top = Canvas.GetTop(this);
var width = _windowEdge is WindowEdge.West or WindowEdge.NorthWest or WindowEdge.SouthWest
? Bounds.Width : _resizeDragStartBounds.Width;
? Bounds.Width
: _resizeDragStartBounds.Width;
var height = _windowEdge is WindowEdge.North or WindowEdge.NorthEast or WindowEdge.NorthWest
? Bounds.Height : _resizeDragStartBounds.Height;
? Bounds.Height
: _resizeDragStartBounds.Height;
var newBounds = CalculateNewBounds(left, top, width, height, diff, ContainerPanel?.Bounds, _windowEdge.Value);
Canvas.SetLeft(this, newBounds.Left);
Canvas.SetTop(this, newBounds.Top);
Expand All @@ -144,37 +152,49 @@ protected override void OnPointerMoved(PointerEventArgs e)
AnchorAndUpdatePositionInfo();
}

private Rect CalculateNewBounds(double left, double top, double width, double height, Vector diff, Rect? containerBounds,
private Rect CalculateNewBounds(double left, double top, double width, double height, Vector diff,
Rect? containerBounds,
WindowEdge windowEdge)
{
diff = CoerceDelta(left, top, width, height, diff, containerBounds, windowEdge);
switch (windowEdge)
{
case WindowEdge.North:
top += diff.Y; height -= diff.Y;
top += diff.Y;
height -= diff.Y;
break;
case WindowEdge.NorthEast:
top += diff.Y; width += diff.X; height -= diff.Y;
top += diff.Y;
width += diff.X;
height -= diff.Y;
break;
case WindowEdge.East:
width += diff.X;
break;
case WindowEdge.SouthEast:
width += diff.X; height += diff.Y;
width += diff.X;
height += diff.Y;
break;
case WindowEdge.South:
height += diff.Y;
break;
case WindowEdge.SouthWest:
left += diff.X; width -= diff.X; height += diff.Y;
left += diff.X;
width -= diff.X;
height += diff.Y;
break;
case WindowEdge.West:
left += diff.X; width -= diff.X;
left += diff.X;
width -= diff.X;
break;
case WindowEdge.NorthWest:
left += diff.X; top += diff.Y; width -= diff.X; height -= diff.Y;
left += diff.X;
top += diff.Y;
width -= diff.X;
height -= diff.Y;
break;
}

return new Rect(left, top, width, height);
}

Expand All @@ -184,18 +204,18 @@ private Vector CoerceDelta(double left, double top, double width, double height,
if (containerBounds is null) return diff;
var minX = windowEdge is WindowEdge.West or WindowEdge.NorthWest or WindowEdge.SouthWest
? -left
: -width;
: -width;
var minY = windowEdge is WindowEdge.North or WindowEdge.NorthEast or WindowEdge.NorthWest
? -top
: -height;
var maxX = windowEdge is WindowEdge.West or WindowEdge.NorthWest or WindowEdge.SouthWest
? width-MinWidth
: containerBounds.Value.Width - left - width;
? width - MinWidth
: containerBounds.Value.Width - left - width;
var maxY = windowEdge is WindowEdge.North or WindowEdge.NorthEast or WindowEdge.NorthWest
? height-MinWidth
? height - MinWidth
: containerBounds.Value.Height - top - height;
return new Vector(MathHelpers.SafeClamp(diff.X, minX, maxX), MathHelpers.SafeClamp(diff.Y, minY, maxY));
}

protected internal abstract void AnchorAndUpdatePositionInfo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Headless;
using Avalonia.Headless.XUnit;
using Avalonia.Input;
using Avalonia.Threading;

namespace HeadlessTest.Ursa.Controls.OverlayShared.Case_Close_Dialog_Clear_Content_Parent;

public class Test
{
[AvaloniaFact]
public void Dialog_Parent_Is_Cleared_After_Close()
{
var ursaWindow = new TestWindow();
ursaWindow.Show();
var button = ursaWindow.FindControl<Button>("button");
ursaWindow.MouseDown(new Point(280, 400), MouseButton.Left);
ursaWindow.MouseUp(new Point(280, 400), MouseButton.Left);
Dispatcher.UIThread.RunJobs();
var parent = ursaWindow.TextBox.Parent;
Assert.NotNull(ursaWindow.TextBox.Parent);
ursaWindow.DialogViewModel.Close();
Dispatcher.UIThread.RunJobs();
Assert.Null(ursaWindow.TextBox.Parent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<u:UrsaWindow xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
mc:Ignorable="d" d:DesignWidth="800"
d:DesignHeight="450"
x:Class="HeadlessTest.Ursa.Controls.OverlayShared.Case_Close_Dialog_Clear_Content_Parent.TestWindow"
Title="TestWindow">
<Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="button" Content="Click" Click="Button_OnClick"></Button>
</u:UrsaWindow>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using Irihi.Avalonia.Shared.Contracts;
using Ursa.Controls;

namespace HeadlessTest.Ursa.Controls.OverlayShared.Case_Close_Dialog_Clear_Content_Parent;

public partial class TestWindow : UrsaWindow
{
public TestWindow()
{
InitializeComponent();
}

internal TextBox TextBox = new TextBox();
internal DialogViewModel DialogViewModel = new DialogViewModel();

private void Button_OnClick(object? sender, RoutedEventArgs e)
{
OverlayDialog.Show(TextBox, DialogViewModel);
}
}

class DialogViewModel : IDialogContext
{
public void Close()
{
RequestClose?.Invoke(this, null);
}

public event EventHandler<object?>? RequestClose;
}

0 comments on commit f07531a

Please sign in to comment.