feat: ultimate refactoring to separate default dialog and custom dialog.

This commit is contained in:
rabbitism
2024-01-25 00:50:27 +08:00
parent a17f1076d0
commit 430dccc958
16 changed files with 605 additions and 255 deletions

View File

@@ -7,8 +7,8 @@
x:CompileBindings="True"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ursa.Demo.Dialogs.DialogWithAction">
<StackPanel Margin="8">
<TextBlock Classes="Strong" Margin="8" Text="{Binding Title}"></TextBlock>
<StackPanel Margin="24">
<TextBlock FontSize="16" FontWeight="600" Margin="8" Text="{Binding Title}"></TextBlock>
<Calendar SelectedDate="{Binding Date}" ></Calendar>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Spacing="8">
<Button Content="Dialog" Command="{Binding DialogCommand}"></Button>

View File

@@ -11,7 +11,12 @@ public partial class DialogWithActionViewModel: ObservableObject, IDialogContext
{
[ObservableProperty] private string _title;
[ObservableProperty] private DateTime _date;
public object? DefaultCloseResult { get; set; } = true;
public void Close()
{
Closed?.Invoke(this, false);
}
public event EventHandler<object?>? Closed;
public ICommand OKCommand { get; set; }
@@ -40,6 +45,6 @@ public partial class DialogWithActionViewModel: ObservableObject, IDialogContext
private async Task ShowDialog()
{
await OverlayDialog.ShowModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(new DialogWithActionViewModel());
await OverlayDialog.ShowCustomModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(new DialogWithActionViewModel());
}
}

View File

@@ -5,7 +5,6 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ursa.Demo.Dialogs.PlainDialog">
<StackPanel Margin="8">
<TextBlock Classes="Strong" Margin="8" Text="{Binding Title}"></TextBlock>
<Calendar SelectedDate="{Binding Date}" ></Calendar>
</StackPanel>
</UserControl>

View File

