Skip to content

Commit

Permalink
Update .net 7 and allow full content xaml dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
cwinland committed Jul 26, 2023
1 parent 31de3f9 commit ec8745d
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 151 deletions.
219 changes: 122 additions & 97 deletions src/Dialogs/DialogBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,26 @@

namespace wpf_material_dialogs.Dialogs
{
/// <inheritdoc />
/// <summary>
/// Class DialogBase.
/// Implements the <see cref="IDialog" />
/// Implements the <see cref="T:wpf_material_dialogs.Interfaces.IDialog" />
/// </summary>
/// <seealso cref="IDialog" />
/// <seealso cref="T:wpf_material_dialogs.Interfaces.IDialog" />
public abstract class DialogBase : IDialog
{
#region Events

/// <inheritdoc />
/// <summary>
/// Occurs when [property changed].
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;

#endregion

#region Fields

private readonly object buttonLock = new();
private readonly List<Type> buttonTypes = new();
private HorizontalAlignment buttonAlignment = HorizontalAlignment.Right;
Expand All @@ -40,6 +53,10 @@ public abstract class DialogBase : IDialog
private FontWeight titleFontWeight = FontWeights.Bold;
private double width = double.NaN;

#endregion

#region Properties

/// <summary>
/// Gets the selected buttons based on the <see cref="DialogButtons" /> selection.
/// </summary>
Expand Down Expand Up @@ -131,36 +148,6 @@ public DialogButton DialogButtons
}
}

/// <summary>
/// Gets the buttons.
/// </summary>
/// <param name="button">The button enum to convert to an IButton.</param>
/// <returns>IButtons.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">button</exception>
/// <exception cref="ArgumentOutOfRangeException">button</exception>
/// <exception cref="ArgumentOutOfRangeException">button</exception>
public IButtons GetButtons(DialogButton button)
{
lock (buttonLock)
{
var assembly = Assembly.GetExecutingAssembly();

if (!buttonTypes?.Any() ?? false)
{
buttonTypes.AddRange(assembly
.GetTypes()
.Where(mytype => mytype.GetInterfaces().Contains(typeof(IButtons))));
}

var buttons = buttonTypes
?.First(mytype => mytype.Name.Equals(button.ToString(), StringComparison.CurrentCultureIgnoreCase));

return buttons?.FullName != null
? assembly.CreateInstance(buttons.FullName) as IButtons
: throw new ArgumentOutOfRangeException(nameof(button));
}
}

/// <summary>
/// Gets or sets the icon brush.
/// </summary>
Expand Down Expand Up @@ -189,44 +176,6 @@ public PackIconKind IconKind
}
}

/// <summary>
/// Notifies the of property changed.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public void NotifyOfPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

/// <summary>
/// Shows the dialog with given configuration.
/// </summary>
/// <param name="dialogHosId">The dialog hos identifier.</param>
/// <param name="openedAction">The opened action.</param>
/// <param name="closingAction">The closing action.</param>
/// <returns><see cref="Tuple{T1,T2}" /> containing <see cref="DialogResult" /> and <see cref="bool" />.</returns>
public async Task<DialogResult> ShowDialog(object dialogHosId = null,
DialogOpenedEventHandler openedAction = null,
DialogClosingEventHandler closingAction = null)
{
return await DialogHost.Show(this, dialogHosId, openedAction, closingAction) as DialogResult? ??
DialogResult.None;
}

/// <summary>
/// Shows the dialog with given configuration.
/// </summary>
/// <param name="dialogHosId">The dialog hos identifier.</param>
/// <param name="openedAction">The opened action.</param>
/// <param name="closingAction">The closing action.</param>
/// <returns><see cref="Tuple{T1,T2}" /> containing <see cref="DialogResult" /> and <see cref="bool" />.</returns>
public async Task<object> ShowDialogObject(object dialogHosId = null,
DialogOpenedEventHandler openedAction = null,
DialogClosingEventHandler closingAction = null)
{
return await DialogHost.Show(this, dialogHosId, openedAction, closingAction);
}

/// <summary>
/// Gets or sets a value indicating whether to show the icon.
/// </summary>
Expand Down Expand Up @@ -297,42 +246,36 @@ public double TitleFontSize
}
}

/// <summary>
/// Occurs when [property changed].
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion

