refactor(PopupManager): rewrite popup

This commit is contained in:
leo 2020-08-06 15:41:25 +08:00
parent 28e3a1bb27
commit 7558bbd4f2
34 changed files with 183 additions and 221 deletions

View file

@ -21,18 +21,10 @@ namespace SourceGit.UI {
public object Page { get; set; }
}
/// <summary>
/// Alert data.
/// </summary>
public class Alert {
public string Title { get; set; }
public string Message { get; set; }
}
/// <summary>
/// Alerts.
/// </summary>
public ObservableCollection<Alert> Alerts { get; set; } = new ObservableCollection<Alert>();
public ObservableCollection<string> Errors { get; set; } = new ObservableCollection<string>();
/// <summary>
/// Opened tabs.
@ -43,19 +35,17 @@ namespace SourceGit.UI {
/// Constructor
/// </summary>
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];
}
/// <summary>
/// Goto opened repository.
/// </summary>
/// <param name="repo"></param>
/// <returns></returns>
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
/// <summary>
/// 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;
}
/// <summary>
@ -128,22 +100,14 @@ namespace SourceGit.UI {
about.ShowDialog();
}
/// <summary>
/// Show alert.
/// </summary>
/// <param name="alert"></param>
private void ShowAlert(Alert alert) {
Dispatcher.Invoke(() => Alerts.Add(alert));
}
/// <summary>
/// Remove an alert.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
}
/// <summary>
/// Extension methods for repository.
/// </summary>
public static class RepositoryTabBindings {
/// <summary>
/// Bring up tab of repository if it was opened before.
/// </summary>
/// <param name="repo"></param>
/// <returns></returns>
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;
}
}
}