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

Commit cead15a

Browse files
Fixing the reactions to events in the UI
1 parent c09e081 commit cead15a

File tree

2 files changed

+66
-64
lines changed

2 files changed

+66
-64
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class BranchesView : Subview
4444
[NonSerialized] private BranchTreeNode newNodeSelection;
4545
[NonSerialized] private BranchesMode targetMode;
4646
[NonSerialized] private bool favoritesHasChanged;
47-
[NonSerialized] private bool branchesHasChanged;
4847

4948
[SerializeField] private BranchTreeNode activeBranchNode;
5049
[SerializeField] private BranchTreeNode localRoot;
@@ -66,7 +65,7 @@ public override void OnEnable()
6665
base.OnEnable();
6766
AttachHandlers(Repository);
6867
favoritesHasChanged = true;
69-
branchesHasChanged = true;
68+
Refresh();
7069
}
7170

7271
public override void OnDisable()
@@ -88,12 +87,6 @@ private void MaybeUpdateData()
8887
favoritesList = Manager.LocalSettings.Get(FavoritesSetting, new List<string>());
8988
favoritesHasChanged = false;
9089
}
91-
92-
if (branchesHasChanged)
93-
{
94-
RunUpdateBranchesOnMainThread();
95-
branchesHasChanged = false;
96-
}
9790
}
9891

9992
public override void OnRepositoryChanged(IRepository oldRepository)
@@ -127,6 +120,12 @@ private void HandleRepositoryBranchChangeEvent(string obj)
127120
RunUpdateBranchesOnMainThread();
128121
}
129122

