ChangesTabPageView.cs
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Unity.Cloud.Collaborate.Assets;
using Unity.Cloud.Collaborate.Views.Adapters.ListAdapters;
using Unity.Cloud.Collaborate.Components;
using Unity.Cloud.Collaborate.Models.Structures;
using Unity.Cloud.Collaborate.Presenters;
using Unity.Cloud.Collaborate.UserInterface;
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UIElements;
namespace Unity.Cloud.Collaborate.Views
{
[UsedImplicitly]
internal class ChangesTabPageView : TabPageComponent, IChangesView
{
[CanBeNull]
IChangesPresenter m_Presenter;
public const string UssClassName = "changes-tab-page-view";
public const string SearchBarUssClassName = UssClassName + "__search-bar";
public const string EntryGroupsUssClassName = UssClassName + "__entry-groups";
public const string PublishButtonUssClassName = UssClassName + "__publish-button";
public const string TextFieldUssClassName = UssClassName + "__text-field";
public const string ListViewUssClassName = UssClassName + "__list-view";
static readonly string k_LayoutPath = $"{CollaborateWindow.LayoutPath}/{nameof(ChangesTabPageView)}.uxml";
static readonly string k_StylePath = $"{CollaborateWindow.StylePath}/{nameof(ChangesTabPageView)}.uss";
readonly IconTextButton m_PublishButton;
readonly BetterTextField m_RevisionSummaryBox;
readonly SearchBar m_SearchBar;
readonly VisualElement m_EntryGroupsContainer;
bool m_Active;
[CanBeNull]
ConflictedChangeListAdapter m_ConflictedChangeListAdapter;
[CanBeNull]
ToggleableChangeListAdapter m_ToggleableChangeListAdapter;
[CanBeNull]
ChangeEntryGroup m_EntryToggleableGroup;
[CanBeNull]
ChangeEntryGroup m_EntryConflictsGroup;
[CanBeNull]
VisualElement m_ActiveGroup;
public ChangesTabPageView()
{
AddToClassList(UssClassName);
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(k_LayoutPath).CloneTree(this);
styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>(k_StylePath));
// Get the components defined in the style / layout files.
m_SearchBar = this.Q<SearchBar>(className: SearchBarUssClassName);
m_RevisionSummaryBox = this.Q<BetterTextField>(className: TextFieldUssClassName);
m_PublishButton = this.Q<IconTextButton>(className: PublishButtonUssClassName);
m_EntryGroupsContainer = this.Q<VisualElement>(className: EntryGroupsUssClassName);
// Initialize the text strings.
m_PublishButton.Text = StringAssets.publishButton;
m_RevisionSummaryBox.Placeholder = StringAssets.publishSummaryPlaceholder;
}
/// <inheritdoc />
public IChangesPresenter Presenter
{
set
{
m_Presenter = value;
SetupEvents();
// If tab active before presenter has been added, call start once we have it.
if (Active)
{
value.Start();
}
}
}
/// <summary>
/// Setup events to communicate with the presenter. Must be called after presenter is set.
/// </summary>
void SetupEvents()
{
Assert.IsNotNull(m_Presenter, "Invalid changes page state.");
// Set up publish invocation.
m_PublishButton.Clicked += m_Presenter.RequestPublish;
// Send text values to the presenter.
m_SearchBar.Search += m_Presenter.SetSearchQuery;
m_RevisionSummaryBox.OnValueChangedHandler += s => m_Presenter.SetRevisionSummary(s);
}
/// <inheritdoc />
public void SetBusyStatus(bool busy)
{
m_EntryGroupsContainer.SetEnabled(!busy);
m_RevisionSummaryBox.SetEnabled(!busy);
}
/// <inheritdoc />
protected override void SetActive()
{
Assert.IsFalse(m_Active, "The view is already active.");
m_Active = true;
m_Presenter?.Start();
}
/// <inheritdoc />
protected override void SetInactive()
{
Assert.IsTrue(m_Active, "The view is already inactive.");
m_Active = false;
m_Presenter?.Stop();
}
/// <inheritdoc />
public void SetSearchQuery(string query)
{
Assert.IsNotNull(m_Presenter, "Invalid state when setting search query.");
m_SearchBar.SetValueWithoutNotify(query);
var isSearching = m_Presenter.Searching;
if (m_EntryConflictsGroup != null) m_EntryConflictsGroup.Searching = isSearching;
if (m_EntryToggleableGroup != null) m_EntryToggleableGroup.Searching = isSearching;
}
/// <inheritdoc />
public void SetRevisionSummary(string message)
{
m_RevisionSummaryBox.SetValueWithoutNotify(message);
}
/// <inheritdoc />
public void SetConflicts(IReadOnlyList<IChangeEntryData> list)
{
Assert.IsNotNull(m_Presenter, "Invalid state while creating conflict list.");
// Initialise conflicts group
if (m_EntryConflictsGroup == null)
{
var conflictsList = new AdapterListView { name = StringAssets.changeListConflictedList, SelectionType = SelectionType.None };
conflictsList.AddToClassList(ListViewUssClassName);
m_ConflictedChangeListAdapter = new ConflictedChangeListAdapter(m_Presenter);
conflictsList.SetAdapter(m_ConflictedChangeListAdapter);
m_EntryConflictsGroup = new ChangeEntryGroup(conflictsList) { Title = StringAssets.changeListConflictedHeader };
m_EntryConflictsGroup.SetOverflowCallback(m_Presenter.OnClickConflictGroupOverflow);
m_EntryConflictsGroup.Searching = m_Presenter.Searching;
}
Assert.IsTrue(m_ConflictedChangeListAdapter != null && m_EntryConflictsGroup != null, "Invalid state while setting conflicted list.");
// Ensure conflict list is displayed
if (m_ActiveGroup != m_EntryConflictsGroup)
{
m_ActiveGroup?.RemoveFromHierarchy();
m_EntryGroupsContainer.Add(m_EntryConflictsGroup);
m_ActiveGroup = m_EntryConflictsGroup;
}
m_ConflictedChangeListAdapter.List = list;
var count = m_Presenter.ConflictedCount;
m_EntryConflictsGroup.NumberMenuItems = m_Presenter.ConflictGroupOverflowEntryCount;
m_EntryConflictsGroup.SelectedEntryCount = count;
m_EntryConflictsGroup.EntryCount = count;
}
/// <inheritdoc />
public void SetSelectedChanges()
{
Assert.IsNotNull(m_Presenter, "Invalid state while setting selected items from toggleable list.");
if(m_ToggleableChangeListAdapter == null)
{
// we might be Selecting partial changes before the view loads the first time,
// so we just ignore it ....
return;
}
Assert.IsTrue(m_ToggleableChangeListAdapter != null && m_EntryToggleableGroup != null, "Invalid state while setting selected items in toggleable list");
var scrollToIndex = m_ToggleableChangeListAdapter.GetFirstToggledIndex();
m_ToggleableChangeListAdapter.NotifyDataSetChanged();
if (scrollToIndex != -1)
{
scrollToIndex = Math.Min(scrollToIndex, m_ToggleableChangeListAdapter.GetEntryCount() - 1);
m_EntryToggleableGroup.ScrollTo(scrollToIndex);
if(m_ToggleableChangeListAdapter.GetLastBoundElementIndex() < scrollToIndex + 3)
{
// the pool of the list is 14 elements .. but the list actually shows only 12 ..
// so the normal scrollTo call of the list view may stop 1 element short of the selected
// index if the scrolled to index is greater than the currently selected index.
m_EntryToggleableGroup.ScrollTo(scrollToIndex + 3);
}
}
}
/// <inheritdoc />
public void SetChanges(IReadOnlyList<IChangeEntryData> list)
{
Assert.IsNotNull(m_Presenter, "Invalid state while creating toggleable list.");
// Initialise the toggleable list if not already initialised.
if (m_EntryToggleableGroup == null)
{
var toggleableListView = new AdapterListView { SelectionType = SelectionType.None };
toggleableListView.AddToClassList(ListViewUssClassName);
m_ToggleableChangeListAdapter = new ToggleableChangeListAdapter(m_Presenter);
toggleableListView.SetAdapter(m_ToggleableChangeListAdapter);
m_EntryToggleableGroup = new ChangeEntryGroup(toggleableListView)
{ Title = StringAssets.changeListFullHeader };
m_EntryToggleableGroup.SetOverflowCallback(m_Presenter.OnClickGroupOverflow);
m_EntryToggleableGroup.Searching = m_Presenter.Searching;
}
Assert.IsTrue(m_ToggleableChangeListAdapter != null && m_EntryToggleableGroup != null, "Invalid state while setting toggleable list");
// Ensure single list is displayed
if (m_ActiveGroup != m_EntryToggleableGroup)
{
m_ActiveGroup?.RemoveFromHierarchy();
m_EntryGroupsContainer.Add(m_EntryToggleableGroup);
m_ActiveGroup = m_EntryToggleableGroup;
}
// Can use list.Count here since searching hides "All".
m_EntryToggleableGroup.EntryCount = m_Presenter.Searching ? list.Count : m_Presenter.TotalCount;
m_ToggleableChangeListAdapter.List = list;
m_EntryToggleableGroup.NumberMenuItems = m_Presenter.GroupOverflowEntryCount;
m_EntryToggleableGroup.SelectedEntryCount = m_Presenter.ToggledCount;
}
/// <inheritdoc />
public void SetToggledCount(int count)
{
if (m_EntryToggleableGroup != null)
{
m_EntryToggleableGroup.SelectedEntryCount = count;
}
}
/// <inheritdoc />
public void SetPublishEnabled(bool enabled, string reason = null)
{
m_PublishButton.SetEnabled(enabled);
// Disabled elements cannot have a tooltip so apply to a empty/dummy parent instead.
m_PublishButton.parent.tooltip = reason;
}
/// <inheritdoc />
public bool DisplayDialogue(string title, string message, string affirmative)
{
return EditorUtility.DisplayDialog(title, message, affirmative);
}
/// <inheritdoc />
public bool DisplayDialogue(string title, string message, string affirmative, string negative)
{
return EditorUtility.DisplayDialog(title, message, affirmative, negative);
}
[UsedImplicitly]
public new class UxmlFactory : UxmlFactory<ChangesTabPageView> { }
}
}