/// <summary>
/// Shows the destroy alert dialog.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="buttons">The buttons.</param>
/// <returns><c>true</c> if <see cref="DialogResult.Yes" />, <c>false</c> otherwise.</returns>
public static async Task<DialogResult> ShowAlertDialog(string text, DialogButton buttons)
public static async Task<DialogResult> ShowAlertDialog(string text, DialogButton buttons) => await new AlertDialog
{
return await new AlertDialog
{
Title = "Alert",
IconBrush = Brushes.Red,
Text = text,
DialogButtons = buttons
}.ShowDialog();
}
Title = "Alert",
IconBrush = Brushes.Red,
Text = text,
DialogButtons = buttons,
}.ShowDialog();

/// <summary>
/// Shows the dialog with a few customizable options.
/// </summary>
/// <param name="dialogText">The dialog text.</param>
/// <param name="buttons">The footer dialog buttons.</param>
/// <param name="title">The dialog title.</param>
/// <param name="buttonType">The footer dialog buttons.</param>
/// <param name="titleText">The dialog title.</param>
/// <param name="dialogHosId">The dialog identifier.</param>
/// <returns><see cref="DialogResult" />.</returns>
public async Task<DialogResult> ShowDialog(string dialogText, DialogButton buttons, string title,
public async Task<DialogResult> ShowDialog(string dialogText, DialogButton buttonType, string titleText,
object dialogHosId = null)
{
Title = title;
Title = titleText;
Text = dialogText;
DialogButtons = buttons;
DialogButtons = buttonType;

return await ShowDialog(dialogHosId);
}
Expand All @@ -341,20 +284,102 @@ public async Task<DialogResult> ShowDialog(string dialogText, DialogButton butto
/// Shows the dialog with a few customizable options.
/// </summary>
/// <param name="dialogText">The dialog text.</param>
/// <param name="buttons">The footer dialog buttons.</param>
/// <param name="iconKind">Kind of the icon.</param>
/// <param name="iconBrush">The icon brush.</param>
/// <param name="title">The dialog title.</param>
/// <param name="buttonType">The footer dialog buttons.</param>
/// <param name="packIconKind">Kind of the icon.</param>
/// <param name="packIconBrush">The icon brush.</param>
/// <param name="titleText">The dialog title.</param>
/// <param name="dialogHosId">The dialog identifier.</param>
/// <returns><see cref="DialogResult" />.</returns>
public async Task<DialogResult> ShowDialog(string dialogText, DialogButton buttons, PackIconKind iconKind,
Brush iconBrush, string title, object dialogHosId = null)
public async Task<DialogResult> ShowDialog(string dialogText, DialogButton buttonType, PackIconKind packIconKind,
Brush packIconBrush, string titleText, object dialogHosId = null)
{
IconKind = iconKind;
IconKind = packIconKind;
ShowIcon = true;
IconBrush = iconBrush;
IconBrush = packIconBrush;

return await ShowDialog(dialogText, buttons, title, dialogHosId);
return await ShowDialog(dialogText, buttonType, titleText, dialogHosId);
}

public static async Task<DialogResult> ShowDialogContent(object content, object dialogHosId = null,
DialogOpenedEventHandler openedAction = null,
DialogClosingEventHandler closingAction = null)
=> content == null
? throw new ArgumentNullException(nameof(content))
: await DialogHost.Show(content, dialogHosId, openedAction, closingAction) as DialogResult? ?? DialogResult.None;

#region IDialog

/// <inheritdoc />
/// <summary>
/// Gets the buttons.
/// </summary>
/// <param name="button">The button enum to convert to an IButton.</param>
/// <returns>IButtons.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">button</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">button</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">button</exception>
public IButtons GetButtons(DialogButton button)
{
// ReSharper disable once PossibleNullReferenceException
lock (buttonLock)
{
var assembly = Assembly.GetExecutingAssembly();

if (!buttonTypes?.Any() ?? false)
{
buttonTypes.AddRange(assembly
.GetTypes()
.Where(mytype => mytype.GetInterfaces().Contains(typeof(IButtons))));
}

var selectedButtonType =
buttonTypes?.First(mytype => button.ToString().Equals(mytype?.Name, StringComparison.CurrentCultureIgnoreCase));

return selectedButtonType?.FullName != null
? assembly.CreateInstance(selectedButtonType.FullName) as IButtons
: throw new ArgumentOutOfRangeException(nameof(button));
}
}

/// <summary>
/// Notifies the of property changed.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
public void NotifyOfPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

/// <inheritdoc />
/// <summary>
/// Shows the dialog with given configuration.
/// </summary>
/// <param name="dialogHosId">The dialog hos identifier.</param>
/// <param name="openedAction">The opened action.</param>
/// <param name="closingAction">The closing action.</param>
/// <returns>
/// <see cref="T:System.Tuple`2" /> containing <see cref="T:System.Windows.Forms.DialogResult" /> and
/// <see cref="T:System.Boolean" />.
/// </returns>
public async Task<DialogResult> ShowDialog(object dialogHosId = null,
DialogOpenedEventHandler openedAction = null,
DialogClosingEventHandler closingAction = null) =>
await DialogHost.Show(this, dialogHosId, openedAction, closingAction) as DialogResult? ??
DialogResult.None;

/// <inheritdoc />
/// <summary>
/// Shows the dialog with given configuration.
/// </summary>
/// <param name="dialogHosId">The dialog hos identifier.</param>
/// <param name="openedAction">The opened action.</param>
/// <param name="closingAction">The closing action.</param>
/// <returns>
/// <see cref="T:System.Tuple`2" /> containing <see cref="T:System.Windows.Forms.DialogResult" /> and
/// <see cref="T:System.Boolean" />.
/// </returns>
public async Task<object> ShowDialogObject(object dialogHosId = null,
DialogOpenedEventHandler openedAction = null,
DialogClosingEventHandler closingAction = null) => await DialogHost.Show(this, dialogHosId, openedAction, closingAction);

#endregion
}
}
10 changes: 5 additions & 5 deletions src/Dialogs/WarningDialog.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using MaterialDesignThemes.Wpf;
using System.ComponentModel;
using System.Windows.Media;
using wpf_material_dialogs.Enums;

