Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit f98db05

Browse files
Merge branch 'enhancements/branches-view-rollup' into fixes/branches-view-remove-serialized-texture2d
2 parents 924a28d + fd4b6ed commit f98db05

File tree

3 files changed

+150
-111
lines changed

3 files changed

+150
-111
lines changed

src/GitHub.Api/Git/GitBranch.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ public struct GitBranch : ITreeData
1717
public string tracking;
1818
public bool isActive;
1919

20-
public string Name { get { return name; } }
21-
public string Tracking { get { return tracking; } }
22-
public bool IsActive { get { return isActive; } }
23-
2420
public GitBranch(string name, string tracking, bool active)
2521
{
2622
Guard.ArgumentNotNullOrWhiteSpace(name, "name");
@@ -30,6 +26,10 @@ public GitBranch(string name, string tracking, bool active)
3026
this.isActive = active;
3127
}
3228

29+
public string Name => name;
30+
public string Tracking => tracking;
31+
public bool IsActive => isActive;
32+
3333
public override string ToString()
3434
{
3535
return $"{Name} Tracking? {Tracking} Active? {IsActive}";

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs

Lines changed: 130 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class BranchesView : Subview
3232
private const string DeleteBranchTitle = "Delete Branch?";
3333
private const string DeleteBranchButton = "Delete";
3434
private const string CancelButtonLabel = "Cancel";
35+
private const string DeleteBranchContextMenuLabel = "Delete";
36+
private const string SwitchBranchContextMenuLabel = "Switch";
37+
private const string CheckoutBranchContextMenuLabel = "Checkout";
3538

3639
[NonSerialized] private int listID = -1;
3740
[NonSerialized] private BranchesMode targetMode;
@@ -226,12 +229,7 @@ private void OnButtonBarGUI()
226229
{
227230
if (GUILayout.Button(DeleteBranchButton, EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
228231
{
229-
var selectedBranchName = treeLocals.SelectedNode.Name;
230-
var dialogMessage = string.Format(DeleteBranchMessageFormatString, selectedBranchName);
231-
if (EditorUtility.DisplayDialog(DeleteBranchTitle, dialogMessage, DeleteBranchButton, CancelButtonLabel))
232-
{
233-
GitClient.DeleteBranch(selectedBranchName, true).Start();
234-
}
232+
DeleteLocalBranch(treeLocals.SelectedNode.Name);
235233
}
236234
}
237235
EditorGUI.EndDisabledGroup();
@@ -345,26 +343,20 @@ private void OnTreeGUI(Rect rect)
345343

346344
var treeHadFocus = treeLocals.SelectedNode != null;
347345

348-
rect = treeLocals.Render(rect, scroll, _ => { }, node =>
349-
{
350-
if (EditorUtility.DisplayDialog(ConfirmSwitchTitle, String.Format(ConfirmSwitchMessage, node.Name), ConfirmSwitchOK,
351-
ConfirmSwitchCancel))
352-
{
353-
GitClient.SwitchBranch(node.Name)
354-
.FinallyInUI((success, e) =>
355-
{
356-
if (success)
357-
{
358-
Redraw();
359-
}
360-
else
361-
{
362-
EditorUtility.DisplayDialog(Localization.SwitchBranchTitle,
363-
String.Format(Localization.SwitchBranchFailedDescription, node.Name),
364-
Localization.Ok);
365-
}
366-
}).Start();
367-
}
346+
rect = treeLocals.Render(rect, scroll,
347+
node =>{ },
348+
node => {
349+
if (node.IsFolder)
350+
return;
351+
352+
SwitchBranch(node.Name);
353+
},
354+
node => {
355+
if (node.IsFolder)
356+
return;
357+
358+
var menu = CreateContextMenuForLocalBranchNode(node);
359+
menu.ShowAsContext();
368360
});
369361

370362
if (treeHadFocus && treeLocals.SelectedNode == null)
@@ -379,55 +371,26 @@ private void OnTreeGUI(Rect rect)
379371

380372
rect.y += Styles.TreePadding;
381373

382-
rect = treeRemotes.Render(rect, scroll, _ => {}, selectedNode =>
383-
{
384-
var indexOfFirstSlash = selectedNode.Name.IndexOf('/');
385-
var originName = selectedNode.Name.Substring(0, indexOfFirstSlash);
386-
var branchName = selectedNode.Name.Substring(indexOfFirstSlash + 1);
374+
rect = treeRemotes.Render(rect, scroll,
375+
node => { },
376+
node => {
377+
if (node.IsFolder)
378+
return;
387379

388-
if (Repository.LocalBranches.Any(localBranch => localBranch.Name == branchName))
389-
{
390-
EditorUtility.DisplayDialog(WarningCheckoutBranchExistsTitle,
391-
String.Format(WarningCheckoutBranchExistsMessage, branchName),
392-
WarningCheckoutBranchExistsOK);
393-
}
394-
else
395-
{
396-
var confirmCheckout = EditorUtility.DisplayDialog(ConfirmCheckoutBranchTitle,
397-
String.Format(ConfirmCheckoutBranchMessage, selectedNode.Name, originName),
398-
ConfirmCheckoutBranchOK,
399-
ConfirmCheckoutBranchCancel);
380+
CheckoutRemoteBranch(node.Name);
381+
},
382+
node => {
383+
if (node.IsFolder)
384+
return;
400385

401-
if (confirmCheckout)
402-
{
403-
GitClient
404-
.CreateBranch(branchName, selectedNode.Name)
405-
.FinallyInUI((success, e) =>
406-
{
407-
if (success)
408-
{
409-
Redraw();
410-
}
411-
else
412-
{
413-
EditorUtility.DisplayDialog(Localization.SwitchBranchTitle,
414-
String.Format(Localization.SwitchBranchFailedDescription, selectedNode.Name),
415-
Localization.Ok);
416-
}
417-
})
418-
.Start();
419-
}
420-
}
386+
var menu = CreateContextMenuForRemoteBranchNode(node);
387+
menu.ShowAsContext();
421388
});
422389

423390
if (treeHadFocus && treeRemotes.SelectedNode == null)
424-
{
425391
treeLocals.Focus();
426-
}
427392
else if (!treeHadFocus && treeRemotes.SelectedNode != null)
428-
{
429393
treeLocals.Blur();
430-
}
431394

432395
if (treeRemotes.RequiresRepaint)
433396
Redraw();
@@ -436,18 +399,109 @@ private void OnTreeGUI(Rect rect)
436399
GUILayout.Space(rect.y - initialRect.y);
437400
}
438401

439-
private int CompareBranches(GitBranch a, GitBranch b)
402+
private GenericMenu CreateContextMenuForLocalBranchNode(TreeNode node)
403+
{
404+
var genericMenu = new GenericMenu();
405+
406+
var deleteGuiContent = new GUIContent(DeleteBranchContextMenuLabel);
407+
var switchGuiContent = new GUIContent(SwitchBranchContextMenuLabel);
408+
409+
if (node.IsActive)
410+
{
411+
genericMenu.AddDisabledItem(deleteGuiContent);
412+
genericMenu.AddDisabledItem(switchGuiContent);
413+
}
414+
else
415+
{
416+
genericMenu.AddItem(deleteGuiContent, false, () => {
417+
DeleteLocalBranch(node.Name);
418+
});
419+
420+
genericMenu.AddItem(switchGuiContent, false, () => {
421+
SwitchBranch(node.Name);
422+
});
423+
}
424+
425+
return genericMenu;
426+
}
427+
428+
private GenericMenu CreateContextMenuForRemoteBranchNode(TreeNode node)
429+
{
430+
var genericMenu = new GenericMenu();
431+
432+
var checkoutGuiContent = new GUIContent(CheckoutBranchContextMenuLabel);
433+
434+
genericMenu.AddItem(checkoutGuiContent, false, () => {
435+
CheckoutRemoteBranch(node.Name);
436+
});
437+
438+
return genericMenu;
439+
}
440+
441+
private void CheckoutRemoteBranch(string branch)
442+
{
443+
var indexOfFirstSlash = branch.IndexOf('/');
444+
var originName = branch.Substring(0, indexOfFirstSlash);
445+
var branchName = branch.Substring(indexOfFirstSlash + 1);
446+
447+
if (Repository.LocalBranches.Any(localBranch => localBranch.Name == branchName))
448+
{
449+
EditorUtility.DisplayDialog(WarningCheckoutBranchExistsTitle,
450+
String.Format(WarningCheckoutBranchExistsMessage, branchName), WarningCheckoutBranchExistsOK);
451+
}
452+
else
453+
{
454+
var confirmCheckout = EditorUtility.DisplayDialog(ConfirmCheckoutBranchTitle,
455+
String.Format(ConfirmCheckoutBranchMessage, branch, originName), ConfirmCheckoutBranchOK,
456+
ConfirmCheckoutBranchCancel);
457+
458+
if (confirmCheckout)
459+
{
460+
GitClient.CreateBranch(branchName, branch).FinallyInUI((success, e) => {
461+
if (success)
462+
{
463+
Redraw();
464+
}
465+
else
466+
{
467+
EditorUtility.DisplayDialog(Localization.SwitchBranchTitle,
468+
String.Format(Localization.SwitchBranchFailedDescription, branch), Localization.Ok);
469+
}
470+
}).Start();
471+
}
472+
}
473+
}
474+
475+
private void SwitchBranch(string branch)
440476
{
441-
//if (IsFavorite(a.Name))
442-
//{
443-
// return -1;
444-
//}
477+
if (EditorUtility.DisplayDialog(ConfirmSwitchTitle, String.Format(ConfirmSwitchMessage, branch), ConfirmSwitchOK,
478+
ConfirmSwitchCancel))
479+
{
480+
GitClient.SwitchBranch(branch).FinallyInUI((success, e) => {
481+
if (success)
482+
{
483+
Redraw();
484+
}
485+
else
486+
{
487+
EditorUtility.DisplayDialog(Localization.SwitchBranchTitle,
488+
String.Format(Localization.SwitchBranchFailedDescription, branch), Localization.Ok);
489+
}
490+
}).Start();
491+
}
492+
}
445493

446-
//if (IsFavorite(b.Name))
447-
//{
448-
// return 1;
449-
//}
494+
private void DeleteLocalBranch(string branch)
495+
{
496+
var dialogMessage = string.Format(DeleteBranchMessageFormatString, branch);
497+
if (EditorUtility.DisplayDialog(DeleteBranchTitle, dialogMessage, DeleteBranchButton, CancelButtonLabel))
498+
{
499+
GitClient.DeleteBranch(branch, true).Start();
500+
}
501+
}
450502

503+
private int CompareBranches(GitBranch a, GitBranch b)
504+
{
451505
if (a.Name.Equals("master"))
452506
{
453507
return -1;
@@ -461,31 +515,6 @@ private int CompareBranches(GitBranch a, GitBranch b)
461515
return a.Name.CompareTo(b.Name);
462516
}
463517

464-
//private bool IsFavorite(string branchName)
465-
//{
466-
// return !String.IsNullOrEmpty(branchName) && favoritesList.Contains(branchName);
467-
//}
468-
469-
//private void SetFavorite(TreeNode branch, bool favorite)
470-
//{
471-
// if (string.IsNullOrEmpty(branch.Name))
472-
// {
473-
// return;
474-
// }
475-
476-
// if (!favorite)
477-
// {
478-
// favorites.Remove(branch);
479-
// Manager.LocalSettings.Set(FavoritesSetting, favorites.Select(x => x.Name).ToList());
480-
// }
481-
// else
482-
// {
483-
// favorites.Remove(branch);
484-
// favorites.Add(branch);
485-
// Manager.LocalSettings.Set(FavoritesSetting, favorites.Select(x => x.Name).ToList());
486-
// }
487-
//}
488-
489518
public override bool IsBusy
490519
{
491520
get { return false; }

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void Load(IEnumerable<ITreeData> data, string title)
123123
foldersKeys = Folders.Keys.Cast<string>().ToList();
124124
}
125125

126-
public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = null, Action<TreeNode> doubleClick = null)
126+
public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = null, Action<TreeNode> doubleClick = null, Action<TreeNode> rightClick = null)
127127
{
128128
Profiler.BeginSample("TreeControl");
129129
bool visible = true;
@@ -179,7 +179,7 @@ public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = nul
179179
{
180180
if (visible)
181181
{
182-
RequiresRepaint = HandleInput(rect, node, i, singleClick, doubleClick);
182+
RequiresRepaint = HandleInput(rect, node, i, singleClick, doubleClick, rightClick);
183183
}
184184
rect.y += ItemHeight + ItemSpacing;
185185
}
@@ -243,7 +243,7 @@ private int ToggleNodeVisibility(int idx, TreeNode rootNode)
243243
return idx;
244244
}
245245

246-
private bool HandleInput(Rect rect, TreeNode currentNode, int index, Action<TreeNode> singleClick = null, Action<TreeNode> doubleClick = null)
246+
private bool HandleInput(Rect rect, TreeNode currentNode, int index, Action<TreeNode> singleClick = null, Action<TreeNode> doubleClick = null, Action<TreeNode> rightClick = null)
247247
{
248248
bool selectionChanged = false;
249249
var clickRect = new Rect(0f, rect.y, rect.width, rect.height);
@@ -253,14 +253,20 @@ private bool HandleInput(Rect rect, TreeNode currentNode, int index, Action<Tree
253253
SelectedNode = currentNode;
254254
selectionChanged = true;
255255
var clickCount = Event.current.clickCount;
256-
if (clickCount == 1 && singleClick != null)
256+
var mouseButton = Event.current.button;
257+
258+
if (mouseButton == 0 && clickCount == 1 && singleClick != null)
257259
{
258260
singleClick(currentNode);
259261
}
260-
if (clickCount > 1 && doubleClick != null)
262+
if (mouseButton == 0 && clickCount > 1 && doubleClick != null)
261263
{
262264
doubleClick(currentNode);
263265
}
266+
if (mouseButton == 1 && clickCount == 1 && rightClick != null)
267+
{
268+
rightClick(currentNode);
269+
}
264270
}
265271

266272
// Keyboard navigation if this child is the current selection
@@ -434,7 +440,9 @@ public bool Render(Rect rect, float indentation, bool isSelected, GUIStyle folde
434440
{
435441
nodeStyle.Draw(fillRect, GUIContent.none, false, false, false, isSelected);
436442
if (IsFolder)
443+
{
437444
style.Draw(nodeRect, content, false, false, !IsCollapsed, isSelected);
445+
}
438446
else
439447
{
440448
style.Draw(nodeRect, content, false, false, false, isSelected);
@@ -443,8 +451,10 @@ public bool Render(Rect rect, float indentation, bool isSelected, GUIStyle folde
443451

444452
if (IsFolder)
445453
{
454+
var toggleRect = new Rect(nodeRect.x, nodeRect.y, style.border.horizontal, nodeRect.height);
455+
446456
EditorGUI.BeginChangeCheck();
447-
GUI.Toggle(nodeRect, !IsCollapsed, GUIContent.none, GUIStyle.none);
457+
GUI.Toggle(toggleRect, !IsCollapsed, GUIContent.none, GUIStyle.none);
448458
changed = EditorGUI.EndChangeCheck();
449459
}
450460

0 commit comments

Comments
 (0)