@@ -15,13 +15,10 @@ public class DialogDemoViewModel: ObservableObject
public ICommand ShowLocalOverlayModalDialogCommand { get; }
public ICommand ShowGlobalOverlayModalDialogCommand { get; }
public ICommand ShowGlobalModalDialogCommand { get; }
public ICommand ShowGlobalOverlayDialogCommand { get; }
public ICommand ShowPlainGlobalOverlayDialogCommand { get; }
private object? _result;
public object? Result
{
get => _result;
@@ -54,31 +51,27 @@ public class DialogDemoViewModel: ObservableObject
private async Task ShowGlobalModalDialog()
{
var result = await Dialog.ShowModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(DialogViewModel);
var result = await Dialog.ShowCustomModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(DialogViewModel);
Result = result;
}
private async Task ShowGlobalOverlayModalDialog()
{
Result = await OverlayDialog.ShowModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(DialogViewModel);
Result = await OverlayDialog.ShowCustomModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(DialogViewModel);
}
private async Task ShowLocalOverlayModalDialog()
{
var vm = new DialogWithActionViewModel();
var result = await OverlayDialog.ShowModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(
DialogViewModel, new DialogOptions() { ExtendToClientArea = true}, "LocalHost");
var result = await OverlayDialog.ShowCustomModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(
DialogViewModel, "LocalHost");
Result = result;
}
public async Task ShowPlainGlobalOverlayDialog()
{
var result = await OverlayDialog.ShowModalAsync<PlainDialog, PlainDialogViewModel, object?>(
var result = await OverlayDialog.ShowCustomModalAsync<PlainDialog, PlainDialogViewModel, object?>(
new PlainDialogViewModel(),
new DialogOptions()
{
DefaultButtons = DialogButton.OKCancel,
},
"LocalHost");
}
}

View File

@@ -13,9 +13,96 @@
Padding="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Classes="Shadow"
CornerRadius="12"
IsHitTestVisible="True"
Theme="{DynamicResource CardBorder}">
<Grid RowDefinitions="Auto, *, Auto">
<Grid RowDefinitions="Auto, *">
<ContentPresenter
Name="PART_ContentPresenter"
Grid.Row="0"
Grid.RowSpan="2"
Content="{TemplateBinding Content}" />
<Grid Grid.Row="0" ColumnDefinitions="*, Auto">
<Panel
Name="{x:Static u:DialogControl.PART_TitleArea}"
Grid.Column="0"
Grid.ColumnSpan="2"
Background="Transparent" />
<Button
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
Grid.Column="1"
Margin="0,24,24,0"
DockPanel.Dock="Right"
Theme="{DynamicResource CloseButton}" />
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^:not(:modal) /template/ Panel#PART_TitleArea">
<Setter Property="ContextFlyout">
<MenuFlyout>
<MenuItem
Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}"
Header="Bring Forward">
<MenuItem.Icon>
<PathIcon
Width="12"
Height="12"
Data="{DynamicResource DialogArrangeBringForwardGlyph}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem
Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.BringToFront}"
Header="Bring To Front">
<MenuItem.Icon>
<PathIcon
Width="12"
Height="12"
Data="{DynamicResource DialogArrangeBringToFrontGlyph}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem
Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.SendBackward}"
Header="Send Backward">
<MenuItem.Icon>
<PathIcon
Width="12"
Height="12"
Data="{DynamicResource DialogArrangeSendBackwardGlyph}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem
Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.SendToBack}"
Header="Send To Back">
<MenuItem.Icon>
<PathIcon
Width="12"
Height="12"
Data="{DynamicResource DialogArrangeSendToBackGlyph}" />
</MenuItem.Icon>
</MenuItem>
</MenuFlyout>
</Setter>
</Style>
</ControlTheme>
<ControlTheme x:Key="{x:Type u:DefaultDialogControl}" TargetType="u:DefaultDialogControl">
<Setter Property="Template">
<ControlTemplate TargetType="u:DefaultDialogControl">
<Border
Padding="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Classes="Shadow"
CornerRadius="12"
IsHitTestVisible="True"
Theme="{DynamicResource CardBorder}">
<Grid Margin="24" RowDefinitions="Auto, *, Auto">
<ContentPresenter
Name="PART_ContentPresenter"
Grid.Row="1"
@@ -47,22 +134,22 @@
HorizontalAlignment="Right"
Orientation="Horizontal">
<Button
Name="{x:Static u:DialogControl.PART_CancelButton}"
Name="{x:Static u:DefaultDialogControl.PART_CancelButton}"
Margin="8,0,0,0"
Classes="Tertiary"
Content="Cancel" />
<Button
Name="{x:Static u:DialogControl.PART_NoButton}"
Name="{x:Static u:DefaultDialogControl.PART_NoButton}"
Margin="8,0,0,0"
Classes="Danger"
Content="No" />
<Button
Name="{x:Static u:DialogControl.PART_YesButton}"
Name="{x:Static u:DefaultDialogControl.PART_YesButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="Yes" />
<Button
Name="{x:Static u:DialogControl.PART_OKButton}"
Name="{x:Static u:DefaultDialogControl.PART_OKButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="OK" />
@@ -71,15 +158,11 @@
</Border>
</ControlTemplate>
</Setter>
<Style Selector="^:extend /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Grid.Row" Value="0" />
<Setter Property="Grid.RowSpan" Value="3" />
</Style>
<Style Selector="^:not(:modal) /template/ Panel#PART_TitleArea">
<Setter Property="ContextFlyout">
<MenuFlyout>
<MenuItem
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}"
Header="Bring Forward">
<MenuItem.Icon>
@@ -90,7 +173,7 @@
</MenuItem.Icon>
</MenuItem>
<MenuItem
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.BringToFront}"
Header="Bring To Front">
<MenuItem.Icon>
@@ -101,7 +184,7 @@
</MenuItem.Icon>
</MenuItem>
<MenuItem
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.SendBackward}"
Header="Send Backward">
<MenuItem.Icon>
@@ -112,7 +195,7 @@
</MenuItem.Icon>
</MenuItem>
<MenuItem
Command="{Binding $parent[u:DialogControl].UpdateLayer}"
Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.SendToBack}"
Header="Send To Back">
<MenuItem.Icon>
@@ -163,13 +246,47 @@
Grid.Row="0"
Grid.RowSpan="2"
Content="{TemplateBinding Content}" />
<Button
Name="{x:Static u:DialogWindow.PART_CloseButton}"
Grid.Row="0"
Margin="0,4,4,0"
<Grid Grid.Row="0" ColumnDefinitions="*, Auto">
<TextBlock
Grid.Column="0"
Margin="8,8,0,0"
FontSize="14"
FontWeight="Bold"
Text="{TemplateBinding Title}"
TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap" />
<Button
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
Grid.Column="1"
Margin="0,4,4,0"
Theme="{DynamicResource CloseButton}" />
</Grid>
<StackPanel
Grid.Row="2"
Margin="0,0,8,8"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Theme="{DynamicResource CloseButton}" />
Orientation="Horizontal">
<Button
Name="{x:Static u:DefaultDialogControl.PART_CancelButton}"
Margin="8,0,0,0"
Classes="Tertiary"
Content="Cancel" />
<Button
Name="{x:Static u:DefaultDialogControl.PART_NoButton}"
Margin="8,0,0,0"
Classes="Danger"
Content="No" />
<Button
Name="{x:Static u:DefaultDialogControl.PART_YesButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="Yes" />
<Button
Name="{x:Static u:DefaultDialogControl.PART_OKButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="OK" />
</StackPanel>
</Grid>
</Panel>
</ControlTemplate>

