feat: implement default button for dialog control.
This commit is contained in:
@@ -12,7 +12,7 @@ public partial class DialogWithActionViewModel: ObservableObject, IDialogContext
|
||||
[ObservableProperty] private string _title;
|
||||
[ObservableProperty] private DateTime _date;
|
||||
public object? DefaultCloseResult { get; set; } = true;
|
||||
public event EventHandler<object>? Closed;
|
||||
public event EventHandler<object?>? Closed;
|
||||
|
||||
public ICommand OKCommand { get; set; }
|
||||
public ICommand CancelCommand { get; set; }
|
||||
|
||||
@@ -4,5 +4,8 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
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>
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
<Button Command="{Binding ShowLocalOverlayModalDialogCommand}">Show Local 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 ShowPlainGlobalOverlayDialogCommand}">Default Buttons</Button>
|
||||
</StackPanel>
|
||||
<Grid Grid.Column="1">
|
||||
<Button
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Ursa.Common;
|
||||
using Ursa.Controls;
|
||||
using Ursa.Demo.Dialogs;
|
||||
using Ursa.Demo.Pages;
|
||||
@@ -16,6 +17,8 @@ public class DialogDemoViewModel: ObservableObject
|
||||
public ICommand ShowGlobalModalDialogCommand { get; }
|
||||
|
||||
public ICommand ShowGlobalOverlayDialogCommand { get; }
|
||||
|
||||
public ICommand ShowPlainGlobalOverlayDialogCommand { get; }
|
||||
|
||||
private object? _result;
|
||||
|
||||
@@ -41,6 +44,7 @@ public class DialogDemoViewModel: ObservableObject
|
||||
ShowGlobalOverlayModalDialogCommand = new AsyncRelayCommand(ShowGlobalOverlayModalDialog);
|
||||
ShowGlobalModalDialogCommand = new AsyncRelayCommand(ShowGlobalModalDialog);
|
||||
ShowGlobalOverlayDialogCommand = new RelayCommand(ShowGlobalOverlayDialog);
|
||||
ShowPlainGlobalOverlayDialogCommand = new AsyncRelayCommand(ShowPlainGlobalOverlayDialog);
|
||||
}
|
||||
|
||||
private void ShowGlobalOverlayDialog()
|
||||
@@ -63,7 +67,18 @@ public class DialogDemoViewModel: ObservableObject
|
||||
{
|
||||
var vm = new DialogWithActionViewModel();
|
||||
var result = await OverlayDialog.ShowModalAsync<DialogWithAction, DialogWithActionViewModel, bool>(
|
||||
DialogViewModel, new DialogOptions() { ExtendToClientArea = true }, "LocalHost");
|
||||
DialogViewModel, new DialogOptions() { ExtendToClientArea = true}, "LocalHost");
|
||||
Result = result;
|
||||
}
|
||||
|
||||
public async Task ShowPlainGlobalOverlayDialog()
|
||||
{
|
||||
var result = await OverlayDialog.ShowModalAsync<PlainDialog, PlainDialogViewModel, object?>(
|
||||
new PlainDialogViewModel(),
|
||||
new DialogOptions()
|
||||
{
|
||||
DefaultButtons = DialogButton.OKCancel,
|
||||
},
|
||||
"LocalHost");
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,32 @@
|
||||
DockPanel.Dock="Right"
|
||||
Theme="{DynamicResource CloseButton}" />
|
||||
</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>
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
|
||||
@@ -68,6 +68,7 @@ public static class OverlayDialog
|
||||
DataContext = vm,
|
||||
ExtendToClientArea = options.ExtendToClientArea,
|
||||
Title = options.Title,
|
||||
Buttons = options.DefaultButtons,
|
||||
};
|
||||
var host = OverlayDialogManager.GetHost(hostId);
|
||||
host?.AddModalDialog(t);
|
||||
@@ -95,6 +96,7 @@ public static class OverlayDialog
|
||||
DataContext = vm,
|
||||
ExtendToClientArea = options.ExtendToClientArea,
|
||||
Title = options.Title,
|
||||
Buttons = options.DefaultButtons,
|
||||
};
|
||||
var host = OverlayDialogManager.GetHost(hostId);
|
||||
host?.AddModalDialog(t);
|
||||
|
||||
@@ -4,22 +4,36 @@ using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
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)]
|
||||
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?>? OnClose;
|
||||
public event EventHandler<object?>? OnDialogControlClose;
|
||||
public event EventHandler<DialogLayerChangeEventArgs>? OnLayerChange;
|
||||
|
||||
public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<DialogControl, string?>(
|
||||
@@ -40,6 +54,8 @@ public class DialogControl: ContentControl
|
||||
set => SetValue(ExtendToClientAreaProperty, value);
|
||||
}
|
||||
|
||||
internal DialogButton Buttons { get; set; }
|
||||
|
||||
static DialogControl()
|
||||
{
|
||||
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)
|
||||
{
|
||||
oldContext.Closed-= Close;
|
||||
oldContext.Closed-= CloseFromContext;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
base.OnApplyTemplate(e);
|
||||
if (_closeButton != null)
|
||||
{
|
||||
_closeButton.Click -= Close;
|
||||
}
|
||||
UnRegisterClickEvent(Close, _closeButton);
|
||||
UnRegisterClickEvent(DefaultButtonsClose, _okButton, _cancelButton, _yesButton, _noButton);
|
||||
_titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove);
|
||||
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
|
||||
|
||||
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
|
||||
_titleArea = e.NameScope.Find<Panel>(PART_TitleArea);
|
||||
if (_closeButton is not null)
|
||||
{
|
||||
_closeButton.Click += Close;
|
||||
}
|
||||
_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;
|
||||
}
|
||||
|
||||
private void OnTitlePointerPressed(object sender, PointerPressedEventArgs e)
|
||||
{
|
||||
e.Source = this;
|
||||
@@ -101,29 +176,60 @@ public class DialogControl: ContentControl
|
||||
if (args is T result)
|
||||
{
|
||||
tcs.SetResult(result);
|
||||
OnClose-= OnCloseHandler;
|
||||
OnDialogControlClose-= OnCloseHandler;
|
||||
}
|
||||
else
|
||||
{
|
||||
tcs.SetResult(default(T));
|
||||
OnClose-= OnCloseHandler;
|
||||
OnDialogControlClose-= OnCloseHandler;
|
||||
}
|
||||
}
|
||||
|
||||
this.OnClose += OnCloseHandler;
|
||||
this.OnDialogControlClose += OnCloseHandler;
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
private void Close(object sender, object args)
|
||||
|
||||
private void Close(object sender, RoutedEventArgs args)
|
||||
{
|
||||
if (this.DataContext is IDialogContext context)
|
||||
{
|
||||
OnClose?.Invoke(this, Equals(sender, _closeButton) ? context.DefaultCloseResult : args);
|
||||
OnDialogControlClose?.Invoke(this, context.DefaultCloseResult);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,5 +7,5 @@ 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.OKCancel;
|
||||
public DialogButton DefaultButtons { get; set; } = DialogButton.None;
|
||||
}
|
||||
@@ -3,5 +3,5 @@ namespace Ursa.Controls;
|
||||
public interface IDialogContext
|
||||
{
|
||||
object? DefaultCloseResult { get; set; }
|
||||
event EventHandler<object> Closed;
|
||||
event EventHandler<object?> Closed;
|
||||
}
|
||||
@@ -94,17 +94,17 @@ public class OverlayDialogHost : Canvas
|
||||
control.Measure(this.Bounds.Size);
|
||||
control.Arrange(new Rect(control.DesiredSize));
|
||||
SetToCenter(control);
|
||||
control.OnClose += OnDialogClose;
|
||||
control.OnDialogControlClose += OnDialogControlDialogClose;
|
||||
control.OnLayerChange += OnDialogLayerChange;
|
||||
ResetZIndices();
|
||||
}
|
||||
|
||||
private void OnDialogClose(object sender, object? e)
|
||||
private void OnDialogControlDialogClose(object sender, object? e)
|
||||
{
|
||||
if (sender is DialogControl control)
|
||||
{
|
||||
this.Children.Remove(control);
|
||||
control.OnClose -= OnDialogClose;
|
||||
control.OnDialogControlClose -= OnDialogControlDialogClose;
|
||||
control.OnLayerChange -= OnDialogLayerChange;
|
||||
if (_dialogs.Contains(control))
|
||||
{
|
||||
@@ -147,7 +147,7 @@ public class OverlayDialogHost : Canvas
|
||||
control.Measure(this.Bounds.Size);
|
||||
control.Arrange(new Rect(control.DesiredSize));
|
||||
SetToCenter(control);
|
||||
control.OnClose += OnDialogClose;
|
||||
control.OnDialogControlClose += OnDialogControlDialogClose;
|
||||
control.OnLayerChange += OnDialogLayerChange;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user