namespace wpf_material_dialogs.Dialogs
{
/// <inheritdoc />
/// <summary>
/// Warning dialog.
/// Implements the <see cref="DialogBase" />
/// Implements the <see cref="INotifyPropertyChanged" />
/// Implements the <see cref="T:wpf_material_dialogs.Dialogs.DialogBase" />
/// Implements the <see cref="T:System.ComponentModel.INotifyPropertyChanged" />
/// </summary>
/// <seealso cref="DialogBase" />
/// <seealso cref="INotifyPropertyChanged" />
/// <seealso cref="T:wpf_material_dialogs.Dialogs.DialogBase" />
/// <seealso cref="T:System.ComponentModel.INotifyPropertyChanged" />
public class WarningDialog : DialogBase
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/wpf-material-dialogs.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFrameworks>net472;net5.0-windows</TargetFrameworks>
<TargetFrameworks>net472;net6.0-windows;net7.0-windows</TargetFrameworks>
<AssemblyTitle>Material Design WPF Dialogs</AssemblyTitle>
<UseWindowsForms>true</UseWindowsForms>
<LangVersion>latest</LangVersion>
Expand Down
12 changes: 6 additions & 6 deletions wpf-material-dialogs.test/DialogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

namespace wpf_material_dialogs.test
{
[Apartment(ApartmentState.STA)]
public class DialogTestBase<TDialog> : IDisposable where TDialog : IDialog, new()
[NUnit.Framework.Ignore("base class")]
internal class DialogTestBase<TDialog> : IDisposable where TDialog : IDialog, new()
{
private DialogHost dialogHost;
private IDialog testDialog;
Expand Down Expand Up @@ -184,22 +184,22 @@ private async Task<DialogResult> OpenTestDialog(IDialog dialog, DialogOpenedEven
}

[Apartment(ApartmentState.STA)]
public class AlertDialogTests : DialogTestBase<AlertDialog>
internal class AlertDialogTests : DialogTestBase<AlertDialog>
{
}

[Apartment(ApartmentState.STA)]
public class ErrorDialogTests : DialogTestBase<ErrorDialog>
internal class ErrorDialogTests : DialogTestBase<ErrorDialog>
{
}

[Apartment(ApartmentState.STA)]
public class InfoDialogTests : DialogTestBase<InfoDialog>
internal class InfoDialogTests : DialogTestBase<InfoDialog>
{
}

[Apartment(ApartmentState.STA)]
public class WarningDialogTests : DialogTestBase<WarningDialog>
internal class WarningDialogTests : DialogTestBase<WarningDialog>
{
}
}
Expand Down
15 changes: 7 additions & 8 deletions wpf-material-dialogs.test/wpf-material-dialogs.test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net7.0-windows</TargetFramework>
<RootNamespace>wpf_material_dialogs.test</RootNamespace>

<IsPackable>false</IsPackable>
Expand All @@ -13,11 +12,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit ec8745d

Please sign in to comment.