View File

@@ -40,7 +40,6 @@
TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap"
Text="{TemplateBinding Title}" />
<!-- A temporary style copied from Semi. Will replace when I get time -->
<Button
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
Grid.Column="1"

View File

@@ -0,0 +1,23 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
namespace Ursa.Common;
internal static class EventHelper
{
public static void RegisterClickEvent(EventHandler<RoutedEventArgs> handler, params Button?[] buttons)
{
foreach (var button in buttons)
{
if(button is not null) button.Click += handler;
}
}
public static void UnregisterClickEvent(EventHandler<RoutedEventArgs> handler, params Button?[] buttons)
{
foreach (var button in buttons)
{
if(button is not null) button.Click -= handler;
}
}
}

View File

@@ -0,0 +1,133 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Ursa.Common;
namespace Ursa.Controls;
[TemplatePart(PART_OKButton, typeof(Button))]
[TemplatePart(PART_CancelButton, typeof(Button))]
[TemplatePart(PART_YesButton, typeof(Button))]
[TemplatePart(PART_NoButton, typeof(Button))]
[TemplatePart(PART_TitleArea, typeof(Panel))]
public class DefaultDialogControl: DialogControl
{
public const string PART_OKButton = "PART_OKButton";
public const string PART_CancelButton = "PART_CancelButton";
public const string PART_YesButton = "PART_YesButton";
public const string PART_NoButton = "PART_NoButton";
private Button? _okButton;
private Button? _cancelButton;
private Button? _yesButton;
private Button? _noButton;
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DefaultDialogControl, string?>(
nameof(Title));
public string? Title
{
get => GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public static readonly StyledProperty<DialogButton> ButtonsProperty = AvaloniaProperty.Register<DefaultDialogControl, DialogButton>(
nameof(Buttons));
public DialogButton Buttons
{
get => GetValue(ButtonsProperty);
set => SetValue(ButtonsProperty, value);
}
public static readonly StyledProperty<DialogIcon> IconProperty = AvaloniaProperty.Register<DefaultDialogControl, DialogIcon>(
nameof(Icon));
public DialogIcon Icon
{
get => GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
EventHelper.UnregisterClickEvent(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton);
_okButton = e.NameScope.Find<Button>(PART_OKButton);
_cancelButton = e.NameScope.Find<Button>(PART_CancelButton);
_yesButton = e.NameScope.Find<Button>(PART_YesButton);
_noButton = e.NameScope.Find<Button>(PART_NoButton);
EventHelper.RegisterClickEvent(DefaultButtonsClose, _yesButton, _noButton, _okButton, _cancelButton);
SetButtonVisibility();
}
private void SetButtonVisibility()
{
switch (Buttons)
{
case DialogButton.None:
SetVisibility(_okButton, false);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break;
case DialogButton.OK:
SetVisibility(_okButton, true);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break;
case DialogButton.OKCancel:
SetVisibility(_okButton, true);
SetVisibility(_cancelButton, true);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break;
case DialogButton.YesNo:
SetVisibility(_okButton, false);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, true);
SetVisibility(_noButton, true);
break;
case DialogButton.YesNoCancel:
SetVisibility(_okButton, false);
SetVisibility(_cancelButton, true);
SetVisibility(_yesButton, true);
SetVisibility(_noButton, true);
break;
}
}
private void SetVisibility(Button? button, bool visible)
{
if (button is not null) button.IsVisible = visible;
}
private void DefaultButtonsClose(object sender, RoutedEventArgs args)
{
if (sender is Button button)
{
if (button == _okButton)
{
OnDialogControlClosing(this, DialogResult.OK);
}
else if (button == _cancelButton)
{
OnDialogControlClosing(this, DialogResult.Cancel);
}
else if (button == _yesButton)
{
OnDialogControlClosing(this, DialogResult.Yes);
}
else if (button == _noButton)
{
OnDialogControlClosing(this, DialogResult.No);
}
}
}
}

