From 037732636339e31a23818656535cd704aad5be37 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 15 Jun 2021 17:35:37 +0800 Subject: [PATCH] optimize<*>: remove PageTabItem --- src/Models/Watcher.cs | 11 ---- src/Resources/Locales/en_US.xaml | 2 + src/Resources/Locales/zh_CN.xaml | 2 + src/Views/Launcher.xaml.cs | 22 ++++---- src/Views/Widgets/Dashboard.xaml | 9 +++- src/Views/Widgets/Dashboard.xaml.cs | 9 ++++ src/Views/Widgets/PageTabBar.xaml | 15 +++++- src/Views/Widgets/PageTabBar.xaml.cs | 51 ++++++++++++++---- src/Views/Widgets/PageTabItem.xaml | 31 ----------- src/Views/Widgets/PageTabItem.xaml.cs | 78 --------------------------- 10 files changed, 83 insertions(+), 147 deletions(-) delete mode 100644 src/Views/Widgets/PageTabItem.xaml delete mode 100644 src/Views/Widgets/PageTabItem.xaml.cs diff --git a/src/Models/Watcher.cs b/src/Models/Watcher.cs index 784faa6a..2c668416 100644 --- a/src/Models/Watcher.cs +++ b/src/Models/Watcher.cs @@ -104,17 +104,6 @@ namespace SourceGit.Models { Navigate?.Invoke(commit); } - /// - /// 强制刷新 - /// - public void Refresh() { - updateWC = 1; - updateBranch = 1; - updateSubmodules = 1; - updateStashes = 1; - updateTags = 1; - } - /// /// 仅强制更新本地变化 /// diff --git a/src/Resources/Locales/en_US.xaml b/src/Resources/Locales/en_US.xaml index 977b632f..8a0da629 100644 --- a/src/Resources/Locales/en_US.xaml +++ b/src/Resources/Locales/en_US.xaml @@ -115,6 +115,8 @@ Open In File Browser Terminal Open Git Bash + Refresh + Refresh Repositoy Search Search Commit Configure this repository diff --git a/src/Resources/Locales/zh_CN.xaml b/src/Resources/Locales/zh_CN.xaml index 7218c9e4..416d6d1a 100644 --- a/src/Resources/Locales/zh_CN.xaml +++ b/src/Resources/Locales/zh_CN.xaml @@ -115,6 +115,8 @@ 在文件浏览器中打开 终端 打开GIT终端 + 刷新 + 刷新仓库数据 查找 查找提交 配置本仓库 diff --git a/src/Views/Launcher.xaml.cs b/src/Views/Launcher.xaml.cs index 0539f2f5..8a960c5f 100644 --- a/src/Views/Launcher.xaml.cs +++ b/src/Views/Launcher.xaml.cs @@ -13,7 +13,7 @@ namespace SourceGit.Views { public Launcher() { Models.Watcher.Opened += OpenRepository; InitializeComponent(); - OnTabAdding(null, null); + tabs.Add(); } #region OPEN_REPO @@ -30,15 +30,14 @@ namespace SourceGit.Views { Commands.AutoFetch.Start(repo.Path); var page = new Widgets.Dashboard(repo); - var tab = new Widgets.PageTabItem(repo.Name, false, repo.Bookmark, repo.Path); container.Add(repo.Path, page); Controls.PopupWidget.RegisterContainer(repo.Path, page); var front = container.Get(tabs.Current); if (front == null || front is Widgets.Dashboard) { - tabs.Add(repo.Path, tab); + tabs.Add(repo.Name, repo.Path, repo.Bookmark); } else { - tabs.Replace(tabs.Current, repo.Path, tab); + tabs.Replace(tabs.Current, repo.Name, repo.Path, repo.Bookmark); } } #endregion @@ -72,14 +71,10 @@ namespace SourceGit.Views { #endregion #region TAB_OPERATION - private void OnTabAdding(object sender, RoutedEventArgs e) { - var id = Guid.NewGuid().ToString(); - var tab = new Widgets.PageTabItem(App.Text("PageSwitcher.Welcome.Title"), true, 0, App.Text("PageSwitcher.Welcome.Tip")); + private void OnTabAdding(object sender, Widgets.PageTabBar.TabEventArgs e) { var page = new Widgets.Welcome(); - - container.Add(id, page); - tabs.Add(id, tab); - Controls.PopupWidget.RegisterContainer(id, page); + container.Add(e.TabId, page); + Controls.PopupWidget.RegisterContainer(e.TabId, page); } private void OnTabSelected(object sender, Widgets.PageTabBar.TabEventArgs e) { @@ -112,7 +107,7 @@ namespace SourceGit.Views { } if (Keyboard.IsKeyDown(Key.T)) { - OnTabAdding(null, null); + tabs.Add(); e.Handled = true; return; } @@ -138,7 +133,8 @@ namespace SourceGit.Views { } if (Keyboard.IsKeyDown(Key.F5)) { - Models.Watcher.Get(tabs.Current)?.Refresh(); + var dashboard = container.Get(tabs.Current) as Widgets.Dashboard; + if (dashboard != null) dashboard.Refresh(null, null); e.Handled = true; return; } diff --git a/src/Views/Widgets/Dashboard.xaml b/src/Views/Widgets/Dashboard.xaml index 72d900ac..63884874 100644 --- a/src/Views/Widgets/Dashboard.xaml +++ b/src/Views/Widgets/Dashboard.xaml @@ -30,7 +30,6 @@ - + diff --git a/src/Views/Widgets/Dashboard.xaml.cs b/src/Views/Widgets/Dashboard.xaml.cs index e2e20020..e9f0bf30 100644 --- a/src/Views/Widgets/Dashboard.xaml.cs +++ b/src/Views/Widgets/Dashboard.xaml.cs @@ -289,6 +289,15 @@ namespace SourceGit.Views.Widgets { e.Handled = true; } + public void Refresh(object sender, RoutedEventArgs e) { + UpdateBraches(); + UpdateWorkingCopy(); + UpdateStashes(); + UpdateTags(); + UpdateSubmodules(); + UpdateSubTrees(); + } + private void OpenFetch(object sender, RoutedEventArgs e) { if (repo.Remotes.Count == 0) { Models.Exception.Raise("No remotes added to this repository!!!"); diff --git a/src/Views/Widgets/PageTabBar.xaml b/src/Views/Widgets/PageTabBar.xaml index 55773d47..bdb24ba2 100644 --- a/src/Views/Widgets/PageTabBar.xaml +++ b/src/Views/Widgets/PageTabBar.xaml @@ -54,8 +54,19 @@ - - + + + + public class Tab { public string Id { get; set; } - public UserControl Control { get; set; } - public Tab(string id, UserControl ctrl) { Id = id; Control = ctrl; } + public bool IsWelcomePage { get; set; } + public string Title { get; set; } + public string Tooltip { get; set; } + public int Bookmark { get; set; } } /// @@ -31,7 +33,7 @@ namespace SourceGit.Views.Widgets { public static readonly RoutedEvent TabAddEvent = EventManager.RegisterRoutedEvent( "TabAdd", RoutingStrategy.Bubble, - typeof(RoutedEventHandler), + typeof(EventHandler), typeof(PageTabBar)); public event RoutedEventHandler TabAdd { @@ -75,18 +77,29 @@ namespace SourceGit.Views.Widgets { InitializeComponent(); } - public void Add(string id, UserControl element) { - var tab = new Tab(id, element); + public void Add() { + NewTab(null, null); + } + + public void Add(string title, string repo, int bookmark) { + var tab = new Tab() { + Id = repo, + IsWelcomePage = false, + Title = title, + Tooltip = repo, + Bookmark = bookmark, + }; + Tabs.Add(tab); container.SelectedItem = tab; } - public void Replace(string oldId, string newId, UserControl element) { + public void Replace(string id, string title, string repo, int bookmark) { var tab = null as Tab; var curTab = container.SelectedItem as Tab; foreach (var one in Tabs) { - if (one.Id == oldId) { + if (one.Id == id) { tab = one; break; } @@ -98,9 +111,16 @@ namespace SourceGit.Views.Widgets { Tabs.RemoveAt(idx); RaiseEvent(new TabEventArgs(TabClosedEvent, this, tab.Id)); - var replaced = new Tab(newId, element); + var replaced = new Tab() { + Id = repo, + IsWelcomePage = false, + Title = title, + Tooltip = repo, + Bookmark = bookmark, + }; + Tabs.Insert(idx, replaced); - if (curTab.Id == oldId) container.SelectedItem = replaced; + if (curTab.Id == id) container.SelectedItem = replaced; } public bool Goto(string id) { @@ -145,7 +165,18 @@ namespace SourceGit.Views.Widgets { } private void NewTab(object sender, RoutedEventArgs e) { - RaiseEvent(new RoutedEventArgs(TabAddEvent)); + var id = Guid.NewGuid().ToString(); + var tab = new Tab() { + Id = id, + IsWelcomePage = true, + Title = App.Text("PageSwitcher.Welcome.Title"), + Tooltip = App.Text("PageSwitcher.Welcome.Tip"), + Bookmark = 0, + }; + + Tabs.Add(tab); + RaiseEvent(new TabEventArgs(TabAddEvent, this, id)); + container.SelectedItem = tab; } private void ScrollLeft(object sender, RoutedEventArgs e) { diff --git a/src/Views/Widgets/PageTabItem.xaml b/src/Views/Widgets/PageTabItem.xaml deleted file mode 100644 index 7e3114b4..00000000 --- a/src/Views/Widgets/PageTabItem.xaml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - diff --git a/src/Views/Widgets/PageTabItem.xaml.cs b/src/Views/Widgets/PageTabItem.xaml.cs deleted file mode 100644 index 0bd76263..00000000 --- a/src/Views/Widgets/PageTabItem.xaml.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System.Windows; -using System.Windows.Controls; -using System.Windows.Media; - -namespace SourceGit.Views.Widgets { - - /// - /// 主界面标题栏中的页面标签 - /// - public partial class PageTabItem : UserControl { - public string Title { get; private set; } - public bool IsWelcomePage { get; private set; } - public int Bookmark { get; private set; } - public string Tip { get; private set; } - - public PageTabItem(string title, bool isWelcomePage, int bookmark, string tip) { - Title = title; - IsWelcomePage = isWelcomePage; - Bookmark = bookmark; - Tip = tip; - - InitializeComponent(); - } - - private void OnContextMenuOpening(object sender, ContextMenuEventArgs ev) { - if (IsWelcomePage) return; - - var refresh = new MenuItem(); - refresh.Header = App.Text("RepoCM.Refresh"); - refresh.Click += (o, e) => { - Models.Watcher.Get(Tip)?.Refresh(); - e.Handled = true; - }; - - var iconBookmark = FindResource("Icon.Bookmark") as Geometry; - var bookmark = new MenuItem(); - bookmark.Header = App.Text("RepoCM.Bookmark"); - for (int i = 0; i < Controls.Bookmark.COLORS.Length; i++) { - var icon = new System.Windows.Shapes.Path(); - icon.Data = iconBookmark; - icon.Fill = Controls.Bookmark.COLORS[i]; - icon.Width = 8; - - var mark = new MenuItem(); - mark.Icon = icon; - mark.Header = $"{i}"; - - var refIdx = i; - mark.Click += (o, e) => { - var repo = Models.Preference.Instance.FindRepository(Tip); - if (repo == null) return; - - repo.Bookmark = refIdx; - Bookmark = refIdx; - ctrlBookmark.GetBindingExpression(Controls.Bookmark.ColorProperty).UpdateTarget(); - e.Handled = true; - }; - - bookmark.Items.Add(mark); - } - - var copyPath = new MenuItem(); - copyPath.Header = App.Text("RepoCM.CopyPath"); - copyPath.Click += (o, e) => { - Clipboard.SetText(Tip); - e.Handled = true; - }; - - var menu = new ContextMenu(); - menu.Items.Add(refresh); - menu.Items.Add(bookmark); - menu.Items.Add(copyPath); - menu.IsOpen = true; - - ev.Handled = true; - } - } -}