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(() => { 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]; } /// /// 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. /// /// /// 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 TabsMouseMove(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); e.Handled = true; } } 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 #region TAB_SCROLL private void OpenedTabsSizeChanged(object sender, SizeChangedEventArgs e) { if (openedTabs.ActualWidth > openedTabsColumn.ActualWidth) { openedTabsOpts.Visibility = Visibility.Visible; } else { openedTabsOpts.Visibility = Visibility.Collapsed; } } private void ScrollToLeft(object sender, RoutedEventArgs e) { openedTabsScroller.LineLeft(); } private void ScrollToRight(object sender, RoutedEventArgs e) { openedTabsScroller.LineRight(); } #endregion } }