diff --git a/src/Commands/Checkout.cs b/src/Commands/Checkout.cs index b6d6ec41..40b2d7f5 100644 --- a/src/Commands/Checkout.cs +++ b/src/Commands/Checkout.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Text; @@ -6,13 +7,16 @@ namespace SourceGit.Commands { /// 检出 /// public class Checkout : Command { + private Action handler = null; public Checkout(string repo) { Cwd = repo; } - public bool Branch(string branch) { - Args = $"checkout {branch}"; + public bool Branch(string branch, Action onProgress) { + Args = $"checkout --progress {branch}"; + TraitErrorAsOutput = true; + handler = onProgress; return Exec(); } @@ -42,5 +46,9 @@ namespace SourceGit.Commands { Args = builder.ToString(); return Exec(); } + + public override void OnReadline(string line) { + handler?.Invoke(line); + } } } diff --git a/src/Views/Popups/Checkout.xaml b/src/Views/Popups/Checkout.xaml new file mode 100644 index 00000000..811c4088 --- /dev/null +++ b/src/Views/Popups/Checkout.xaml @@ -0,0 +1,10 @@ + + diff --git a/src/Views/Popups/Checkout.xaml.cs b/src/Views/Popups/Checkout.xaml.cs new file mode 100644 index 00000000..6a531dfd --- /dev/null +++ b/src/Views/Popups/Checkout.xaml.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; + +namespace SourceGit.Views.Popups { + + /// + /// 切换分支 + /// + public partial class Checkout : Controls.PopupWidget { + private string repo; + private string branch; + + public Checkout(string repo, string branch) { + this.repo = repo; + this.branch = branch; + + InitializeComponent(); + } + + public override string GetTitle() { + return App.Text("BranchCM.Checkout", branch); + } + + public override Task Start() { + UpdateProgress(GetTitle()); + + return Task.Run(() => { + Models.Watcher.SetEnabled(repo, false); + new Commands.Checkout(repo).Branch(branch, UpdateProgress); + Models.Watcher.SetEnabled(repo, true); + return true; + }); + } + } +} diff --git a/src/Views/Widgets/Dashboard.xaml.cs b/src/Views/Widgets/Dashboard.xaml.cs index 42902ea1..c65ec5c5 100644 --- a/src/Views/Widgets/Dashboard.xaml.cs +++ b/src/Views/Widgets/Dashboard.xaml.cs @@ -424,7 +424,7 @@ namespace SourceGit.Views.Widgets { if (node.Type == BranchNodeType.Branch) NavigateTo((node.Data as Models.Branch).Head); } - private async void OnTreeDoubleClick(object sender, MouseButtonEventArgs e) { + private void OnTreeDoubleClick(object sender, MouseButtonEventArgs e) { var item = sender as Controls.TreeItem; if (item == null) return; @@ -434,9 +434,7 @@ namespace SourceGit.Views.Widgets { var branch = node.Data as Models.Branch; if (!branch.IsLocal || branch.IsCurrent) return; - Models.Watcher.SetEnabled(repo.Path, false); - await Task.Run(() => new Commands.Checkout(repo.Path).Branch(branch.Name)); - Models.Watcher.SetEnabled(repo.Path, true); + new Popups.Checkout(repo.Path, branch.Name).ShowAndStart(); } private void OnTreeContextMenuOpening(object sender, ContextMenuEventArgs e) { @@ -510,10 +508,8 @@ namespace SourceGit.Views.Widgets { var checkout = new MenuItem(); checkout.Header = App.Text("BranchCM.Checkout", branch.Name); - checkout.Click += async (o, e) => { - Models.Watcher.SetEnabled(repo.Path, false); - await Task.Run(() => new Commands.Checkout(repo.Path).Branch(branch.Name)); - Models.Watcher.SetEnabled(repo.Path, true); + checkout.Click += (o, e) => { + new Popups.Checkout(repo.Path, branch.Name).ShowAndStart(); e.Handled = true; }; menu.Items.Add(checkout); @@ -676,14 +672,11 @@ namespace SourceGit.Views.Widgets { var checkout = new MenuItem(); checkout.Header = App.Text("BranchCM.Checkout", branch.Name); - checkout.Click += async (o, e) => { + checkout.Click += (o, e) => { foreach (var b in repo.Branches) { if (b.IsLocal && b.Upstream == branch.FullName) { if (b.IsCurrent) return; - - Models.Watcher.SetEnabled(repo.Path, false); - await Task.Run(() => new Commands.Checkout(repo.Path).Branch(b.Name)); - Models.Watcher.SetEnabled(repo.Path, true); + new Popups.Checkout(repo.Path, b.Name).ShowAndStart(); return; } } diff --git a/src/Views/Widgets/Histories.xaml.cs b/src/Views/Widgets/Histories.xaml.cs index e76cae16..ea9a9a24 100644 --- a/src/Views/Widgets/Histories.xaml.cs +++ b/src/Views/Widgets/Histories.xaml.cs @@ -457,10 +457,8 @@ namespace SourceGit.Views.Widgets { var checkout = new MenuItem(); checkout.Header = App.Text("BranchCM.Checkout", branch.Name); - checkout.Click += async (o, e) => { - Models.Watcher.SetEnabled(repo.Path, false); - await Task.Run(() => new Commands.Checkout(repo.Path).Branch(branch.Name)); - Models.Watcher.SetEnabled(repo.Path, true); + checkout.Click += (o, e) => { + new Popups.Checkout(repo.Path, branch.Name).ShowAndStart(); e.Handled = true; }; submenu.Items.Add(checkout); @@ -528,14 +526,11 @@ namespace SourceGit.Views.Widgets { var checkout = new MenuItem(); checkout.Header = App.Text("BranchCM.Checkout", name); - checkout.Click += async (o, e) => { + checkout.Click += (o, e) => { foreach (var b in repo.Branches) { if (b.IsLocal && b.Upstream == branch.FullName) { if (b.IsCurrent) return; - - Models.Watcher.SetEnabled(repo.Path, false); - await Task.Run(() => new Commands.Checkout(repo.Path).Branch(b.Name)); - Models.Watcher.SetEnabled(repo.Path, true); + new Popups.Checkout(repo.Path, b.Name).ShowAndStart(); return; } }