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

Commit 0938eda

Browse files
Organizing HistoryView
1 parent 3eb968c commit 0938eda

File tree

1 file changed

+143
-147
lines changed

1 file changed

+143
-147
lines changed

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

Lines changed: 143 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class HistoryView : Subview
3030
private const int HistoryExtraItemCount = 10;
3131
private const float MaxChangelistHeightRatio = .2f;
3232

33+
[NonSerialized] private bool currentLogHasUpdate;
34+
[NonSerialized] private bool currentRemoteHasUpdate;
35+
[NonSerialized] private bool currentStatusHasUpdate;
3336
[NonSerialized] private int historyStartIndex;
3437
[NonSerialized] private int historyStopIndex;
3538
[NonSerialized] private int listID;
@@ -39,26 +42,19 @@ class HistoryView : Subview
3942
[NonSerialized] private int selectionIndex;
4043
[NonSerialized] private bool useScrollTime;
4144

42-
[SerializeField] private Vector2 detailsScroll;
43-
[SerializeField] private Vector2 scroll;
44-
[SerializeField] private string selectionID;
45-
[SerializeField] private int statusAhead;
46-
[SerializeField] private int statusBehind;
47-
4845
[SerializeField] private ChangesetTreeView changesetTree = new ChangesetTreeView();
49-
[SerializeField] private List<GitLogEntry> history = new List<GitLogEntry>();
5046
[SerializeField] private string currentRemoteName;
51-
[SerializeField] private bool hasRemote;
47+
[SerializeField] private Vector2 detailsScroll;
5248
[SerializeField] private bool hasItemsToCommit;
53-
49+
[SerializeField] private bool hasRemote;
50+
[SerializeField] private List<GitLogEntry> history = new List<GitLogEntry>();
5451
[SerializeField] private CacheUpdateEvent lastCurrentRemoteChangedEvent;
55-
[NonSerialized] private bool currentRemoteHasUpdate;
56-
57-
[SerializeField] private CacheUpdateEvent lastStatusChangedEvent;
58-
[NonSerialized] private bool currentStatusHasUpdate;
59-
6052
[SerializeField] private CacheUpdateEvent lastLogChangedEvent;
61-
[NonSerialized] private bool currentLogHasUpdate;
53+
[SerializeField] private CacheUpdateEvent lastStatusChangedEvent;
54+
[SerializeField] private Vector2 scroll;
55+
[SerializeField] private string selectionID;
56+
[SerializeField] private int statusAhead;
57+
[SerializeField] private int statusBehind;
6258

6359
public override void InitializeView(IView parent)
6460
{
@@ -100,138 +96,6 @@ public override void OnGUI()
10096
OnEmbeddedGUI();
10197
}
10298

