Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Wpf.Ui.Gallery/Controls/TermsOfUseContentDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Windows;
using System.Windows.Controls;
using Wpf.Ui.Controls;
using Wpf.Ui.Controls.ContentDialogControl;

namespace Wpf.Ui.Gallery.Controls;
Expand All @@ -12,14 +11,15 @@ public TermsOfUseContentDialog(ContentPresenter contentPresenter) : base(content
InitializeComponent();
}

protected override bool OnButtonClick(ContentDialogButton button)
protected override void OnButtonClick(ContentDialogButton button)
{
if (CheckBox.IsChecked != false)
return true;
{
base.OnButtonClick(button);
return;
};

TextBlock.Visibility = Visibility.Visible;
CheckBox.Focus();

return false;
}
}
55 changes: 29 additions & 26 deletions src/Wpf.Ui/Controls/ContentDialogControl/ContentDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,13 @@ public ContentDialog(ContentPresenter contentPresenter)
ContentPresenter = contentPresenter;

SetValue(TemplateButtonCommandProperty,
new RelayCommand<ContentDialogButton>(OnTemplateButtonClick));
new RelayCommand<ContentDialogButton>(OnButtonClick));

Loaded += static (sender, _) =>
{
var self = (ContentDialog)sender;
self.OnLoaded();
};
}

protected readonly ContentPresenter ContentPresenter;
Expand All @@ -377,7 +383,7 @@ public ContentDialog(ContentPresenter contentPresenter)
public async Task<ContentDialogResult> ShowAsync(CancellationToken cancellationToken = default)
{
Tcs = new TaskCompletionSource<ContentDialogResult>();
var tokenRegistration = cancellationToken.Register(o => Tcs.TrySetCanceled((CancellationToken)o!), cancellationToken);
CancellationTokenRegistration tokenRegistration = cancellationToken.Register(o => Tcs.TrySetCanceled((CancellationToken)o!), cancellationToken);

try
{
Expand All @@ -391,31 +397,37 @@ public async Task<ContentDialogResult> ShowAsync(CancellationToken cancellationT
#else
tokenRegistration.Dispose();
#endif
ContentPresenter.Content = null;
OnClosed();
}
}

/// <summary>
/// Hides the dialog
/// Hides the dialog with result
/// </summary>
public virtual void Hide()
public virtual void Hide(ContentDialogResult result = ContentDialogResult.None)
{
ContentPresenter.Content = null;
Tcs?.TrySetResult(result);
}

protected override void OnInitialized(EventArgs e)
/// <summary>
/// Occurs after Loading event
/// </summary>
protected virtual void OnLoaded()
{
base.OnInitialized(e);
if (VisualChildrenCount <= 0 || GetVisualChild(0) is not UIElement frameworkElement)
return;

Loaded += static (sender, _) =>
{
var self = (ContentDialog)sender;
ResizeToContentSize(frameworkElement);
Focus();
}

if (self.VisualChildrenCount <= 0 || self.GetVisualChild(0) is not UIElement frameworkElement)
return;
/// <summary>
/// Occurs after ContentPresenter.Content = null
/// </summary>
protected virtual void OnClosed()
{

self.ResizeToContentSize(frameworkElement);
self.Focus();
};
}

/// <summary>
Expand Down Expand Up @@ -459,24 +471,15 @@ protected virtual void ResizeToContentSize(UIElement content)
/// Occurs after the <see cref="ContentDialogButton"/> is clicked
/// </summary>
/// <param name="button"></param>
/// <returns>
///
/// </returns>
protected virtual bool OnButtonClick(ContentDialogButton button) { return true; }

private void OnTemplateButtonClick(ContentDialogButton button)
protected virtual void OnButtonClick(ContentDialogButton button)
{
if (!OnButtonClick(button))
return;

ContentDialogResult result = button switch
{
ContentDialogButton.Primary => ContentDialogResult.Primary,
ContentDialogButton.Secondary => ContentDialogResult.Secondary,
_ => ContentDialogResult.None
};

Hide();
Tcs?.TrySetResult(result);
Hide(result);
}
}
1 change: 1 addition & 0 deletions src/Wpf.Ui/Controls/IconElements/IconSourceElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public IconSource? IconSource