123+
public override void Refresh()
124+
{
125+
base.Refresh();
126+
UpdateBranches();
127+
}
128+
130129
private void RunUpdateBranchesOnMainThread()
131130
{
132131
new ActionTask(TaskManager.Token, _ => UpdateBranches())
@@ -537,7 +536,7 @@ private void OnButtonBarGUI()
537536
.FinallyInUI((success, e) => {
538537
if (success)
539538
{
540-
Refresh();
539+
Redraw();
541540
}
542541
else
543542
{
@@ -658,7 +657,7 @@ private void OnTreeNodeGUI(BranchTreeNode node)
658657
{
659658
if (success)
660659
{
661-
Refresh();
660+
Redraw();
662661
}
663662
else
664663
{
@@ -694,7 +693,7 @@ private void OnTreeNodeGUI(BranchTreeNode node)
694693
{
695694
if (success)
696695
{
697-
Refresh();
696+
Redraw();
698697
}
699698
else
700699
{

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

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class HistoryView : Subview
4343
[NonSerialized] private float scrollOffset;
4444
[NonSerialized] private DateTimeOffset scrollTime = DateTimeOffset.Now;
4545
[NonSerialized] private int selectionIndex;
46-
[NonSerialized] private bool updated = true;
46+
[NonSerialized] private bool logHasChanged;
4747
[NonSerialized] private bool useScrollTime;
4848
[NonSerialized] private bool isBusy;
4949

@@ -74,6 +74,7 @@ public override void OnEnable()
7474
{
7575
base.OnEnable();
7676
AttachHandlers(Repository);
77+
UpdateLog();
7778
}
7879

7980
public override void OnDisable()
@@ -94,21 +95,13 @@ public override void OnRepositoryChanged(IRepository oldRepository)
9495

9596
DetachHandlers(oldRepository);
9697
AttachHandlers(Repository);
97-
Refresh();
98-
}
99-
100-
public override void Refresh()
101-
{
102-
base.Refresh();
103-
RefreshLog();
10498
}
10599

106100
public override void OnSelectionChange()
107101
{
108102
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(Selection.activeObject)))
109103
{
110104
historyTarget = Selection.activeObject;
111-
Refresh();
112105
}
113106
}
114107

@@ -123,8 +116,8 @@ private void AttachHandlers(IRepository repository)
123116
return;
124117
repository.OnLocalBranchChanged += Refresh;
125118
repository.OnStatusChanged += UpdateStatusOnMainThread;
126-
repository.OnCurrentBranchChanged += s => Refresh();
127-
repository.OnCurrentRemoteChanged += s => Refresh();
119+
repository.OnCurrentBranchChanged += Repository_OnCurrentBranchChanged();
120+
repository.OnCurrentRemoteChanged += Repository_OnCurrentRemoteChanged();
128121
}
129122

130123
private void DetachHandlers(IRepository repository)
@@ -133,8 +126,18 @@ private void DetachHandlers(IRepository repository)
133126
return;
134127
repository.OnLocalBranchChanged -= Refresh;
135128
repository.OnStatusChanged -= UpdateStatusOnMainThread;
136-
repository.OnCurrentBranchChanged -= s => Refresh();
137-
repository.OnCurrentRemoteChanged -= s => Refresh();
129+
repository.OnCurrentBranchChanged -= Repository_OnCurrentBranchChanged();
130+
repository.OnCurrentRemoteChanged -= Repository_OnCurrentRemoteChanged();
131+
}
132+
133+
private Action<string> Repository_OnCurrentRemoteChanged()
134+
{
135+
return s => Refresh();
136+
}
137+
138+
private Action<string> Repository_OnCurrentBranchChanged()
139+
{
140+
return s => Refresh();
138141
}
139142

140143
private void UpdateStatusOnMainThread(GitStatus status)
@@ -149,68 +152,68 @@ private void UpdateStatus(GitStatus status)
149152
statusBehind = status.Behind;
150153
}
151154

152-
private void RefreshLog()
155+
private void UpdateLog()
153156
{
154157
if (Repository != null)
155158
{
156159
Repository.Log().ThenInUI((success, log) => {
157-
if (success) OnLogUpdate(log);
160+
if (success)
161+
{
162+
Logger.Trace("OnLogUpdate");
163+
GitLogCache.Instance.Log = log;
164+
logHasChanged = true;
165+
Redraw();
166+
}
158167
}).Start();
159168
}
160169
}
161170

162-
private void OnLogUpdate(List<GitLogEntry> entries)
163-
{
164-
Logger.Trace("OnLogUpdate");
165-
GitLogCache.Instance.Log = entries;
166-
updated = true;
167-
Redraw();
168-
}
169-
170171
private void MaybeUpdateData()
171172
{
172173
isPublished = Repository != null && Repository.CurrentRemote.HasValue;
173174
currentRemote = isPublished ? Repository.CurrentRemote.Value.Name : "placeholder";
174175

175-
if (!updated)
176-
return;
177-
updated = false;
176+
if (logHasChanged)
177+
{
178+
logHasChanged = false;
178179

179-
history = GitLogCache.Instance.Log;
180+
history = GitLogCache.Instance.Log;
180181

181-
if (history.Any())
182-
{
183-
// Make sure that scroll as much as possible focuses the same time period in the new entry list
184-
if (useScrollTime)
182+
if (history.Any())
185183
{
186-
var closestIndex = -1;
187-
double closestDifference = Mathf.Infinity;
188-
for (var index = 0; index < history.Count; ++index)
184+
// Make sure that scroll as much as possible focuses the same time period in the new entry list
185+
if (useScrollTime)
189186
{
190-
var diff = Math.Abs((history[index].Time - scrollTime).TotalSeconds);
191-
if (diff < closestDifference)
187+
var closestIndex = -1;
188+
double closestDifference = Mathf.Infinity;
189+
for (var index = 0; index < history.Count; ++index)
192190
{
193-
closestDifference = diff;
194-
closestIndex = index;
191+
var diff = Math.Abs((history[index].Time - scrollTime).TotalSeconds);
192+
if (diff < closestDifference)
193+
{
194+
closestDifference = diff;
195+
closestIndex = index;
196+
}
195197
}
198+
199+
ScrollTo(closestIndex, scrollOffset);
196200
}
197201

198-
ScrollTo(closestIndex, scrollOffset);
202+
CullHistory();
199203
}
200204

201-
CullHistory();
202-
}
203-
204-
// Restore selection index or clear it
205-
newSelectionIndex = -1;
206-
if (!string.IsNullOrEmpty(selectionID))
207-
{
208-
selectionIndex =
209-
Enumerable.Range(1, history.Count + 1).FirstOrDefault(index => history[index - 1].CommitID.Equals(selectionID)) - 1;
210-
211-
if (selectionIndex < 0)
205+
// Restore selection index or clear it
206+
newSelectionIndex = -1;
207+
if (!string.IsNullOrEmpty(selectionID))
212208
{
213-
selectionID = string.Empty;
209+
selectionIndex = Enumerable.Range(1, history.Count + 1)
210+
.FirstOrDefault(
211+
index => history[index - 1].CommitID.Equals(selectionID)) - 1;
212+
213+
if (selectionIndex < 0)
214+
{
215+
selectionID = string.Empty;
216+
}
214217
}
215218
}
216219
}
@@ -305,7 +308,7 @@ public void OnEmbeddedGUI()
305308
// Only update time scroll
306309
var lastScroll = scroll;
307310
scroll = GUILayout.BeginScrollView(scroll);
308-
if (lastScroll != scroll && !updated)
311+
if (lastScroll != scroll && !logHasChanged)
309312
{
310313
scrollTime = history[historyStartIndex].Time;
311314
scrollOffset = scroll.y - historyStartIndex * EntryHeight;
@@ -411,7 +414,7 @@ public void OnEmbeddedGUI()
411414
if (Event.current.type == EventType.Repaint)
412415
{
413416
CullHistory();
414-
updated = false;
417+
logHasChanged = false;
415418

416419
if (newSelectionIndex >= 0 || newSelectionIndex == -2)
417420
{

0 commit comments

Comments
 (0)