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

Commit f7f5dc2

Browse files
committed
Fix styling and selection of locks
1 parent 8e1c6d9 commit f7f5dc2

File tree

2 files changed

+93
-69
lines changed

2 files changed

+93
-69
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Styles.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class Styles
8686
historyDetailsTitleStyle,
8787
historyDetailsMetaInfoStyle,
8888
genericBoxStyle,
89-
hyperlinkStyle;
89+
hyperlinkStyle,
90+
selectedArea,
91+
selectedLabel;
9092

9193
private static Texture2D branchIcon,
9294
activeBranchIcon,
@@ -202,6 +204,23 @@ public static GUIStyle HistoryFileTreeBoxStyle
202204
}
203205
}
204206

207+
public static GUIStyle SelectedArea
208+
{
209+
get
210+
{
211+
if (selectedArea == null)
212+
{
213+
selectedArea = new GUIStyle(GUI.skin.label);
214+
selectedArea.name = "SelectedArea";
215+
216+
var hierarchyStyle = GUI.skin.FindStyle("PR Label");
217+
selectedArea.normal.background = hierarchyStyle.onFocused.background;
218+
selectedArea.focused.background = hierarchyStyle.onFocused.background;
219+
}
220+
return selectedArea;
221+
}
222+
}
223+
205224
public static GUIStyle Label
206225
{
207226
get
@@ -216,11 +235,34 @@ public static GUIStyle Label
216235
label.onNormal.textColor = hierarchyStyle.onNormal.textColor;
217236
label.onFocused.background = hierarchyStyle.onFocused.background;
218237
label.onFocused.textColor = hierarchyStyle.onFocused.textColor;
238+
label.wordWrap = true;
219239
}
220240
return label;
221241
}
222242
}
223243

244+
public static GUIStyle SelectedLabel
245+
{
246+
get
247+
{
248+
if (selectedLabel == null)
249+
{
250+
selectedLabel = new GUIStyle(GUI.skin.label);
251+
selectedLabel.name = "SelectedLabel";
252+
253+
var hierarchyStyle = GUI.skin.FindStyle("PR Label");
254+
selectedLabel.onNormal.background = hierarchyStyle.onFocused.background;
255+
selectedLabel.onNormal.textColor = hierarchyStyle.onFocused.textColor;
256+
selectedLabel.onFocused.background = hierarchyStyle.onFocused.background;
257+
selectedLabel.onFocused.textColor = hierarchyStyle.onFocused.textColor;
258+
selectedLabel.normal.background = hierarchyStyle.onFocused.background;
259+
selectedLabel.normal.textColor = hierarchyStyle.onFocused.textColor;
260+
selectedLabel.wordWrap = true;
261+
}
262+
return selectedLabel;
263+
}
264+
}
265+
224266
public static GUIStyle HeaderBranchLabelStyle
225267
{
226268
get

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

Lines changed: 50 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ public class GitLockEntry
1717
{
1818
public static GitLockEntry Default = new GitLockEntry(GitLock.Default, GitFileStatus.None);
1919

20-
[SerializeField] private GitLock gitLock;
21-
[SerializeField] private GitFileStatus gitFileStatus;
22-
2320
[NonSerialized] public Texture Icon;
2421
[NonSerialized] public Texture IconBadge;
2522

23+
[SerializeField] private GitLock gitLock;
24+
[SerializeField] private GitFileStatus gitFileStatus;
25+
[SerializeField] private string lockedAt;
26+
2627
public GitLockEntry(GitLock gitLock, GitFileStatus gitFileStatus)
2728
{
2829
this.gitLock = gitLock;
2930
this.gitFileStatus = gitFileStatus;
31+
this.lockedAt = gitLock.LockedAt.ToLocalTime().CreateRelativeTime(DateTimeOffset.Now);
3032
}
3133

3234
public GitLock GitLock
@@ -39,29 +41,23 @@ public GitFileStatus GitFileStatus
3941
get { return gitFileStatus; }
4042
}
4143

42-
public string PrettyTimeString
43-
{
44-
get
45-
{
46-
return gitLock.LockedAt.ToLocalTime().CreateRelativeTime(DateTimeOffset.Now);
47-
}
48-
}
44+
public string LockedAt { get { return lockedAt; } }
4945
}
5046

