refactor: more changes to use async methods

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-08-14 16:10:09 +08:00
parent f0adb02986
commit 1522da89a2
12 changed files with 113 additions and 96 deletions

View File

@@ -193,7 +193,7 @@ namespace SourceGit.ViewModels
}
}
public bool CheckoutBranchByDecorator(Models.Decorator decorator)
public async Task<bool> CheckoutBranchByDecoratorAsync(Models.Decorator decorator)
{
if (decorator == null)
return false;
@@ -208,7 +208,7 @@ namespace SourceGit.ViewModels
if (b == null)
return false;
_repo.CheckoutBranch(b);
await _repo.CheckoutBranchAsync(b);
return true;
}
@@ -231,7 +231,7 @@ namespace SourceGit.ViewModels
}
else if (!lb.IsCurrent)
{
_repo.CheckoutBranch(lb);
await _repo.CheckoutBranchAsync(lb);
}
return true;
@@ -240,7 +240,7 @@ namespace SourceGit.ViewModels
return false;
}
public void CheckoutBranchByCommit(Models.Commit commit)
public async Task CheckoutBranchByCommitAsync(Models.Commit commit)
{
if (commit.IsCurrentHead)
return;
@@ -254,7 +254,7 @@ namespace SourceGit.ViewModels
if (b == null)
continue;
_repo.CheckoutBranch(b);
await _repo.CheckoutBranchAsync(b);
return;
}

View File

@@ -89,15 +89,7 @@ namespace SourceGit.ViewModels
return _popup is not { InProgress: true };
}
public void StartPopup(Popup popup)
{
Popup = popup;
if (popup.CanStartDirectly())
ProcessPopup();
}
public async void ProcessPopup()
public async Task ProcessPopupAsync()
{
if (_popup is { InProgress: false } dump)
{

View File

@@ -636,9 +636,13 @@ namespace SourceGit.ViewModels
page.Popup = popup;
}
public void ShowAndStartPopup(Popup popup)
public async Task ShowAndStartPopupAsync(Popup popup)
{
GetOwnerPage()?.StartPopup(popup);
var page = GetOwnerPage();
page.Popup = popup;
if (popup.CanStartDirectly())
await page.ProcessPopupAsync();
}
public bool IsGitFlowEnabled()
@@ -802,7 +806,7 @@ namespace SourceGit.ViewModels
});
}
public void Fetch(bool autoStart)
public async Task FetchAsync(bool autoStart)
{
if (!CanCreatePopup())
return;
@@ -814,12 +818,12 @@ namespace SourceGit.ViewModels
}
if (autoStart)
ShowAndStartPopup(new Fetch(this));
await ShowAndStartPopupAsync(new Fetch(this));
else
ShowPopup(new Fetch(this));
}
public void Pull(bool autoStart)
public async Task PullAsync(bool autoStart)
{
if (IsBare || !CanCreatePopup())
return;
@@ -838,12 +842,12 @@ namespace SourceGit.ViewModels
var pull = new Pull(this, null);
if (autoStart && pull.SelectedBranch != null)
ShowAndStartPopup(pull);
await ShowAndStartPopupAsync(pull);
else
ShowPopup(pull);
}
public void Push(bool autoStart)
public async Task PushAsync(bool autoStart)
{
if (!CanCreatePopup())
return;
@@ -861,7 +865,7 @@ namespace SourceGit.ViewModels
}
if (autoStart)
ShowAndStartPopup(new Push(this, null));
await ShowAndStartPopupAsync(new Push(this, null));
else
ShowPopup(new Push(this, null));
}
@@ -872,7 +876,7 @@ namespace SourceGit.ViewModels
ShowPopup(new Apply(this));
}
public void ExecCustomAction(Models.CustomAction action, object scope)
public async Task ExecCustomActionAsync(Models.CustomAction action, object scope)
{
if (!CanCreatePopup())
return;
@@ -886,15 +890,15 @@ namespace SourceGit.ViewModels
};
if (action.Controls.Count == 0)
ShowAndStartPopup(popup);
await ShowAndStartPopupAsync(popup);
else
ShowPopup(popup);
}
public void Cleanup()
public async Task CleanupAsync()
{
if (CanCreatePopup())
ShowAndStartPopup(new Cleanup(this));
await ShowAndStartPopupAsync(new Cleanup(this));
}
public void ClearFilter()
@@ -1126,9 +1130,9 @@ namespace SourceGit.ViewModels
RefreshHistoriesFilters(refresh);
}
public void StashAll(bool autoStart)
public async Task StashAllAsync(bool autoStart)
{
_workingCopy?.StashAll(autoStart);
await _workingCopy?.StashAllAsync(autoStart);
}
public async Task SkipMergeAsync()
@@ -1441,7 +1445,7 @@ namespace SourceGit.ViewModels
ShowPopup(new CreateBranch(this, _currentBranch));
}
public void CheckoutBranch(Models.Branch branch)
public async Task CheckoutBranchAsync(Models.Branch branch)
{
if (branch.IsLocal)
{
@@ -1464,7 +1468,7 @@ namespace SourceGit.ViewModels
if (_localChangesCount > 0 || _submodules.Count > 0)
ShowPopup(new Checkout(this, branch.Name));
else
ShowAndStartPopup(new Checkout(this, branch.Name));
await ShowAndStartPopupAsync(new Checkout(this, branch.Name));
}
else
{
@@ -1477,7 +1481,7 @@ namespace SourceGit.ViewModels
if (b.TrackStatus.Behind.Count > 0)
ShowPopup(new CheckoutAndFastForward(this, b, branch));
else if (!b.IsCurrent)
CheckoutBranch(b);
await CheckoutBranchAsync(b);
return;
}
@@ -1491,7 +1495,7 @@ namespace SourceGit.ViewModels
{
var c = await new Commands.QuerySingleCommit(_fullpath, tag.SHA).GetResultAsync();
if (c != null)
_histories?.CheckoutBranchByCommit(c);
await _histories?.CheckoutBranchByCommitAsync(c);
}
public async Task CompareBranchWithWorktree(Models.Branch branch)
@@ -1593,10 +1597,10 @@ namespace SourceGit.ViewModels
ShowPopup(new AddWorktree(this));
}
public void PruneWorktrees()
public async Task PruneWorktreesAsync()
{
if (CanCreatePopup())
ShowAndStartPopup(new PruneWorktrees(this));
await ShowAndStartPopupAsync(new PruneWorktrees(this));
}
public void OpenWorktree(Models.Worktree worktree)

