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" x:CompileBindings="True"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ursa.Demo.Dialogs.DialogWithAction"> x:Class="Ursa.Demo.Dialogs.DialogWithAction">
<StackPanel Margin="8"> <StackPanel Margin="24">
<TextBlock Classes="Strong" Margin="8" Text="{Binding Title}"></TextBlock> <TextBlock FontSize="16" FontWeight="600" Margin="8" Text="{Binding Title}"></TextBlock>
<Calendar SelectedDate="{Binding Date}" ></Calendar> <Calendar SelectedDate="{Binding Date}" ></Calendar>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Spacing="8"> <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Spacing="8">
<Button Content="Dialog" Command="{Binding DialogCommand}"></Button> <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 string _title;
[ObservableProperty] private DateTime _date; [ObservableProperty] private DateTime _date;
public object? DefaultCloseResult { get; set; } = true;
public void Close()
{
Closed?.Invoke(this, false);
}
public event EventHandler<object?>? Closed; public event EventHandler<object?>? Closed;
public ICommand OKCommand { get; set; } public ICommand OKCommand { get; set; }
@@ -40,6 +45,6 @@ public partial class DialogWithActionViewModel: ObservableObject, IDialogContext
private async Task ShowDialog() 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" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Ursa.Demo.Dialogs.PlainDialog"> x:Class="Ursa.Demo.Dialogs.PlainDialog">
<StackPanel Margin="8"> <StackPanel Margin="8">
<TextBlock Classes="Strong" Margin="8" Text="{Binding Title}"></TextBlock>
<Calendar SelectedDate="{Binding Date}" ></Calendar> <Calendar SelectedDate="{Binding Date}" ></Calendar>
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

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

View File

@@ -13,9 +13,96 @@
Padding="0" Padding="0"
HorizontalAlignment="Center" HorizontalAlignment="Center"
VerticalAlignment="Center" VerticalAlignment="Center"
Classes="Shadow"
CornerRadius="12"
IsHitTestVisible="True" IsHitTestVisible="True"
Theme="{DynamicResource CardBorder}"> 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 <ContentPresenter
Name="PART_ContentPresenter" Name="PART_ContentPresenter"
Grid.Row="1" Grid.Row="1"
@@ -47,22 +134,22 @@
HorizontalAlignment="Right" HorizontalAlignment="Right"
Orientation="Horizontal"> Orientation="Horizontal">
<Button <Button
Name="{x:Static u:DialogControl.PART_CancelButton}" Name="{x:Static u:DefaultDialogControl.PART_CancelButton}"
Margin="8,0,0,0" Margin="8,0,0,0"
Classes="Tertiary" Classes="Tertiary"
Content="Cancel" /> Content="Cancel" />
<Button <Button
Name="{x:Static u:DialogControl.PART_NoButton}" Name="{x:Static u:DefaultDialogControl.PART_NoButton}"
Margin="8,0,0,0" Margin="8,0,0,0"
Classes="Danger" Classes="Danger"
Content="No" /> Content="No" />
<Button <Button
Name="{x:Static u:DialogControl.PART_YesButton}" Name="{x:Static u:DefaultDialogControl.PART_YesButton}"
Margin="8,0,0,0" Margin="8,0,0,0"
Classes="Primary" Classes="Primary"
Content="Yes" /> Content="Yes" />
<Button <Button
Name="{x:Static u:DialogControl.PART_OKButton}" Name="{x:Static u:DefaultDialogControl.PART_OKButton}"
Margin="8,0,0,0" Margin="8,0,0,0"
Classes="Primary" Classes="Primary"
Content="OK" /> Content="OK" />
@@ -71,15 +158,11 @@
</Border> </Border>
</ControlTemplate> </ControlTemplate>
</Setter> </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"> <Style Selector="^:not(:modal) /template/ Panel#PART_TitleArea">
<Setter Property="ContextFlyout"> <Setter Property="ContextFlyout">
<MenuFlyout> <MenuFlyout>
<MenuItem <MenuItem
Command="{Binding $parent[u:DialogControl].UpdateLayer}" Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}" CommandParameter="{x:Static u:DialogLayerChangeType.BringForward}"
Header="Bring Forward"> Header="Bring Forward">
<MenuItem.Icon> <MenuItem.Icon>
@@ -90,7 +173,7 @@
</MenuItem.Icon> </MenuItem.Icon>
</MenuItem> </MenuItem>
<MenuItem <MenuItem
Command="{Binding $parent[u:DialogControl].UpdateLayer}" Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.BringToFront}" CommandParameter="{x:Static u:DialogLayerChangeType.BringToFront}"
Header="Bring To Front"> Header="Bring To Front">
<MenuItem.Icon> <MenuItem.Icon>
@@ -101,7 +184,7 @@
</MenuItem.Icon> </MenuItem.Icon>
</MenuItem> </MenuItem>
<MenuItem <MenuItem
Command="{Binding $parent[u:DialogControl].UpdateLayer}" Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.SendBackward}" CommandParameter="{x:Static u:DialogLayerChangeType.SendBackward}"
Header="Send Backward"> Header="Send Backward">
<MenuItem.Icon> <MenuItem.Icon>
@@ -112,7 +195,7 @@
</MenuItem.Icon> </MenuItem.Icon>
</MenuItem> </MenuItem>
<MenuItem <MenuItem
Command="{Binding $parent[u:DialogControl].UpdateLayer}" Command="{Binding $parent[u:DefaultDialogControl].UpdateLayer}"
CommandParameter="{x:Static u:DialogLayerChangeType.SendToBack}" CommandParameter="{x:Static u:DialogLayerChangeType.SendToBack}"
Header="Send To Back"> Header="Send To Back">
<MenuItem.Icon> <MenuItem.Icon>
@@ -163,14 +246,48 @@
Grid.Row="0" Grid.Row="0"
Grid.RowSpan="2" Grid.RowSpan="2"
Content="{TemplateBinding Content}" /> Content="{TemplateBinding Content}" />
<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 <Button
Name="{x:Static u:DialogWindow.PART_CloseButton}" Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
Grid.Row="0" Grid.Column="1"
Margin="0,4,4,0" Margin="0,4,4,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Theme="{DynamicResource CloseButton}" /> Theme="{DynamicResource CloseButton}" />
</Grid> </Grid>
<StackPanel
Grid.Row="2"
Margin="0,0,8,8"
HorizontalAlignment="Right"
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> </Panel>
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>

