feat: implement default button for dialog control.

This commit is contained in:
rabbitism
2024-01-24 12:40:45 +08:00
parent a174f0cd85
commit a17f1076d0
10 changed files with 181 additions and 28 deletions

View File

@@ -12,7 +12,7 @@ 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 object? DefaultCloseResult { get; set; } = true;
public event EventHandler<object>? Closed; public event EventHandler<object?>? Closed;
public ICommand OKCommand { get; set; } public ICommand OKCommand { get; set; }
public ICommand CancelCommand { get; set; } public ICommand CancelCommand { get; set; }

View File

@@ -4,5 +4,8 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
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">
<TextBlock Classes="Strong" Margin="8" Text="{Binding Title}"></TextBlock>
<Calendar SelectedDate="{Binding Date}" ></Calendar>
</StackPanel>
</UserControl> </UserControl>

View File

@@ -25,6 +25,7 @@
<Button Command="{Binding ShowLocalOverlayModalDialogCommand}">Show Local Overlay Modal Dialog</Button> <Button Command="{Binding ShowLocalOverlayModalDialogCommand}">Show Local Overlay Modal Dialog</Button>
<Button Command="{Binding ShowGlobalOverlayModalDialogCommand}"> Show Global Overlay Modal Dialog </Button> <Button Command="{Binding ShowGlobalOverlayModalDialogCommand}"> Show Global Overlay Modal Dialog </Button>
<Button Command="{Binding ShowGlobalOverlayDialogCommand}">Show Global Overlay Dialog</Button> <Button Command="{Binding ShowGlobalOverlayDialogCommand}">Show Global Overlay Dialog</Button>
<Button Command="{Binding ShowPlainGlobalOverlayDialogCommand}">Default Buttons</Button>
</StackPanel> </StackPanel>
<Grid Grid.Column="1"> <Grid Grid.Column="1">
<Button <Button

View File

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using System.Windows.Input; using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using Ursa.Common;
using Ursa.Controls; using Ursa.Controls;
using Ursa.Demo.Dialogs; using Ursa.Demo.Dialogs;
using Ursa.Demo.Pages; using Ursa.Demo.Pages;
@@ -16,6 +17,8 @@ public class DialogDemoViewModel: ObservableObject
public ICommand ShowGlobalModalDialogCommand { get; } public ICommand ShowGlobalModalDialogCommand { get; }
public ICommand ShowGlobalOverlayDialogCommand { get; } public ICommand ShowGlobalOverlayDialogCommand { get; }
public ICommand ShowPlainGlobalOverlayDialogCommand { get; }
private object? _result; private object? _result;
@@ -41,6 +44,7 @@ public class DialogDemoViewModel: ObservableObject
ShowGlobalOverlayModalDialogCommand = new AsyncRelayCommand(ShowGlobalOverlayModalDialog); ShowGlobalOverlayModalDialogCommand = new AsyncRelayCommand(ShowGlobalOverlayModalDialog);
ShowGlobalModalDialogCommand = new AsyncRelayCommand(ShowGlobalModalDialog); ShowGlobalModalDialogCommand = new AsyncRelayCommand(ShowGlobalModalDialog);
ShowGlobalOverlayDialogCommand = new RelayCommand(ShowGlobalOverlayDialog); ShowGlobalOverlayDialogCommand = new RelayCommand(ShowGlobalOverlayDialog);
ShowPlainGlobalOverlayDialogCommand = new AsyncRelayCommand(ShowPlainGlobalOverlayDialog);
} }
private void ShowGlobalOverlayDialog() private void ShowGlobalOverlayDialog()
@@ -63,7 +67,18 @@ public class DialogDemoViewModel: ObservableObject
{ {
var vm = new DialogWithActionViewModel(); var vm = new DialogWithActionViewModel();
var result = await OverlayDialog.ShowModalAsync<DialogWithAction, DialogWithActionViewModel, bool>( var result = await OverlayDialog.ShowModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(
DialogViewModel, new DialogOptions() { ExtendToClientArea = true }, "LocalHost"); DialogViewModel, new DialogOptions() { ExtendToClientArea = true}, "LocalHost");
Result = result; Result = result;
} }
public async Task ShowPlainGlobalOverlayDialog()
{
var result = await OverlayDialog.ShowModalAsync<PlainDialog, PlainDialogViewModel, object?>(
new PlainDialogViewModel(),
new DialogOptions()
{
DefaultButtons = DialogButton.OKCancel,
},
"LocalHost");
}
} }

View File

@@ -41,6 +41,32 @@
DockPanel.Dock="Right" DockPanel.Dock="Right"
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:DialogControl.PART_CancelButton}"
Margin="8,0,0,0"
Classes="Tertiary"
Content="Cancel" />
<Button
Name="{x:Static u:DialogControl.PART_NoButton}"
Margin="8,0,0,0"
Classes="Danger"
Content="No" />
<Button
Name="{x:Static u:DialogControl.PART_YesButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="Yes" />
<Button
Name="{x:Static u:DialogControl.PART_OKButton}"
Margin="8,0,0,0"
Classes="Primary"
Content="OK" />
</StackPanel>
</Grid> </Grid>
</Border> </Border>
</ControlTemplate> </ControlTemplate>

View File