103-
private void RepositoryOnStatusChanged(CacheUpdateEvent cacheUpdateEvent)
104-
{
105-
if (!lastStatusChangedEvent.Equals(cacheUpdateEvent))
106-
{
107-
new ActionTask(TaskManager.Token, () =>
108-
{
109-
lastStatusChangedEvent = cacheUpdateEvent;
110-
currentStatusHasUpdate = true;
111-
Redraw();
112-
})
113-
{ Affinity = TaskAffinity.UI }.Start();
114-
}
115-
}
116-
117-
private void RepositoryOnLogChanged(CacheUpdateEvent cacheUpdateEvent)
118-
{
119-
if (!lastLogChangedEvent.Equals(cacheUpdateEvent))
120-
{
121-
new ActionTask(TaskManager.Token, () =>
122-
{
123-
lastLogChangedEvent = cacheUpdateEvent;
124-
currentLogHasUpdate = true;
125-
Redraw();
126-
})
127-
{ Affinity = TaskAffinity.UI }.Start();
128-
}
129-
}
130-
131-
private void RepositoryOnCurrentRemoteChanged(CacheUpdateEvent cacheUpdateEvent)
132-
{
133-
if (!lastCurrentRemoteChangedEvent.Equals(cacheUpdateEvent))
134-
{
135-
new ActionTask(TaskManager.Token, () =>
136-
{
137-
lastCurrentRemoteChangedEvent = cacheUpdateEvent;
138-
currentRemoteHasUpdate = true;
139-
Redraw();
140-
})
141-
{ Affinity = TaskAffinity.UI }.Start();
142-
}
143-
}
144-
145-
private void AttachHandlers(IRepository repository)
146-
{
147-
if (repository == null)
148-
return;
149-
150-
repository.StatusChanged += RepositoryOnStatusChanged;
151-
repository.LogChanged += RepositoryOnLogChanged;
152-
repository.CurrentRemoteChanged += RepositoryOnCurrentRemoteChanged;
153-
}
154-
155-
private void DetachHandlers(IRepository repository)
156-
{
157-
if (repository == null)
158-
return;
159-
160-
repository.StatusChanged -= RepositoryOnStatusChanged;
161-
repository.LogChanged -= RepositoryOnLogChanged;
162-
repository.CurrentRemoteChanged -= RepositoryOnCurrentRemoteChanged;
163-
}
164-
165-
private void MaybeUpdateData()
166-
{
167-
if (Repository == null)
168-
return;
169-
170-
if (currentRemoteHasUpdate)
171-
{
172-
currentRemoteHasUpdate = false;
173-
174-
var currentRemote = Repository.CurrentRemote;
175-
hasRemote = currentRemote.HasValue;
176-
currentRemoteName = hasRemote ? currentRemote.Value.Name : "placeholder";
177-
}
178-
179-
if (currentStatusHasUpdate)
180-
{
181-
currentStatusHasUpdate = false;
182-
183-
var currentStatus = Repository.CurrentStatus;
184-
statusAhead = currentStatus.Ahead;
185-
statusBehind = currentStatus.Behind;
186-
hasItemsToCommit = currentStatus.Entries != null &&
187-
currentStatus.GetEntriesExcludingIgnoredAndUntracked().Any();
188-
}
189-
190-
if (currentLogHasUpdate)
191-
{
192-
currentLogHasUpdate = false;
193-
194-
history = Repository.CurrentLog;
195-
196-
if (history.Any())
197-
{
198-
// Make sure that scroll as much as possible focuses the same time period in the new entry list
199-
if (useScrollTime)
200-
{
201-
var closestIndex = -1;
202-
double closestDifference = Mathf.Infinity;
203-
for (var index = 0; index < history.Count; ++index)
204-
{
205-
var diff = Math.Abs((history[index].Time - scrollTime).TotalSeconds);
206-
if (diff < closestDifference)
207-
{
208-
closestDifference = diff;
209-
closestIndex = index;
210-
}
211-
}
212-
213-
ScrollTo(closestIndex, scrollOffset);
214-
}
215-
216-
CullHistory();
217-
}
218-
219-
// Restore selection index or clear it
220-
newSelectionIndex = -1;
221-
if (!string.IsNullOrEmpty(selectionID))
222-
{
223-
selectionIndex = Enumerable.Range(1, history.Count + 1)
224-
.FirstOrDefault(
225-
index => history[index - 1].CommitID.Equals(selectionID)) - 1;
226-
227-
if (selectionIndex < 0)
228-
{
229-
selectionID = string.Empty;
230-
}
231-
}
232-
}
233-
}
234-
23599
public void OnEmbeddedGUI()
236100
{
237101
// History toolbar
@@ -428,6 +292,138 @@ public void OnEmbeddedGUI()
428292
}
429293
}
430294