View File

@@ -40,7 +40,6 @@
TextTrimming="CharacterEllipsis" TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap" TextWrapping="NoWrap"
Text="{TemplateBinding Title}" /> Text="{TemplateBinding Title}" />
<!-- A temporary style copied from Semi. Will replace when I get time -->
<Button <Button
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}" Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
Grid.Column="1" 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,50 +3,129 @@ using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Shapes; using Avalonia.Controls.Shapes;
using Avalonia.Media; using Avalonia.Media;
using Ursa.Common;
namespace Ursa.Controls; namespace Ursa.Controls;
public static class Dialog 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() where TView : Control, new()
{ {
var mainWindow = GetMainWindow();
return await ShowCustomModalAsync<TView, TViewModel, TResult>(mainWindow, vm);
}
var lifetime = Application.Current?.ApplicationLifetime;
if (lifetime is IClassicDesktopStyleApplicationLifetime classLifetime) /// <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 var window = new DialogWindow
{ {
Content = new TView { DataContext = vm }, Content = new TView { DataContext = vm },
DataContext = vm, DataContext = vm,
}; };
if (classLifetime.MainWindow is not { } main) if (owner is null)
{ {
window.Show(); window.Show();
return default; return default;
} }
var result = await window.ShowDialog<TResult>(main); else
{
var result = await window.ShowDialog<TResult>(owner);
return result; return result;
} }
return default(TResult);
} }
public static async Task<TResult> ShowModalAsync<TView, TViewModel, TResult>(Window owner, TViewModel? vm) 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() where TView : Control, new()
{ {
var window = new DialogWindow var window = new DefaultDialogWindow()
{ {
Content = new TView() { DataContext = vm }, Content = new TView() { DataContext = vm },
DataContext = vm DataContext = vm,
Buttons = buttons,
Title = title,
Icon = icon,
}; };
return await window.ShowDialog<TResult>(owner); 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 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 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<DialogResult>();
}
public static Task<TResult> ShowCustomModalAsync<TView, TViewModel, TResult>(
TViewModel vm,
string? hostId = null)
where TView: Control, new() where TView: Control, new()
{ {
var t = new DialogControl() var t = new DialogControl()
@@ -59,23 +138,27 @@ public static class OverlayDialog
return t.ShowAsync<TResult>(); return t.ShowAsync<TResult>();
} }
public static Task<TResult> ShowModalAsync<TView, TViewModel, TResult>(TViewModel vm, DialogOptions options, 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() where TView: Control, new()
{ {
var t = new DialogControl() var t = new DefaultDialogControl()
{ {
Content = new TView() { DataContext = vm }, Content = new TView() { DataContext = vm },
DataContext = vm, DataContext = vm,
ExtendToClientArea = options.ExtendToClientArea, Buttons = buttons,
Title = options.Title, Title = title,
Buttons = options.DefaultButtons, Icon = icon,
}; };
var host = OverlayDialogManager.GetHost(hostId); var host = OverlayDialogManager.GetHost(hostId);
host?.AddModalDialog(t); host?.AddDialog(t);
return t.ShowAsync<TResult>();
} }
public static void Show<TView, TViewModel>(TViewModel vm, string? hostId = null) public static void ShowCustom<TView, TViewModel>(TViewModel vm, string? hostId = null)
where TView: Control, new() where TView: Control, new()
{ {
var t = new DialogControl() var t = new DialogControl()
@@ -87,18 +170,4 @@ public static class OverlayDialog
host?.AddDialog(t); 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;
using Avalonia.Controls.Metadata; using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
@@ -9,57 +9,22 @@ using Ursa.Common;
namespace Ursa.Controls; namespace Ursa.Controls;
[TemplatePart(PART_CloseButton, typeof(Button))] [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))] [TemplatePart(PART_TitleArea, typeof(Panel))]
[PseudoClasses(PC_ExtendClientArea)] [PseudoClasses(PC_Modal)]
public class DialogControl: ContentControl public class DialogControl: ContentControl
{ {
public const string PART_CloseButton = "PART_CloseButton"; 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 PART_TitleArea = "PART_TitleArea";
public const string PC_ExtendClientArea = ":extend";
public const string PC_Modal = ":modal"; public const string PC_Modal = ":modal";
private Button? _closeButton; private Button? _closeButton;
private Button? _okButton;
private Button? _cancelButton;
private Button? _yesButton;
private Button? _noButton;
private Panel? _titleArea; private Panel? _titleArea;
public event EventHandler<object?>? OnDialogControlClose; public event EventHandler<DialogLayerChangeEventArgs>? LayerChanged;
public event EventHandler<DialogLayerChangeEventArgs>? OnLayerChange; public event EventHandler<object?>? DialogControlClosing;
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; }
static DialogControl() static DialogControl()
{ {
DataContextProperty.Changed.AddClassHandler<DialogControl, object?>((o, e) => o.OnDataContextChange(e)); 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) private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> args)
@@ -79,81 +44,18 @@ public class DialogControl: ContentControl
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
UnRegisterClickEvent(Close, _closeButton); EventHelper.UnregisterClickEvent(Close, _closeButton);
UnRegisterClickEvent(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton);
_titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove); _titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove);
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed); _titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
_closeButton = e.NameScope.Find<Button>(PART_CloseButton); _closeButton = e.NameScope.Find<Button>(PART_CloseButton);
_titleArea = e.NameScope.Find<Panel>(PART_TitleArea); _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(PointerMovedEvent, OnTitlePointerMove, RoutingStrategies.Bubble);
_titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble); _titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble);
RegisterClickEvent(Close, _closeButton); EventHelper.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;
} }
private void OnTitlePointerPressed(object sender, PointerPressedEventArgs e) private void OnTitlePointerPressed(object sender, PointerPressedEventArgs e)
@@ -170,22 +72,21 @@ public class DialogControl: ContentControl
public Task<T> ShowAsync<T>() public Task<T> ShowAsync<T>()
{ {
var tcs = new TaskCompletionSource<T>(); var tcs = new TaskCompletionSource<T>();
PseudoClasses.Set(PC_Modal, true);
void OnCloseHandler(object sender, object? args) void OnCloseHandler(object sender, object? args)
{ {
if (args is T result) if (args is T result)
{ {
tcs.SetResult(result); tcs.SetResult(result);
OnDialogControlClose-= OnCloseHandler; DialogControlClosing-= OnCloseHandler;
} }
else else
{ {
tcs.SetResult(default(T)); tcs.SetResult(default(T));
OnDialogControlClose-= OnCloseHandler; DialogControlClosing-= OnCloseHandler;
} }
} }
this.OnDialogControlClose += OnCloseHandler; this.DialogControlClosing += OnCloseHandler;
return tcs.Task; return tcs.Task;
} }
@@ -193,12 +94,11 @@ public class DialogControl: ContentControl
{ {
if (this.DataContext is IDialogContext context) if (this.DataContext is IDialogContext context)
{ {
OnDialogControlClose?.Invoke(this, context.DefaultCloseResult); context.Close();
} }
else else
{ {
OnDialogControlClose?.Invoke(this, null); DialogControlClosing?.Invoke(this, DialogResult.None);
} }
} }
@@ -206,38 +106,26 @@ public class DialogControl: ContentControl
{ {
if (this.DataContext is IDialogContext context) 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) public void UpdateLayer(object? o)
{ {
if (o is DialogLayerChangeType t) 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.Controls.Primitives;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Ursa.Common;
namespace Ursa.Controls; namespace Ursa.Controls;
@@ -25,27 +26,24 @@ public class DialogWindow: Window
{ {
if (args.OldValue.Value is IDialogContext oldContext) if (args.OldValue.Value is IDialogContext oldContext)
{ {
oldContext.Closed-= OnDialogClose; oldContext.Closed-= OnDialogContextClose;
} }
if (args.NewValue.Value is IDialogContext newContext) if (args.NewValue.Value is IDialogContext newContext)
{ {
newContext.Closed += OnDialogClose; newContext.Closed += OnDialogContextClose;
} }
} }
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
EventHelper.UnregisterClickEvent(OnDefaultClose, _closeButton);
_closeButton = e.NameScope.Find<Button>(PART_CloseButton); _closeButton = e.NameScope.Find<Button>(PART_CloseButton);
EventHelper.RegisterClickEvent(OnDefaultClose, _closeButton);
if (_closeButton != null)
{
_closeButton.Click += OnDefaultClose;
}
} }
private void OnDialogClose(object? sender, object? args) private void OnDialogContextClose(object? sender, object? args)
{ {
Close(args); Close(args);
} }
@@ -54,7 +52,7 @@ public class DialogWindow: Window
{ {
if (DataContext is IDialogContext context) if (DataContext is IDialogContext context)
{ {
Close(context.DefaultCloseResult); context.Close();
} }
else else
{ {

View File

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

View File

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