feature<Welcome>: supports set bookmark of selected repository from context menu.

This commit is contained in:
leo 2023-08-21 17:38:36 +08:00
parent 7e1b1d7324
commit 5d3088d520
6 changed files with 97 additions and 78 deletions

View file

@ -14,6 +14,11 @@ namespace SourceGit.Models {
/// </summary>
public static event Action<Repository> Opened;
/// <summary>
/// 仓库的书签变化了
/// </summary>
public static event Action<string, int> BookmarkChanged;
/// <summary>
/// 跳转到指定提交的事件
/// </summary>
@ -97,6 +102,15 @@ namespace SourceGit.Models {
}
}
/// <summary>
/// 设置仓库标签变化
/// </summary>
/// <param name="repo"></param>
/// <param name="bookmark"></param>
public static void SetBookmark(string repo, int bookmark) {
BookmarkChanged?.Invoke(repo, bookmark);
}
/// <summary>
/// 跳转到指定的提交
/// </summary>

View file

@ -1,54 +0,0 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace SourceGit.Views.Controls {
/// <summary>
/// Implementation of <see cref="INotifyPropertyChanged" /> to simplify models.
/// </summary>
public abstract class BindableBase : INotifyPropertyChanged {
/// <summary>
/// Multicast event for property change notifications.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Checks if a property already matches a desired value. Sets the property and
/// notifies listeners only when necessary.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="storage">Reference to a property with both getter and setter.</param>
/// <param name="value">Desired value for the property.</param>
/// <param name="propertyName">
/// Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers that
/// support CallerMemberName.
/// </param>
/// <returns>
/// True if the value was changed, false if the existing value matched the
/// desired value.
/// </returns>
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) {
if (Equals(storage, value)) {
return false;
}
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
/// <summary>
/// Notifies listeners that a property value has changed.
/// </summary>
/// <param name="propertyName">
/// Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers
/// that support <see cref="CallerMemberNameAttribute" />.
/// </param>
protected void OnPropertyChanged([CallerMemberName] string propertyName = null) {
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

View file

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

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
@ -16,10 +18,12 @@ namespace SourceGit.Views.Widgets {
/// <summary>
/// 标签数据
/// </summary>
public class Tab : Controls.BindableBase {
public class Tab : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
public string Id { get; set; }
public bool IsRepository { get; set; }
private string title;
public string Title {
get => title;
@ -39,12 +43,13 @@ namespace SourceGit.Views.Widgets {
get => isSeperatorVisible;
set => SetProperty(ref isSeperatorVisible, value);
}
}
/// <summary>
/// 仓库标签页编辑事件参数
/// </summary>
public event Action<Tab> OnTabEdited;
public void SetProperty<T>(ref T storage, T value, [CallerMemberName] string propName = null) {
if (Equals(storage, value)) return;
storage = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
/// <summary>
/// 标签相关事件参数
@ -99,6 +104,15 @@ namespace SourceGit.Views.Widgets {
public PageTabBar() {
Tabs = new ObservableCollection<Tab>();
InitializeComponent();
Models.Watcher.BookmarkChanged += (repoPath, bookmark) => {
foreach (var tab in Tabs) {
if (tab.Id == repoPath) {
tab.Bookmark = bookmark;
break;
}
}
};
}
public void Add() {
@ -350,12 +364,7 @@ namespace SourceGit.Views.Widgets {
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);
}
Models.Watcher.SetBookmark(tab.Id, refIdx);
ev.Handled = true;
};
bookmark.Items.Add(mark);

View file

@ -107,7 +107,7 @@
<ItemsControl.ItemTemplate>
<DataTemplate>
<Control MouseDoubleClick="OnDoubleClickRepository">
<Control MouseDoubleClick="OnDoubleClickRepository" ContextMenuOpening="OnRepositoryContextMenuOpening">
<Control.Template>
<ControlTemplate>
<Grid Height="38" Margin="2,0,2,6">

View file

@ -19,6 +19,14 @@ namespace SourceGit.Views.Widgets {
InitializeComponent();
UpdateVisibles();
Models.Theme.AddListener(this, UpdateVisibles);
Models.Watcher.BookmarkChanged += (repoPath, bookmark) => {
var repo = Models.Preference.Instance.FindRepository(repoPath);
if (repo != null) {
repo.Bookmark = bookmark;
UpdateVisibles();
}
};
}
#region FUNC_EVENTS
@ -89,7 +97,7 @@ namespace SourceGit.Views.Widgets {
}
private void OnRemoveRepository(object sender, RoutedEventArgs e) {
var repo = (sender as Button).DataContext as Models.Repository;
var repo = (sender as Control).DataContext as Models.Repository;
if (repo == null) return;
var confirmDialog = new ConfirmDialog(
@ -108,6 +116,56 @@ namespace SourceGit.Views.Widgets {
OnOpenRepository(sender, e);
}
private void OnRepositoryContextMenuOpening(object sender, ContextMenuEventArgs e) {
var control = sender as Control;
if (control == null) return;
var repo = control.DataContext as Models.Repository;
if (repo == null) return;
var menu = new ContextMenu();
menu.Placement = PlacementMode.MousePoint;
menu.PlacementTarget = control;
menu.StaysOpen = false;
menu.Focusable = true;
var open = new MenuItem();
open.Header = App.Text("RepoCM.Open");
open.Click += OnOpenRepository;
menu.Items.Add(open);
menu.Items.Add(new Separator());
var bookmark = new MenuItem();
bookmark.Header = App.Text("PageTabBar.Tab.Bookmark");
for (int i = 0; i < Converters.IntToBookmarkBrush.COLORS.Length; i++) {
var icon = new System.Windows.Shapes.Path();
icon.Data = new EllipseGeometry(new Point(0, 0), 12, 12);
icon.Fill = Converters.IntToBookmarkBrush.COLORS[i];
icon.Width = 12;
var mark = new MenuItem();
mark.Icon = icon;
mark.Header = $"{i}";
var refIdx = i;
mark.Click += (o, ev) => {
Models.Watcher.SetBookmark(repo.Path, refIdx);
ev.Handled = true;
};
bookmark.Items.Add(mark);
}
menu.Items.Add(bookmark);
menu.Items.Add(new Separator());
var remove = new MenuItem();
remove.Header = App.Text("Welcome.Delete");
remove.Click += OnRemoveRepository;
menu.Items.Add(remove);
menu.IsOpen = true;
e.Handled = true;
}
private void OnOpenRepository(object sender, RoutedEventArgs e) {
var repo = (sender as Control).DataContext as Models.Repository;
if (repo == null) return;
@ -125,7 +183,7 @@ namespace SourceGit.Views.Widgets {
}
private void OnOpenRepositoryTerminal(object sender, RoutedEventArgs e) {
var repo = (sender as Button).DataContext as Models.Repository;
var repo = (sender as Control).DataContext as Models.Repository;
if (repo == null) return;
var bash = Path.Combine(Models.Preference.Instance.Git.Path, "..", "bash.exe");