From 7558bbd4f2176d1a91870356161460e72eaedc65 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 6 Aug 2020 15:41:25 +0800 Subject: [PATCH] refactor(PopupManager): rewrite popup --- SourceGit/App.xaml.cs | 31 +--------- SourceGit/UI/AddSubmodule.xaml.cs | 7 +-- SourceGit/UI/Apply.xaml.cs | 7 +-- SourceGit/UI/CherryPick.xaml.cs | 10 +-- SourceGit/UI/Clone.xaml.cs | 15 ++--- SourceGit/UI/Configure.xaml.cs | 5 +- SourceGit/UI/CreateBranch.xaml.cs | 14 ++--- SourceGit/UI/CreateTag.xaml.cs | 11 ++-- SourceGit/UI/Dashboard.xaml.cs | 2 +- SourceGit/UI/DeleteBranch.xaml.cs | 7 +-- SourceGit/UI/DeleteRemote.xaml.cs | 7 +-- SourceGit/UI/DeleteTag.xaml.cs | 7 +-- SourceGit/UI/Discard.xaml.cs | 17 +++-- SourceGit/UI/Fetch.xaml.cs | 7 +-- SourceGit/UI/FileHistories.xaml.cs | 1 - SourceGit/UI/GitFlowFinishBranch.xaml.cs | 10 ++- SourceGit/UI/GitFlowSetup.xaml.cs | 7 +-- SourceGit/UI/GitFlowStartBranch.xaml.cs | 10 ++- SourceGit/UI/Init.xaml.cs | 17 ++--- SourceGit/UI/Launcher.xaml | 8 +-- SourceGit/UI/Launcher.xaml.cs | 79 ++++++++++-------------- SourceGit/UI/Manager.xaml.cs | 8 +-- SourceGit/UI/Merge.xaml.cs | 9 ++- SourceGit/UI/PopupManager.xaml.cs | 39 ++++++++++++ SourceGit/UI/Pull.xaml.cs | 7 +-- SourceGit/UI/Push.xaml.cs | 9 ++- SourceGit/UI/PushTag.xaml.cs | 8 +-- SourceGit/UI/Rebase.xaml.cs | 10 ++- SourceGit/UI/Remote.xaml.cs | 6 +- SourceGit/UI/RenameBranch.xaml.cs | 6 +- SourceGit/UI/Reset.xaml.cs | 6 +- SourceGit/UI/Revert.xaml.cs | 8 +-- SourceGit/UI/Stash.xaml.cs | 7 +-- SourceGit/UI/Waiting.xaml.cs | 2 +- 34 files changed, 183 insertions(+), 221 deletions(-) diff --git a/SourceGit/App.xaml.cs b/SourceGit/App.xaml.cs index ce22b182..0636a298 100644 --- a/SourceGit/App.xaml.cs +++ b/SourceGit/App.xaml.cs @@ -28,40 +28,13 @@ namespace SourceGit { } } - /// - /// Error handler. - /// - public static Action OnError { - get; - set; - } - /// /// Raise error message. /// /// - public static void RaiseError(string message) { - OnError?.Invoke(message); - } - - /// - /// Get popup manager by repository - /// - /// - /// - public static UI.PopupManager GetPopupManager(Git.Repository repo) { + public static void RaiseError(string msg) { var main = Current.MainWindow as UI.Launcher; - if (main == null) return null; - if (repo == null) return (main.Tabs[0].Page as UI.Manager).popupManager; - - for (int i = 1; i < main.Tabs.Count; i++) { - var opened = main.Tabs[i]; - if (opened.Repo.Path == repo.Path) { - return (opened.Page as UI.Dashboard).popupManager; - } - } - - return null; + main.Dispatcher.Invoke(() => main.Errors.Add(msg)); } /// diff --git a/SourceGit/UI/AddSubmodule.xaml.cs b/SourceGit/UI/AddSubmodule.xaml.cs index ebc46cec..7a9c9471 100644 --- a/SourceGit/UI/AddSubmodule.xaml.cs +++ b/SourceGit/UI/AddSubmodule.xaml.cs @@ -34,8 +34,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo) { - var popup = App.GetPopupManager(repo); - popup?.Show(new AddSubmodule(repo)); + repo.GetPopupManager()?.Show(new AddSubmodule(repo)); } #region EVENTS @@ -58,7 +57,7 @@ namespace SourceGit.UI { if (Validation.GetHasError(txtPath)) return; var recursive = chkRecursive.IsChecked == true; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => repo.AddSubmodule(RepoURL, LocalPath, recursive, msg => { @@ -68,7 +67,7 @@ namespace SourceGit.UI { } private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } #endregion } diff --git a/SourceGit/UI/Apply.xaml.cs b/SourceGit/UI/Apply.xaml.cs index 7d881a98..0c025c24 100644 --- a/SourceGit/UI/Apply.xaml.cs +++ b/SourceGit/UI/Apply.xaml.cs @@ -52,8 +52,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository opened) { - var popup = App.GetPopupManager(opened); - popup?.Show(new Apply(opened)); + opened.GetPopupManager()?.Show(new Apply(opened)); } /// @@ -83,7 +82,7 @@ namespace SourceGit.UI { txtPatchFile.GetBindingExpression(TextBox.TextProperty).UpdateSource(); if (Validation.GetHasError(txtPatchFile)) return; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); var mode = combWhitespaceOptions.SelectedItem as WhitespaceOption; @@ -99,7 +98,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/CherryPick.xaml.cs b/SourceGit/UI/CherryPick.xaml.cs index 9491bdc2..0d315529 100644 --- a/SourceGit/UI/CherryPick.xaml.cs +++ b/SourceGit/UI/CherryPick.xaml.cs @@ -29,8 +29,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo, Git.Commit commit) { - var popup = App.GetPopupManager(repo); - popup?.Show(new CherryPick(repo, commit)); + repo.GetPopupManager()?.Show(new CherryPick(repo, commit)); } /// @@ -40,9 +39,7 @@ namespace SourceGit.UI { /// private void Start(object sender, RoutedEventArgs e) { repo.CherryPick(commitSHA, chkCommitChanges.IsChecked != true); - - var popup = App.GetPopupManager(repo); - popup?.Close(); + repo.GetPopupManager()?.Close(); } /// @@ -51,8 +48,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); - popup?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Clone.xaml.cs b/SourceGit/UI/Clone.xaml.cs index fad40a03..278f13cb 100644 --- a/SourceGit/UI/Clone.xaml.cs +++ b/SourceGit/UI/Clone.xaml.cs @@ -9,6 +9,7 @@ namespace SourceGit.UI { /// Clone dialog. /// public partial class Clone : UserControl { + private PopupManager popup = null; /// /// Remote repository @@ -33,19 +34,12 @@ namespace SourceGit.UI { /// /// Constructor. /// - public Clone() { + public Clone(PopupManager mgr) { ParentFolder = App.Preference.GitDefaultCloneDir; + popup = mgr; InitializeComponent(); } - /// - /// Show clone dialog. - /// - public static void Show() { - var popup = App.GetPopupManager(null); - popup?.Show(new Clone()); - } - /// /// Select parent folder. /// @@ -92,7 +86,6 @@ namespace SourceGit.UI { rName = RemoteName; } - var popup = App.GetPopupManager(null); popup.Lock(); var repo = await Task.Run(() => { @@ -113,7 +106,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(null).Close(); + popup.Close(); } } } diff --git a/SourceGit/UI/Configure.xaml.cs b/SourceGit/UI/Configure.xaml.cs index b16c3157..cdabed7b 100644 --- a/SourceGit/UI/Configure.xaml.cs +++ b/SourceGit/UI/Configure.xaml.cs @@ -43,8 +43,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo) { - var popup = App.GetPopupManager(repo); - popup?.Show(new Configure(repo)); + repo.GetPopupManager()?.Show(new Configure(repo)); } #region EVENTS @@ -64,7 +63,7 @@ namespace SourceGit.UI { } private void Close(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } #endregion } diff --git a/SourceGit/UI/CreateBranch.xaml.cs b/SourceGit/UI/CreateBranch.xaml.cs index 5efc18cf..4e08fc41 100644 --- a/SourceGit/UI/CreateBranch.xaml.cs +++ b/SourceGit/UI/CreateBranch.xaml.cs @@ -58,9 +58,7 @@ namespace SourceGit.UI { dialog.basedOnDesc.Content = branch.Name; if (!branch.IsLocal) dialog.txtName.Text = branch.Name.Substring(branch.Remote.Length + 1); - - var popup = App.GetPopupManager(repo); - popup?.Show(dialog); + repo.GetPopupManager()?.Show(dialog); } /// @@ -74,8 +72,7 @@ namespace SourceGit.UI { dialog.basedOnType.Data = dialog.FindResource("Icon.Tag") as Geometry; dialog.basedOnDesc.Content = tag.Name; - var popup = App.GetPopupManager(repo); - popup?.Show(dialog); + repo.GetPopupManager()?.Show(dialog); } /// @@ -89,8 +86,7 @@ namespace SourceGit.UI { dialog.basedOnType.Data = dialog.FindResource("Icon.Commit") as Geometry; dialog.basedOnDesc.Content = $"{commit.ShortSHA} {commit.Subject}"; - var popup = App.GetPopupManager(repo); - popup?.Show(dialog); + repo.GetPopupManager()?.Show(dialog); } /// @@ -102,7 +98,7 @@ namespace SourceGit.UI { txtName.GetBindingExpression(TextBox.TextProperty).UpdateSource(); if (Validation.GetHasError(txtName)) return; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); bool checkout = chkCheckout.IsChecked == true; @@ -135,7 +131,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/CreateTag.xaml.cs b/SourceGit/UI/CreateTag.xaml.cs index bf2ef238..ffbf0b07 100644 --- a/SourceGit/UI/CreateTag.xaml.cs +++ b/SourceGit/UI/CreateTag.xaml.cs @@ -52,8 +52,7 @@ namespace SourceGit.UI { dialog.basedOnType.Data = dialog.FindResource("Icon.Branch") as Geometry; dialog.basedOnDesc.Content = branch.Name; - var popup = App.GetPopupManager(repo); - popup?.Show(dialog); + repo.GetPopupManager()?.Show(dialog); } /// @@ -67,8 +66,7 @@ namespace SourceGit.UI { dialog.basedOnType.Data = dialog.FindResource("Icon.Commit") as Geometry; dialog.basedOnDesc.Content = $"{commit.ShortSHA} {commit.Subject}"; - var popup = App.GetPopupManager(repo); - popup?.Show(dialog); + repo.GetPopupManager()?.Show(dialog); } /// @@ -82,8 +80,7 @@ namespace SourceGit.UI { Git.Tag.Add(repo, TagName, based, tagMessage.Text); - var popup = App.GetPopupManager(repo); - popup?.Close(); + repo.GetPopupManager()?.Close(); } /// @@ -92,7 +89,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Dashboard.xaml.cs b/SourceGit/UI/Dashboard.xaml.cs index d4994fab..181ac094 100644 --- a/SourceGit/UI/Dashboard.xaml.cs +++ b/SourceGit/UI/Dashboard.xaml.cs @@ -959,7 +959,7 @@ namespace SourceGit.UI { sub.Name = Path.GetFileName(path); sub.Parent = repo; - if (!(App.Current.MainWindow as Launcher).GotoRepo(sub)) sub.Open(); + if (!sub.BringUpTab()) sub.Open(); } #endregion diff --git a/SourceGit/UI/DeleteBranch.xaml.cs b/SourceGit/UI/DeleteBranch.xaml.cs index d5f56321..8ae826fc 100644 --- a/SourceGit/UI/DeleteBranch.xaml.cs +++ b/SourceGit/UI/DeleteBranch.xaml.cs @@ -28,8 +28,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository opened, Git.Branch branch) { - var popup = App.GetPopupManager(opened); - popup?.Show(new DeleteBranch(opened, branch)); + opened.GetPopupManager()?.Show(new DeleteBranch(opened, branch)); } /// @@ -38,7 +37,7 @@ namespace SourceGit.UI { /// /// private async void Sure(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => branch.Delete(repo)); popup?.Close(true); @@ -50,7 +49,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/DeleteRemote.xaml.cs b/SourceGit/UI/DeleteRemote.xaml.cs index 960c614c..76c05c15 100644 --- a/SourceGit/UI/DeleteRemote.xaml.cs +++ b/SourceGit/UI/DeleteRemote.xaml.cs @@ -29,8 +29,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository opened, string remote) { - var popup = App.GetPopupManager(opened); - popup?.Show(new DeleteRemote(opened, remote)); + opened.GetPopupManager()?.Show(new DeleteRemote(opened, remote)); } /// @@ -39,7 +38,7 @@ namespace SourceGit.UI { /// /// private async void Sure(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => Git.Remote.Delete(repo, remote)); popup?.Close(true); @@ -51,7 +50,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/DeleteTag.xaml.cs b/SourceGit/UI/DeleteTag.xaml.cs index 320c6167..8233c016 100644 --- a/SourceGit/UI/DeleteTag.xaml.cs +++ b/SourceGit/UI/DeleteTag.xaml.cs @@ -30,8 +30,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo, Git.Tag tag) { - var popup = App.GetPopupManager(repo); - popup?.Show(new DeleteTag(repo, tag)); + repo.GetPopupManager()?.Show(new DeleteTag(repo, tag)); } /// @@ -40,7 +39,7 @@ namespace SourceGit.UI { /// /// private async void Start(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); var push = chkWithRemote.IsChecked == true; @@ -55,7 +54,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Discard.xaml.cs b/SourceGit/UI/Discard.xaml.cs index 794835d8..752f5e57 100644 --- a/SourceGit/UI/Discard.xaml.cs +++ b/SourceGit/UI/Discard.xaml.cs @@ -40,19 +40,28 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository opened, List targets) { - var popup = App.GetPopupManager(opened); - popup?.Show(new Discard(opened, targets)); + opened.GetPopupManager()?.Show(new Discard(opened, targets)); } + /// + /// Start to discard changes + /// + /// + /// private async void Sure(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => repo.Discard(changes)); popup?.Close(true); } + /// + /// Cancel + /// + /// + /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Fetch.xaml.cs b/SourceGit/UI/Fetch.xaml.cs index 3efb3eb9..bc4de0df 100644 --- a/SourceGit/UI/Fetch.xaml.cs +++ b/SourceGit/UI/Fetch.xaml.cs @@ -40,8 +40,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo, string preferRemote = null) { - var popup = App.GetPopupManager(repo); - popup?.Show(new Fetch(repo, preferRemote)); + repo.GetPopupManager()?.Show(new Fetch(repo, preferRemote)); } /// @@ -52,7 +51,7 @@ namespace SourceGit.UI { private async void Start(object sender, RoutedEventArgs e) { bool prune = chkPrune.IsChecked == true; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); if (chkFetchAll.IsChecked == true) { @@ -71,7 +70,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/FileHistories.xaml.cs b/SourceGit/UI/FileHistories.xaml.cs index 5f72b4a2..48f0eed8 100644 --- a/SourceGit/UI/FileHistories.xaml.cs +++ b/SourceGit/UI/FileHistories.xaml.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; diff --git a/SourceGit/UI/GitFlowFinishBranch.xaml.cs b/SourceGit/UI/GitFlowFinishBranch.xaml.cs index bcd48dad..83bf6047 100644 --- a/SourceGit/UI/GitFlowFinishBranch.xaml.cs +++ b/SourceGit/UI/GitFlowFinishBranch.xaml.cs @@ -36,8 +36,7 @@ namespace SourceGit.UI { txtBranchType.Content = "Hotfix :"; break; default: - var popup = App.GetPopupManager(repo); - popup?.Close(); + repo.GetPopupManager()?.Close(); return; } @@ -50,8 +49,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo, Git.Branch branch) { - var popup = App.GetPopupManager(repo); - popup?.Show(new GitFlowFinishBranch(repo, branch)); + repo.GetPopupManager()?.Show(new GitFlowFinishBranch(repo, branch)); } /// @@ -60,7 +58,7 @@ namespace SourceGit.UI { /// /// private async void Sure(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => repo.FinishGitFlowBranch(branch)); popup?.Close(true); @@ -72,7 +70,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/GitFlowSetup.xaml.cs b/SourceGit/UI/GitFlowSetup.xaml.cs index 75ac07bb..8b9ae8db 100644 --- a/SourceGit/UI/GitFlowSetup.xaml.cs +++ b/SourceGit/UI/GitFlowSetup.xaml.cs @@ -26,8 +26,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo) { - var popup = App.GetPopupManager(repo); - popup?.Show(new GitFlowSetup(repo)); + repo.GetPopupManager()?.Show(new GitFlowSetup(repo)); } /// @@ -36,7 +35,7 @@ namespace SourceGit.UI { /// /// private async void Sure(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); var master = txtMaster.Text; @@ -56,7 +55,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } /// diff --git a/SourceGit/UI/GitFlowStartBranch.xaml.cs b/SourceGit/UI/GitFlowStartBranch.xaml.cs index 5a0cc3e2..8d602ccb 100644 --- a/SourceGit/UI/GitFlowStartBranch.xaml.cs +++ b/SourceGit/UI/GitFlowStartBranch.xaml.cs @@ -51,8 +51,7 @@ namespace SourceGit.UI { nameValidator.Prefix = hotfixPrefix; break; default: - var popup = App.GetPopupManager(repo); - popup?.Close(); + repo.GetPopupManager()?.Close(); return; } } @@ -63,8 +62,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo, Git.Branch.Type type) { - var popup = App.GetPopupManager(repo); - popup?.Show(new GitFlowStartBranch(repo, type)); + repo.GetPopupManager()?.Show(new GitFlowStartBranch(repo, type)); } /// @@ -76,7 +74,7 @@ namespace SourceGit.UI { txtName.GetBindingExpression(TextBox.TextProperty).UpdateSource(); if (Validation.GetHasError(txtName)) return; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => repo.StartGitFlowBranch(type, SubName)); popup?.Close(true); @@ -88,7 +86,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Init.xaml.cs b/SourceGit/UI/Init.xaml.cs index d7bd449a..13fe1ce5 100644 --- a/SourceGit/UI/Init.xaml.cs +++ b/SourceGit/UI/Init.xaml.cs @@ -8,34 +8,27 @@ namespace SourceGit.UI { /// `git init` confirm panel. /// public partial class Init : UserControl { + private PopupManager popup = null; private string workingDir = null; /// /// Constructor. /// + /// /// - public Init(string path) { + public Init(PopupManager mgr, string path) { + popup = mgr; workingDir = path; InitializeComponent(); txtPath.Content = path; } - /// - /// Show this dialog. - /// - /// - public static void Show(string path) { - var popup = App.GetPopupManager(null); - popup.Show(new Init(path)); - } - /// /// Do `git init` /// /// /// private async void Sure(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(null); popup.Lock(); await Task.Run(() => { @@ -59,7 +52,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(null).Close(); + popup.Close(); } } } diff --git a/SourceGit/UI/Launcher.xaml b/SourceGit/UI/Launcher.xaml index 66f07899..c561e377 100644 --- a/SourceGit/UI/Launcher.xaml +++ b/SourceGit/UI/Launcher.xaml @@ -255,7 +255,7 @@ HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" Panel.ZIndex="100"> - + @@ -272,12 +272,12 @@ - - public class Alert { - public string Title { get; set; } - public string Message { get; set; } - } - /// /// Alerts. /// - public ObservableCollection Alerts { get; set; } = new ObservableCollection(); + public ObservableCollection Errors { get; set; } = new ObservableCollection(); /// /// Opened tabs. @@ -43,19 +35,17 @@ namespace SourceGit.UI { /// Constructor /// public Launcher() { - App.OnError = msg => { - ShowAlert(new Alert() { Title = "ERROR", Message = msg }); - }; - Git.Repository.OnOpen = repo => { Dispatcher.Invoke(() => { + var page = new Dashboard(repo); var tab = new Tab() { Title = repo.Parent == null ? repo.Name : $"{repo.Parent.Name} : {repo.Name}", Tooltip = repo.Path, Repo = repo, - Page = new Dashboard(repo), + Page = page, }; + repo.SetPopupManager(page.popupManager); Tabs.Add(tab); openedTabs.SelectedItem = tab; }); @@ -71,23 +61,6 @@ namespace SourceGit.UI { openedTabs.SelectedItem = Tabs[0]; } - /// - /// Goto opened repository. - /// - /// - /// - public bool GotoRepo(Git.Repository repo) { - for (int i = 1; i < Tabs.Count; i++) { - var opened = Tabs[i]; - if (opened.Repo.Path == repo.Path) { - openedTabs.SelectedItem = opened; - return true; - } - } - - return false; - } - #region LAYOUT_CONTENT /// /// Close repository tab. @@ -98,12 +71,11 @@ namespace SourceGit.UI { var tab = (sender as Button).DataContext as Tab; if (tab == null || tab.Repo == null) return; - var popup = (tab.Page as Dashboard).popupManager; - if (popup.IsLocked()) popup.Close(true); Tabs.Remove(tab); - tab.Page = null; + tab.Repo.RemovePopup(); tab.Repo.Close(); + tab.Repo = null; } /// @@ -128,22 +100,14 @@ namespace SourceGit.UI { about.ShowDialog(); } - /// - /// Show alert. - /// - /// - private void ShowAlert(Alert alert) { - Dispatcher.Invoke(() => Alerts.Add(alert)); - } - /// /// Remove an alert. /// /// /// - private void RemoveAlert(object sender, RoutedEventArgs e) { - var alert = (sender as Button).DataContext as Alert; - Alerts.Remove(alert); + private void RemoveError(object sender, RoutedEventArgs e) { + var alert = (sender as Button).DataContext as string; + Errors.Remove(alert); } #endregion @@ -222,4 +186,29 @@ namespace SourceGit.UI { } #endregion } + + /// + /// Extension methods for repository. + /// + public static class RepositoryTabBindings { + + /// + /// Bring up tab of repository if it was opened before. + /// + /// + /// + public static bool BringUpTab(this Git.Repository repo) { + var main = App.Current.MainWindow as Launcher; + + for (int i = 1; i < main.Tabs.Count; i++) { + var opened = main.Tabs[i]; + if (opened.Repo.Path == repo.Path) { + main.openedTabs.SelectedItem = opened; + return true; + } + } + + return false; + } + } } diff --git a/SourceGit/UI/Manager.xaml.cs b/SourceGit/UI/Manager.xaml.cs index b4f55d7c..78ee129d 100644 --- a/SourceGit/UI/Manager.xaml.cs +++ b/SourceGit/UI/Manager.xaml.cs @@ -61,7 +61,7 @@ namespace SourceGit.UI { /// /// private void CloneRepo(object sender, RoutedEventArgs e) { - if (MakeSureReady()) Clone.Show(); + if (MakeSureReady()) popupManager.Show(new Clone(popupManager)); } #endregion @@ -350,7 +350,7 @@ namespace SourceGit.UI { private void ShowBrief(Git.Repository repo) { if (repo == null || !Git.Repository.IsValid(repo.Path)) { if (Directory.Exists(repo.Path)) { - Init.Show(repo.Path); + popupManager.Show(new Init(popupManager, repo.Path)); } else { App.RaiseError("Path is NOT valid git repository or has been removed."); App.Preference.RemoveRepository(repo.Path); @@ -426,7 +426,7 @@ namespace SourceGit.UI { if (!Git.Repository.IsValid(path)) { if (Directory.Exists(path)) { - Init.Show(path); + popupManager.Show(new Init(popupManager, path)); return; } @@ -435,7 +435,7 @@ namespace SourceGit.UI { } var repo = App.Preference.AddRepository(path, ""); - if (!(App.Current.MainWindow as Launcher).GotoRepo(repo)) repo.Open(); + if (!repo.BringUpTab()) repo.Open(); } /// diff --git a/SourceGit/UI/Merge.xaml.cs b/SourceGit/UI/Merge.xaml.cs index 0d9fbeef..ea9fbc02 100644 --- a/SourceGit/UI/Merge.xaml.cs +++ b/SourceGit/UI/Merge.xaml.cs @@ -53,8 +53,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository opened, string source, string dest) { - var popup = App.GetPopupManager(opened); - popup?.Show(new Merge(opened, source, dest)); + opened.GetPopupManager()?.Show(new Merge(opened, source, dest)); } /// @@ -65,7 +64,7 @@ namespace SourceGit.UI { /// public static void StartDirectly(Git.Repository opened, string source, string dest) { var merge = new Merge(opened, source, dest); - var popup = App.GetPopupManager(opened); + var popup = opened.GetPopupManager(); popup?.Show(merge); popup?.Lock(); @@ -83,7 +82,7 @@ namespace SourceGit.UI { /// /// private async void Start(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); var branch = sourceBranch.Content as string; @@ -99,7 +98,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/PopupManager.xaml.cs b/SourceGit/UI/PopupManager.xaml.cs index 948bc8bf..5e7adb9e 100644 --- a/SourceGit/UI/PopupManager.xaml.cs +++ b/SourceGit/UI/PopupManager.xaml.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -111,4 +112,42 @@ namespace SourceGit.UI { Close(); } } + + /// + /// Extension methods for Git.Repository + /// + public static class RepositoryPopupBindings { + private static Dictionary popups = new Dictionary(); + + /// + /// Set popup manager for given repo. + /// + /// + /// + public static void SetPopupManager(this Git.Repository repo, PopupManager popup) { + if (popups.ContainsKey(repo.Path)) popups[repo.Path] = popup; + else popups.Add(repo.Path, popup); + } + + /// + /// Remove popup manager by given repo. + /// + /// + public static void RemovePopup(this Git.Repository repo) { + if (popups.ContainsKey(repo.Path)) { + popups[repo.Path].Unlock(); + popups.Remove(repo.Path); + } + } + + /// + /// + /// + /// + /// + public static PopupManager GetPopupManager(this Git.Repository repo) { + if (popups.ContainsKey(repo.Path)) return popups[repo.Path]; + return null; + } + } } diff --git a/SourceGit/UI/Pull.xaml.cs b/SourceGit/UI/Pull.xaml.cs index fd97ed9f..3ab19379 100644 --- a/SourceGit/UI/Pull.xaml.cs +++ b/SourceGit/UI/Pull.xaml.cs @@ -30,8 +30,7 @@ namespace SourceGit.UI { /// Opened repository /// Prefered remote branch public static void Show(Git.Repository opened, string preferRemoteBranch = null) { - var popup = App.GetPopupManager(opened); - popup?.Show(new Pull(opened, preferRemoteBranch)); + opened.GetPopupManager()?.Show(new Pull(opened, preferRemoteBranch)); } /// @@ -77,7 +76,7 @@ namespace SourceGit.UI { if (remote == null || branch == null) return; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => repo.Pull(remote, branch.Substring(branch.IndexOf('/')+1), msg => popup?.UpdateStatus(msg), rebase, autoStash)); popup?.Close(true); @@ -89,7 +88,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } /// diff --git a/SourceGit/UI/Push.xaml.cs b/SourceGit/UI/Push.xaml.cs index 3587e070..a7eec549 100644 --- a/SourceGit/UI/Push.xaml.cs +++ b/SourceGit/UI/Push.xaml.cs @@ -29,8 +29,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo, Git.Branch prefer = null) { - var popup = App.GetPopupManager(repo); - popup?.Show(new Push(repo, prefer)); + repo.GetPopupManager()?.Show(new Push(repo, prefer)); } /// @@ -45,7 +44,7 @@ namespace SourceGit.UI { } var push = new Push(repo, current); - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Show(push); popup?.Lock(); @@ -98,7 +97,7 @@ namespace SourceGit.UI { remoteBranch = remoteBranch.Substring(0, remoteBranch.Length - 6); } - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => repo.Push(remote, localBranch.Name, remoteBranch, msg => popup?.UpdateStatus(msg), tags, track, force)); popup?.Close(true); @@ -110,7 +109,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } /// diff --git a/SourceGit/UI/PushTag.xaml.cs b/SourceGit/UI/PushTag.xaml.cs index b2fc8fc0..ca8a06d6 100644 --- a/SourceGit/UI/PushTag.xaml.cs +++ b/SourceGit/UI/PushTag.xaml.cs @@ -32,8 +32,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo, Git.Tag tag) { - var popup = App.GetPopupManager(repo); - popup?.Show(new PushTag(repo, tag)); + repo.GetPopupManager()?.Show(new PushTag(repo, tag)); } /// @@ -45,7 +44,7 @@ namespace SourceGit.UI { var remote = combRemotes.SelectedItem as Git.Remote; if (remote == null) return; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => Git.Tag.Push(repo, tag.Name, remote.Name)); popup?.Close(true); @@ -57,8 +56,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); - popup?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Rebase.xaml.cs b/SourceGit/UI/Rebase.xaml.cs index 1c583882..cba8ac3a 100644 --- a/SourceGit/UI/Rebase.xaml.cs +++ b/SourceGit/UI/Rebase.xaml.cs @@ -38,8 +38,7 @@ namespace SourceGit.UI { dialog.type.Data = dialog.FindResource("Icon.Branch") as Geometry; dialog.desc.Content = branch.Name; - var popup = App.GetPopupManager(opened); - popup?.Show(dialog); + opened.GetPopupManager()?.Show(dialog); } /// @@ -57,8 +56,7 @@ namespace SourceGit.UI { dialog.type.Data = dialog.FindResource("Icon.Commit") as Geometry; dialog.desc.Content = $"{commit.ShortSHA} {commit.Subject}"; - var popup = App.GetPopupManager(opened); - popup?.Show(dialog); + opened.GetPopupManager()?.Show(dialog); } /// @@ -67,7 +65,7 @@ namespace SourceGit.UI { /// /// private async void Start(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); var autoStash = chkAutoStash.IsChecked == true; @@ -77,7 +75,7 @@ namespace SourceGit.UI { } private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Remote.xaml.cs b/SourceGit/UI/Remote.xaml.cs index 3c6ecee9..c612eab5 100644 --- a/SourceGit/UI/Remote.xaml.cs +++ b/SourceGit/UI/Remote.xaml.cs @@ -44,7 +44,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository opened, Git.Remote editing = null) { - App.GetPopupManager(opened)?.Show(new Remote(opened, editing)); + opened.GetPopupManager()?.Show(new Remote(opened, editing)); } /// @@ -59,7 +59,7 @@ namespace SourceGit.UI { txtUrl.GetBindingExpression(TextBox.TextProperty).UpdateSource(); if (Validation.GetHasError(txtUrl)) return; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => { @@ -79,7 +79,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/RenameBranch.xaml.cs b/SourceGit/UI/RenameBranch.xaml.cs index a89c30a7..76e1d2bf 100644 --- a/SourceGit/UI/RenameBranch.xaml.cs +++ b/SourceGit/UI/RenameBranch.xaml.cs @@ -38,7 +38,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository opened, Git.Branch branch) { - App.GetPopupManager(opened)?.Show(new RenameBranch(opened, branch)); + opened.GetPopupManager()?.Show(new RenameBranch(opened, branch)); } /// @@ -50,7 +50,7 @@ namespace SourceGit.UI { txtNewName.GetBindingExpression(TextBox.TextProperty).UpdateSource(); if (Validation.GetHasError(txtNewName)) return; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => branch.Rename(repo, NewName)); popup?.Close(true); @@ -62,7 +62,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Reset.xaml.cs b/SourceGit/UI/Reset.xaml.cs index 60cebfd7..0de69642 100644 --- a/SourceGit/UI/Reset.xaml.cs +++ b/SourceGit/UI/Reset.xaml.cs @@ -59,7 +59,7 @@ namespace SourceGit.UI { var current = repo.CurrentBranch(); if (current == null) return; - App.GetPopupManager(repo)?.Show(new Reset(repo, current, commit)); + repo.GetPopupManager()?.Show(new Reset(repo, current, commit)); } /// @@ -71,7 +71,7 @@ namespace SourceGit.UI { var mode = combMode.SelectedItem as Mode; if (mode == null) return; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => repo.Reset(revision, mode.Arg)); popup?.Close(true); @@ -83,7 +83,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Revert.xaml.cs b/SourceGit/UI/Revert.xaml.cs index 7bee143e..2012104b 100644 --- a/SourceGit/UI/Revert.xaml.cs +++ b/SourceGit/UI/Revert.xaml.cs @@ -30,8 +30,7 @@ namespace SourceGit.UI { /// /// public static void Show(Git.Repository repo, Git.Commit commit) { - var popup = App.GetPopupManager(repo); - popup?.Show(new Revert(repo, commit)); + repo.GetPopupManager()?.Show(new Revert(repo, commit)); } /// @@ -41,7 +40,7 @@ namespace SourceGit.UI { /// private async void Sure(object sender, RoutedEventArgs e) { bool autoCommit = chkCommit.IsChecked == true; - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Lock(); await Task.Run(() => repo.Revert(sha, autoCommit)); popup?.Close(true); @@ -53,8 +52,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - var popup = App.GetPopupManager(repo); - popup?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Stash.xaml.cs b/SourceGit/UI/Stash.xaml.cs index 1d3ba0e9..7fa8d279 100644 --- a/SourceGit/UI/Stash.xaml.cs +++ b/SourceGit/UI/Stash.xaml.cs @@ -28,8 +28,7 @@ namespace SourceGit.UI { /// Opened repository /// Special files to stash public static void Show(Git.Repository repo, List files) { - var popup = App.GetPopupManager(repo); - popup?.Show(new Stash(repo, files)); + repo.GetPopupManager()?.Show(new Stash(repo, files)); } /// @@ -42,7 +41,7 @@ namespace SourceGit.UI { string message = txtName.Text; Git.Stash.Push(repo, includeUntracked, message, files); - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } /// @@ -51,7 +50,7 @@ namespace SourceGit.UI { /// /// private void Cancel(object sender, RoutedEventArgs e) { - App.GetPopupManager(repo)?.Close(); + repo.GetPopupManager()?.Close(); } } } diff --git a/SourceGit/UI/Waiting.xaml.cs b/SourceGit/UI/Waiting.xaml.cs index 27e8836d..c606719e 100644 --- a/SourceGit/UI/Waiting.xaml.cs +++ b/SourceGit/UI/Waiting.xaml.cs @@ -23,7 +23,7 @@ namespace SourceGit.UI { /// public static void Show(Git.Repository repo, Action job) { var dialog = new Waiting(); - var popup = App.GetPopupManager(repo); + var popup = repo.GetPopupManager(); popup?.Show(dialog); popup?.Lock();