View File

@@ -0,0 +1,125 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using Ursa.Common;
namespace Ursa.Controls;
[TemplatePart(PART_YesButton, typeof(Button))]
[TemplatePart(PART_NoButton, typeof(Button))]
[TemplatePart(PART_OKButton, typeof(Button))]
[TemplatePart(PART_CancelButton, typeof(Button))]
public class DefaultDialogWindow: DialogWindow
{
protected override Type StyleKeyOverride { get; } = typeof(DefaultDialogWindow);
public const string PART_YesButton = "PART_YesButton";
public const string PART_NoButton = "PART_NoButton";
public const string PART_OKButton = "PART_OKButton";
public const string PART_CancelButton = "PART_CancelButton";
private Button? _yesButton;
private Button? _noButton;
private Button? _okButton;
private Button? _cancelButton;
public static readonly StyledProperty<DialogButton> ButtonsProperty = AvaloniaProperty.Register<DefaultDialogWindow, DialogButton>(
nameof(Buttons));
public DialogButton Buttons
{
get => GetValue(ButtonsProperty);
set => SetValue(ButtonsProperty, value);
}
public new static readonly StyledProperty<DialogIcon> IconProperty = AvaloniaProperty.Register<DefaultDialogWindow, DialogIcon>(
nameof(Icon));
public new DialogIcon Icon
{
get => GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
EventHelper.UnregisterClickEvent(OnDefaultClose, _okButton, _cancelButton, _yesButton, _noButton);
_okButton = e.NameScope.Find<Button>(PART_OKButton);
_cancelButton = e.NameScope.Find<Button>(PART_CancelButton);
_yesButton = e.NameScope.Find<Button>(PART_YesButton);
_noButton = e.NameScope.Find<Button>(PART_NoButton);
EventHelper.RegisterClickEvent(OnDefaultClose, _yesButton, _noButton, _okButton, _cancelButton);
SetButtonVisibility();
}
private void OnDefaultClose(object sender, RoutedEventArgs e)
{
if (sender == _yesButton)
{
Close(DialogResult.Yes);
return;
}
if(sender == _noButton)
{
Close(DialogResult.No);
return;
}
if(sender == _okButton)
{
Close(DialogResult.OK);
return;
}
if(sender == _cancelButton)
{
Close(DialogResult.Cancel);
return;
}
}
private void SetButtonVisibility()
{
switch (Buttons)
{
case DialogButton.None:
SetVisibility(_okButton, false);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break;
case DialogButton.OK:
SetVisibility(_okButton, true);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break;
case DialogButton.OKCancel:
SetVisibility(_okButton, true);
SetVisibility(_cancelButton, true);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break;
case DialogButton.YesNo:
SetVisibility(_okButton, false);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, true);
SetVisibility(_noButton, true);
break;
case DialogButton.YesNoCancel:
SetVisibility(_okButton, false);
SetVisibility(_cancelButton, true);
SetVisibility(_yesButton, true);
SetVisibility(_noButton, true);
break;
}
}
private void SetVisibility(Button? button, bool visible)
{
if (button is not null) button.IsVisible = visible;
}
}

View File

