Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CanClose hook for UrsaWindow #431

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions demo/Ursa.Demo/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Ursa.Controls;

namespace Ursa.Demo.Views;
Expand All @@ -8,4 +9,10 @@ public MainWindow()
{
InitializeComponent();
}

protected override async Task<bool> CanClose()
{
var result = await MessageBox.ShowOverlayAsync("Are you sure you want to exit?\n您确定要退出吗?", "Exit", button: MessageBoxButton.YesNo);
return result == MessageBoxResult.Yes;
}
}
20 changes: 20 additions & 0 deletions src/Ursa/Windows/UrsaWindow.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using Avalonia;
using Avalonia.Controls;

Expand Down Expand Up @@ -100,5 +101,24 @@ public Thickness TitleBarMargin
set => SetValue(TitleBarMarginProperty, value);
}

protected virtual async Task<bool> 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);
}
}