feat: re-organize file, add IsCloseButtonVisible option.

This commit is contained in:
rabbitism
2024-02-03 20:07:39 +08:00
parent 96ca0d9075
commit 39457f9724
9 changed files with 87 additions and 34 deletions

View File

@@ -68,6 +68,8 @@ public class DefaultDialogControl: DialogControl
private void SetButtonVisibility() private void SetButtonVisibility()
{ {
bool isCloseButtonVisible = DataContext is IDialogContext || Buttons != DialogButton.YesNo;
SetVisibility(_closeButton, isCloseButtonVisible);
switch (Buttons) switch (Buttons)
{ {
case DialogButton.None: case DialogButton.None:
@@ -130,4 +132,25 @@ public class DefaultDialogControl: DialogControl
} }
} }
} }
internal override void CloseDialog()
{
if (DataContext is IDialogContext context)
{
context.Close();
}
else
{
DialogResult result = Buttons switch
{
DialogButton.None => DialogResult.None,
DialogButton.OK => DialogResult.OK,
DialogButton.OKCancel => DialogResult.Cancel,
DialogButton.YesNo => DialogResult.No,
DialogButton.YesNoCancel => DialogResult.Cancel,
_ => DialogResult.None
};
OnDialogControlClosing(this, result);
}
}
} }

View File

@@ -83,6 +83,8 @@ public class DefaultDialogWindow: DialogWindow
private void SetButtonVisibility() private void SetButtonVisibility()
{ {
bool closeButtonVisible = DataContext is IDialogContext || Buttons != DialogButton.YesNo;
SetVisibility(_closeButton, closeButtonVisible);
switch (Buttons) switch (Buttons)
{ {
case DialogButton.None: case DialogButton.None:
@@ -122,4 +124,25 @@ public class DefaultDialogWindow: DialogWindow
{ {
if (button is not null) button.IsVisible = visible; if (button is not null) button.IsVisible = visible;
} }
protected internal override void OnCloseButtonClicked(object sender, RoutedEventArgs args)
{
if (DataContext is IDialogContext context)
{
context.Close();
}
else
{
DialogResult result = Buttons switch
{
DialogButton.None => DialogResult.None,
DialogButton.OK => DialogResult.OK,
DialogButton.OKCancel => DialogResult.Cancel,
DialogButton.YesNo => DialogResult.No,
DialogButton.YesNoCancel => DialogResult.Cancel,
_ => DialogResult.None
};
Close(result);
}
}
} }

View File

@@ -31,6 +31,7 @@ public class DialogControl: ContentControl
internal double? HorizontalOffsetRatio { get; set; } internal double? HorizontalOffsetRatio { get; set; }
internal double? VerticalOffsetRatio { get; set; } internal double? VerticalOffsetRatio { get; set; }
internal bool CanClickOnMaskToClose { get; set; } internal bool CanClickOnMaskToClose { get; set; }
internal bool IsCloseButtonVisible { get; set; }
public event EventHandler<DialogLayerChangeEventArgs>? LayerChanged; public event EventHandler<DialogLayerChangeEventArgs>? LayerChanged;
public event EventHandler<object?>? DialogControlClosing; public event EventHandler<object?>? DialogControlClosing;
@@ -57,7 +58,7 @@ public class DialogControl: ContentControl
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
EventHelper.UnregisterClickEvent(Close, _closeButton); EventHelper.UnregisterClickEvent(OnCloseButtonClick, _closeButton);
_titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove); _titleArea?.RemoveHandler(PointerMovedEvent, OnTitlePointerMove);
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed); _titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
@@ -65,12 +66,14 @@ public class DialogControl: ContentControl
_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)
{
_closeButton.IsVisible = IsCloseButtonVisible;
}
_titleArea?.AddHandler(PointerMovedEvent, OnTitlePointerMove, RoutingStrategies.Bubble); _titleArea?.AddHandler(PointerMovedEvent, OnTitlePointerMove, RoutingStrategies.Bubble);
_titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble); _titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble);
_titleArea?.AddHandler(PointerReleasedEvent, OnTitlePointerRelease, RoutingStrategies.Bubble); _titleArea?.AddHandler(PointerReleasedEvent, OnTitlePointerRelease, RoutingStrategies.Bubble);
EventHelper.RegisterClickEvent(Close, _closeButton); EventHelper.RegisterClickEvent(OnCloseButtonClick, _closeButton);
} }
private void OnTitlePointerPressed(object sender, PointerPressedEventArgs e) private void OnTitlePointerPressed(object sender, PointerPressedEventArgs e)
@@ -113,25 +116,12 @@ public class DialogControl: ContentControl
this.DialogControlClosing += OnCloseHandler; this.DialogControlClosing += OnCloseHandler;
return tcs.Task; return tcs.Task;
} }
private void Close(object sender, RoutedEventArgs args) private void OnCloseButtonClick(object sender, RoutedEventArgs args) => CloseDialog();
{
if (this.DataContext is IDialogContext context)
{
context.Close();
}
else
{
DialogControlClosing?.Invoke(this, DialogResult.None);
}
}
private void OnContextRequestClose(object sender, object? args) private void OnContextRequestClose(object sender, object? args)
{ {
if (this.DataContext is IDialogContext context) DialogControlClosing?.Invoke(this, args);
{
DialogControlClosing?.Invoke(this, args);
}
} }
@@ -143,7 +133,12 @@ public class DialogControl: ContentControl
} }
} }
protected virtual void OnDialogControlClosing(object sender, object? args) /// <summary>
/// Used for inherited classes to invoke the DialogControlClosing event.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
protected internal virtual void OnDialogControlClosing(object sender, object? args)
{ {
DialogControlClosing?.Invoke(this, args); DialogControlClosing?.Invoke(this, args);
} }
@@ -153,15 +148,20 @@ public class DialogControl: ContentControl
PseudoClasses.Set(PC_Modal, modal); PseudoClasses.Set(PC_Modal, modal);
} }
internal void CloseDialog() /// <summary>
/// This method is exposed internally for closing the dialog from neither context nor closing by clicking on the close button.
/// It is also exposed to be bound to context flyout.
/// It is virtual because inherited classes may return a different result by default.
/// </summary>
internal virtual void CloseDialog()
{ {
if (this.DataContext is IDialogContext context) if (DataContext is IDialogContext context)
{ {
context.Close(); context.Close();
} }
else else
{ {
DialogControlClosing?.Invoke(this, DialogResult.None); DialogControlClosing?.Invoke(this, null);
} }
} }
} }

