8
8
9
9
namespace DialogHostAvalonia ;
10
10
11
- public class DialogOverlayPopupHost : ContentControl , ICustomKeyboardNavigation {
11
+ public class DialogOverlayPopupHost ( Panel root ) : ContentControl , ICustomKeyboardNavigation {
12
12
public static readonly DirectProperty < DialogOverlayPopupHost , bool > IsOpenProperty =
13
13
AvaloniaProperty . RegisterDirect < DialogOverlayPopupHost , bool > (
14
14
nameof ( IsOpen ) ,
@@ -28,17 +28,10 @@ public class DialogOverlayPopupHost : ContentControl, ICustomKeyboardNavigation
28
28
o => o . PopupPositioner ,
29
29
( o , v ) => o . PopupPositioner = v ) ;
30
30
31
- private readonly Grid _root ;
32
-
33
31
private bool _disableOpeningAnimation ;
34
32
private bool _isOpen ;
35
33
private IDialogPopupPositioner ? _popupPositioner ;
36
34
37
- public DialogOverlayPopupHost ( Grid root )
38
- {
39
- this . _root = root ;
40
- }
41
-
42
35
public bool IsOpen {
43
36
get => _isOpen ;
44
37
set {
@@ -71,52 +64,49 @@ public IDialogPopupPositioner? PopupPositioner {
71
64
}
72
65
}
73
66
74
- public void Show ( )
75
- {
67
+ public void Show ( ) {
76
68
if ( Parent == null ) {
77
- _root . Children . Add ( this ) ;
69
+ root . Children . Add ( this ) ;
78
70
}
71
+
79
72
// Set the minimum priority to allow overriding it everywhere
80
73
ClearValue ( IsActuallyOpenProperty ) ;
81
74
Focus ( ) ;
82
75
UpdatePosition ( ) ;
83
76
}
84
77
85
- public void Hide ( )
86
- {
87
- _root . Children . Remove ( this ) ;
78
+ public void Hide ( ) {
79
+ root . Children . Remove ( this ) ;
88
80
}
89
81
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 ) {
94
84
return base . MeasureOverride ( constrainable . Constrain ( availableSize ) ) ;
95
85
}
86
+
96
87
return base . MeasureOverride ( availableSize ) ;
97
88
}
98
89
99
90
/// <inheritdoc />
100
91
protected override void ArrangeCore ( Rect finalRect ) {
101
92
var margin = Margin ;
102
-
93
+
103
94
var size = new Size (
104
95
Math . Max ( 0 , finalRect . Width - margin . Left - margin . Right ) ,
105
96
Math . Max ( 0 , finalRect . Height - margin . Top - margin . Bottom ) ) ;
106
-
97
+
107
98
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 ) ,
109
100
Math . Min ( size . Height , DesiredSize . Height - margin . Top - margin . Bottom ) ) ;
110
101
var positioner = PopupPositioner ?? CenteredDialogPopupPositioner . Instance ;
111
102
var bounds = positioner . Update ( size , contentSize ) ;
112
-
103
+
113
104
var ( finalWidth , finalHeight ) = ArrangeOverride ( bounds . Size ) . Constrain ( size ) ;
114
105
Bounds = new Rect ( bounds . X + margin . Left , bounds . Y + margin . Top , finalWidth , finalHeight ) ;
115
106
}
116
107
117
108
118
- private void UpdatePosition ( )
119
- {
109
+ private void UpdatePosition ( ) {
120
110
// Don't bother the positioner with layout system artifacts
121
111
// if (_positionerParameters.Size.Width == 0 || _positionerParameters.Size.Height == 0)
122
112
// return;
@@ -131,7 +121,7 @@ private void UpdatePosition()
131
121
if ( ! element . Equals ( this ) ) {
132
122
return ( false , null ) ;
133
123
}
134
-
124
+
135
125
// Finding the focusable descendant
136
126
var focusable = this . GetVisualDescendants ( )
137
127
. OfType < IInputElement > ( )
@@ -146,6 +136,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
146
136
if ( change . Property == IsActuallyOpenProperty && ! change . GetNewValue < bool > ( ) ) {
147
137
Hide ( ) ;
148
138
}
139
+
149
140
base . OnPropertyChanged ( change ) ;
150
141
}
151
142
}
0 commit comments