feature<PageTabBar>: add context menu to close tabs and modify bookmarks

This commit is contained in:
Jai 2021-08-19 08:58:41 +08:00
parent 232c209079
commit 602f934fae
6 changed files with 107 additions and 1 deletions

View file

@ -284,6 +284,10 @@
<sys:String x:Key="Text.PageTabBar.New">NEW PAGE</sys:String>
<sys:String x:Key="Text.PageTabBar.Welcome.Title">*Repositories</sys:String>
<sys:String x:Key="Text.PageTabBar.Welcome.Tip">WELCOME PAGE</sys:String>
<sys:String x:Key="Text.PageTabBar.Tab.Close">Close Tab</sys:String>
<sys:String x:Key="Text.PageTabBar.Tab.CloseOther">Close Other Tabs</sys:String>
<sys:String x:Key="Text.PageTabBar.Tab.CloseRight">Close Tabs to the Right</sys:String>
<sys:String x:Key="Text.PageTabBar.Tab.Bookmark">Bookmark</sys:String>
<sys:String x:Key="Text.Merge">Merge Branch</sys:String>
<sys:String x:Key="Text.Merge.Source">Source Branch :</sys:String>

View file

@ -283,6 +283,10 @@
<sys:String x:Key="Text.PageTabBar.New">新建空白页</sys:String>
<sys:String x:Key="Text.PageTabBar.Welcome.Title">新标签页</sys:String>
<sys:String x:Key="Text.PageTabBar.Welcome.Tip">起始页</sys:String>
<sys:String x:Key="Text.PageTabBar.Tab.Close">关闭标签页</sys:String>
<sys:String x:Key="Text.PageTabBar.Tab.CloseOther">关闭其他标签页</sys:String>
<sys:String x:Key="Text.PageTabBar.Tab.CloseRight">关闭右侧标签页</sys:String>
<sys:String x:Key="Text.PageTabBar.Tab.Bookmark">设置书签</sys:String>
<sys:String x:Key="Text.Merge">合并分支</sys:String>
<sys:String x:Key="Text.Merge.Source">合并分支 </sys:String>

View file

@ -15,6 +15,14 @@ namespace SourceGit.Views {
Models.Watcher.Opened += OpenRepository;
InitializeComponent();
tabs.Add();
tabs.OnTabEdited += (t) => {
foreach (var tab in tabs.Tabs) {
if (!tab.IsWelcomePage) continue;
var page = container.Get(tab.Id) as Widgets.Welcome;
if (page != null) page.UpdateNodes(t.Id, t.Bookmark);
}
};
}
private void OnClosing(object sender, CancelEventArgs e) {

View file

@ -48,6 +48,7 @@
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="ContextMenuOpening" Handler="OnTabContextMenuOpening" />
<Setter Property="AllowDrop" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">

View file

@ -1,8 +1,10 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace SourceGit.Views.Widgets {
@ -33,6 +35,11 @@ namespace SourceGit.Views.Widgets {
}
}
/// <summary>
/// 仓库标签页编辑事件参数
/// </summary>
public event Action<Tab> OnTabEdited;
/// <summary>
/// 标签相关事件参数
/// </summary>
@ -216,7 +223,10 @@ namespace SourceGit.Views.Widgets {
private void CloseTab(object sender, RoutedEventArgs e) {
var tab = (sender as Button).DataContext as Tab;
if (tab == null) return;
CloseTab(tab);
}
private void CloseTab(Tab tab) {
var curTab = container.SelectedItem as Tab;
if (curTab != null && tab.Id == curTab.Id) {
var idx = Tabs.IndexOf(tab);
@ -234,7 +244,6 @@ namespace SourceGit.Views.Widgets {
} else {
Tabs.Remove(tab);
}
RaiseEvent(new TabEventArgs(TabClosedEvent, this, tab.Id));
}
@ -265,5 +274,69 @@ namespace SourceGit.Views.Widgets {
container.SelectedItem = tabSrc;
e.Handled = true;
}
private void OnTabContextMenuOpening(object sender, ContextMenuEventArgs e) {
var tab = (sender as ListBoxItem).DataContext as Tab;
if (tab == null) return;
var menu = new ContextMenu();
var close = new MenuItem();
close.Header = App.Text("PageTabBar.Tab.Close");
close.Click += (_, __) => {
CloseTab(tab);
};
var closeOther = new MenuItem();
closeOther.Header = App.Text("PageTabBar.Tab.CloseOther");
closeOther.Click += (_, __) => {
Tabs.ToList().ForEach(t => { if (tab != t) CloseTab(t); });
};
var closeRight = new MenuItem();
closeRight.Header = App.Text("PageTabBar.Tab.CloseRight");
closeRight.Click += (_, __) => {
var tabs = Tabs.ToList();
tabs.RemoveRange(0, tabs.IndexOf(tab) + 1);
tabs.ForEach(t => CloseTab(t));
};
menu.Items.Add(close);
menu.Items.Add(closeOther);
menu.Items.Add(closeRight);
if (!tab.IsWelcomePage) {
var iconBookmark = FindResource("Icon.Bookmark") as Geometry;
var bookmark = new MenuItem();
bookmark.Header = App.Text("PageTabBar.Tab.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, ev) => {
var repo = Models.Preference.Instance.FindRepository(tab.Id);
if (repo != null) {
repo.Bookmark = refIdx;
tab.Bookmark = refIdx;
OnTabEdited?.Invoke(tab);
}
ev.Handled = true;
};
bookmark.Items.Add(mark);
}
menu.Items.Add(new Separator());
menu.Items.Add(bookmark);
}
menu.IsOpen = true;
e.Handled = true;
}
}
}

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
@ -368,6 +369,21 @@ namespace SourceGit.Views.Widgets {
var repo = Models.Preference.Instance.AddRepository(root, gitDir, "");
Models.Watcher.Open(repo);
}
public void UpdateNodes(string id, int bookmark, IEnumerable<Node> nodes = null) {
if (nodes == null) nodes = tree.ItemsSource.OfType<Node>();
foreach (var node in nodes) {
if (!node.IsGroup) {
if (node.Id == id) {
node.Bookmark = bookmark;
break;
}
} else if (node.Children.Count > 0) {
UpdateNodes(id, bookmark, node.Children);
}
}
}
#endregion
#region RENAME_NODES