refactor: use popup to change tracking branch

This commit is contained in:
leo 2025-01-07 12:02:09 +08:00
parent 25a2bf603f
commit 6fe4d8162b
No known key found for this signature in database
13 changed files with 170 additions and 47 deletions

View file

@ -1624,38 +1624,12 @@ namespace SourceGit.ViewModels
var tracking = new MenuItem();
tracking.Header = App.Text("BranchCM.Tracking");
tracking.Icon = App.CreateMenuIcon("Icons.Track");
foreach (var b in remoteBranches)
tracking.Click += (_, e) =>
{
var upstream = b.FullName.Replace("refs/remotes/", "");
var target = new MenuItem();
target.Header = upstream;
if (branch.Upstream == b.FullName)
target.Icon = App.CreateMenuIcon("Icons.Check");
target.Click += (_, e) =>
{
if (Commands.Branch.SetUpstream(_fullpath, branch.Name, upstream))
Task.Run(RefreshBranches);
e.Handled = true;
};
tracking.Items.Add(target);
}
var unsetUpstream = new MenuItem();
unsetUpstream.Header = App.Text("BranchCM.UnsetUpstream");
unsetUpstream.Click += (_, e) =>
{
if (Commands.Branch.SetUpstream(_fullpath, branch.Name, string.Empty))
Task.Run(RefreshBranches);
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new SetUpstream(this, branch, remoteBranches));
e.Handled = true;
};
tracking.Items.Add(new MenuItem() { Header = "-" });
tracking.Items.Add(unsetUpstream);
menu.Items.Add(tracking);
}

View file

@ -0,0 +1,78 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia.Threading;
namespace SourceGit.ViewModels
{
public class SetUpstream : Popup
{
public Models.Branch Local
{
get;
private set;
}
public List<Models.Branch> RemoteBranches
{
get;
private set;
}
public Models.Branch SelectedRemoteBranch
{
get;
set;
}
public bool Unset
{
get => _unset;
set => SetProperty(ref _unset, value);
}
public SetUpstream(Repository repo, Models.Branch local, List<Models.Branch> remoteBranches)
{
_repo = repo;
Local = local;
RemoteBranches = remoteBranches;
Unset = false;
if (!string.IsNullOrEmpty(local.Upstream))
{
var upstream = remoteBranches.Find(x => x.FullName == local.Upstream);
if (upstream != null)
SelectedRemoteBranch = upstream;
}
if (SelectedRemoteBranch == null)
{
var upstream = remoteBranches.Find(x => x.Name == local.Name);
if (upstream != null)
SelectedRemoteBranch = upstream;
}
View = new Views.SetUpstream() { DataContext = this };
}
public override Task<bool> Sure()
{
SetProgressDescription("Setting upstream...");
var upstream = (_unset || SelectedRemoteBranch == null) ? string.Empty : SelectedRemoteBranch.FullName;
if (upstream == Local.Upstream)
return null;
return Task.Run(() =>
{
var succ = Commands.Branch.SetUpstream(_repo.FullPath, Local.Name, upstream.Replace("refs/remotes/", ""));
if (succ)
_repo.RefreshBranches();
return true;
});
}
private Repository _repo;
private bool _unset = false;
}
}