Skip to content

Commit

Permalink
Merge pull request #80 from AvaloniaUtils/replaceGrid
Browse files Browse the repository at this point in the history
Replaces PART_DialogHostRoot Grid with Panel to simplify calculations
  • Loading branch information
SKProCH authored Jan 3, 2025
2 parents 0fcd729 + 9e9cc80 commit 89c5bfe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
7 changes: 3 additions & 4 deletions DialogHost.Avalonia/DialogHost.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
<Setter Property="dialogHostAvalonia:DialogHostStyle.CornerRadius" Value="2" />
<Setter Property="Template">
<ControlTemplate>
<Grid Name="PART_DialogHostRoot" Focusable="False">
<Panel Name="PART_DialogHostRoot" Focusable="False">
<ContentPresenter Name="PART_ContentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Tag="{TemplateBinding BlurBackgroundRadius}"/>
Tag="{TemplateBinding BlurBackgroundRadius}" />
<Rectangle Name="PART_ContentCover"
Fill="{TemplateBinding OverlayBackground}"
Focusable="False">
Expand All @@ -47,7 +47,7 @@
</Style>
</Rectangle.Styles>
</Rectangle>
</Grid>
</Panel>
</ControlTemplate>
</Setter>
<Setter Property="PopupTemplate">
Expand Down Expand Up @@ -85,7 +85,6 @@
</Style>
</Style>


<Style Selector="^[IsOpen=True] /template/ Rectangle#PART_ContentCover">
<Setter Property="IsHitTestVisible" Value="True" />
<Setter Property="Opacity" Value="0.56" />
Expand Down
7 changes: 5 additions & 2 deletions DialogHost.Avalonia/DialogHost.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
Expand Down Expand Up @@ -215,7 +216,7 @@ public static readonly StyledProperty<double> BlurBackgroundRadiusProperty
private IDialogPopupPositioner? _popupPositioner;
private IInputElement? _restoreFocusDialogClose;

private Grid? _root;
private Panel _root;

private IDisposable? _templateDisposables;

Expand Down Expand Up @@ -627,7 +628,9 @@ protected void RaiseCommandsCanExecuteChanged() {
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) {
_templateDisposables?.Dispose();

_root = e.NameScope.Find<Grid>(DialogHostRoot) ?? throw new InvalidOperationException($"No Grid with name {DialogHostRoot} found");
_root = e.NameScope.Find<Panel>(DialogHostRoot)
?? throw new InvalidOperationException($"No Panel with name {DialogHostRoot} found. " +
$"Did you add the styles as stated in getting started?");
_overlayPopupHost = new DialogOverlayPopupHost(_root) {
Content = DialogContent, ContentTemplate = DialogContentTemplate, Template = PopupTemplate,
Padding = DialogMargin, ClipToBounds = false, DisableOpeningAnimation = DisableOpeningAnimation,
Expand Down
41 changes: 16 additions & 25 deletions DialogHost.Avalonia/DialogOverlayPopupHost.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace DialogHostAvalonia;

public class DialogOverlayPopupHost : ContentControl, ICustomKeyboardNavigation {
public class DialogOverlayPopupHost(Panel root) : ContentControl, ICustomKeyboardNavigation {
public static readonly DirectProperty<DialogOverlayPopupHost, bool> IsOpenProperty =
AvaloniaProperty.RegisterDirect<DialogOverlayPopupHost, bool>(
nameof(IsOpen),
Expand All @@ -28,17 +28,10 @@ public class DialogOverlayPopupHost : ContentControl, ICustomKeyboardNavigation
o => o.PopupPositioner,
(o, v) => o.PopupPositioner = v);

private readonly Grid _root;

private bool _disableOpeningAnimation;
private bool _isOpen;
private IDialogPopupPositioner? _popupPositioner;

public DialogOverlayPopupHost(Grid root)
{
this._root = root;
}

public bool IsOpen {
get => _isOpen;
set {
Expand Down Expand Up @@ -71,52 +64,49 @@ public IDialogPopupPositioner? PopupPositioner {
}
}

public void Show()
{
public void Show() {
if (Parent == null) {
_root.Children.Add(this);
root.Children.Add(this);
}

// Set the minimum priority to allow overriding it everywhere
ClearValue(IsActuallyOpenProperty);
Focus();
UpdatePosition();
}

public void Hide()
{
_root.Children.Remove(this);
public void Hide() {
root.Children.Remove(this);
}

protected override Size MeasureOverride(Size availableSize)
{
if (PopupPositioner is IDialogPopupPositionerConstrainable constrainable)
{
protected override Size MeasureOverride(Size availableSize) {
if (PopupPositioner is IDialogPopupPositionerConstrainable constrainable) {
return base.MeasureOverride(constrainable.Constrain(availableSize));
}

return base.MeasureOverride(availableSize);
}

/// <inheritdoc />
protected override void ArrangeCore(Rect finalRect) {
var margin = Margin;

var size = new Size(
Math.Max(0, finalRect.Width - margin.Left - margin.Right),
Math.Max(0, finalRect.Height - margin.Top - margin.Bottom));

var contentSize = new Size(
Math.Min(size.Width, DesiredSize.Width - margin.Left - margin.Right),
Math.Min(size.Width, DesiredSize.Width - margin.Left - margin.Right),
Math.Min(size.Height, DesiredSize.Height - margin.Top - margin.Bottom));
var positioner = PopupPositioner ?? CenteredDialogPopupPositioner.Instance;
var bounds = positioner.Update(size, contentSize);

var (finalWidth, finalHeight) = ArrangeOverride(bounds.Size).Constrain(size);
Bounds = new Rect(bounds.X + margin.Left, bounds.Y + margin.Top, finalWidth, finalHeight);
}


private void UpdatePosition()
{
private void UpdatePosition() {
// Don't bother the positioner with layout system artifacts
// if (_positionerParameters.Size.Width == 0 || _positionerParameters.Size.Height == 0)
// return;
Expand All @@ -131,7 +121,7 @@ private void UpdatePosition()
if (!element.Equals(this)) {
return (false, null);
}

// Finding the focusable descendant
var focusable = this.GetVisualDescendants()
.OfType<IInputElement>()
Expand All @@ -146,6 +136,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
if (change.Property == IsActuallyOpenProperty && !change.GetNewValue<bool>()) {
Hide();
}

base.OnPropertyChanged(change);
}
}

0 comments on commit 89c5bfe

Please sign in to comment.