Skip to content

Commit bdc0e75

Browse files
committed
Improve WindowedContentDialog
1 parent c7e0df7 commit bdc0e75

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

dev/DevWinUI.Controls/Controls/WindowedContentDialog/ContentDialogContent.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ internal sealed partial class ContentDialogContent : ContentControl
77
public ContentDialogContent() : base()
88
{
99
DefaultStyleKey = typeof(ContentDialogContent);
10+
Unloaded += (o, e) => needsCustomMeasure = false;
1011
}
1112

1213
private Button PrimaryButton;
@@ -21,6 +22,14 @@ public ContentDialogContent() : base()
2122
public Grid DialogSpace { get; private set; }
2223
public Grid CommandSpace { get; private set; }
2324

25+
/// <summary>
26+
/// Whether customized measurement in MeasureOverride is needed.
27+
/// <br/>
28+
/// This variable is set to avoid redundant calculations.
29+
/// <br/>
30+
/// If the first measurement after Loaded is finished, there will be no need for customized measurement until Unloaded.
31+
/// </summary>
32+
private bool needsCustomMeasure;
2433
protected override void OnApplyTemplate()
2534
{
2635
base.OnApplyTemplate();
@@ -42,9 +51,11 @@ protected override void OnApplyTemplate()
4251
}
4352
protected override Size MeasureOverride(Size availableSize)
4453
{
45-
if (IsLoaded)
54+
if (needsCustomMeasure)
4655
return base.MeasureOverride(availableSize);
4756

57+
needsCustomMeasure = IsLoaded;
58+
4859
int countButtons = 0;
4960
double buttonLongestWidth = 0.0;
5061
double buttonMaxWidth = (double)Application.Current.Resources["ContentDialogButtonMaxWidth"];

dev/DevWinUI.Controls/Controls/WindowedContentDialog/ContentDialogWindow.xaml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ public void SetParent(Window? parent, bool modal = true, bool center = true)
110110
_presenter.IsModal = parent is not null && modal;
111111
}
112112

113-
private ElementTheme _requestedTheme;
114113
public ElementTheme RequestedTheme
115114
{
116-
get => content is not null ? content.RequestedTheme : _requestedTheme;
115+
get => content.RequestedTheme;
117116
set
118117
{
119-
_requestedTheme = value;
120118
content.RequestedTheme = value;
121119
AppWindow.TitleBar.PreferredTheme = value switch
122120
{

dev/DevWinUI.Controls/Controls/WindowedContentDialog/WindowedContentDialog.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,13 @@ private void DialogWindow_Closed(object sender, WindowEventArgs e)
215215

216216
private void AttachPopupLifecycle(ContentDialogWindow dialogWindow, Popup popup)
217217
{
218-
dialogWindow.Loaded -= DialogWindow_Loaded;
218+
dialogWindow.Opened -= OnDialogWindowOpened;
219219
dialogWindow.Closed -= DialogWindow_ClosedPopup;
220220

221-
dialogWindow.Loaded += DialogWindow_Loaded;
221+
dialogWindow.Opened += OnDialogWindowOpened;
222222
dialogWindow.Closed += DialogWindow_ClosedPopup;
223223

224-
void DialogWindow_Loaded(object sender, EventArgs e)
224+
void OnDialogWindowOpened(object sender, EventArgs e)
225225
{
226226
popup.IsOpen = true;
227227
popup.Child.Opacity = 1.0;

0 commit comments

Comments
 (0)