/
ChangesetHistoryContextMenu.xaml.cs
213 lines (189 loc) · 7.53 KB
/
ChangesetHistoryContextMenu.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using System.Linq;
using GG.Models;
using GG.Libraries;
using System.Windows.Documents;
namespace GG.UserControls
{
/// <summary>
/// Interaction logic for ChangesetHistoryContextMenu.xaml
/// </summary>
public partial class ChangesetHistoryContextMenu : ContextMenu
{
public ChangesetHistoryContextMenu()
{
InitializeComponent();
}
/// <summary>
/// Returns the currently selected commit.
/// </summary>
/// <returns></returns>
private Commit GetCurrentSelectedCommit()
{
return ((DataGrid) PlacementTarget).SelectedItem as Commit;
}
/// <summary>
/// Returns the current repository view model instance being used.
/// </summary>
/// <returns></returns>
private RepositoryViewModel GetRepositoryViewModel()
{
return ((DataGrid) PlacementTarget).DataContext as RepositoryViewModel;
}
/// <summary>
/// Preprocessing prior to menu opening.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnOpened(object sender, System.Windows.RoutedEventArgs e)
{
var menuItemIndex = 0;
var commit = GetCurrentSelectedCommit();
var repositoryViewModel = GetRepositoryViewModel();
var highlightColor = new SolidColorBrush()
{
Color = Color.FromRgb(8, 94, 160)
};
// Remove previously added Dynamic menu items. We determine dynamically added menu items by their Tag == "Dynamic".
RemoveDynamicallyAddedMenuItems();
if (commit == null)
{
return;
}
// Add Checkout menu items. TODO: Add also non-branch-checkouts.
var numberOfCheckoutItems = 0;
foreach (var branch in commit.Branches)
{
// Only if the right clicked commit is the tip of the branch, AND the branch is not already checkout out, continue.
if (branch.Tip == commit && (repositoryViewModel.Head) != branch && branch.IsRemote == false)
{
var text = new TextBlock();
text.Inlines.AddRange(new Inline[]
{
new Run("Checkout "),
new Run(branch.Name) {Foreground = highlightColor}
});
Items.Insert(menuItemIndex++, CreateMenuItem(text, "Checkout"));
numberOfCheckoutItems++;
}
}
if (numberOfCheckoutItems == 0)
{
var text = new TextBlock();
text.Inlines.AddRange(new Inline[]
{
new Run("Checkout commit "),
new Run(commit.HashShort) {Foreground = highlightColor}
});
Items.Insert(menuItemIndex++, CreateMenuItem(text, "Checkout"));
numberOfCheckoutItems++;
}
if (numberOfCheckoutItems > 1)
{
Items.Insert(menuItemIndex++, new Separator
{
Tag = "Dynamic"
});
}
// Add Merge menu items.
var numberOfMergeItems = 0;
foreach (Branch branch in commit.Branches)
{
if (branch.Tip == commit && branch != (Branch) repositoryViewModel.Head)
{
// Add those that track this branch.
foreach (Branch branchThatTracks in RepoUtil.GetBranchesThatTrack(branch, repositoryViewModel.Branches))
{
if (branchThatTracks.BehindBy > 0 &&
branchThatTracks.IsRemote == false &&
branch.Tip != branchThatTracks.Tip &&
repositoryViewModel.Head is DetachedHead == false &&
branch.Tip != branchThatTracks.Tip)
{
var text = new TextBlock();
text.Inlines.AddRange(new Inline[]
{
new Run("Merge "),
new Run(branch.Name) {Foreground = highlightColor},
new Run(" into "),
new Run(branchThatTracks.Name) {Foreground = highlightColor}
});
Items.Insert(menuItemIndex++, CreateMenuItem(text, "Merge"));
numberOfCheckoutItems++;
}
}
}
if (branch.Tip == commit &&
branch != (Branch) repositoryViewModel.Head &&
repositoryViewModel.Head is DetachedHead == false &&
commit != ((Branch) repositoryViewModel.Head).Tip)
{
var text = new TextBlock();
text.Inlines.AddRange(new Inline[]
{
new Run("Merge "),
new Run(branch.Name) {Foreground = highlightColor},
new Run(" into "),
new Run(((Branch) repositoryViewModel.Head).Name) {Foreground = highlightColor}
});
Items.Insert(menuItemIndex++, CreateMenuItem(text, "Merge"));
numberOfCheckoutItems++;
}
}
if (numberOfMergeItems > 1)
{
Items.Insert(menuItemIndex++, new Separator
{
Tag = "Dynamic"
});
}
}
/// <summary>
/// Removes all dynamically added menu items.
/// </summary>
private void RemoveDynamicallyAddedMenuItems()
{
IList<object> itemsToRemove = Items.Cast<object>().Where(mi =>
{
if (mi is MenuItem == false)
return false;
var tag = ((MenuItem) mi).Tag;
return tag != null && tag.ToString() == "Dynamic";
}).ToList();
foreach (MenuItem item in itemsToRemove)
{
Items.Remove(item);
}
}
/// <summary>
/// Creates and returns a new menu item.
/// </summary>
/// <returns></returns>
private MenuItem CreateMenuItem(object header, string icon)
{
return new MenuItem
{
Header = header,
Tag = "Dynamic",
Icon = new Image
{
Stretch = Stretch.None,
Source = new BitmapImage(new Uri(@"pack://application:,,,/Resources/Icons/" + icon + ".png", UriKind.Absolute))
}
};
}
private void OnContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var commit = GetCurrentSelectedCommit();
if (commit == null)
{
e.Handled = true;
return;
}
}
}
}