From 8ee920bde479cf280b4c549fc79a7c0bbf36efd6 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Mon, 7 Oct 2024 17:53:41 +0800 Subject: [PATCH 1/2] feat: add CanClose hook to easily block window closing. --- demo/Ursa.Demo/Views/MainWindow.axaml.cs | 7 +++++++ src/Ursa/Windows/UrsaWindow.cs | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/demo/Ursa.Demo/Views/MainWindow.axaml.cs b/demo/Ursa.Demo/Views/MainWindow.axaml.cs index f5b4304d..4ed6d7c6 100644 --- a/demo/Ursa.Demo/Views/MainWindow.axaml.cs +++ b/demo/Ursa.Demo/Views/MainWindow.axaml.cs @@ -1,3 +1,4 @@ +using System.Threading.Tasks; using Ursa.Controls; namespace Ursa.Demo.Views; @@ -8,4 +9,10 @@ public MainWindow() { InitializeComponent(); } + + protected override async Task CanClose() + { + var result = await MessageBox.ShowOverlayAsync("Are you sure you want to exit?", "Exit", button: MessageBoxButton.YesNo); + return result == MessageBoxResult.Yes; + } } \ No newline at end of file diff --git a/src/Ursa/Windows/UrsaWindow.cs b/src/Ursa/Windows/UrsaWindow.cs index a6be365e..17194933 100644 --- a/src/Ursa/Windows/UrsaWindow.cs +++ b/src/Ursa/Windows/UrsaWindow.cs @@ -1,3 +1,4 @@ +using System.ComponentModel; using Avalonia; using Avalonia.Controls; @@ -100,5 +101,24 @@ public Thickness TitleBarMargin set => SetValue(TitleBarMarginProperty, value); } + protected virtual async Task CanClose() + { + return await Task.FromResult(true); + } + private bool _canClose = false; + protected override async void OnClosing(WindowClosingEventArgs e) + { + VerifyAccess(); + if (!_canClose) + { + e.Cancel = true; + _canClose = await CanClose(); + if (_canClose) + { + Close(); + } + } + base.OnClosing(e); + } } \ No newline at end of file From 37c16e2080ddd290d6283e37e14af6c2ee0d2483 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Mon, 7 Oct 2024 18:10:56 +0800 Subject: [PATCH 2/2] feat: temporary add both Chinese and English in demo message. --- demo/Ursa.Demo/Views/MainWindow.axaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/Ursa.Demo/Views/MainWindow.axaml.cs b/demo/Ursa.Demo/Views/MainWindow.axaml.cs index 4ed6d7c6..3edd6fe3 100644 --- a/demo/Ursa.Demo/Views/MainWindow.axaml.cs +++ b/demo/Ursa.Demo/Views/MainWindow.axaml.cs @@ -12,7 +12,7 @@ public MainWindow() protected override async Task CanClose() { - var result = await MessageBox.ShowOverlayAsync("Are you sure you want to exit?", "Exit", button: MessageBoxButton.YesNo); + var result = await MessageBox.ShowOverlayAsync("Are you sure you want to exit?\n您确定要退出吗?", "Exit", button: MessageBoxButton.YesNo); return result == MessageBoxResult.Yes; } } \ No newline at end of file