using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace SourceGit.Views.Widgets {
///
/// 贮藏管理
///
public partial class Stashes : UserControl {
private string repo = null;
private string selected = null;
public Stashes(string repo) {
this.repo = repo;
InitializeComponent();
}
public void SetData(List data) {
stashList.ItemsSource = data;
changeList.ItemsSource = null;
}
private async void OnStashSelectionChanged(object sender, SelectionChangedEventArgs e) {
changeList.ItemsSource = null;
selected = null;
var stash = stashList.SelectedItem as Models.Stash;
if (stash == null) return;
selected = stash.SHA;
diffViewer.Reset();
var changes = await Task.Run(() => new Commands.StashChanges(repo, selected).Result());
changeList.ItemsSource = changes;
}
private void OnChangeSelectionChanged(object sender, SelectionChangedEventArgs e) {
var change = changeList.SelectedItem as Models.Change;
if (change == null) return;
diffViewer.Diff(repo, new DiffViewer.Option() {
RevisionRange = new string[] { selected + "^", selected },
Path = change.Path,
OrgPath = change.OriginalPath
});
}
private void OnStashContextMenuOpening(object sender, ContextMenuEventArgs ev) {
var stash = (sender as Border).DataContext as Models.Stash;
if (stash == null) return;
var apply = new MenuItem();
apply.Header = App.Text("StashCM.Apply");
apply.Click += (o, e) => Start(() => new Commands.Stash(repo).Apply(stash.Name));
var pop = new MenuItem();
pop.Header = App.Text("StashCM.Pop");
pop.Click += (o, e) => Start(() => new Commands.Stash(repo).Pop(stash.Name));
var delete = new MenuItem();
delete.Header = App.Text("StashCM.Drop");
delete.Click += (o, e) => Start(() => new Commands.Stash(repo).Drop(stash.Name));
var menu = new ContextMenu();
menu.Items.Add(apply);
menu.Items.Add(pop);
menu.Items.Add(delete);
menu.IsOpen = true;
ev.Handled = true;
}
private async void Start(Func job) {
waiting.Visibility = Visibility.Visible;
waiting.IsAnimating = true;
Models.Watcher.SetEnabled(repo, false);
await Task.Run(job);
Models.Watcher.SetEnabled(repo, true);
waiting.Visibility = Visibility.Collapsed;
waiting.IsAnimating = false;
}
}
}