295+
private void RepositoryOnStatusChanged(CacheUpdateEvent cacheUpdateEvent)
296+
{
297+
if (!lastStatusChangedEvent.Equals(cacheUpdateEvent))
298+
{
299+
new ActionTask(TaskManager.Token, () => {
300+
lastStatusChangedEvent = cacheUpdateEvent;
301+
currentStatusHasUpdate = true;
302+
Redraw();
303+
}) { Affinity = TaskAffinity.UI }.Start();
304+
}
305+
}
306+
307+
private void RepositoryOnLogChanged(CacheUpdateEvent cacheUpdateEvent)
308+
{
309+
if (!lastLogChangedEvent.Equals(cacheUpdateEvent))
310+
{
311+
new ActionTask(TaskManager.Token, () => {
312+
lastLogChangedEvent = cacheUpdateEvent;
313+
currentLogHasUpdate = true;
314+
Redraw();
315+
}) { Affinity = TaskAffinity.UI }.Start();
316+
}
317+
}
318+
319+
private void RepositoryOnCurrentRemoteChanged(CacheUpdateEvent cacheUpdateEvent)
320+
{
321+
if (!lastCurrentRemoteChangedEvent.Equals(cacheUpdateEvent))
322+
{
323+
new ActionTask(TaskManager.Token, () => {
324+
lastCurrentRemoteChangedEvent = cacheUpdateEvent;
325+
currentRemoteHasUpdate = true;
326+
Redraw();
327+
}) { Affinity = TaskAffinity.UI }.Start();
328+
}
329+
}
330+
331+
private void AttachHandlers(IRepository repository)
332+
{
333+
if (repository == null)
334+
{
335+
return;
336+
}
337+
338+
repository.StatusChanged += RepositoryOnStatusChanged;
339+
repository.LogChanged += RepositoryOnLogChanged;
340+
repository.CurrentRemoteChanged += RepositoryOnCurrentRemoteChanged;
341+
}
342+
343+
private void DetachHandlers(IRepository repository)
344+
{
345+
if (repository == null)
346+
{
347+
return;
348+
}
349+
350+
repository.StatusChanged -= RepositoryOnStatusChanged;
351+
repository.LogChanged -= RepositoryOnLogChanged;
352+
repository.CurrentRemoteChanged -= RepositoryOnCurrentRemoteChanged;
353+
}
354+
355+
private void MaybeUpdateData()
356+
{
357+
if (Repository == null)
358+
{
359+
return;
360+
}
361+
362+
if (currentRemoteHasUpdate)
363+
{
364+
currentRemoteHasUpdate = false;
365+
366+
var currentRemote = Repository.CurrentRemote;
367+
hasRemote = currentRemote.HasValue;
368+
currentRemoteName = hasRemote ? currentRemote.Value.Name : "placeholder";
369+
}
370+
371+
if (currentStatusHasUpdate)
372+
{
373+
currentStatusHasUpdate = false;
374+
375+
var currentStatus = Repository.CurrentStatus;
376+
statusAhead = currentStatus.Ahead;
377+
statusBehind = currentStatus.Behind;
378+
hasItemsToCommit = currentStatus.Entries != null &&
379+
currentStatus.GetEntriesExcludingIgnoredAndUntracked().Any();
380+
}
381+
382+
if (currentLogHasUpdate)
383+
{
384+
currentLogHasUpdate = false;
385+
386+
history = Repository.CurrentLog;
387+
388+
if (history.Any())
389+
{
390+
// Make sure that scroll as much as possible focuses the same time period in the new entry list
391+
if (useScrollTime)
392+
{
393+
var closestIndex = -1;
394+
double closestDifference = Mathf.Infinity;
395+
for (var index = 0; index < history.Count; ++index)
396+
{
397+
var diff = Math.Abs((history[index].Time - scrollTime).TotalSeconds);
398+
if (diff < closestDifference)
399+
{
400+
closestDifference = diff;
401+
closestIndex = index;
402+
}
403+
}
404+
405+
ScrollTo(closestIndex, scrollOffset);
406+
}
407+
408+
CullHistory();
409+
}
410+
411+
// Restore selection index or clear it
412+
newSelectionIndex = -1;
413+
if (!string.IsNullOrEmpty(selectionID))
414+
{
415+
selectionIndex = Enumerable.Range(1, history.Count + 1)
416+
.FirstOrDefault(
417+
index => history[index - 1].CommitID.Equals(selectionID)) - 1;
418+
419+
if (selectionIndex < 0)
420+
{
421+
selectionID = string.Empty;
422+
}
423+
}
424+
}
425+
}
426+
431427
private void ScrollTo(int index, float offset = 0f)
432428
{
433429
scroll.Set(scroll.x, EntryHeight * index + offset);

0 commit comments

Comments
 (0)