5147
[Serializable]
5248
class LocksControl
5349
{
54-
[SerializeField] private Vector2 scroll;
55-
[SerializeField] private List<GitLockEntry> gitLockEntries = new List<GitLockEntry>();
56-
[SerializeField] public GitLockEntryDictionary assets = new GitLockEntryDictionary();
57-
[SerializeField] public GitStatusDictionary gitStatusDictionary = new GitStatusDictionary();
58-
5950
[NonSerialized] private Action<GitLock> rightClickNextRender;
6051
[NonSerialized] private GitLockEntry rightClickNextRenderEntry;
61-
[NonSerialized] private GitLockEntry selectedEntry;
6252
[NonSerialized] private int controlId;
6353
[NonSerialized] private UnityEngine.Object lastActivatedObject;
6454

55+
[SerializeField] private Vector2 scroll;
56+
[SerializeField] private List<GitLockEntry> gitLockEntries = new List<GitLockEntry>();
57+
[SerializeField] public GitLockEntryDictionary assets = new GitLockEntryDictionary();
58+
[SerializeField] public GitStatusDictionary gitStatusDictionary = new GitStatusDictionary();
59+
[SerializeField] private GitLockEntry selectedEntry;
60+
6561
public GitLockEntry SelectedEntry
6662
{
6763
get
@@ -72,8 +68,8 @@ public GitLockEntry SelectedEntry
7268
{
7369
selectedEntry = value;
7470

75-
var activeObject = selectedEntry != null
76-
? AssetDatabase.LoadMainAssetAtPath(selectedEntry.GitLock.Path)
71+
var activeObject = selectedEntry != null && selectedEntry.GitLock != GitLock.Default
72+
? AssetDatabase.LoadMainAssetAtPath(selectedEntry.GitLock.Path.MakeAbsolute().RelativeTo(EntryPoint.Environment.UnityProjectPath))
7773
: null;
7874

7975
lastActivatedObject = activeObject;
@@ -115,16 +111,16 @@ public bool Render(Rect containingRect, Action<GitLock> singleClick = null,
115111
var entryRect = new Rect(rect.x, rect.y, rect.width, Styles.LocksEntryHeight);
116112

117113
var shouldRenderEntry = !(entryRect.y > endDisplay || entryRect.yMax < startDisplay);
118-
if (shouldRenderEntry && Event.current.type == EventType.Repaint)
114+
if (shouldRenderEntry)
119115
{
120-
RenderEntry(entryRect, entry);
116+
entryRect = RenderEntry(entryRect, entry);
121117
}
122118

123119
var entryRequiresRepaint =
124120
HandleInput(entryRect, entry, index, singleClick, doubleClick, rightClick);
125121
requiresRepaint = requiresRepaint || entryRequiresRepaint;
126122

127-
rect.y += Styles.LocksEntryHeight;
123+
rect.y += entryRect.height;
128124
}
129125

130126
GUILayout.Space(rect.y - containingRect.y);
@@ -134,30 +130,30 @@ public bool Render(Rect containingRect, Action<GitLock> singleClick = null,
134130
return requiresRepaint;
135131
}
136132

137-
private void RenderEntry(Rect entryRect, GitLockEntry entry)
133+
private Rect RenderEntry(Rect entryRect, GitLockEntry entry)
138134
{
139135
var isSelected = entry == SelectedEntry;
140-
141136
var iconWidth = 32;
142137
var iconHeight = 32;
143-
var iconRect = new Rect(entryRect.x + Styles.BaseSpacing / 2, entryRect.y + (Styles.LocksEntryHeight - iconHeight) / 2, iconWidth, iconHeight);
144-
145138
var iconBadgeWidth = 16;
146-
var iconBasgeHeight = 16;
147-
var iconBadgeRect = new Rect(iconRect.x + iconBadgeWidth, iconRect.y + iconBasgeHeight, iconBadgeWidth, iconBasgeHeight);
148-
149-
var entryBodyX = iconRect.x + iconRect.width + Styles.BaseSpacing / 2;
150-
151-
var pathRect = new Rect(entryBodyX, entryRect.y + Styles.BaseSpacing, entryRect.width - entryBodyX, 11f * 2);
152-
var metaDataRect = new Rect(entryBodyX, pathRect.y + pathRect.height + 2, entryRect.width - entryBodyX, 9f * 2);
153-
139+
var iconBadgeHeight = 16;
154140
var hasKeyboardFocus = GUIUtility.keyboardControl == controlId;
155141

156-
Styles.Label.Draw(entryRect, GUIContent.none, false, false, isSelected, hasKeyboardFocus);
157-
Styles.Label.Draw(iconRect, entry.Icon, false, false, isSelected, hasKeyboardFocus);
158-
Styles.Label.Draw(iconBadgeRect, entry.IconBadge, false, false, isSelected, hasKeyboardFocus);
159-
Styles.LockPathStyle.Draw(pathRect, entry.GitLock.Path, false, false, isSelected, hasKeyboardFocus);
160-
Styles.LockMetaDataStyle.Draw(metaDataRect, string.Format("Locked {0} by {1}", entry.PrettyTimeString, entry.GitLock.Owner.Name), false, false, isSelected, hasKeyboardFocus);
142+
GUILayout.BeginHorizontal(isSelected ? Styles.SelectedArea : Styles.Label);
143+
GUILayout.Label(entry.Icon, GUILayout.Height(iconWidth), GUILayout.Width(iconHeight));
144+
if (Event.current.type == EventType.Repaint)
145+
{
146+
var iconRect = GUILayoutUtility.GetLastRect();
147+
var iconBadgeRect = new Rect(iconRect.x + iconBadgeWidth, iconRect.y + iconBadgeHeight, iconBadgeWidth, iconBadgeHeight);
148+
Styles.Label.Draw(iconBadgeRect, entry.IconBadge, false, false, false, hasKeyboardFocus);
149+
}
150+
GUILayout.BeginVertical();
151+
GUILayout.Label(entry.GitLock.Path, isSelected ? Styles.SelectedLabel : Styles.Label);
152+
GUILayout.Label(string.Format("Locked {0} by {1}", entry.LockedAt, entry.GitLock.Owner.Name), isSelected ? Styles.SelectedLabel : Styles.Label);
153+
GUILayout.EndVertical();
154+
GUILayout.EndHorizontal();
155+
var itemRect = GUILayoutUtility.GetLastRect();
156+
return itemRect;
161157
}
162158

163159
private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLock> singleClick = null,
@@ -214,32 +210,32 @@ private bool HandleInput(Rect rect, GitLockEntry entry, int index, Action<GitLoc
214210

215211
public void Load(List<GitLock> locks, List<GitStatusEntry> gitStatusEntries)
216212
{
217-
var statusEntries = gitStatusEntries.ToDictionary(entry => entry.Path.ToNPath().ToString(SlashMode.Forward), entry => entry.status);
218-
213+
var statusEntries = new Dictionary<string, int>();
214+
for (int i = 0; i < gitStatusEntries.Count; i++)
215+
statusEntries.Add(gitStatusEntries[i].Path.ToNPath().ToString(SlashMode.Forward), i);
219216
var selectedLockId = SelectedEntry != null && SelectedEntry.GitLock != GitLock.Default
220217
? (int?) SelectedEntry.GitLock.ID
221218
: null;
222219

223220
var scrollValue = scroll.y;
224-
225221
var previousCount = gitLockEntries.Count;
226-
227222
var scrollIndex = (int)(scrollValue / Styles.LocksEntryHeight);
228223

229224
assets.Clear();
230225

231-
gitLockEntries = locks.Select(gitLock => {
232-
233-
GitFileStatus gitFileStatus;
234-
if (!statusEntries.TryGetValue(gitLock.Path.ToString(SlashMode.Forward), out gitFileStatus))
226+
gitLockEntries = locks.Select(gitLock =>
227+
{
228+
int index = -1;
229+
GitFileStatus gitFileStatus = GitFileStatus.None;
230+
if (statusEntries.TryGetValue(gitLock.Path.ToString(SlashMode.Forward), out index))
235231
{
236-
gitFileStatus = GitFileStatus.None;
232+
gitFileStatus = gitStatusEntries[index].Status;
237233
}
238234

239235
var gitLockEntry = new GitLockEntry(gitLock, gitFileStatus);
240236
LoadIcon(gitLockEntry, true);
241-
242-
var assetGuid = AssetDatabase.AssetPathToGUID(gitLock.Path);
237+
var path = gitLock.Path.MakeAbsolute().RelativeTo(EntryPoint.Environment.UnityProjectPath);
238+
var assetGuid = AssetDatabase.AssetPathToGUID(path);
243239
if (!string.IsNullOrEmpty(assetGuid))
244240
{
245241
assets.Add(assetGuid, gitLockEntry);
@@ -359,12 +355,10 @@ public bool OnSelectionChange()
359355
if (!LocksControlHasFocus)
360356
{
361357
GitLockEntry gitLockEntry = GitLockEntry.Default;
362-
363358
if (Selection.activeObject != lastActivatedObject)
364359
{
365360
var activeAssetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
366361
var activeAssetGuid = AssetDatabase.AssetPathToGUID(activeAssetPath);
367-
368362
assets.TryGetValue(activeAssetGuid, out gitLockEntry);
369363
}
370364

@@ -383,7 +377,6 @@ class LocksView : Subview
383377
[NonSerialized] private bool currentLocksHasUpdate;
384378

385379
[SerializeField] private LocksControl locksControl;
386-
[SerializeField] private GitLock selectedEntry = GitLock.Default;
387380

388381
[SerializeField] private CacheUpdateEvent lastLocksChangedEvent;
389382
[SerializeField] private CacheUpdateEvent lastStatusEntriesChangedEvent;
@@ -430,11 +423,10 @@ public override void OnGUI()
430423
var rect = GUILayoutUtility.GetLastRect();
431424
if (locksControl != null)
432425
{
433-
var lockControlRect = new Rect(0f, 0f, Position.width, Position.height - rect.height);
426+
var lockControlRect = new Rect(rect.x, rect.y, Position.width, Position.height - rect.height);
434427

435428
var requiresRepaint = locksControl.Render(lockControlRect,
436429
entry => {
437-
selectedEntry = entry;
438430
},
439431
entry => { },
440432
entry => {
@@ -446,11 +438,8 @@ public override void OnGUI()
446438
unlockFile = "Unlock File";
447439
menuFunction = UnlockSelectedEntry;
448440
}
449-
else
450-
{
451-
unlockFile = "Force Unlock File";
452-
menuFunction = ForceUnlockSelectedEntry;
453-
}
441+
unlockFile = "Force Unlock File";
442+
menuFunction = ForceUnlockSelectedEntry;
454443

455444
var menu = new GenericMenu();
456445
menu.AddItem(new GUIContent(unlockFile), false, menuFunction);
@@ -465,14 +454,14 @@ public override void OnGUI()
465454
private void UnlockSelectedEntry()
466455
{
467456
Repository
468-
.ReleaseLock(selectedEntry.Path, false)
457+
.ReleaseLock(locksControl.SelectedEntry.GitLock.Path, false)
469458
.Start();
470459
}
471460

472461
private void ForceUnlockSelectedEntry()
473462
{
474463
Repository
475-
.ReleaseLock(selectedEntry.Path, true)
464+
.ReleaseLock(locksControl.SelectedEntry.GitLock.Path, true)
476465
.Start();
477466
}
478467

@@ -549,7 +538,6 @@ private void MaybeUpdateData()
549538
{
550539
currentStatusEntriesHasUpdate = false;
551540
currentLocksHasUpdate = false;
552-
553541
BuildLocksControl();
554542
}
555543
}
@@ -562,12 +550,6 @@ private void BuildLocksControl()
562550
}
563551

564552
locksControl.Load(lockedFiles, gitStatusEntries);
565-
566-
if (!selectedEntry.Equals(GitLock.Default)
567-
&& selectedEntry.ID != locksControl.SelectedEntry.GitLock.ID)
568-
{
569-
selectedEntry = GitLock.Default;
570-
}
571553
}
572554
public override void OnSelectionChange()
573555
{

0 commit comments

Comments
 (0)