sourcegit/src/Views/Widgets/CommitDetail.xaml.cs

164 lines
5.9 KiB
C#
Raw Normal View History

2021-04-29 05:05:55 -07:00
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
2021-04-29 05:05:55 -07:00
using System.Windows.Navigation;
namespace SourceGit.Views.Widgets {
/// <summary>
/// 提交详情
/// </summary>
public partial class CommitDetail : UserControl {
private string repo = null;
private Models.Commit commit = null;
private Commands.Context cancelToken = new Commands.Context();
2021-04-29 05:05:55 -07:00
public CommitDetail() {
InitializeComponent();
}
public void SetData(string repo, Models.Commit commit) {
cancelToken.IsCancelRequested = true;
cancelToken = new Commands.Context();
2021-04-29 05:05:55 -07:00
this.repo = repo;
this.commit = commit;
revisionFiles.SetData(repo, commit.SHA, cancelToken);
2021-04-29 05:05:55 -07:00
UpdateInformation(commit);
UpdateChanges();
}
#region DATA
private void UpdateInformation(Models.Commit commit) {
txtSHA.Text = commit.SHA;
txtMessage.Text = (commit.Subject + "\n\n" + commit.Message.Trim()).Trim();
avatarAuthor.Email = commit.Author.Email;
avatarAuthor.FallbackLabel = commit.Author.Name;
txtAuthorName.Text = commit.Author.Name;
txtAuthorEmail.Text = commit.Author.Email;
txtAuthorTime.Text = commit.Author.Time;
avatarCommitter.Email = commit.Committer.Email;
avatarCommitter.FallbackLabel = commit.Committer.Name;
txtCommitterName.Text = commit.Committer.Name;
txtCommitterEmail.Text = commit.Committer.Email;
txtCommitterTime.Text = commit.Committer.Time;
if (commit.Committer.Email == commit.Author.Email && commit.Committer.Time == commit.Author.Time) {
avatarCommitter.Visibility = Visibility.Hidden;
committerInfoPanel.Visibility = Visibility.Hidden;
} else {
avatarCommitter.Visibility = Visibility.Visible;
committerInfoPanel.Visibility = Visibility.Visible;
}
if (commit.Parents.Count == 0) {
rowParents.Height = new GridLength(0);
} else {
rowParents.Height = GridLength.Auto;
var shortPIDs = new List<string>();
foreach (var p in commit.Parents) shortPIDs.Add(p.Substring(0, 10));
listParents.ItemsSource = shortPIDs;
}
if (!commit.HasDecorators) {
rowRefs.Height = new GridLength(0);
} else {
rowRefs.Height = GridLength.Auto;
listRefs.ItemsSource = commit.Decorators;
}
}
private void UpdateChanges() {
var cmd = new Commands.CommitChanges(repo, commit.SHA) { Ctx = cancelToken };
2021-04-29 05:05:55 -07:00
Task.Run(() => {
var changes = cmd.Result();
if (cmd.Ctx.IsCancelRequested) return;
2021-04-29 05:05:55 -07:00
Dispatcher.Invoke(() => {
changeList.ItemsSource = changes;
changeContainer.SetData(repo, new List<Models.Commit>() { commit }, changes);
});
});
}
#endregion
#region EVENTS
2021-04-29 05:05:55 -07:00
private void OnNavigateParent(object sender, RequestNavigateEventArgs e) {
Models.Watcher.Get(repo)?.NavigateTo(e.Uri.OriginalString);
}
private void OnChangeListMouseDoubleClick(object sender, MouseButtonEventArgs e) {
var row = sender as DataGridRow;
if (row == null) return;
var change = row.DataContext as Models.Change;
if (change == null) return;
body.SelectedIndex = 1;
changeContainer.Select(change);
e.Handled = true;
}
2021-04-29 05:05:55 -07:00
private void OnChangeListContextMenuOpening(object sender, ContextMenuEventArgs e) {
var row = sender as DataGridRow;
if (row == null) return;
var change = row.DataContext as Models.Change;
if (change == null) return;
var menu = new ContextMenu();
if (change.Index != Models.Change.Status.Deleted) {
2021-04-29 05:05:55 -07:00
var history = new MenuItem();
history.Header = App.Text("FileHistory");
history.IsEnabled = change.Index != Models.Change.Status.Deleted;
2021-04-29 05:05:55 -07:00
history.Click += (o, ev) => {
var viewer = new Views.Histories(repo, change.Path);
2021-04-29 05:05:55 -07:00
viewer.Show();
ev.Handled = true;
};
var blame = new MenuItem();
blame.Header = App.Text("Blame");
blame.IsEnabled = change.Index != Models.Change.Status.Deleted;
2021-04-29 05:05:55 -07:00
blame.Click += (obj, ev) => {
var viewer = new Blame(repo, change.Path, commit.SHA);
2021-04-29 05:05:55 -07:00
viewer.Show();
ev.Handled = true;
};
var explore = new MenuItem();
explore.Header = App.Text("RevealFile");
explore.IsEnabled = change.Index != Models.Change.Status.Deleted;
2021-04-29 05:05:55 -07:00
explore.Click += (o, ev) => {
var full = Path.GetFullPath(repo + "\\" + change.Path);
2021-04-29 05:05:55 -07:00
Process.Start("explorer", $"/select,{full}");
ev.Handled = true;
};
menu.Items.Add(history);
menu.Items.Add(blame);
menu.Items.Add(explore);
}
var copyPath = new MenuItem();
copyPath.Header = App.Text("CopyPath");
copyPath.Click += (obj, ev) => {
Clipboard.SetText(change.Path);
ev.Handled = true;
2021-04-29 05:05:55 -07:00
};
menu.Items.Add(copyPath);
menu.IsOpen = true;
2021-04-29 05:05:55 -07:00
e.Handled = true;
}
#endregion
}
}