protected override UIElement InitializeChildren()
{
//TODO come up with an elegant solution
throw new InvalidOperationException($"Use {nameof(IconSourceElementConverter)} class.");
}
}
80 changes: 41 additions & 39 deletions src/Wpf.Ui/Controls/MessageBoxControl/MessageBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,15 @@ public bool IsPrimaryButtonEnabled
public MessageBox()
{
Topmost = true;
SetValue(TemplateButtonCommandProperty, new RelayCommand<MessageBoxButton>(OnTemplateButtonClick));
SetValue(TemplateButtonCommandProperty, new RelayCommand<MessageBoxButton>(OnButtonClick));

PreviewMouseDoubleClick += static (_, args) => args.Handled = true;

Loaded += static (sender, _) =>
{
var self = (MessageBox)sender;
self.OnLoaded();
};
}

protected TaskCompletionSource<MessageBoxResult>? Tcs;
Expand All @@ -259,27 +267,35 @@ public MessageBox()
[Obsolete($"Use {nameof(ShowDialogAsync)} instead")]
public new bool? ShowDialog()
{
RemoveTitleBarAndApplyMica();
return base.ShowDialog();
throw new InvalidOperationException($"Use {nameof(ShowDialogAsync)} instead");
}

[Obsolete($"Use {nameof(Close)} with MessageBoxResult instead")]
public new void Close()
{
throw new InvalidOperationException($"Use {nameof(Close)} with MessageBoxResult instead");
}

/// <summary>
/// Displays a message box
/// </summary>
/// <returns><see cref="MessageBoxResult"/></returns>
/// <exception cref="TaskCanceledException"></exception>
public async Task<MessageBoxResult> ShowDialogAsync(CancellationToken cancellationToken = default)
public async Task<MessageBoxResult> ShowDialogAsync(bool showAsDialog = true, CancellationToken cancellationToken = default)
{
Tcs = new TaskCompletionSource<MessageBoxResult>();
var tokenRegistration = cancellationToken.Register(o => Tcs.TrySetCanceled((CancellationToken)o!), cancellationToken);
CancellationTokenRegistration tokenRegistration = cancellationToken.Register(o => Tcs.TrySetCanceled((CancellationToken)o!), cancellationToken);

try
{
#pragma warning disable CS0618
ShowDialog();
#pragma warning restore CS0618
RemoveTitleBarAndApplyMica();

if (showAsDialog)
base.ShowDialog();
else
base.Show();

return await Tcs!.Task;
return await Tcs.Task;
}
finally
{
Expand All @@ -291,22 +307,16 @@ public async Task<MessageBoxResult> ShowDialogAsync(CancellationToken cancellati
}
}

protected override void OnInitialized(EventArgs e)
/// <summary>
/// Occurs after Loading event
/// </summary>
protected virtual void OnLoaded()
{
base.OnInitialized(e);

PreviewMouseDoubleClick += static (_, args) => args.Handled = true;

Loaded += static (sender, _) =>
{
var self = (MessageBox)sender;

if (self.VisualChildrenCount <= 0 || self.GetVisualChild(0) is not UIElement content)
return;
if (VisualChildrenCount <= 0 || GetVisualChild(0) is not UIElement content)
return;

self.ResizeToContentSize(content);
self.CenterWindowOnScreen();
};
ResizeToContentSize(content);
CenterWindowOnScreen();
}

protected override void OnClosing(CancelEventArgs e)
Expand Down Expand Up @@ -369,22 +379,8 @@ protected virtual void CenterWindowOnScreen()
/// Occurs after the <see cref="MessageBoxButton"/> is clicked
/// </summary>
/// <param name="button"></param>
/// <returns>
///
/// </returns>
protected virtual bool OnButtonClick(MessageBoxButton button) { return true; }

private void RemoveTitleBarAndApplyMica()
protected virtual void OnButtonClick(MessageBoxButton button)
{
UnsafeNativeMethods.RemoveWindowTitlebarContents(this);
WindowBackdrop.ApplyBackdrop(this, WindowBackdropType.Mica);
}

private void OnTemplateButtonClick(MessageBoxButton button)
{
if (!OnButtonClick(button))
return;

MessageBoxResult result = button switch
{
MessageBoxButton.Primary => MessageBoxResult.Primary,
Expand All @@ -393,6 +389,12 @@ private void OnTemplateButtonClick(MessageBoxButton button)
};

Tcs?.TrySetResult(result);
Close();
base.Close();
}

private void RemoveTitleBarAndApplyMica()
{
UnsafeNativeMethods.RemoveWindowTitlebarContents(this);
WindowBackdrop.ApplyBackdrop(this, WindowBackdropType.Mica);
}
}
3 changes: 2 additions & 1 deletion src/Wpf.Ui/Styles/Controls/ContentDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}">
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}">

<ContentPresenter.Resources>
<Style BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="{x:Type TextBlock}">
Expand Down