2021-04-29 05:05:55 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.ObjectModel;
|
2021-08-18 17:58:41 -07:00
|
|
|
using System.Linq;
|
2021-04-29 05:05:55 -07:00
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Controls;
|
|
|
|
using System.Windows.Input;
|
2021-08-18 17:58:41 -07:00
|
|
|
using System.Windows.Media;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
namespace SourceGit.Views.Widgets {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 主窗体标题栏的标签页容器控件
|
|
|
|
/// </summary>
|
|
|
|
public partial class PageTabBar : UserControl {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 标签数据
|
|
|
|
/// </summary>
|
2021-08-13 03:28:07 -07:00
|
|
|
public class Tab : Controls.BindableBase {
|
2021-04-29 05:05:55 -07:00
|
|
|
public string Id { get; set; }
|
2021-06-15 02:35:37 -07:00
|
|
|
public bool IsWelcomePage { get; set; }
|
2021-08-13 03:28:07 -07:00
|
|
|
|
|
|
|
private string title;
|
|
|
|
public string Title {
|
|
|
|
get => title;
|
|
|
|
set => SetProperty(ref title, value);
|
|
|
|
}
|
|
|
|
|
2021-06-15 02:35:37 -07:00
|
|
|
public string Tooltip { get; set; }
|
2021-08-13 03:28:07 -07:00
|
|
|
|
|
|
|
private int bookmark = 0;
|
|
|
|
public int Bookmark {
|
|
|
|
get => bookmark;
|
|
|
|
set => SetProperty(ref bookmark, value);
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
}
|
|
|
|
|
2021-08-18 17:58:41 -07:00
|
|
|
/// <summary>
|
|
|
|
/// 仓库标签页编辑事件参数
|
|
|
|
/// </summary>
|
|
|
|
public event Action<Tab> OnTabEdited;
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
/// <summary>
|
|
|
|
/// 标签相关事件参数
|
|
|
|
/// </summary>
|
|
|
|
public class TabEventArgs : RoutedEventArgs {
|
|
|
|
public string TabId { get; set; }
|
|
|
|
public TabEventArgs(RoutedEvent e, object o, string id) : base(e, o) { TabId = id; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly RoutedEvent TabAddEvent = EventManager.RegisterRoutedEvent(
|
|
|
|
"TabAdd",
|
|
|
|
RoutingStrategy.Bubble,
|
2021-06-15 02:35:37 -07:00
|
|
|
typeof(EventHandler<TabEventArgs>),
|
2021-04-29 05:05:55 -07:00
|
|
|
typeof(PageTabBar));
|
|
|
|
|
|
|
|
public event RoutedEventHandler TabAdd {
|
|
|
|
add { AddHandler(TabAddEvent, value); }
|
|
|
|
remove { RemoveHandler(TabAddEvent, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly RoutedEvent TabSelectedEvent = EventManager.RegisterRoutedEvent(
|
|
|
|
"TabSelected",
|
|
|
|
RoutingStrategy.Bubble,
|
|
|
|
typeof(EventHandler<TabEventArgs>),
|
|
|
|
typeof(PageTabBar));
|
|
|
|
|
|
|
|
public event RoutedEventHandler TabSelected {
|
|
|
|
add { AddHandler(TabSelectedEvent, value); }
|
|
|
|
remove { RemoveHandler(TabSelectedEvent, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public static readonly RoutedEvent TabClosedEvent = EventManager.RegisterRoutedEvent(
|
|
|
|
"TabClosed",
|
|
|
|
RoutingStrategy.Bubble,
|
|
|
|
typeof(EventHandler<TabEventArgs>),
|
|
|
|
typeof(PageTabBar));
|
|
|
|
|
|
|
|
public event RoutedEventHandler TabClosed {
|
|
|
|
add { AddHandler(TabClosedEvent, value); }
|
|
|
|
remove { RemoveHandler(TabClosedEvent, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public ObservableCollection<Tab> Tabs {
|
|
|
|
get;
|
|
|
|
private set;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Current {
|
|
|
|
get { return (container.SelectedItem as Tab).Id; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public PageTabBar() {
|
|
|
|
Tabs = new ObservableCollection<Tab>();
|
|
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
|
2021-06-15 02:35:37 -07:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
Tabs.Add(tab);
|
|
|
|
container.SelectedItem = tab;
|
|
|
|
}
|
|
|
|
|
2021-06-15 02:35:37 -07:00
|
|
|
public void Replace(string id, string title, string repo, int bookmark) {
|
2021-04-29 05:05:55 -07:00
|
|
|
var tab = null as Tab;
|
|
|
|
var curTab = container.SelectedItem as Tab;
|
|
|
|
|
|
|
|
foreach (var one in Tabs) {
|
2021-06-15 02:35:37 -07:00
|
|
|
if (one.Id == id) {
|
2021-04-29 05:05:55 -07:00
|
|
|
tab = one;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tab == null) return;
|
|
|
|
|
|
|
|
var idx = Tabs.IndexOf(tab);
|
|
|
|
Tabs.RemoveAt(idx);
|
|
|
|
RaiseEvent(new TabEventArgs(TabClosedEvent, this, tab.Id));
|
|
|
|
|
2021-06-15 02:35:37 -07:00
|
|
|
var replaced = new Tab() {
|
|
|
|
Id = repo,
|
|
|
|
IsWelcomePage = false,
|
|
|
|
Title = title,
|
|
|
|
Tooltip = repo,
|
|
|
|
Bookmark = bookmark,
|
|
|
|
};
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
Tabs.Insert(idx, replaced);
|
2021-06-15 02:35:37 -07:00
|
|
|
if (curTab.Id == id) container.SelectedItem = replaced;
|
2021-04-29 05:05:55 -07:00
|
|
|
}
|
|
|
|
|
2021-08-13 03:28:07 -07:00
|
|
|
public void Update(string id, int bookmark, string title) {
|
|
|
|
foreach (var one in Tabs) {
|
|
|
|
if (one.Id == id) {
|
|
|
|
one.Bookmark = bookmark;
|
|
|
|
one.Title = title;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
public bool Goto(string id) {
|
|
|
|
foreach (var tab in Tabs) {
|
|
|
|
if (tab.Id == id) {
|
|
|
|
container.SelectedItem = tab;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-02 02:46:19 -07:00
|
|
|
public void Next() {
|
|
|
|
container.SelectedIndex = (container.SelectedIndex + 1) % Tabs.Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CloseCurrent() {
|
|
|
|
var curTab = container.SelectedItem as Tab;
|
2021-08-10 23:16:43 -07:00
|
|
|
var idx = container.SelectedIndex;
|
|
|
|
Tabs.Remove(curTab);
|
|
|
|
if (Tabs.Count == 0) {
|
2021-06-02 02:46:19 -07:00
|
|
|
Application.Current.Shutdown();
|
|
|
|
} else {
|
2021-06-02 21:06:53 -07:00
|
|
|
var last = Tabs.Count - 1;
|
|
|
|
var next = idx > last ? Tabs[last] : Tabs[idx];
|
2021-06-02 02:46:19 -07:00
|
|
|
container.SelectedItem = next;
|
2021-08-10 23:16:43 -07:00
|
|
|
RaiseEvent(new TabEventArgs(TabClosedEvent, this, curTab.Id));
|
2021-06-02 02:46:19 -07:00
|
|
|
RaiseEvent(new TabEventArgs(TabSelectedEvent, this, next.Id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
private void CalcScrollerVisibilty(object sender, SizeChangedEventArgs e) {
|
|
|
|
if ((sender as StackPanel).ActualWidth > scroller.ActualWidth) {
|
|
|
|
leftScroller.Visibility = Visibility.Visible;
|
|
|
|
rightScroller.Visibility = Visibility.Visible;
|
|
|
|
} else {
|
|
|
|
leftScroller.Visibility = Visibility.Collapsed;
|
|
|
|
rightScroller.Visibility = Visibility.Collapsed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void NewTab(object sender, RoutedEventArgs e) {
|
2021-06-15 02:35:37 -07:00
|
|
|
var id = Guid.NewGuid().ToString();
|
|
|
|
var tab = new Tab() {
|
|
|
|
Id = id,
|
|
|
|
IsWelcomePage = true,
|
2021-08-11 05:34:01 -07:00
|
|
|
Title = App.Text("PageTabBar.Welcome.Title"),
|
|
|
|
Tooltip = App.Text("PageTabBar.Welcome.Tip"),
|
2021-06-15 02:35:37 -07:00
|
|
|
Bookmark = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
Tabs.Add(tab);
|
|
|
|
RaiseEvent(new TabEventArgs(TabAddEvent, this, id));
|
|
|
|
container.SelectedItem = tab;
|
2021-04-29 05:05:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void ScrollLeft(object sender, RoutedEventArgs e) {
|
|
|
|
scroller.LineLeft();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ScrollRight(object sender, RoutedEventArgs e) {
|
|
|
|
scroller.LineRight();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SelectionChanged(object sender, SelectionChangedEventArgs e) {
|
|
|
|
var tab = container.SelectedItem as Tab;
|
|
|
|
if (tab == null) return;
|
|
|
|
RaiseEvent(new TabEventArgs(TabSelectedEvent, this, tab.Id));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CloseTab(object sender, RoutedEventArgs e) {
|
2021-06-02 02:46:19 -07:00
|
|
|
var tab = (sender as Button).DataContext as Tab;
|
2021-04-29 05:05:55 -07:00
|
|
|
if (tab == null) return;
|
2021-08-18 17:58:41 -07:00
|
|
|
CloseTab(tab);
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
2021-08-18 17:58:41 -07:00
|
|
|
private void CloseTab(Tab tab) {
|
2021-04-29 05:05:55 -07:00
|
|
|
var curTab = container.SelectedItem as Tab;
|
|
|
|
if (curTab != null && tab.Id == curTab.Id) {
|
2021-08-10 23:16:43 -07:00
|
|
|
var idx = Tabs.IndexOf(tab);
|
|
|
|
Tabs.Remove(tab);
|
|
|
|
|
|
|
|
if (Tabs.Count == 0) {
|
2021-04-29 05:05:55 -07:00
|
|
|
Application.Current.Shutdown();
|
2021-08-10 23:16:43 -07:00
|
|
|
return;
|
2021-04-29 05:05:55 -07:00
|
|
|
}
|
2021-08-10 23:16:43 -07:00
|
|
|
|
|
|
|
var last = Tabs.Count - 1;
|
|
|
|
var next = idx > last ? Tabs[last] : Tabs[idx];
|
|
|
|
container.SelectedItem = next;
|
|
|
|
RaiseEvent(new TabEventArgs(TabSelectedEvent, this, next.Id));
|
2021-04-29 05:05:55 -07:00
|
|
|
} else {
|
|
|
|
Tabs.Remove(tab);
|
|
|
|
}
|
|
|
|
RaiseEvent(new TabEventArgs(TabClosedEvent, this, tab.Id));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnMouseMove(object sender, MouseEventArgs e) {
|
|
|
|
var item = sender as ListBoxItem;
|
|
|
|
if (item == null) return;
|
|
|
|
|
2021-08-18 20:43:14 -07:00
|
|
|
var tab = item.DataContext as Tab;
|
|
|
|
if (tab == null || tab != container.SelectedItem) return;
|
|
|
|
|
|
|
|
if (e.LeftButton == MouseButtonState.Pressed) {
|
2021-04-29 05:05:55 -07:00
|
|
|
var dragging = new Controls.DragDropAdorner(item);
|
|
|
|
DragDrop.DoDragDrop(item, item.DataContext, DragDropEffects.Move);
|
|
|
|
dragging.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnDrop(object sender, DragEventArgs e) {
|
|
|
|
var tabSrc = e.Data.GetData(typeof(Tab)) as Tab;
|
|
|
|
if (tabSrc == null) return;
|
|
|
|
|
|
|
|
var dst = e.Source as FrameworkElement;
|
|
|
|
if (dst == null) return;
|
|
|
|
|
|
|
|
var tabDst = dst.DataContext as Tab;
|
|
|
|
if (tabSrc.Id == tabDst.Id) return;
|
|
|
|
|
|
|
|
int dstIdx = Tabs.IndexOf(tabDst);
|
|
|
|
Tabs.Remove(tabSrc);
|
|
|
|
Tabs.Insert(dstIdx, tabSrc);
|
|
|
|
container.SelectedItem = tabSrc;
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
2021-08-18 17:58:41 -07:00
|
|
|
|
|
|
|
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);
|
2021-10-08 23:29:44 -07:00
|
|
|
|
|
|
|
var copyPath = new MenuItem();
|
|
|
|
copyPath.Header = App.Text("PageTabBar.Tab.CopyPath");
|
|
|
|
copyPath.Click += (_, __) => {
|
|
|
|
Clipboard.SetDataObject(tab.Id);
|
|
|
|
};
|
|
|
|
menu.Items.Add(new Separator());
|
|
|
|
menu.Items.Add(copyPath);
|
|
|
|
}
|
2021-08-18 17:58:41 -07:00
|
|
|
|
|
|
|
menu.IsOpen = true;
|
|
|
|
e.Handled = true;
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
}
|
|
|
|
}
|