@@ -68,6 +68,7 @@ public static class OverlayDialog
DataContext = vm, DataContext = vm,
ExtendToClientArea = options.ExtendToClientArea, ExtendToClientArea = options.ExtendToClientArea,
Title = options.Title, Title = options.Title,
Buttons = options.DefaultButtons,
}; };
var host = OverlayDialogManager.GetHost(hostId); var host = OverlayDialogManager.GetHost(hostId);
host?.AddModalDialog(t); host?.AddModalDialog(t);
@@ -95,6 +96,7 @@ public static class OverlayDialog
DataContext = vm, DataContext = vm,
ExtendToClientArea = options.ExtendToClientArea, ExtendToClientArea = options.ExtendToClientArea,
Title = options.Title, Title = options.Title,
Buttons = options.DefaultButtons,
}; };
var host = OverlayDialogManager.GetHost(hostId); var host = OverlayDialogManager.GetHost(hostId);
host?.AddModalDialog(t); host?.AddModalDialog(t);

View File

@@ -4,22 +4,36 @@ 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;
[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_ExtendClientArea)]
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_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?>? OnClose; public event EventHandler<object?>? OnDialogControlClose;
public event EventHandler<DialogLayerChangeEventArgs>? OnLayerChange; public event EventHandler<DialogLayerChangeEventArgs>? OnLayerChange;
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DialogControl, string?>( public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DialogControl, string?>(
@@ -40,6 +54,8 @@ public class DialogControl: ContentControl
set => SetValue(ExtendToClientAreaProperty, value); 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));
@@ -50,12 +66,12 @@ public class DialogControl: ContentControl
{ {
if (args.OldValue.Value is IDialogContext oldContext) if (args.OldValue.Value is IDialogContext oldContext)
{ {
oldContext.Closed-= Close; oldContext.Closed-= CloseFromContext;
} }
if (args.NewValue.Value is IDialogContext newContext) if (args.NewValue.Value is IDialogContext newContext)
{ {
newContext.Closed += Close; newContext.Closed += CloseFromContext;
} }
} }
@@ -63,24 +79,83 @@ public class DialogControl: ContentControl
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
if (_closeButton != null) UnRegisterClickEvent(Close, _closeButton);
{ UnRegisterClickEvent(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton);
_closeButton.Click -= Close;
}
_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);
if (_closeButton is not null) _okButton = e.NameScope.Find<Button>(PART_OKButton);
{ _cancelButton = e.NameScope.Find<Button>(PART_CancelButton);
_closeButton.Click += Close; _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);
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)
{ {
e.Source = this; e.Source = this;
@@ -101,29 +176,60 @@ public class DialogControl: ContentControl
if (args is T result) if (args is T result)
{ {
tcs.SetResult(result); tcs.SetResult(result);
OnClose-= OnCloseHandler; OnDialogControlClose-= OnCloseHandler;
} }
else else
{ {
tcs.SetResult(default(T)); tcs.SetResult(default(T));
OnClose-= OnCloseHandler; OnDialogControlClose-= OnCloseHandler;
} }
} }
this.OnClose += OnCloseHandler; this.OnDialogControlClose += OnCloseHandler;
return tcs.Task; return tcs.Task;
} }
private void Close(object sender, object args) private void Close(object sender, RoutedEventArgs args)
{ {
if (this.DataContext is IDialogContext context) if (this.DataContext is IDialogContext context)
{ {
OnClose?.Invoke(this, Equals(sender, _closeButton) ? context.DefaultCloseResult : args); OnDialogControlClose?.Invoke(this, context.DefaultCloseResult);
} }
else else
{ {
OnClose?.Invoke(this, null); OnDialogControlClose?.Invoke(this, null);
}
}
private void CloseFromContext(object sender, object? args)
{
if (this.DataContext is IDialogContext context)
{
OnDialogControlClose?.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);
}
} }
} }

View File

@@ -7,5 +7,5 @@ public record DialogOptions
public bool ShowCloseButton { get; set; } = true; public bool ShowCloseButton { get; set; } = true;
public string? Title { get; set; } public string? Title { get; set; }
public bool ExtendToClientArea { get; set; } = false; public bool ExtendToClientArea { get; set; } = false;
public DialogButton DefaultButtons { get; set; } = DialogButton.OKCancel; public DialogButton DefaultButtons { get; set; } = DialogButton.None;
} }

View File

@@ -3,5 +3,5 @@ namespace Ursa.Controls;
public interface IDialogContext public interface IDialogContext
{ {
object? DefaultCloseResult { get; set; } object? DefaultCloseResult { get; set; }
event EventHandler<object> Closed; event EventHandler<object?> Closed;
} }

View File

@@ -94,17 +94,17 @@ 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.OnClose += OnDialogClose; control.OnDialogControlClose += OnDialogControlDialogClose;
control.OnLayerChange += OnDialogLayerChange; control.OnLayerChange += OnDialogLayerChange;
ResetZIndices(); ResetZIndices();
} }
private void OnDialogClose(object sender, object? e) private void OnDialogControlDialogClose(object sender, object? e)
{ {
if (sender is DialogControl control) if (sender is DialogControl control)
{ {
this.Children.Remove(control); this.Children.Remove(control);
control.OnClose -= OnDialogClose; control.OnDialogControlClose -= OnDialogControlDialogClose;
control.OnLayerChange -= OnDialogLayerChange; control.OnLayerChange -= OnDialogLayerChange;
if (_dialogs.Contains(control)) if (_dialogs.Contains(control))
{ {
@@ -147,7 +147,7 @@ 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.OnClose += OnDialogClose; control.OnDialogControlClose += OnDialogControlDialogClose;
control.OnLayerChange += OnDialogLayerChange; control.OnLayerChange += OnDialogLayerChange;
} }