Skip to content

Commit 89c5bfe

Browse files
authored
Merge pull request #80 from AvaloniaUtils/replaceGrid
Replaces PART_DialogHostRoot Grid with Panel to simplify calculations
2 parents 0fcd729 + 9e9cc80 commit 89c5bfe

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

DialogHost.Avalonia/DialogHost.axaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
<Setter Property="dialogHostAvalonia:DialogHostStyle.CornerRadius" Value="2" />
3232
<Setter Property="Template">
3333
<ControlTemplate>
34-
<Grid Name="PART_DialogHostRoot" Focusable="False">
34+
<Panel Name="PART_DialogHostRoot" Focusable="False">
3535
<ContentPresenter Name="PART_ContentPresenter"
3636
Content="{TemplateBinding Content}"
3737
ContentTemplate="{TemplateBinding ContentTemplate}"
3838
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
3939
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
40-
Tag="{TemplateBinding BlurBackgroundRadius}"/>
40+
Tag="{TemplateBinding BlurBackgroundRadius}" />
4141
<Rectangle Name="PART_ContentCover"
4242
Fill="{TemplateBinding OverlayBackground}"
4343
Focusable="False">
@@ -47,7 +47,7 @@
4747
</Style>
4848
</Rectangle.Styles>
4949
</Rectangle>
50-
</Grid>
50+
</Panel>
5151
</ControlTemplate>
5252
</Setter>
5353
<Setter Property="PopupTemplate">
@@ -85,7 +85,6 @@
8585
</Style>
8686
</Style>
8787

88-
8988
<Style Selector="^[IsOpen=True] /template/ Rectangle#PART_ContentCover">
9089
<Setter Property="IsHitTestVisible" Value="True" />
9190
<Setter Property="Opacity" Value="0.56" />

DialogHost.Avalonia/DialogHost.axaml.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Windows.Input;
66
using Avalonia;
77
using Avalonia.Controls;
8+
using Avalonia.Controls.Metadata;
89
using Avalonia.Controls.Primitives;
910
using Avalonia.Controls.Shapes;
1011
using Avalonia.Controls.Templates;
@@ -215,7 +216,7 @@ public static readonly StyledProperty<double> BlurBackgroundRadiusProperty
215216
private IDialogPopupPositioner? _popupPositioner;
216217
private IInputElement? _restoreFocusDialogClose;
217218

218-
private Grid? _root;
219+
private Panel _root;
219220

220221
private IDisposable? _templateDisposables;
221222

@@ -627,7 +628,9 @@ protected void RaiseCommandsCanExecuteChanged() {
627628
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) {
628629
_templateDisposables?.Dispose();
629630

630-
_root = e.NameScope.Find<Grid>(DialogHostRoot) ?? throw new InvalidOperationException($"No Grid with name {DialogHostRoot} found");
631+
_root = e.NameScope.Find<Panel>(DialogHostRoot)
632+
?? throw new InvalidOperationException($"No Panel with name {DialogHostRoot} found. " +
633+
$"Did you add the styles as stated in getting started?");
631634
_overlayPopupHost = new DialogOverlayPopupHost(_root) {
632635
Content = DialogContent, ContentTemplate = DialogContentTemplate, Template = PopupTemplate,
633636
Padding = DialogMargin, ClipToBounds = false, DisableOpeningAnimation = DisableOpeningAnimation,

DialogHost.Avalonia/DialogOverlayPopupHost.axaml.cs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace DialogHostAvalonia;
1010

11-
public class DialogOverlayPopupHost : ContentControl, ICustomKeyboardNavigation {
11+
public class DialogOverlayPopupHost(Panel root) : ContentControl, ICustomKeyboardNavigation {
1212
public static readonly DirectProperty<DialogOverlayPopupHost, bool> IsOpenProperty =
1313
AvaloniaProperty.RegisterDirect<DialogOverlayPopupHost, bool>(
1414
nameof(IsOpen),
@@ -28,17 +28,10 @@ public class DialogOverlayPopupHost : ContentControl, ICustomKeyboardNavigation
2828
o => o.PopupPositioner,
2929
(o, v) => o.PopupPositioner = v);
3030

31-
private readonly Grid _root;
32-
3331
private bool _disableOpeningAnimation;
3432
private bool _isOpen;
3533
private IDialogPopupPositioner? _popupPositioner;
3634

37-
public DialogOverlayPopupHost(Grid root)
38-
{
39-
this._root = root;
40-
}
41-
4235
public bool IsOpen {
4336
get => _isOpen;
4437
set {
@@ -71,52 +64,49 @@ public IDialogPopupPositioner? PopupPositioner {
7164
}
7265
}
7366

74-
public void Show()
75-
{
67+
public void Show() {
7668
if (Parent == null) {
77-
_root.Children.Add(this);
69+
root.Children.Add(this);
7870
}
71+
7972
// Set the minimum priority to allow overriding it everywhere
8073
ClearValue(IsActuallyOpenProperty);
8174
Focus();
8275
UpdatePosition();
8376
}
8477

85-
public void Hide()
86-
{
87-
_root.Children.Remove(this);
78+
public void Hide() {
79+
root.Children.Remove(this);
8880
}
8981

90-
protected override Size MeasureOverride(Size availableSize)
91-
{
92-
if (PopupPositioner is IDialogPopupPositionerConstrainable constrainable)
93-
{
82+
protected override Size MeasureOverride(Size availableSize) {
83+
if (PopupPositioner is IDialogPopupPositionerConstrainable constrainable) {
9484
return base.MeasureOverride(constrainable.Constrain(availableSize));
9585
}
86+
9687
return base.MeasureOverride(availableSize);
9788
}
9889

9990
/// <inheritdoc />
10091
protected override void ArrangeCore(Rect finalRect) {
10192
var margin = Margin;
102-
93+
10394
var size = new Size(
10495
Math.Max(0, finalRect.Width - margin.Left - margin.Right),
10596
Math.Max(0, finalRect.Height - margin.Top - margin.Bottom));
106-
97+
10798
var contentSize = new Size(
108-
Math.Min(size.Width, DesiredSize.Width - margin.Left - margin.Right),
99+
Math.Min(size.Width, DesiredSize.Width - margin.Left - margin.Right),
109100
Math.Min(size.Height, DesiredSize.Height - margin.Top - margin.Bottom));
110101
var positioner = PopupPositioner ?? CenteredDialogPopupPositioner.Instance;
111102
var bounds = positioner.Update(size, contentSize);
112-
103+
113104
var (finalWidth, finalHeight) = ArrangeOverride(bounds.Size).Constrain(size);
114105
Bounds = new Rect(bounds.X + margin.Left, bounds.Y + margin.Top, finalWidth, finalHeight);
115106
}
116107

117108

118-
private void UpdatePosition()
119-
{
109+
private void UpdatePosition() {
120110
// Don't bother the positioner with layout system artifacts
121111
// if (_positionerParameters.Size.Width == 0 || _positionerParameters.Size.Height == 0)
122112
// return;
@@ -131,7 +121,7 @@ private void UpdatePosition()
131121
if (!element.Equals(this)) {
132122
return (false, null);
133123
}
134-
124+
135125
// Finding the focusable descendant
136126
var focusable = this.GetVisualDescendants()
137127
.OfType<IInputElement>()
@@ -146,6 +136,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
146136
if (change.Property == IsActuallyOpenProperty && !change.GetNewValue<bool>()) {
147137
Hide();
148138
}
139+
149140
base.OnPropertyChanged(change);
150141
}
151142
}

0 commit comments

Comments
 (0)