diff --git a/src/Models/AvatarManager.cs b/src/Models/AvatarManager.cs index 69e12819..86181a9b 100644 --- a/src/Models/AvatarManager.cs +++ b/src/Models/AvatarManager.cs @@ -51,7 +51,7 @@ namespace SourceGit.Models LoadDefaultAvatar("noreply@github.com", "github.png"); LoadDefaultAvatar("unrealbot@epicgames.com", "unreal.png"); - Task.Run(() => + Task.Run(async () => { while (true) { @@ -84,7 +84,7 @@ namespace SourceGit.Models { using var client = new HttpClient(); client.Timeout = TimeSpan.FromSeconds(2); - var rsp = client.GetAsync(url).Result; + var rsp = await client.GetAsync(url); if (rsp.IsSuccessStatusCode) { using (var stream = rsp.Content.ReadAsStream()) diff --git a/src/ViewModels/EditRemote.cs b/src/ViewModels/EditRemote.cs index 4c7e3237..d5fdf52e 100644 --- a/src/ViewModels/EditRemote.cs +++ b/src/ViewModels/EditRemote.cs @@ -1,6 +1,7 @@ using System.ComponentModel.DataAnnotations; using System.IO; using System.Threading.Tasks; +using Avalonia.Threading; namespace SourceGit.ViewModels { @@ -53,7 +54,16 @@ namespace SourceGit.ViewModels _useSSH = Models.Remote.IsSSH(remote.URL); if (_useSSH) - SSHKey = new Commands.Config(repo.FullPath).GetAsync($"remote.{remote.Name}.sshkey").Result; + { + Task.Run(async () => + { + var sshKey = await new Commands.Config(repo.FullPath) + .GetAsync($"remote.{remote.Name}.sshkey") + .ConfigureAwait(false); + + Dispatcher.UIThread.Post(() => SSHKey = sshKey); + }); + } } public static ValidationResult ValidateRemoteName(string name, ValidationContext ctx) diff --git a/src/ViewModels/Histories.cs b/src/ViewModels/Histories.cs index fb4db9b2..5459a9bb 100644 --- a/src/ViewModels/Histories.cs +++ b/src/ViewModels/Histories.cs @@ -2,8 +2,10 @@ using System.Collections; using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; using Avalonia.Controls; +using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; namespace SourceGit.ViewModels @@ -133,34 +135,20 @@ namespace SourceGit.ViewModels public void NavigateTo(string commitSHA) { var commit = _commits.Find(x => x.SHA.StartsWith(commitSHA, StringComparison.Ordinal)); - if (commit == null) - { - AutoSelectedCommit = null; - commit = new Commands.QuerySingleCommit(_repo.FullPath, commitSHA).GetResultAsync().Result; - } - else - { - AutoSelectedCommit = commit; - NavigationId = _navigationId + 1; - } - if (commit != null) { - if (_detailContext is CommitDetail detail) - { - detail.Commit = commit; - } - else - { - var commitDetail = new CommitDetail(_repo, true); - commitDetail.Commit = commit; - DetailContext = commitDetail; - } + NavigateTo(commit); + return; } - else + + Task.Run(async () => { - DetailContext = null; - } + var c = await new Commands.QuerySingleCommit(_repo.FullPath, commitSHA) + .GetResultAsync() + .ConfigureAwait(false); + + Dispatcher.UIThread.Post(() => NavigateTo(c)); + }); } public void Select(IList commits) @@ -297,6 +285,31 @@ namespace SourceGit.ViewModels } } + private void NavigateTo(Models.Commit commit) + { + AutoSelectedCommit = commit; + + if (commit == null) + { + DetailContext = null; + } + else + { + NavigationId = _navigationId + 1; + + if (_detailContext is CommitDetail detail) + { + detail.Commit = commit; + } + else + { + var commitDetail = new CommitDetail(_repo, true); + commitDetail.Commit = commit; + DetailContext = commitDetail; + } + } + } + private Repository _repo = null; private bool _isLoading = true; private List _commits = new List(); diff --git a/src/ViewModels/Reword.cs b/src/ViewModels/Reword.cs index 2ce27853..e89c9de0 100644 --- a/src/ViewModels/Reword.cs +++ b/src/ViewModels/Reword.cs @@ -18,10 +18,10 @@ namespace SourceGit.ViewModels set => SetProperty(ref _message, value, true); } - public Reword(Repository repo, Models.Commit head) + public Reword(Repository repo, Models.Commit head, string oldMessage) { _repo = repo; - _oldMessage = new Commands.QueryCommitFullMessage(_repo.FullPath, head.SHA).GetResultAsync().Result; + _oldMessage = oldMessage; _message = _oldMessage; Head = head; } diff --git a/src/ViewModels/Squash.cs b/src/ViewModels/Squash.cs index a575a64c..31216f9c 100644 --- a/src/ViewModels/Squash.cs +++ b/src/ViewModels/Squash.cs @@ -17,10 +17,10 @@ namespace SourceGit.ViewModels set => SetProperty(ref _message, value, true); } - public Squash(Repository repo, Models.Commit target, string shaToGetPreferMessage) + public Squash(Repository repo, Models.Commit target, string message) { _repo = repo; - _message = new Commands.QueryCommitFullMessage(_repo.FullPath, shaToGetPreferMessage).GetResultAsync().Result; + _message = message; Target = target; } diff --git a/src/Views/CommitMessagePresenter.cs b/src/Views/CommitMessagePresenter.cs index ea1b98f2..6457394b 100644 --- a/src/Views/CommitMessagePresenter.cs +++ b/src/Views/CommitMessagePresenter.cs @@ -241,7 +241,6 @@ namespace SourceGit.Views { var sha = link.Link; - // If we have already queried this SHA, just use it. if (_inlineCommits.TryGetValue(sha, out var exist)) { ToolTip.SetTip(this, exist); @@ -251,22 +250,18 @@ namespace SourceGit.Views var parentView = this.FindAncestorOfType(); if (parentView is { DataContext: ViewModels.CommitDetail detail }) { - // Record the SHA of current viewing commit in the CommitDetail panel to determine if it is changed after - // asynchronous queries. var lastDetailCommit = detail.Commit.SHA; - Task.Run(() => + Task.Run(async () => { - var c = detail.GetCommitAsync(sha).Result; - Dispatcher.UIThread.Invoke(() => + var c = await detail.GetCommitAsync(sha); + + Dispatcher.UIThread.Post(() => { - // Make sure the DataContext of CommitBaseInfo is not changed. var currentParent = this.FindAncestorOfType(); if (currentParent is { DataContext: ViewModels.CommitDetail currentDetail } && currentDetail.Commit.SHA == lastDetailCommit) { _inlineCommits.TryAdd(sha, c); - - // Make sure user still hovers the target SHA. if (_lastHover == link && c != null) ToolTip.SetTip(this, c); } diff --git a/src/Views/Histories.axaml.cs b/src/Views/Histories.axaml.cs index 189482fa..01fb3e1c 100644 --- a/src/Views/Histories.axaml.cs +++ b/src/Views/Histories.axaml.cs @@ -526,10 +526,14 @@ namespace SourceGit.Views var reword = new MenuItem(); reword.Header = App.Text("CommitCM.Reword"); reword.Icon = App.CreateMenuIcon("Icons.Edit"); - reword.Click += (_, e) => + reword.Click += async (_, e) => { if (repo.CanCreatePopup()) - repo.ShowPopup(new ViewModels.Reword(repo, commit)); + { + var message = await new Commands.QueryCommitFullMessage(repo.FullPath, commit.SHA).GetResultAsync(); + repo.ShowPopup(new ViewModels.Reword(repo, commit, message)); + } + e.Handled = true; }; menu.Items.Add(reword); @@ -538,13 +542,14 @@ namespace SourceGit.Views squash.Header = App.Text("CommitCM.Squash"); squash.Icon = App.CreateMenuIcon("Icons.SquashIntoParent"); squash.IsEnabled = commit.Parents.Count == 1; - squash.Click += (_, e) => + squash.Click += async (_, e) => { if (commit.Parents.Count == 1) { + var message = await new Commands.QueryCommitFullMessage(repo.FullPath, commit.SHA).GetResultAsync(); var parent = vm.Commits.Find(x => x.SHA.Equals(commit.Parents[0])); if (parent != null && repo.CanCreatePopup()) - repo.ShowPopup(new ViewModels.Squash(repo, parent, commit.SHA)); + repo.ShowPopup(new ViewModels.Squash(repo, parent, message)); } e.Handled = true;