View File

@@ -330,13 +330,13 @@ namespace SourceGit.ViewModels
Native.OS.OpenWithDefaultEditor(absPath);
}
public void StashAll(bool autoStart)
public async Task StashAllAsync(bool autoStart)
{
if (!_repo.CanCreatePopup())
return;
if (autoStart)
_repo.ShowAndStartPopup(new StashChanges(_repo, _cached, false));
await _repo.ShowAndStartPopupAsync(new StashChanges(_repo, _cached, false));
else
_repo.ShowPopup(new StashChanges(_repo, _cached, false));
}
@@ -679,7 +679,7 @@ namespace SourceGit.ViewModels
}
if (_repo.CanCreatePopup())
_repo.ShowAndStartPopup(new Push(_repo, pushBranch));
await _repo.ShowAndStartPopupAsync(new Push(_repo, pushBranch));
}
}

View File

@@ -516,7 +516,7 @@ namespace SourceGit.Views
e.Handled = true;
}
private void OnDoubleTappedBranchNode(object sender, TappedEventArgs _)
private async void OnDoubleTappedBranchNode(object sender, TappedEventArgs _)
{
if (sender is Grid { DataContext: ViewModels.BranchTreeNode node })
{
@@ -526,7 +526,7 @@ namespace SourceGit.Views
return;
if (DataContext is ViewModels.Repository { Settings: not null } repo)
repo.CheckoutBranch(branch);
await repo.CheckoutBranchAsync(branch);
}
else
{
@@ -606,10 +606,10 @@ namespace SourceGit.Views
fastForward.Header = App.Text("BranchCM.FastForward", upstream.FriendlyName);
fastForward.Icon = App.CreateMenuIcon("Icons.FastForward");
fastForward.IsEnabled = branch.TrackStatus.Ahead.Count == 0 && branch.TrackStatus.Behind.Count > 0;
fastForward.Click += (_, e) =>
fastForward.Click += async (_, e) =>
{
if (repo.CanCreatePopup())
repo.ShowAndStartPopup(new ViewModels.Merge(repo, upstream, branch.Name, true));
await repo.ShowAndStartPopupAsync(new ViewModels.Merge(repo, upstream, branch.Name, true));
e.Handled = true;
};
@@ -638,9 +638,9 @@ namespace SourceGit.Views
var checkout = new MenuItem();
checkout.Header = App.Text("BranchCM.Checkout", branch.Name);
checkout.Icon = App.CreateMenuIcon("Icons.Check");
checkout.Click += (_, e) =>
checkout.Click += async (_, e) =>
{
repo.CheckoutBranch(branch);
await repo.CheckoutBranchAsync(branch);
e.Handled = true;
};
menu.Items.Add(checkout);
@@ -654,10 +654,10 @@ namespace SourceGit.Views
fastForward.Header = App.Text("BranchCM.FastForward", upstream.FriendlyName);
fastForward.Icon = App.CreateMenuIcon("Icons.FastForward");
fastForward.IsEnabled = branch.TrackStatus.Ahead.Count == 0 && branch.TrackStatus.Behind.Count > 0;
fastForward.Click += (_, e) =>
fastForward.Click += async (_, e) =>
{
if (repo.CanCreatePopup())
repo.ShowAndStartPopup(new ViewModels.ResetWithoutCheckout(repo, branch, upstream));
await repo.ShowAndStartPopupAsync(new ViewModels.ResetWithoutCheckout(repo, branch, upstream));
e.Handled = true;
};
menu.Items.Add(fastForward);
@@ -666,10 +666,10 @@ namespace SourceGit.Views
fetchInto.Header = App.Text("BranchCM.FetchInto", upstream.FriendlyName, branch.Name);
fetchInto.Icon = App.CreateMenuIcon("Icons.Fetch");
fetchInto.IsEnabled = branch.TrackStatus.Ahead.Count == 0;
fetchInto.Click += (_, e) =>
fetchInto.Click += async (_, e) =>
{
if (repo.CanCreatePopup())
repo.ShowAndStartPopup(new ViewModels.FetchInto(repo, branch, upstream));
await repo.ShowAndStartPopupAsync(new ViewModels.FetchInto(repo, branch, upstream));
e.Handled = true;
};
@@ -898,10 +898,10 @@ namespace SourceGit.Views
var prune = new MenuItem();
prune.Header = App.Text("RemoteCM.Prune");
prune.Icon = App.CreateMenuIcon("Icons.Clean");
prune.Click += (_, e) =>
prune.Click += async (_, e) =>
{
if (repo.CanCreatePopup())
repo.ShowAndStartPopup(new ViewModels.PruneRemote(repo, remote));
await repo.ShowAndStartPopupAsync(new ViewModels.PruneRemote(repo, remote));
e.Handled = true;
};
@@ -952,9 +952,9 @@ namespace SourceGit.Views
var checkout = new MenuItem();
checkout.Header = App.Text("BranchCM.Checkout", name);
checkout.Icon = App.CreateMenuIcon("Icons.Check");
checkout.Click += (_, e) =>
checkout.Click += async (_, e) =>
{
repo.CheckoutBranch(branch);
await repo.CheckoutBranchAsync(branch);
e.Handled = true;
};
menu.Items.Add(checkout);
@@ -1099,9 +1099,9 @@ namespace SourceGit.Views
var item = new MenuItem();
item.Icon = App.CreateMenuIcon("Icons.Action");
item.Header = label;
item.Click += (_, e) =>
item.Click += async (_, e) =>
{
repo.ExecCustomAction(dup, branch);
await repo.ExecCustomActionAsync(dup, branch);
e.Handled = true;
};

View File

@@ -298,7 +298,7 @@ namespace SourceGit.Views
}
}
private void OnCommitListDoubleTapped(object sender, TappedEventArgs e)
private async void OnCommitListDoubleTapped(object sender, TappedEventArgs e)
{
e.Handled = true;
@@ -310,12 +310,13 @@ namespace SourceGit.Views
if (e.Source is CommitRefsPresenter crp)
{
var decorator = crp.DecoratorAt(e.GetPosition(crp));
if (histories.CheckoutBranchByDecorator(decorator))
var succ = await histories.CheckoutBranchByDecoratorAsync(decorator);
if (succ)
return;
}
if (e.Source is Control { DataContext: Models.Commit c })
histories.CheckoutBranchByCommit(c);
await histories.CheckoutBranchByCommitAsync(c);
}
}
@@ -811,9 +812,9 @@ namespace SourceGit.Views
var item = new MenuItem();
item.Icon = App.CreateMenuIcon("Icons.Action");
item.Header = label;
item.Click += (_, e) =>
item.Click += async (_, e) =>
{
repo.ExecCustomAction(dup, commit);
await repo.ExecCustomActionAsync(dup, commit);
e.Handled = true;
};
@@ -913,14 +914,14 @@ namespace SourceGit.Views
fastForward.Header = App.Text("BranchCM.FastForward", upstream);
fastForward.Icon = App.CreateMenuIcon("Icons.FastForward");
fastForward.IsEnabled = current.TrackStatus.Ahead.Count == 0 && current.TrackStatus.Behind.Count > 0;
fastForward.Click += (_, e) =>
fastForward.Click += async (_, e) =>
{
var b = repo.Branches.Find(x => x.FriendlyName == upstream);
if (b == null)
return;
if (repo.CanCreatePopup())
repo.ShowAndStartPopup(new ViewModels.Merge(repo, b, current.Name, true));
await repo.ShowAndStartPopupAsync(new ViewModels.Merge(repo, b, current.Name, true));
e.Handled = true;
};
@@ -1011,9 +1012,9 @@ namespace SourceGit.Views
var checkout = new MenuItem();
checkout.Header = App.Text("BranchCM.Checkout", branch.Name);
checkout.Icon = App.CreateMenuIcon("Icons.Check");
checkout.Click += (_, e) =>
checkout.Click += async (_, e) =>
{
repo.CheckoutBranch(branch);
await repo.CheckoutBranchAsync(branch);
e.Handled = true;
};
submenu.Items.Add(checkout);
@@ -1103,9 +1104,9 @@ namespace SourceGit.Views
var checkout = new MenuItem();
checkout.Header = App.Text("BranchCM.Checkout", name);
checkout.Icon = App.CreateMenuIcon("Icons.Check");
checkout.Click += (_, e) =>
checkout.Click += async (_, e) =>
{
repo.CheckoutBranch(branch);
await repo.CheckoutBranchAsync(branch);
e.Handled = true;
};
submenu.Items.Add(checkout);