@@ -3,79 +3,162 @@ using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Shapes;
using Avalonia.Media;
using Ursa.Common;
namespace Ursa.Controls;
public static class Dialog
{
public static async Task<TResult?> ShowModalAsync<TView, TViewModel, TResult>(TViewModel vm)
/// <summary>
/// Show a Window Modal Dialog that with all content fully customized.
/// </summary>
/// <param name="vm"></param>
/// <typeparam name="TView"></typeparam>
/// <typeparam name="TViewModel"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <returns></returns>
public static async Task<TResult?> ShowCustomModalAsync<TView, TViewModel, TResult>(TViewModel vm)
where TView : Control, new()
{
var lifetime = Application.Current?.ApplicationLifetime;
if (lifetime is IClassicDesktopStyleApplicationLifetime classLifetime)
{
var window = new DialogWindow
{
Content = new TView { DataContext = vm },
DataContext = vm,
};
if (classLifetime.MainWindow is not { } main)
{
window.Show();
return default;
}
var result = await window.ShowDialog<TResult>(main);
return result;
}
return default(TResult);
var mainWindow = GetMainWindow();
return await ShowCustomModalAsync<TView, TViewModel, TResult>(mainWindow, vm);
}
public static async Task<TResult> ShowModalAsync<TView, TViewModel, TResult>(Window owner, TViewModel? vm)
/// <summary>
/// Show a Window Modal Dialog that with all content fully customized. And the owner of the dialog is specified.
/// </summary>
/// <param name="owner"></param>
/// <param name="vm"></param>
/// <typeparam name="TView"></typeparam>
/// <typeparam name="TViewModel"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <returns></returns>
public static async Task<TResult> ShowCustomModalAsync<TView, TViewModel, TResult>(Window? owner, TViewModel? vm)
where TView: Control, new()
{
var window = new DialogWindow
{
Content = new TView() { DataContext = vm },
DataContext = vm
Content = new TView { DataContext = vm },
DataContext = vm,
};
return await window.ShowDialog<TResult>(owner);
if (owner is null)
{
window.Show();
return default;
}
else
{
var result = await window.ShowDialog<TResult>(owner);
return result;
}
}
public static async Task<DialogResult> ShowModalAsync<TView, TViewModel>(
Window? owner,
TViewModel vm,
string? title = null,
DialogIcon icon = DialogIcon.None,
DialogButton buttons = DialogButton.OKCancel)
where TView : Control, new()
{
var window = new DefaultDialogWindow()
{
Content = new TView() { DataContext = vm },
DataContext = vm,
Buttons = buttons,
Title = title,
Icon = icon,
};
if (owner is null)
{
window.Show();
return DialogResult.None;
}
else
{
var result = await window.ShowDialog<DialogResult>(owner);
return result;
}
}
public static async Task<DialogResult> ShowModalAsync<TView, TViewModel>(
TViewModel vm,
string? title = null,
DialogIcon icon = DialogIcon.None,
DialogButton buttons = DialogButton.OKCancel)
where TView: Control, new()
{
var mainWindow = GetMainWindow();
return await ShowModalAsync<TView, TViewModel>(mainWindow, vm, title, icon, buttons);
}
private static Window? GetMainWindow()
{
var lifetime = Application.Current?.ApplicationLifetime;
return lifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } w } ? w : null;
}
}
public static class OverlayDialog
{
public static Task<TResult> ShowModalAsync<TView, TViewModel, TResult>(TViewModel vm, string? hostId = null)
public static Task<DialogResult> ShowModalAsync<TView, TViewModel>(
TViewModel vm,
string? hostId = null,
string? title = null,
DialogIcon icon = DialogIcon.None,
DialogButton buttons = DialogButton.OKCancel)
where TView : Control, new()
{
var t = new DialogControl()
var t = new DefaultDialogControl()
{
Content = new TView(){ DataContext = vm },
DataContext = vm,
Buttons = buttons,
Title = title,
Icon = icon,
};
var host = OverlayDialogManager.GetHost(hostId);
host?.AddModalDialog(t);
return t.ShowAsync<TResult>();
return t.ShowAsync<DialogResult>();
}
public static Task<TResult> ShowModalAsync<TView, TViewModel, TResult>(TViewModel vm, DialogOptions options, string? hostId = null)
where TView : Control, new()
public static Task<TResult> ShowCustomModalAsync<TView, TViewModel, TResult>(
TViewModel vm,
string? hostId = null)
where TView: Control, new()
{
var t = new DialogControl()
{
Content = new TView() { DataContext = vm },
DataContext = vm,
ExtendToClientArea = options.ExtendToClientArea,
Title = options.Title,
Buttons = options.DefaultButtons,
};
var host = OverlayDialogManager.GetHost(hostId);
host?.AddModalDialog(t);
return t.ShowAsync<TResult>();
}
public static void Show<TView, TViewModel>(TViewModel vm, string? hostId = null)
public static void Show<TView, TViewModel>(
TViewModel vm,
string? hostId = null,
string? title = null,
DialogIcon icon = DialogIcon.None,
DialogButton buttons = DialogButton.OKCancel)
where TView: Control, new()
{
var t = new DefaultDialogControl()
{
Content = new TView() { DataContext = vm },
DataContext = vm,
Buttons = buttons,
Title = title,
Icon = icon,
};
var host = OverlayDialogManager.GetHost(hostId);
host?.AddDialog(t);
}
public static void ShowCustom<TView, TViewModel>(TViewModel vm, string? hostId = null)
where TView: Control, new()
{
var t = new DialogControl()
@@ -87,18 +170,4 @@ public static class OverlayDialog
host?.AddDialog(t);
}
public static void Show<TView, TViewModel>(TViewModel vm, DialogOptions options, string? hostId)
where TView: Control, new()
{
var t = new DialogControl()
{
Content = new TView() { DataContext = vm },
DataContext = vm,
ExtendToClientArea = options.ExtendToClientArea,
Title = options.Title,
Buttons = options.DefaultButtons,
};
var host = OverlayDialogManager.GetHost(hostId);
host?.AddModalDialog(t);
}
}

