PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/GitUI/UserControls/RevisionGridClasses/RevisionGridMenuCommands.cs

https://github.com/qgppl/gitextensions
C# | 506 lines | 395 code | 96 blank | 15 comment | 19 complexity | b93c7200aee5b3c728c43721e280ba52 MD5 | raw file
Possible License(s): GPL-3.0
  1. using GitUI.CommandsDialogs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using GitUI.Hotkey;
  7. using System.Windows.Forms;
  8. using System.ComponentModel;
  9. using GitUI.CommandsDialogs.BrowseDialog;
  10. using ResourceManager;
  11. using System.Diagnostics;
  12. using GitCommands;
  13. namespace GitUI.UserControls.RevisionGridClasses
  14. {
  15. internal class RevisionGridMenuCommands : MenuCommandsBase
  16. {
  17. private readonly TranslationString _quickSearchQuickHelp =
  18. new TranslationString("Start typing in revision grid to start quick search.");
  19. private readonly TranslationString _noRevisionFoundError =
  20. new TranslationString("No revision found.");
  21. RevisionGrid _revisionGrid;
  22. // must both be created only once
  23. IEnumerable<MenuCommand> _navigateMenuCommands;
  24. IEnumerable<MenuCommand> _viewMenuCommands;
  25. public RevisionGridMenuCommands(RevisionGrid revisionGrid)
  26. {
  27. _revisionGrid = revisionGrid;
  28. CreateOrUpdateMenuCommands(); // for translation
  29. TranslationCategoryName = "RevisionGrid";
  30. Translate();
  31. }
  32. /// <summary>
  33. /// ... "Update" because the hotkey settings might change
  34. /// </summary>
  35. public void CreateOrUpdateMenuCommands()
  36. {
  37. if (_navigateMenuCommands == null && _viewMenuCommands == null)
  38. {
  39. _navigateMenuCommands = CreateNavigateMenuCommands();
  40. _viewMenuCommands = CreateViewMenuCommands();
  41. }
  42. if (_navigateMenuCommands != null && _viewMenuCommands != null)
  43. {
  44. var navigateMenuCommands2 = CreateNavigateMenuCommands();
  45. var viewMenuCommands2 = CreateViewMenuCommands();
  46. UpdateMenuCommandShortcutKeyDisplayString(_navigateMenuCommands, navigateMenuCommands2);
  47. UpdateMenuCommandShortcutKeyDisplayString(_viewMenuCommands, viewMenuCommands2);
  48. if (_revisionGrid != null) // null when TranslationApp is started
  49. {
  50. TriggerMenuChanged(); // trigger refresh
  51. }
  52. }
  53. }
  54. public void TriggerMenuChanged()
  55. {
  56. Debug.WriteLine("RevisionGridMenuCommands.TriggerMenuChanged()");
  57. OnMenuChanged();
  58. }
  59. private void UpdateMenuCommandShortcutKeyDisplayString(IEnumerable<MenuCommand> targetList, IEnumerable<MenuCommand> sourceList)
  60. {
  61. foreach (var sourceMc in sourceList.Where(mc => !mc.IsSeparator))
  62. {
  63. var targetMc = targetList.Single(mc => !mc.IsSeparator && mc.Name == sourceMc.Name);
  64. targetMc.ShortcutKeyDisplayString = sourceMc.ShortcutKeyDisplayString;
  65. }
  66. }
  67. public IEnumerable<MenuCommand> GetNavigateMenuCommands()
  68. {
  69. return _navigateMenuCommands;
  70. }
  71. private IEnumerable<MenuCommand> CreateNavigateMenuCommands()
  72. {
  73. var resultList = new List<MenuCommand>();
  74. {
  75. var menuCommand = new MenuCommand();
  76. menuCommand.Name = "GotoCurrentRevision";
  77. menuCommand.Text = "Go to current revision";
  78. menuCommand.Image = global::GitUI.Properties.Resources.IconGotoCurrentRevision;
  79. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.SelectCurrentRevision);
  80. menuCommand.ExecuteAction = SelectCurrentRevisionExecute;
  81. resultList.Add(menuCommand);
  82. }
  83. {
  84. var menuCommand = new MenuCommand();
  85. menuCommand.Name = "GotoCommit";
  86. menuCommand.Text = "Go to commit...";
  87. menuCommand.Image = global::GitUI.Properties.Resources.IconGotoCommit;
  88. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.GoToCommit);
  89. menuCommand.ExecuteAction = GotoCommitExcecute;
  90. resultList.Add(menuCommand);
  91. }
  92. resultList.Add(MenuCommand.CreateSeparator());
  93. {
  94. var menuCommand = new MenuCommand();
  95. menuCommand.Name = "GotoChildCommit";
  96. menuCommand.Text = "Go to child commit";
  97. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.GoToChild);
  98. menuCommand.ExecuteAction = () => _revisionGrid.ExecuteCommand(GitUI.RevisionGrid.Commands.GoToChild);
  99. resultList.Add(menuCommand);
  100. }
  101. {
  102. var menuCommand = new MenuCommand();
  103. menuCommand.Name = "GotoParentCommit";
  104. menuCommand.Text = "Go to parent commit";
  105. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.GoToParent);
  106. menuCommand.ExecuteAction = () => _revisionGrid.ExecuteCommand(GitUI.RevisionGrid.Commands.GoToParent);
  107. resultList.Add(menuCommand);
  108. }
  109. resultList.Add(MenuCommand.CreateSeparator());
  110. {
  111. var menuCommand = new MenuCommand();
  112. menuCommand.Name = "NavigateBackward";
  113. menuCommand.Text = "Navigate backward";
  114. menuCommand.ShortcutKeyDisplayString = (Keys.Alt | Keys.Left).ToShortcutKeyDisplayString();
  115. menuCommand.ExecuteAction = () => _revisionGrid.ExecuteCommand(GitUI.RevisionGrid.Commands.NavigateBackward);
  116. resultList.Add(menuCommand);
  117. }
  118. {
  119. var menuCommand = new MenuCommand();
  120. menuCommand.Name = "NavigateForward";
  121. menuCommand.Text = "Navigate forward";
  122. menuCommand.ShortcutKeyDisplayString = (Keys.Alt | Keys.Right).ToShortcutKeyDisplayString();
  123. menuCommand.ExecuteAction = () => _revisionGrid.ExecuteCommand(GitUI.RevisionGrid.Commands.NavigateForward);
  124. resultList.Add(menuCommand);
  125. }
  126. resultList.Add(MenuCommand.CreateSeparator());
  127. {
  128. var menuCommand = new MenuCommand();
  129. menuCommand.Name = "QuickSearch";
  130. menuCommand.Text = "Quick search";
  131. menuCommand.ExecuteAction = () => MessageBox.Show(_quickSearchQuickHelp.Text);
  132. resultList.Add(menuCommand);
  133. }
  134. {
  135. var menuCommand = new MenuCommand();
  136. menuCommand.Name = "PrevQuickSearch";
  137. menuCommand.Text = "Quick search previous";
  138. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.PrevQuickSearch);
  139. menuCommand.ExecuteAction = () => _revisionGrid.ExecuteCommand(GitUI.RevisionGrid.Commands.PrevQuickSearch);
  140. resultList.Add(menuCommand);
  141. }
  142. {
  143. var menuCommand = new MenuCommand();
  144. menuCommand.Name = "NextQuickSearch";
  145. menuCommand.Text = "Quick search next";
  146. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.NextQuickSearch);
  147. menuCommand.ExecuteAction = () => _revisionGrid.ExecuteCommand(GitUI.RevisionGrid.Commands.NextQuickSearch);
  148. resultList.Add(menuCommand);
  149. }
  150. return resultList;
  151. }
  152. /// <summary>
  153. /// this is needed because _revsionGrid is null when TranslationApp is called
  154. /// </summary>
  155. /// <param name="revGridCommands"></param>
  156. /// <returns></returns>
  157. private string GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands revGridCommands)
  158. {
  159. if (_revisionGrid == null)
  160. {
  161. return null;
  162. }
  163. return _revisionGrid.GetShortcutKeys(revGridCommands).ToShortcutKeyDisplayString();
  164. }
  165. private IEnumerable<MenuCommand> CreateViewMenuCommands()
  166. {
  167. var resultList = new List<MenuCommand>();
  168. // the next three MenuCommands just reuse (the currently rather
  169. // convoluted) logic from RevisionGrid.
  170. // After refactoring the three items should be added to RevisionGrid
  171. // as done with "ShowRemoteBranches" and not via RevisionGrid.Designer.cs
  172. {
  173. var menuCommand = new MenuCommand();
  174. menuCommand.Name = "ShowAllBranches";
  175. menuCommand.Text = "Show all branches";
  176. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.ShowAllBranches);
  177. menuCommand.ExecuteAction = () => _revisionGrid.ShowAllBranches_ToolStripMenuItemClick(null, null);
  178. menuCommand.IsCheckedFunc = () => _revisionGrid.ShowAllBranches_ToolStripMenuItemChecked;
  179. resultList.Add(menuCommand);
  180. }
  181. {
  182. var menuCommand = new MenuCommand();
  183. menuCommand.Name = "ShowCurrentBranchOnly";
  184. menuCommand.Text = "Show current branch only";
  185. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.ShowCurrentBranchOnly);
  186. menuCommand.ExecuteAction = () => _revisionGrid.ShowCurrentBranchOnly_ToolStripMenuItemClick(null, null);
  187. menuCommand.IsCheckedFunc = () => _revisionGrid.ShowCurrentBranchOnly_ToolStripMenuItemChecked;
  188. resultList.Add(menuCommand);
  189. }
  190. {
  191. var menuCommand = new MenuCommand();
  192. menuCommand.Name = "ShowFilteredBranches";
  193. menuCommand.Text = "Show filtered branches";
  194. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.ShowFilteredBranches);
  195. menuCommand.ExecuteAction = () => _revisionGrid.ShowFilteredBranches_ToolStripMenuItemClick(null, null);
  196. menuCommand.IsCheckedFunc = () => _revisionGrid.ShowFilteredBranches_ToolStripMenuItemChecked;
  197. resultList.Add(menuCommand);
  198. }
  199. resultList.Add(MenuCommand.CreateSeparator());
  200. {
  201. var menuCommand = new MenuCommand();
  202. menuCommand.Name = "ShowRemoteBranches";
  203. menuCommand.Text = "Show remote branches";
  204. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.ShowRemoteBranches);
  205. menuCommand.ExecuteAction = () => _revisionGrid.ShowRemoteBranches_ToolStripMenuItemClick(null, null);
  206. menuCommand.IsCheckedFunc = () => AppSettings.ShowRemoteBranches;
  207. resultList.Add(menuCommand);
  208. }
  209. resultList.Add(MenuCommand.CreateSeparator());
  210. {
  211. var menuCommand = new MenuCommand();
  212. menuCommand.Name = "ShowSuperprojectTags";
  213. menuCommand.Text = "Show superproject tags";
  214. menuCommand.ExecuteAction = () => _revisionGrid.ShowSuperprojectTags_ToolStripMenuItemClick(null, null);
  215. menuCommand.IsCheckedFunc = () => AppSettings.ShowSuperprojectTags;
  216. resultList.Add(menuCommand);
  217. }
  218. {
  219. var menuCommand = new MenuCommand();
  220. menuCommand.Name = "ShowSuperprojectBranches";
  221. menuCommand.Text = "Show superproject branches";
  222. menuCommand.ExecuteAction = () => _revisionGrid.ShowSuperprojectBranches_ToolStripMenuItemClick(null, null);
  223. menuCommand.IsCheckedFunc = () => AppSettings.ShowSuperprojectBranches;
  224. resultList.Add(menuCommand);
  225. }
  226. {
  227. var menuCommand = new MenuCommand();
  228. menuCommand.Name = "ShowSuperprojectRemoteBranches";
  229. menuCommand.Text = "Show superproject remote branches";
  230. menuCommand.ExecuteAction = () => _revisionGrid.ShowSuperprojectRemoteBranches_ToolStripMenuItemClick(null, null);
  231. menuCommand.IsCheckedFunc = () => AppSettings.ShowSuperprojectRemoteBranches;
  232. resultList.Add(menuCommand);
  233. }
  234. resultList.Add(MenuCommand.CreateSeparator());
  235. {
  236. var menuCommand = new MenuCommand();
  237. menuCommand.Name = "showRevisionGraphToolStripMenuItem";
  238. menuCommand.Text = "Show revision graph";
  239. menuCommand.ExecuteAction = () => _revisionGrid.ShowRevisionGraph_ToolStripMenuItemClick(null, null);
  240. menuCommand.IsCheckedFunc = () => _revisionGrid.IsGraphLayout();
  241. resultList.Add(menuCommand);
  242. }
  243. {
  244. var menuCommand = new MenuCommand();
  245. menuCommand.Name = "drawNonrelativesGrayToolStripMenuItem";
  246. menuCommand.Text = "Draw non relatives gray";
  247. menuCommand.ExecuteAction = () => _revisionGrid.DrawNonrelativesGray_ToolStripMenuItemClick(null, null);
  248. menuCommand.IsCheckedFunc = () => AppSettings.RevisionGraphDrawNonRelativesGray;
  249. resultList.Add(menuCommand);
  250. }
  251. {
  252. var menuCommand = new MenuCommand();
  253. menuCommand.Name = "orderRevisionsByDateToolStripMenuItem";
  254. menuCommand.Text = "Order revisions by date";
  255. menuCommand.ExecuteAction = () => _revisionGrid.OrderRevisionsByDate_ToolStripMenuItemClick(null, null);
  256. menuCommand.IsCheckedFunc = () => AppSettings.OrderRevisionByDate;
  257. resultList.Add(menuCommand);
  258. }
  259. {
  260. var menuCommand = new MenuCommand();
  261. menuCommand.Name = "showAuthorDateToolStripMenuItem";
  262. menuCommand.Text = "Show author date";
  263. menuCommand.ExecuteAction = () => _revisionGrid.ShowAuthorDate_ToolStripMenuItemClick(null, null);
  264. menuCommand.IsCheckedFunc = () => AppSettings.ShowAuthorDate;
  265. resultList.Add(menuCommand);
  266. }
  267. {
  268. var menuCommand = new MenuCommand();
  269. menuCommand.Name = "showRelativeDateToolStripMenuItem";
  270. menuCommand.Text = "Show relative date";
  271. menuCommand.ExecuteAction = () => _revisionGrid.ShowRelativeDate_ToolStripMenuItemClick(null, null);
  272. menuCommand.IsCheckedFunc = () => AppSettings.RelativeDate;
  273. resultList.Add(menuCommand);
  274. }
  275. {
  276. var menuCommand = new MenuCommand();
  277. menuCommand.Name = "showMergeCommitsToolStripMenuItem";
  278. menuCommand.Text = "Show merge commits";
  279. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.ToggleShowMergeCommits);
  280. menuCommand.ExecuteAction = () => _revisionGrid.ShowMergeCommits_ToolStripMenuItemClick(null, null);
  281. menuCommand.IsCheckedFunc = () => AppSettings.ShowMergeCommits;
  282. resultList.Add(menuCommand);
  283. }
  284. {
  285. var menuCommand = new MenuCommand();
  286. menuCommand.Name = "showTagsToolStripMenuItem";
  287. menuCommand.Text = "Show tags";
  288. menuCommand.ExecuteAction = () => _revisionGrid.ShowTags_ToolStripMenuItemClick(null, null);
  289. menuCommand.IsCheckedFunc = () => AppSettings.ShowTags;
  290. resultList.Add(menuCommand);
  291. }
  292. {
  293. var menuCommand = new MenuCommand();
  294. menuCommand.Name = "showIdsToolStripMenuItem";
  295. menuCommand.Text = "Show SHA1";
  296. menuCommand.ExecuteAction = () => _revisionGrid.ShowIds_ToolStripMenuItemClick(null, null);
  297. menuCommand.IsCheckedFunc = () => AppSettings.ShowIds;
  298. resultList.Add(menuCommand);
  299. }
  300. {
  301. var menuCommand = new MenuCommand();
  302. menuCommand.Name = "showGitNotesToolStripMenuItem";
  303. menuCommand.Text = "Show git notes";
  304. menuCommand.ExecuteAction = () => _revisionGrid.ShowGitNotes_ToolStripMenuItemClick(null, null);
  305. menuCommand.IsCheckedFunc = () => AppSettings.ShowGitNotes;
  306. resultList.Add(menuCommand);
  307. }
  308. {
  309. var menuCommand = new MenuCommand();
  310. menuCommand.Name = "showIsMessageMultilineToolStripMenuItem";
  311. menuCommand.Text = "Show indicator for multiline message";
  312. menuCommand.ExecuteAction = () =>
  313. {
  314. AppSettings.ShowIndicatorForMultilineMessage = !AppSettings.ShowIndicatorForMultilineMessage;
  315. _revisionGrid.ForceRefreshRevisions();
  316. };
  317. menuCommand.IsCheckedFunc = () => AppSettings.ShowIndicatorForMultilineMessage;
  318. resultList.Add(menuCommand);
  319. }
  320. resultList.Add(MenuCommand.CreateSeparator());
  321. {
  322. var menuCommand = new MenuCommand();
  323. menuCommand.Name = "ToggleHighlightSelectedBranch";
  324. menuCommand.Text = "Highlight selected branch (until refresh)";
  325. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.ToggleHighlightSelectedBranch);
  326. menuCommand.ExecuteAction = () => _revisionGrid.ExecuteCommand(GitUI.RevisionGrid.Commands.ToggleHighlightSelectedBranch);
  327. resultList.Add(menuCommand);
  328. }
  329. {
  330. var menuCommand = new MenuCommand();
  331. menuCommand.Name = "ToggleRevisionCardLayout";
  332. menuCommand.Text = "Change commit view layout";
  333. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.ToggleRevisionCardLayout);
  334. menuCommand.ExecuteAction = () => _revisionGrid.ToggleRevisionCardLayout();
  335. resultList.Add(menuCommand);
  336. }
  337. resultList.Add(MenuCommand.CreateSeparator());
  338. {
  339. var menuCommand = new MenuCommand();
  340. menuCommand.Name = "showFirstParent";
  341. menuCommand.Text = "Show first parents";
  342. menuCommand.Image = global::GitUI.Properties.Resources.IconShowFirstParent;
  343. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.ShowFirstParent);
  344. menuCommand.ExecuteAction = () => _revisionGrid.ShowFirstParent_ToolStripMenuItemClick(null, null);
  345. menuCommand.IsCheckedFunc = () => AppSettings.ShowFirstParent;
  346. resultList.Add(menuCommand);
  347. }
  348. {
  349. var menuCommand = new MenuCommand();
  350. menuCommand.Name = "filterToolStripMenuItem";
  351. menuCommand.Text = "Set advanced filter";
  352. menuCommand.Image = global::GitUI.Properties.Resources.IconFilter;
  353. menuCommand.ShortcutKeyDisplayString = GetShortcutKeyDisplayStringFromRevisionGridIfAvailable(GitUI.RevisionGrid.Commands.RevisionFilter);
  354. menuCommand.ExecuteAction = () => _revisionGrid.FilterToolStripMenuItemClick(null, null);
  355. resultList.Add(menuCommand);
  356. }
  357. return resultList;
  358. }
  359. public IEnumerable<MenuCommand> GetViewMenuCommands()
  360. {
  361. return _viewMenuCommands;
  362. }
  363. public event EventHandler MenuChanged;
  364. // taken from http://stackoverflow.com/questions/5058254/inotifypropertychanged-propertychangedeventhandler-event-is-always-null
  365. // paramenter name not used
  366. protected void OnMenuChanged()
  367. {
  368. Debug.WriteLine("RevisionGridMenuCommands.OnPropertyChanged()");
  369. EventHandler handler = MenuChanged;
  370. if (handler != null)
  371. {
  372. handler(this, null);
  373. }
  374. foreach (var menuCommand in GetMenuCommandsWithoutSeparators())
  375. {
  376. menuCommand.SetCheckForRegisteredMenuItems();
  377. menuCommand.UpdateMenuItemsShortcutKeyDisplayString();
  378. }
  379. }
  380. protected override IEnumerable<MenuCommand> GetMenuCommandsForTranslation()
  381. {
  382. return GetMenuCommandsWithoutSeparators();
  383. }
  384. private IEnumerable<MenuCommand> GetMenuCommandsWithoutSeparators()
  385. {
  386. return _navigateMenuCommands.Concat(_viewMenuCommands).Where(mc => !mc.IsSeparator);
  387. }
  388. public void SelectCurrentRevisionExecute()
  389. {
  390. _revisionGrid.ExecuteCommand(GitUI.RevisionGrid.Commands.SelectCurrentRevision);
  391. }
  392. public void GotoCommitExcecute()
  393. {
  394. using (FormGoToCommit formGoToCommit = new FormGoToCommit(_revisionGrid.UICommands))
  395. {
  396. if (formGoToCommit.ShowDialog(_revisionGrid) != DialogResult.OK)
  397. return;
  398. string revisionGuid = formGoToCommit.ValidateAndGetSelectedRevision();
  399. if (!string.IsNullOrEmpty(revisionGuid))
  400. {
  401. _revisionGrid.SetSelectedRevision(new GitRevision(_revisionGrid.Module, revisionGuid));
  402. }
  403. else
  404. {
  405. MessageBox.Show(_revisionGrid, _noRevisionFoundError.Text);
  406. }
  407. }
  408. }
  409. }
  410. }