View File

@@ -15,7 +15,7 @@ namespace SourceGit.Views
InitializeComponent();
}
private void OnPopupSureByHotKey(object sender, RoutedEventArgs e)
private async void OnPopupSureByHotKey(object sender, RoutedEventArgs e)
{
var children = this.GetLogicalDescendants();
foreach (var child in children)
@@ -39,13 +39,16 @@ namespace SourceGit.Views
}
}
OnPopupSure(sender, e);
if (DataContext is ViewModels.LauncherPage page)
await page.ProcessPopupAsync();
e.Handled = true;
}
private void OnPopupSure(object _, RoutedEventArgs e)
private async void OnPopupSure(object _, RoutedEventArgs e)
{
if (DataContext is ViewModels.LauncherPage page)
page.ProcessPopup();
await page.ProcessPopupAsync();
e.Handled = true;
}

View File

@@ -379,7 +379,7 @@
Classes="icon_button"
Width="14"
Margin="8,0"
Command="{Binding PruneWorktrees}"
Click="OnPruneWorktrees"
IsVisible="{Binding Worktrees, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"
ToolTip.Tip="{DynamicResource Text.Repository.Worktrees.Prune}">
<Path Width="12" Height="12" Data="{StaticResource Icons.Loading}"/>

View File

@@ -643,6 +643,14 @@ namespace SourceGit.Views
e.Handled = true;
}
private async void OnPruneWorktrees(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)
await repo.PruneWorktreesAsync();
e.Handled = true;
}
private async void OnSkipInProgress(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)