View File

@@ -1,4 +1,4 @@
using Avalonia;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
@@ -9,57 +9,22 @@ using Ursa.Common;
namespace Ursa.Controls;
[TemplatePart(PART_CloseButton, typeof(Button))]
[TemplatePart(PART_OKButton, typeof(Button))]
[TemplatePart(PART_CancelButton, typeof(Button))]
[TemplatePart(PART_YesButton, typeof(Button))]
[TemplatePart(PART_NoButton, typeof(Button))]
[TemplatePart(PART_TitleArea, typeof(Panel))]
[PseudoClasses(PC_ExtendClientArea)]
[PseudoClasses(PC_Modal)]
public class DialogControl: ContentControl
{
public const string PART_CloseButton = "PART_CloseButton";
public const string PART_OKButton = "PART_OKButton";
public const string PART_CancelButton = "PART_CancelButton";
public const string PART_YesButton = "PART_YesButton";
public const string PART_NoButton = "PART_NoButton";
public const string PART_TitleArea = "PART_TitleArea";
public const string PC_ExtendClientArea = ":extend";
public const string PC_Modal = ":modal";
private Button? _closeButton;
private Button? _okButton;
private Button? _cancelButton;
private Button? _yesButton;
private Button? _noButton;
private Panel? _titleArea;
public event EventHandler<object?>? OnDialogControlClose;
public event EventHandler<DialogLayerChangeEventArgs>? OnLayerChange;
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DialogControl, string?>(
nameof(Title));
public string? Title
{
get => GetValue(TitleProperty);
set => SetValue(TitleProperty, value);
}
public static readonly StyledProperty<bool> ExtendToClientAreaProperty = AvaloniaProperty.Register<DialogControl, bool>(
nameof(ExtendToClientArea));
public bool ExtendToClientArea
{
get => GetValue(ExtendToClientAreaProperty);
set => SetValue(ExtendToClientAreaProperty, value);
}
internal DialogButton Buttons { get; set; }
public event EventHandler<DialogLayerChangeEventArgs>? LayerChanged;
public event EventHandler<object?>? DialogControlClosing;
static DialogControl()
{
DataContextProperty.Changed.AddClassHandler<DialogControl, object?>((o, e) => o.OnDataContextChange(e));
ExtendToClientAreaProperty.Changed.AddClassHandler<DialogControl, bool>((o, e) => o.PseudoClasses.Set(PC_ExtendClientArea, e.NewValue.Value));
}
private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> args)
@@ -79,81 +44,18 @@ public class DialogControl: ContentControl
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
UnRegisterClickEvent(Close, _closeButton);
UnRegisterClickEvent(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton);
EventHelper.UnregisterClickEvent(Close, _closeButton);
_titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove);
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
_titleArea = e.NameScope.Find<Panel>(PART_TitleArea);
_okButton = e.NameScope.Find<Button>(PART_OKButton);
_cancelButton = e.NameScope.Find<Button>(PART_CancelButton);
_yesButton = e.NameScope.Find<Button>(PART_YesButton);
_noButton = e.NameScope.Find<Button>(PART_NoButton);
_titleArea?.AddHandler(PointerMovedEvent, OnTitlePointerMove, RoutingStrategies.Bubble);
_titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble);
RegisterClickEvent(Close, _closeButton);
RegisterClickEvent(DefaultButtonsClose, _yesButton, _noButton, _okButton, _cancelButton);
SetButtonVisibility();
}
private void UnRegisterClickEvent(EventHandler<RoutedEventArgs> action, params Button?[] buttons)
{
foreach (var button in buttons)
{
if(button is not null) button.Click -= action;
}
}
private void RegisterClickEvent(EventHandler<RoutedEventArgs> action, params Button?[] buttons)
{
foreach (var button in buttons)
{
if(button is not null) button.Click += action;
}
}
private void SetButtonVisibility()
{
switch (Buttons)
{
case DialogButton.None:
SetVisibility(_okButton, false);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break;
case DialogButton.OK:
SetVisibility(_okButton, true);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break;
case DialogButton.OKCancel:
SetVisibility(_okButton, true);
SetVisibility(_cancelButton, true);
SetVisibility(_yesButton, false);
SetVisibility(_noButton, false);
break;
case DialogButton.YesNo:
SetVisibility(_okButton, false);
SetVisibility(_cancelButton, false);
SetVisibility(_yesButton, true);
SetVisibility(_noButton, true);
break;
case DialogButton.YesNoCancel:
SetVisibility(_okButton, false);
SetVisibility(_cancelButton, true);
SetVisibility(_yesButton, true);
SetVisibility(_noButton, true);
break;
}
}
private void SetVisibility(Button? button, bool visible)
{
if (button is not null) button.IsVisible = visible;
EventHelper.RegisterClickEvent(Close, _closeButton);
}
private void OnTitlePointerPressed(object sender, PointerPressedEventArgs e)
@@ -170,22 +72,21 @@ public class DialogControl: ContentControl
public Task<T> ShowAsync<T>()
{
var tcs = new TaskCompletionSource<T>();
PseudoClasses.Set(PC_Modal, true);
void OnCloseHandler(object sender, object? args)
{
if (args is T result)
{
tcs.SetResult(result);
OnDialogControlClose-= OnCloseHandler;
DialogControlClosing-= OnCloseHandler;
}
else
{
tcs.SetResult(default(T));
OnDialogControlClose-= OnCloseHandler;
DialogControlClosing-= OnCloseHandler;
}
}
this.OnDialogControlClose += OnCloseHandler;
this.DialogControlClosing += OnCloseHandler;
return tcs.Task;
}
@@ -193,12 +94,11 @@ public class DialogControl: ContentControl
{
if (this.DataContext is IDialogContext context)
{
OnDialogControlClose?.Invoke(this, context.DefaultCloseResult);
context.Close();
}
else
{
OnDialogControlClose?.Invoke(this, null);
DialogControlClosing?.Invoke(this, DialogResult.None);
}
}
@@ -206,38 +106,26 @@ public class DialogControl: ContentControl
{
if (this.DataContext is IDialogContext context)
{
OnDialogControlClose?.Invoke(this, args);
DialogControlClosing?.Invoke(this, args);
}
}
private void DefaultButtonsClose(object sender, RoutedEventArgs args)
{
if (sender is Button button)
{
if (button == _okButton)
{
OnDialogControlClose?.Invoke(this, DialogResult.OK);
}
else if (button == _cancelButton)
{
OnDialogControlClose?.Invoke(this, DialogResult.Cancel);
}
else if (button == _yesButton)
{
OnDialogControlClose?.Invoke(this, DialogResult.Yes);
}
else if (button == _noButton)
{
OnDialogControlClose?.Invoke(this, DialogResult.No);
}
}
}
public void UpdateLayer(object? o)
{
if (o is DialogLayerChangeType t)
{
OnLayerChange?.Invoke(this, new DialogLayerChangeEventArgs(t));
LayerChanged?.Invoke(this, new DialogLayerChangeEventArgs(t));
}
}
protected virtual void OnDialogControlClosing(object sender, object? args)
{
DialogControlClosing?.Invoke(this, args);
}
internal void SetAsModal(bool modal)
{
PseudoClasses.Set(PC_Modal, modal);
}
}

