Skip to content
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
8 changes: 7 additions & 1 deletion src/ViewModels/LauncherPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,15 @@ public async Task ProcessPopupAsync()

public void CancelPopup()
{
if (_popup == null || _popup.InProgress)
if (_popup == null)
return;

if (_popup.InProgress)
{
_popup.Cancel();
return;
}

_popup?.Cleanup();
Popup = null;
}
Expand Down
14 changes: 13 additions & 1 deletion src/ViewModels/Popup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void OnReceiveCommandLog(string data)
ProgressDescription = desc;
}

public void Cleanup()
public virtual void Cleanup()
{
_log?.Unsubscribe(this);
}
Expand All @@ -44,6 +44,18 @@ public virtual bool CanStartDirectly()
return true;
}

/// <summary>
/// Indicates the in-progress operation of this popup can be interrupted.
/// </summary>
public virtual bool CanCancel => false;

/// <summary>
/// Interrupts the in-progress operation of this popup.
/// </summary>
public virtual void Cancel()
{
}

public virtual Task<bool> Sure()
{
return null;
Expand Down
21 changes: 20 additions & 1 deletion src/ViewModels/Push.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;

namespace SourceGit.ViewModels
Expand Down Expand Up @@ -190,6 +191,9 @@ public override async Task<bool> Sure()
{
using var lockWatcher = _repo.LockWatcher();

_cts?.Cancel();
_cts = new CancellationTokenSource();

var remoteBranchName = _selectedRemoteBranch.Name;
ProgressDescription = $"Push {_selectedLocalBranch.Name} -> {_selectedRemote.Name}/{remoteBranchName} ...";

Expand All @@ -204,12 +208,26 @@ public override async Task<bool> Sure()
PushAllTags,
_repo.Submodules.Count > 0 && CheckSubmodules,
_isSetTrackOptionVisible && _tracking,
ForcePush).Use(log).RunAsync();
ForcePush).Use(log).WithCancellation(_cts.Token).RunAsync();

log.Complete();
return succ;
}

public override bool CanCancel => true;

public override void Cancel()
{
_cts?.Cancel();
}

public override void Cleanup()
{
base.Cleanup();
_cts?.Dispose();
_cts = null;
}

private void AutoSelectBranchByRemote()
{
if (_selectedRemote == null || _selectedLocalBranch == null)
Expand Down Expand Up @@ -266,5 +284,6 @@ private void AutoSelectBranchByRemote()
private Models.Branch _selectedRemoteBranch = null;
private bool _isSetTrackOptionVisible = false;
private bool _tracking = true;
private CancellationTokenSource _cts = null;
}
}
4 changes: 2 additions & 2 deletions src/Views/LauncherPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@
<v:PopupRunningStatus Margin="12,8"
Focusable="False"
Description="{Binding ProgressDescription}"
IsVisible="{Binding InProgress}"
IsHitTestVisible="False"/>
CanCancel="{Binding CanCancel}"
IsVisible="{Binding InProgress}"/>
</StackPanel>
</DataTemplate>
</ContentControl.DataTemplates>
Expand Down
9 changes: 9 additions & 0 deletions src/Views/PopupRunningStatus.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
<StackPanel Orientation="Horizontal" Margin="0,8">
<ContentPresenter x:Name="Icon" Width="12" Height="12"/>
<TextBlock Margin="6,0,0,0" FontSize="14" FontWeight="Bold" Text="{DynamicResource Text.Running}"/>
<Button Classes="flat"
Width="100" Height="28"
Margin="12,0,0,0"
Padding="0"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Content="{DynamicResource Text.Cancel}"
Click="OnCancelRequested"
IsVisible="{Binding #ThisControl.CanCancel}"/>
</StackPanel>

<TextBlock HorizontalAlignment="Stretch"
Expand Down
21 changes: 21 additions & 0 deletions src/Views/PopupRunningStatus.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ public string Description
set => SetAndRaise(DescriptionProperty, ref _description, value);
}

public static readonly DirectProperty<PopupRunningStatus, bool> CanCancelProperty =
AvaloniaProperty.RegisterDirect<PopupRunningStatus, bool>(
nameof(CanCancel),
static o => o.CanCancel,
static (o, v) => o.CanCancel = v);

public bool CanCancel
{
get => _canCancel;
set => SetAndRaise(CanCancelProperty, ref _canCancel, value);
}

public PopupRunningStatus()
{
InitializeComponent();
Expand Down Expand Up @@ -52,6 +64,14 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
}
}

private void OnCancelRequested(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Popup popup)
popup.Cancel();

e.Handled = true;
}

private void StartAnim()
{
Icon.Content = new Path() { Classes = { "waiting" } };
Expand All @@ -67,6 +87,7 @@ private void StopAnim()
}

private string _description = string.Empty;
private bool _canCancel = false;
private bool _isUnloading = false;
}
}