View File

@@ -16,8 +16,10 @@ public class DialogWindow: Window
public const string PART_TitleArea = "PART_TitleArea"; public const string PART_TitleArea = "PART_TitleArea";
protected override Type StyleKeyOverride { get; } = typeof(DialogWindow); protected override Type StyleKeyOverride { get; } = typeof(DialogWindow);
private Button? _closeButton; protected internal Button? _closeButton;
private Panel? _titleArea; private Panel? _titleArea;
internal bool IsCloseButtonVisible { get; set; }
static DialogWindow() static DialogWindow()
{ {
@@ -40,12 +42,16 @@ public class DialogWindow: Window
protected override void OnApplyTemplate(TemplateAppliedEventArgs e) protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{ {
base.OnApplyTemplate(e); base.OnApplyTemplate(e);
EventHelper.UnregisterClickEvent(OnDefaultClose, _closeButton); EventHelper.UnregisterClickEvent(OnCloseButtonClicked, _closeButton);
_titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed); _titleArea?.RemoveHandler(PointerPressedEvent, OnTitlePointerPressed);
_closeButton = e.NameScope.Find<Button>(PART_CloseButton); _closeButton = e.NameScope.Find<Button>(PART_CloseButton);
if (_closeButton is not null)
{
_closeButton.IsVisible = IsCloseButtonVisible;
}
_titleArea = e.NameScope.Find<Panel>(PART_TitleArea); _titleArea = e.NameScope.Find<Panel>(PART_TitleArea);
_titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble); _titleArea?.AddHandler(PointerPressedEvent, OnTitlePointerPressed, RoutingStrategies.Bubble);
EventHelper.RegisterClickEvent(OnDefaultClose, _closeButton); EventHelper.RegisterClickEvent(OnCloseButtonClicked, _closeButton);
} }
private void OnContextRequestClose(object? sender, object? args) private void OnContextRequestClose(object? sender, object? args)
@@ -53,7 +59,7 @@ public class DialogWindow: Window
Close(args); Close(args);
} }
private void OnDefaultClose(object sender, RoutedEventArgs args) protected internal virtual void OnCloseButtonClicked(object sender, RoutedEventArgs args)
{ {
if (DataContext is IDialogContext context) if (DataContext is IDialogContext context)
{ {

View File

@@ -1,4 +1,4 @@
namespace Ursa.Common; namespace Ursa.Controls;
public enum DialogButton public enum DialogButton
{ {

View File

@@ -1,11 +1,11 @@
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Ursa.Common;
namespace Ursa.Controls; namespace Ursa.Controls;
public class DialogOptions public class DialogOptions
{ {
public static DialogOptions Default { get; } = new DialogOptions();
/// <summary> /// <summary>
/// The Startup Location of DialogWindow. Default is <see cref="WindowStartupLocation.CenterOwner"/> /// The Startup Location of DialogWindow. Default is <see cref="WindowStartupLocation.CenterOwner"/>
/// </summary> /// </summary>
@@ -24,4 +24,6 @@ public class DialogOptions
public DialogMode Mode { get; set; } = DialogMode.None; public DialogMode Mode { get; set; } = DialogMode.None;
public DialogButton Button { get; set; } = DialogButton.OKCancel; public DialogButton Button { get; set; } = DialogButton.OKCancel;
public bool IsCloseButtonVisible { get; set; } = true;
} }

View File

@@ -1,4 +1,4 @@
namespace Ursa.Common; namespace Ursa.Controls;
public enum DialogResult public enum DialogResult
{ {

View File

@@ -1,6 +1,4 @@
using Ursa.Common; namespace Ursa.Controls;
namespace Ursa.Controls;
public enum HorizontalPosition public enum HorizontalPosition
{ {
@@ -26,4 +24,5 @@ public class OverlayDialogOptions
public DialogMode Mode { get; set; } = DialogMode.None; public DialogMode Mode { get; set; } = DialogMode.None;
public DialogButton Buttons { get; set; } = DialogButton.OKCancel; public DialogButton Buttons { get; set; } = DialogButton.OKCancel;
public string? Title { get; set; } = null; public string? Title { get; set; } = null;
public bool IsCloseButtonVisible { get; set; } = true;
} }