View File

@@ -0,0 +1,11 @@
namespace Ursa.Controls;
public enum DialogIcon
{
Info,
Warning,
Error,
Question,
None,
Success,
}

View File

@@ -1,11 +0,0 @@
using Ursa.Common;
namespace Ursa.Controls;
public record DialogOptions
{
public bool ShowCloseButton { get; set; } = true;
public string? Title { get; set; }
public bool ExtendToClientArea { get; set; } = false;
public DialogButton DefaultButtons { get; set; } = DialogButton.None;
}

View File

@@ -4,6 +4,7 @@ using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Ursa.Common;
namespace Ursa.Controls;
@@ -25,27 +26,24 @@ public class DialogWindow: Window
{
if (args.OldValue.Value is IDialogContext oldContext)
{
oldContext.Closed-= OnDialogClose;
oldContext.Closed-= OnDialogContextClose;
}
if (args.NewValue.Value is IDialogContext newContext)
{
newContext.Closed += OnDialogClose;
newContext.Closed += OnDialogContextClose;
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
EventHelper.UnregisterClickEvent(OnDefaultClose, _closeButton);
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
if (_closeButton != null)
{
_closeButton.Click += OnDefaultClose;
}
EventHelper.RegisterClickEvent(OnDefaultClose, _closeButton);
}
private void OnDialogClose(object? sender, object? args)
private void OnDialogContextClose(object? sender, object? args)
{
Close(args);
}
@@ -54,7 +52,7 @@ public class DialogWindow: Window
{
if (DataContext is IDialogContext context)
{
Close(context.DefaultCloseResult);
context.Close();
}
else
{

View File

@@ -2,6 +2,6 @@ namespace Ursa.Controls;
public interface IDialogContext
{
object? DefaultCloseResult { get; set; }
void Close();
event EventHandler<object?> Closed;
}

View File

@@ -63,7 +63,7 @@ public class OverlayDialogHost : Canvas
protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
if (e.Source is DialogControl item)
if (e.Source is DefaultDialogControl item)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
@@ -94,18 +94,18 @@ public class OverlayDialogHost : Canvas
control.Measure(this.Bounds.Size);
control.Arrange(new Rect(control.DesiredSize));
SetToCenter(control);
control.OnDialogControlClose += OnDialogControlDialogClose;
control.OnLayerChange += OnDialogLayerChange;
control.DialogControlClosing += OnDialogControlClosing;
control.LayerChanged += OnDialogLayerChanged;
ResetZIndices();
}
private void OnDialogControlDialogClose(object sender, object? e)
private void OnDialogControlClosing(object sender, object? e)
{
if (sender is DialogControl control)
{
this.Children.Remove(control);
control.OnDialogControlClose -= OnDialogControlDialogClose;
control.OnLayerChange -= OnDialogLayerChange;
control.DialogControlClosing -= OnDialogControlClosing;
control.LayerChanged -= OnDialogLayerChanged;
if (_dialogs.Contains(control))
{
_dialogs.Remove(control);
@@ -137,6 +137,7 @@ public class OverlayDialogHost : Canvas
var mask = CreateOverlayMask();
_masks.Add(mask);
_modalDialogs.Add(control);
control.SetAsModal(true);
for (int i = 0; i < _masks.Count-1; i++)
{
_masks[i].Opacity = 0.5;
@@ -147,14 +148,14 @@ public class OverlayDialogHost : Canvas
control.Measure(this.Bounds.Size);
control.Arrange(new Rect(control.DesiredSize));
SetToCenter(control);
control.OnDialogControlClose += OnDialogControlDialogClose;
control.OnLayerChange += OnDialogLayerChange;
control.DialogControlClosing += OnDialogControlClosing;
control.LayerChanged += OnDialogLayerChanged;
}
// Handle dialog layer change event
private void OnDialogLayerChange(object sender, DialogLayerChangeEventArgs e)
private void OnDialogLayerChanged(object sender, DialogLayerChangeEventArgs e)
{
if (sender is not DialogControl control)
if (sender is not DefaultDialogControl control)
return;
if (!_dialogs.Contains(control))
return;