using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace SourceGit.UI { /// /// Main window for this app. /// public partial class Launcher : Window { /// /// Tab data. /// public class Tab { public string Title { get; set; } public bool IsActive { get; set; } public Git.Repository Repo { get; set; } public object Page { get; set; } } /// /// Alert data. /// public class Alert { public string Title { get; set; } public string Message { get; set; } } /// /// Alerts. /// public ObservableCollection Alerts { get; set; } = new ObservableCollection(); /// /// Opened tabs. /// public ObservableCollection Tabs { get; set; } = new ObservableCollection(); /// /// Constructor /// public Launcher() { App.OnError = msg => { ShowAlert(new Alert() { Title = "ERROR", Message = msg }); }; Git.Repository.OnOpen = repo => { Dispatcher.Invoke(() => { foreach (var item in openedTabs.Items) { var opened = item as Tab; if (opened != null && opened.Repo != null && repo.Path == opened.Repo.Path) { openedTabs.SelectedItem = opened; return; } } var tab = new Tab() { Title = repo.Parent == null ? repo.Name : $"{repo.Parent.Name} : {repo.Name}", Repo = repo, Page = new Dashboard(repo), }; Tabs.Add(tab); openedTabs.SelectedItem = tab; }); }; Tabs.Add(new Tab() { Title = "SOURCE GIT", Page = new Manager(), }); InitializeComponent(); openedTabs.SelectedItem = Tabs[0]; } /// /// Get popup manager from given active page. /// /// /// public PopupManager GetPopupManager(Git.Repository repo) { if (repo == null) return (Tabs[0].Page as Manager).popupManager; foreach (var tab in Tabs) { if (tab.Repo != null && tab.Repo.Path == repo.Path) { return (tab.Page as Dashboard).popupManager; } } return null; } #region LAYOUT_CONTENT /// /// Close repository tab. /// /// /// private void CloseRepo(object sender, RoutedEventArgs e) { 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.Close(); } /// /// Open preference dialog. /// /// /// private void ShowPreference(object sender, RoutedEventArgs e) { var dialog = new Preference(); dialog.Owner = this; dialog.ShowDialog(); } /// /// Open about dialog. /// /// /// private void ShowAbout(object sender, RoutedEventArgs e) { var about = new About(); about.Owner = this; 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); } #endregion #region WINDOW_COMMANDS /// /// Minimize /// private void Minimize(object sender, RoutedEventArgs e) { SystemCommands.MinimizeWindow(this); } /// /// Maximize/Restore /// private void MaximizeOrRestore(object sender, RoutedEventArgs e) { if (WindowState == WindowState.Normal) { SystemCommands.MaximizeWindow(this); } else { SystemCommands.RestoreWindow(this); } } /// /// Quit /// private void Quit(object sender, RoutedEventArgs e) { App.Current.Shutdown(); } #endregion #region DRAG_DROP private void TabsPreviewMouseMove(object sender, MouseEventArgs e) { var tab = e.Source as TabItem; if (tab == null || (tab.DataContext as Tab).Repo == null) return; if (Mouse.LeftButton == MouseButtonState.Pressed) { DragDrop.DoDragDrop(tab, tab, DragDropEffects.All); } } private void TabsDrop(object sender, DragEventArgs e) { var tabItemSrc = e.Data.GetData(typeof(TabItem)) as TabItem; var tabItemDst = e.Source as TabItem; if (tabItemSrc.Equals(tabItemDst)) return; var tabSrc = tabItemSrc.DataContext as Tab; var tabDst = tabItemDst.DataContext as Tab; if (tabDst.Repo == null) { Tabs.Remove(tabSrc); Tabs.Insert(1, tabSrc); } else { int dstIdx = Tabs.IndexOf(tabDst); Tabs.Remove(tabSrc); Tabs.Insert(dstIdx, tabSrc); } } #endregion } }