Skip to content

Commit 4b2a1b0

Browse files
committed
-if no DialogOptions are supplied, fall back to Defaults
-added IDialogService interface -modified obsolete message
1 parent 9b5eb8a commit 4b2a1b0

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
namespace MaterialDesignThemes.Wpf;
1010

11+
public interface IDialogService
12+
{
13+
Task<object?> Show(object? content, DialogOptions? options);
14+
}
15+
1116
public record DialogOptions
1217
{
1318
// Internal properties to "restore" the dialogs looks as it was before a fullscreen dialog was shown
@@ -62,7 +67,7 @@ public enum DialogHostOpenDialogCommandDataContextSource
6267
[TemplatePart(Name = CloseButtonPartName, Type = typeof(Button))]
6368
[TemplateVisualState(GroupName = "PopupStates", Name = OpenStateName)]
6469
[TemplateVisualState(GroupName = "PopupStates", Name = ClosedStateName)]
65-
public class DialogHost : ContentControl
70+
public class DialogHost : ContentControl, IDialogService
6671
{
6772
public const string PopupPartName = "PART_Popup";
6873
public const string PopupContentPartName = "PART_PopupContentElement";
@@ -101,8 +106,9 @@ public class DialogHost : ContentControl
101106

102107
private const string OBSOLETE_SHOW_API_MESSAGE = $"""
103108
This method is obsolete and will be removed in a future version.
104-
Please use a different overload of this method which accepts {nameof(DialogOptions)} instead.
109+
Please use the '{nameof(IDialogService)}' interface and the default implementation '{nameof(DialogHost)}' instead.
105110
""";
111+
private const bool OLD_API_THROWS_ERROR = false;
106112

107113
static DialogHost()
108114
{
@@ -116,7 +122,7 @@ static DialogHost()
116122
/// </summary>
117123
/// <param name="content">Content to show (can be a control or view model).</param>
118124
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
119-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
125+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
120126
public static Task<object?> Show(object content)
121127
=> Show(content, null, null);
122128

@@ -126,7 +132,7 @@ static DialogHost()
126132
/// <param name="content">Content to show (can be a control or view model).</param>
127133
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
128134
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
129-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
135+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
130136
public static Task<object?> Show(object content, DialogOpenedEventHandler? openedEventHandler)
131137
=> Show(content, null, openedEventHandler, null);
132138

@@ -136,7 +142,7 @@ static DialogHost()
136142
/// <param name="content">Content to show (can be a control or view model).</param>
137143
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
138144
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
139-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
145+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
140146
public static Task<object?> Show(object content, DialogClosingEventHandler? closingEventHandler)
141147
=> Show(content, null, null, closingEventHandler);
142148

@@ -147,7 +153,7 @@ static DialogHost()
147153
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
148154
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
149155
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
150-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
156+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
151157
public static Task<object?> Show(object content, DialogOpenedEventHandler? openedEventHandler, DialogClosingEventHandler? closingEventHandler)
152158
=> Show(content, null, openedEventHandler, closingEventHandler);
153159

@@ -159,7 +165,7 @@ static DialogHost()
159165
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
160166
/// <param name="closedEventHandler">Allows access to closed event which would otherwise have been subscribed to on a instance.</param>
161167
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
162-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
168+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
163169
public static Task<object?> Show(object content, DialogOpenedEventHandler? openedEventHandler, DialogClosingEventHandler? closingEventHandler, DialogClosedEventHandler? closedEventHandler)
164170
=> Show(content, null, openedEventHandler, closingEventHandler, closedEventHandler);
165171

@@ -169,7 +175,7 @@ static DialogHost()
169175
/// <param name="content">Content to show (can be a control or view model).</param>
170176
/// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifier set in XAML. <c>null</c> is allowed.</param>
171177
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
172-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
178+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
173179
public static Task<object?> Show(object content, object dialogIdentifier)
174180
=> Show(content, dialogIdentifier, null, null);
175181

@@ -180,7 +186,7 @@ static DialogHost()
180186
/// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifier set in XAML. <c>null</c> is allowed.</param>
181187
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
182188
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
183-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
189+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
184190
public static Task<object?> Show(object content, object dialogIdentifier, DialogOpenedEventHandler? openedEventHandler)
185191
=> Show(content, dialogIdentifier, openedEventHandler, null);
186192

@@ -191,7 +197,7 @@ static DialogHost()
191197
/// <param name="dialogIdentifier"><see cref="Identifier"/> of the instance where the dialog should be shown. Typically this will match an identifier set in XAML. <c>null</c> is allowed.</param>
192198
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
193199
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
194-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
200+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
195201
public static Task<object?> Show(object content, object dialogIdentifier, DialogClosingEventHandler? closingEventHandler)
196202
=> Show(content, dialogIdentifier, null, closingEventHandler);
197203

@@ -203,7 +209,7 @@ static DialogHost()
203209
/// <param name="openedEventHandler">Allows access to opened event which would otherwise have been subscribed to on a instance.</param>
204210
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
205211
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
206-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
212+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
207213
public static Task<object?> Show(object content, object? dialogIdentifier, DialogOpenedEventHandler? openedEventHandler, DialogClosingEventHandler? closingEventHandler)
208214
=> Show(content, dialogIdentifier, openedEventHandler, closingEventHandler, null);
209215

@@ -216,7 +222,7 @@ static DialogHost()
216222
/// <param name="closingEventHandler">Allows access to closing event which would otherwise have been subscribed to on a instance.</param>
217223
/// <param name="closedEventHandler">Allows access to closed event which would otherwise have been subscribed to on a instance.</param>
218224
/// <returns>Task result is the parameter used to close the dialog, typically what is passed to the <see cref="CloseDialogCommand"/> command.</returns>
219-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
225+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
220226
public static async Task<object?> Show(object content, object? dialogIdentifier, DialogOpenedEventHandler? openedEventHandler, DialogClosingEventHandler? closingEventHandler, DialogClosedEventHandler? closedEventHandler)
221227
{
222228
#if NET7_0_OR_GREATER
@@ -307,7 +313,7 @@ private static DialogHost GetInstance(object? dialogIdentifier)
307313
return targets[0];
308314
}
309315

310-
[Obsolete(OBSOLETE_SHOW_API_MESSAGE)]
316+
[Obsolete(OBSOLETE_SHOW_API_MESSAGE, OLD_API_THROWS_ERROR)]
311317
internal async Task<object?> ShowInternal(object content, DialogOpenedEventHandler? openedEventHandler, DialogClosingEventHandler? closingEventHandler, DialogClosedEventHandler? closedEventHandler)
312318
{
313319
if (IsOpen)
@@ -334,10 +340,10 @@ private static DialogHost GetInstance(object? dialogIdentifier)
334340
return result;
335341
}
336342

337-
public static Task<object?> Show(object? content, DialogOptions options)
343+
public Task<object?> Show(object? content, DialogOptions? options = null)
338344
=> Show(content, null, options);
339345

340-
public static async Task<object?> Show(object? content, object? dialogIdentifier, DialogOptions options)
346+
public static async Task<object?> Show(object? content, object? dialogIdentifier, DialogOptions? options = null)
341347
{
342348
#if NET7_0_OR_GREATER
343349
ArgumentNullException.ThrowIfNull(content);
@@ -439,11 +445,13 @@ private void RevertDialogOptions(DialogOptions options)
439445
}
440446
}
441447

442-
internal async Task<object?> ShowInternal(object content, DialogOptions options)
448+
internal async Task<object?> ShowInternal(object content, DialogOptions? options = null)
443449
{
444450
if (IsOpen)
445451
throw new InvalidOperationException("DialogHost is already open.");
446452

453+
options ??= DialogOptions.Default;
454+
447455
_dialogTaskCompletionSource = new TaskCompletionSource<object?>();
448456

449457
AssertTargetableContent();

0 commit comments

Comments
 (0)