View File

@@ -103,7 +103,7 @@
<Path Width="14" Height="14" Data="{StaticResource Icons.Action}"/>
</Button>
<Button Classes="icon_button" Width="32" Margin="8,0,0,0" Command="{Binding Cleanup}" ToolTip.Tip="{DynamicResource Text.Repository.Clean}">
<Button Classes="icon_button" Width="32" Margin="8,0,0,0" Click="Cleanup" ToolTip.Tip="{DynamicResource Text.Repository.Clean}">
<Path Width="14" Height="14" Margin="0,1,0,0" Data="{StaticResource Icons.Clean}"/>
</Button>
</StackPanel>

View File

@@ -122,65 +122,65 @@ namespace SourceGit.Views
}
}
private void Fetch(object sender, TappedEventArgs e)
private async void Fetch(object sender, TappedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)
{
repo.Fetch(e.KeyModifiers is KeyModifiers.Control);
await repo.FetchAsync(e.KeyModifiers is KeyModifiers.Control);
e.Handled = true;
}
}
private void FetchDirectlyByHotKey(object sender, RoutedEventArgs e)
private async void FetchDirectlyByHotKey(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)
{
repo.Fetch(true);
await repo.FetchAsync(true);
e.Handled = true;
}
}
private void Pull(object sender, TappedEventArgs e)
private async void Pull(object sender, TappedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)
{
repo.Pull(e.KeyModifiers is KeyModifiers.Control);
await repo.PullAsync(e.KeyModifiers is KeyModifiers.Control);
e.Handled = true;
}
}
private void PullDirectlyByHotKey(object sender, RoutedEventArgs e)
private async void PullDirectlyByHotKey(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)
{
repo.Pull(true);
await repo.PullAsync(true);
e.Handled = true;
}
}
private void Push(object sender, TappedEventArgs e)
private async void Push(object sender, TappedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)
{
repo.Push(e.KeyModifiers is KeyModifiers.Control);
await repo.PushAsync(e.KeyModifiers is KeyModifiers.Control);
e.Handled = true;
}
}
private void PushDirectlyByHotKey(object sender, RoutedEventArgs e)
private async void PushDirectlyByHotKey(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)
{
repo.Push(true);
await repo.PushAsync(true);
e.Handled = true;
}
}
private void StashAll(object _, TappedEventArgs e)
private async void StashAll(object _, TappedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)
{
repo.StashAll(e.KeyModifiers is KeyModifiers.Control);
await repo.StashAllAsync(e.KeyModifiers is KeyModifiers.Control);
e.Handled = true;
}
}
@@ -277,12 +277,12 @@ namespace SourceGit.Views
fetch.Header = App.Text("GitLFS.Fetch");
fetch.Icon = App.CreateMenuIcon("Icons.Fetch");
fetch.IsEnabled = repo.Remotes.Count > 0;
fetch.Click += (_, e) =>
fetch.Click += async (_, e) =>
{
if (repo.CanCreatePopup())
{
if (repo.Remotes.Count == 1)
repo.ShowAndStartPopup(new ViewModels.LFSFetch(repo));
await repo.ShowAndStartPopupAsync(new ViewModels.LFSFetch(repo));
else
repo.ShowPopup(new ViewModels.LFSFetch(repo));
}
@@ -295,12 +295,12 @@ namespace SourceGit.Views
pull.Header = App.Text("GitLFS.Pull");
pull.Icon = App.CreateMenuIcon("Icons.Pull");
pull.IsEnabled = repo.Remotes.Count > 0;
pull.Click += (_, e) =>
pull.Click += async (_, e) =>
{
if (repo.CanCreatePopup())
{
if (repo.Remotes.Count == 1)
repo.ShowAndStartPopup(new ViewModels.LFSPull(repo));
await repo.ShowAndStartPopupAsync(new ViewModels.LFSPull(repo));
else
repo.ShowPopup(new ViewModels.LFSPull(repo));
}
@@ -313,12 +313,12 @@ namespace SourceGit.Views
push.Header = App.Text("GitLFS.Push");
push.Icon = App.CreateMenuIcon("Icons.Push");
push.IsEnabled = repo.Remotes.Count > 0;
push.Click += (_, e) =>
push.Click += async (_, e) =>
{
if (repo.CanCreatePopup())
{
if (repo.Remotes.Count == 1)
repo.ShowAndStartPopup(new ViewModels.LFSPush(repo));
await repo.ShowAndStartPopupAsync(new ViewModels.LFSPush(repo));
else
repo.ShowPopup(new ViewModels.LFSPush(repo));
}
@@ -330,10 +330,10 @@ namespace SourceGit.Views
var prune = new MenuItem();
prune.Header = App.Text("GitLFS.Prune");
prune.Icon = App.CreateMenuIcon("Icons.Clean");
prune.Click += (_, e) =>
prune.Click += async (_, e) =>
{
if (repo.CanCreatePopup())
repo.ShowAndStartPopup(new ViewModels.LFSPrune(repo));
await repo.ShowAndStartPopupAsync(new ViewModels.LFSPrune(repo));
e.Handled = true;
};
@@ -406,6 +406,15 @@ namespace SourceGit.Views
e.Handled = true;
}
private async void Cleanup(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Repository repo)
{
await repo.CleanupAsync();
e.Handled = true;
}
}
private void OpenCustomActionMenu(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Repository repo && sender is Control control)
@@ -422,9 +431,9 @@ namespace SourceGit.Views
var item = new MenuItem();
item.Icon = App.CreateMenuIcon("Icons.Action");
item.Header = label;
item.Click += (_, e) =>
item.Click += async (_, e) =>
{
repo.ExecCustomAction(dup, null);
await repo.ExecCustomActionAsync(dup, null);
e.Handled = true;
};

View File

@@ -280,9 +280,9 @@ namespace SourceGit.Views
var item = new MenuItem();
item.Icon = App.CreateMenuIcon("Icons.Action");
item.Header = label;
item.Click += (_, e) =>
item.Click += async (_, e) =>
{
repo.ExecCustomAction(dup, tag);
await repo.ExecCustomActionAsync(dup, tag);
e.Handled = true;
};