Skip to content

Commit

Permalink
Merge pull request #10296 from soosr/release-fix-dialog-close
Browse files Browse the repository at this point in the history
[Release] dialog close prevention
  • Loading branch information
molnard committed Mar 15, 2023
2 parents 27a0cb5 + 7dc0978 commit 6c71c61
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions WalletWasabi.Fluent/Controls/Dialog.axaml.cs
@@ -1,4 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
Expand All @@ -7,6 +6,8 @@
using Avalonia.Interactivity;
using Avalonia.VisualTree;
using ReactiveUI;
using WalletWasabi.Fluent.Extensions;
using WalletWasabi.Fluent.Helpers;

namespace WalletWasabi.Fluent.Controls;

Expand All @@ -17,7 +18,9 @@ public class Dialog : ContentControl
{
private Panel? _dismissPanel;
private Panel? _overlayPanel;
private bool _canCancelOnPointerPressed;
private bool _canCancelOpenedOnPointerPressed;
private bool _canCancelActivatedOnPointerPressed;
private IDisposable? _updateActivatedDelayDisposable;

public static readonly StyledProperty<bool> IsDialogOpenProperty =
AvaloniaProperty.Register<Dialog, bool>(nameof(IsDialogOpen));
Expand Down Expand Up @@ -69,7 +72,7 @@ public class Dialog : ContentControl

public Dialog()
{
this.GetObservable(IsDialogOpenProperty).Subscribe(UpdateDelay);
this.GetObservable(IsDialogOpenProperty).SubscribeAsync(UpdateOpenedDelayAsync);

this.WhenAnyValue(x => x.Bounds)
.Subscribe(bounds =>
Expand Down Expand Up @@ -188,25 +191,42 @@ private bool IncreasedSizeEnabled
set => SetValue(IncreasedSizeEnabledProperty, value);
}

private CancellationTokenSource? CancelPointerPressedDelay { get; set; }
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);

_updateActivatedDelayDisposable = ApplicationHelper.MainWindowActivated.SubscribeAsync(UpdateActivatedDelayAsync);
}

private void UpdateDelay(bool isDialogOpen)
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
try
{
_canCancelOnPointerPressed = false;
CancelPointerPressedDelay?.Cancel();
base.OnDetachedFromVisualTree(e);

if (isDialogOpen)
{
CancelPointerPressedDelay = new CancellationTokenSource();
_updateActivatedDelayDisposable?.Dispose();
}

Task.Delay(TimeSpan.FromSeconds(1), CancelPointerPressedDelay.Token).ContinueWith(_ => _canCancelOnPointerPressed = true);
}
private async Task UpdateOpenedDelayAsync(bool isDialogOpen)
{
_canCancelOpenedOnPointerPressed = false;

if (isDialogOpen)
{
await Task.Delay(TimeSpan.FromSeconds(1));
_canCancelOpenedOnPointerPressed = true;
}
}

private async Task UpdateActivatedDelayAsync(bool isWindowActivated)
{
if (!isWindowActivated)
{
_canCancelActivatedOnPointerPressed = false;
}
catch (OperationCanceledException)

if (isWindowActivated)
{
// ignored
await Task.Delay(TimeSpan.FromSeconds(1));
_canCancelActivatedOnPointerPressed = true;
}
}

Expand Down Expand Up @@ -246,7 +266,14 @@ private void Close()

private void CancelPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (IsDialogOpen && IsActive && EnableCancelOnPressed && !IsBusy && _dismissPanel is { } && _overlayPanel is { } && _canCancelOnPointerPressed)
if (IsDialogOpen
&& IsActive
&& EnableCancelOnPressed
&& !IsBusy
&& _dismissPanel is { }
&& _overlayPanel is { }
&& _canCancelOpenedOnPointerPressed
&& _canCancelActivatedOnPointerPressed)
{
var point = e.GetPosition(_dismissPanel);
var isPressedOnTitleBar = e.GetPosition(_overlayPanel).Y < 30;
Expand Down

0 comments on commit 6c71c61

Please sign in to comment.