Initial Commit

This commit is contained in:
Sebastian Cabrera 2021-08-02 05:44:37 -04:00
parent 53eb92e9af
commit 270ab7d11f
15341 changed files with 700234 additions and 0 deletions

View file

@ -0,0 +1,29 @@
using UnityEditor;
using UnityEditor.Collaboration;
using UnityEngine;
using Unity.Cloud.Collaborate.UserInterface;
namespace CollabProxy.UI
{
[InitializeOnLoad]
public class Bootstrap
{
static Bootstrap()
{
var toolbar = new ToolbarButton { Width = 32f };
Toolbar.AddSubToolbar(toolbar);
toolbar.Update();
Collab.ShowHistoryWindow += () =>
{
CollaborateWindow.Init(CollaborateWindow.FocusTarget.History);
};
Collab.ShowChangesWindow += () =>
{
CollaborateWindow.Init(CollaborateWindow.FocusTarget.Changes);
};
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5186430bc91980d48a94ba1f5b29de5c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,254 @@
using System;
using System.Collections.Generic;
using Unity.Cloud.Collaborate.Assets;
using Unity.Cloud.Collaborate.Components;
using Unity.Cloud.Collaborate.Components.Menus;
using Unity.Cloud.Collaborate.Models;
using Unity.Cloud.Collaborate.Models.Api;
using Unity.Cloud.Collaborate.Models.Enums;
using Unity.Cloud.Collaborate.Models.Providers;
using Unity.Cloud.Collaborate.Views;
using Unity.Cloud.Collaborate.Presenters;
using Unity.Cloud.Collaborate.Settings;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.Cloud.Collaborate.UserInterface
{
internal class CollaborateWindow : EditorWindow
{
public const string UssClassName = "main-window";
public const string ContainerUssClassName = UssClassName + "__container";
public const string PackagePath = "Packages/com.unity.collab-proxy";
public const string UserInterfacePath = PackagePath + "/Editor/UserInterface";
public const string ResourcePath = PackagePath + "/Editor/Assets";
public const string LayoutPath = ResourcePath + "/Layouts";
public const string StylePath = ResourcePath + "/Styles";
public const string IconPath = ResourcePath + "/Icons";
public const string TestWindowPath = UserInterfacePath + "/TestWindows";
const string k_LayoutPath = LayoutPath + "/main-window.uxml";
public const string MainStylePath = StylePath + "/styles.uss";
MainPageView m_MainView;
ErrorPageView m_ErrorPageView;
StartPageView m_StartView;
VisualElement m_ViewContainer;
PageComponent m_ActivePage;
ISourceControlProvider m_Provider;
List<IModel> m_Models;
[MenuItem("Window/Collaborate")]
internal static void Init()
{
Init(FocusTarget.None);
}
internal static void Init(FocusTarget focusTarget)
{
var openLocation = CollabSettingsManager.Get(CollabSettings.settingDefaultOpenLocation, fallback: CollabSettings.OpenLocation.Docked);
CollaborateWindow window;
if (openLocation == CollabSettings.OpenLocation.Docked)
{
// Dock next to inspector, if available
var inspectorType = Type.GetType("UnityEditor.InspectorWindow,UnityEditor.dll");
window = GetWindow<CollaborateWindow>(inspectorType);
}
else
{
window = GetWindow<CollaborateWindow>();
}
// Set up window
window.titleContent = new GUIContent("Collaborate");
window.minSize = new Vector2(256, 400);
// Display window
window.Show();
window.Focus();
if (focusTarget != FocusTarget.None)
{
window.RequestFocus(focusTarget);
}
}
void OnDisable()
{
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
AssemblyReloadEvents.beforeAssemblyReload -= OnBeforeAssemblyReload;
AssemblyReloadEvents.afterAssemblyReload -= OnAfterAssemblyReload;
m_Provider.UpdatedProjectStatus -= OnUpdatedProjectStatus;
m_Models.ForEach(m => m.OnStop());
}
void OnEnable()
{
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
var root = rootVisualElement;
root.styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>(MainStylePath));
root.AddToClassList(EditorGUIUtility.isProSkin
? UiConstants.ussDark
: UiConstants.ussLight);
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(k_LayoutPath).CloneTree(root);
m_Provider = new Collab();
m_Provider.UpdatedProjectStatus += OnUpdatedProjectStatus;
m_ViewContainer = root.Q<VisualElement>(className: ContainerUssClassName);
// Create models and configure them.
var mainModel = new MainModel(m_Provider);
var startModel = new StartModel(m_Provider);
m_Models = new List<IModel> { mainModel, startModel };
m_Models.ForEach(m => m.OnStart());
// Get the views and configure them.
m_MainView = new MainPageView();
m_MainView.Presenter = new MainPresenter(m_MainView, mainModel);
m_StartView = new StartPageView();
m_StartView.Presenter = new StartPresenter(m_StartView, startModel);
m_ErrorPageView = new ErrorPageView();
// Add floating dialogue so it can be displayed anywhere in the window.
root.Add(FloatingDialogue.Instance);
OnUpdatedProjectStatus(m_Provider.GetProjectStatus());
}
/// <summary>
/// React to the play mode state changing. When in play mode, disable collab.
/// </summary>
/// <param name="state">Editor play mode state.</param>
void OnPlayModeStateChanged(PlayModeStateChange state)
{
bool enabled;
switch (state)
{
case PlayModeStateChange.EnteredEditMode:
case PlayModeStateChange.ExitingEditMode:
enabled = true;
break;
case PlayModeStateChange.EnteredPlayMode:
case PlayModeStateChange.ExitingPlayMode:
enabled = false;
break;
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
m_ViewContainer.SetEnabled(enabled);
}
/// <summary>
/// Restore window state after assembly reload.
/// </summary>
void OnAfterAssemblyReload()
{
m_Models.ForEach(m => m.RestoreState(WindowCache.Instance));
}
/// <summary>
/// Save state before domain reload.
/// </summary>
void OnBeforeAssemblyReload()
{
m_Models.ForEach(m => m.SaveState(WindowCache.Instance));
WindowCache.Instance.Serialize();
}
/// <summary>
/// Respond to changes in the project status.
/// </summary>
/// <param name="status">New project status.</param>
void OnUpdatedProjectStatus(ProjectStatus status)
{
if (status == ProjectStatus.Ready)
{
UpdateDisplayMode(Display.Main);
}
else
{
WindowCache.Instance.Clear();
m_Models.ForEach(m => m.RestoreState(WindowCache.Instance));
UpdateDisplayMode(Display.Add);
}
}
void RequestFocus(FocusTarget focusTarget)
{
if (m_ActivePage != m_MainView)
{
// Cannot focus changes or history pane if we're not already on mainview
return;
}
if (focusTarget == FocusTarget.Changes)
{
m_MainView.SetTab(MainPageView.ChangesTabIndex);
}
else if (focusTarget == FocusTarget.History)
{
m_MainView.SetTab(MainPageView.HistoryTabIndex);
}
else
{
Debug.LogError("Collab Error: Attempting to focus unknown target.");
}
}
/// <summary>
/// Switch the view displayed in the window.
/// </summary>
/// <param name="newDisplay">Display to switch the window to.</param>
void UpdateDisplayMode(Display newDisplay)
{
m_ActivePage?.RemoveFromHierarchy();
m_ActivePage?.SetActive(false);
m_ViewContainer.Clear();
// Get new page to display
switch (newDisplay)
{
case Display.Add:
m_ActivePage = m_StartView;
break;
case Display.Error:
m_ActivePage = m_ErrorPageView;
break;
case Display.Main:
m_ActivePage = m_MainView;
break;
default:
throw new ArgumentOutOfRangeException();
}
m_ActivePage.SetActive(true);
m_ViewContainer.Add(m_ActivePage);
}
enum Display
{
Add,
Error,
Main
}
public enum FocusTarget
{
None,
History,
Changes
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 95b8172b39a93e64981b6a57ccc39d67
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,24 @@
# Collaborate User Interface
This directory contains the logic to present the collaborate UI.
## Overview
This is the structure of the directory:
```none
<root>
├── TestWindows/
├── Bootstrap.cs
├── CollaborateWindow.cs
├── ToolbarButton.cs
└── WindowCache.cs
```
The `TestWindows/` directory contains testing windows and is not present in release builds.
`Bootstrap.cs` provides the code to initialize the toolbar button on start up.
`CollaborateWindow.cs` is the entry point for the user interface. It spawns a EditorWindow and sets up the UI.
`ToolbarButton.cs` contains the code to create, update, and handle the collaborate button in the toolbar.
`WindowCache.cs` provides a collection of fields that are preserved during domain reload and editor restart. Some
examples are the the current commit message and the currently selected items for the simple UI/UX. Any data that would
impact UX if lost during reload or exit, should be saved in here.

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 663354cc746bed14db99b8593b1f6ab5
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,216 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Collaboration;
using UnityEditor.Connect;
using UnityEngine;
namespace Unity.Cloud.Collaborate.UserInterface
{
internal class ToolbarButton : SubToolbar
{
protected enum ToolbarButtonState
{
NeedToEnableCollab,
UpToDate,
Conflict,
OperationError,
ServerHasChanges,
FilesToPush,
InProgress,
Disabled,
Offline
}
ToolbarButtonState m_CurrentState;
string m_ErrorMessage;
readonly Dictionary<ToolbarButtonState, GUIContent> m_IconCache = new Dictionary<ToolbarButtonState, GUIContent>();
ButtonWithAnimatedIconRotation m_CollabButton;
public ToolbarButton()
{
Collab.instance.StateChanged += OnCollabStateChanged;
UnityConnect.instance.StateChanged += OnUnityConnectStateChanged;
UnityConnect.instance.UserStateChanged += OnUnityConnectUserStateChanged;
}
~ToolbarButton()
{
Collab.instance.StateChanged -= OnCollabStateChanged;
UnityConnect.instance.StateChanged -= OnUnityConnectStateChanged;
UnityConnect.instance.UserStateChanged -= OnUnityConnectUserStateChanged;
}
void OnUnityConnectUserStateChanged(UserInfo state)
{
Update();
}
void OnUnityConnectStateChanged(ConnectInfo state)
{
Update();
}
void OnCollabStateChanged(CollabInfo info)
{
Update();
}
[CanBeNull]
static Texture LoadIcon([NotNull] string iconName)
{
var hidpi = EditorGUIUtility.pixelsPerPoint > 1f ? "@2x" : string.Empty;
return AssetDatabase.LoadAssetAtPath<Texture>($"{CollaborateWindow.IconPath}/{iconName}-{(EditorGUIUtility.isProSkin ? "dark" : "light")}{hidpi}.png");
}
[NotNull]
GUIContent GetIconForState()
{
// Get cached icon, or construct it.
if (!m_IconCache.TryGetValue(m_CurrentState, out var content))
{
string iconName;
string tooltip;
switch (m_CurrentState)
{
case ToolbarButtonState.NeedToEnableCollab:
iconName = "collaborate";
tooltip = "You need to enable collab.";
break;
case ToolbarButtonState.UpToDate:
iconName = "collaborate";
tooltip = "You are up to date.";
break;
case ToolbarButtonState.Conflict:
iconName = "collaborate-error";
tooltip = "Please fix your conflicts prior to publishing.";
break;
case ToolbarButtonState.OperationError:
iconName = "collaborate-error";
tooltip = "Last operation failed. Please retry later.";
break;
case ToolbarButtonState.ServerHasChanges:
iconName = "collaborate-incoming";
tooltip = "Please update, there are server changes.";
break;
case ToolbarButtonState.FilesToPush:
iconName = "collaborate-available-changes";
tooltip = "You have files to publish.";
break;
case ToolbarButtonState.InProgress:
iconName = "collaborate-progress";
tooltip = "Operation in progress.";
break;
case ToolbarButtonState.Disabled:
iconName = "collaborate";
tooltip = "Collab is disabled.";
break;
case ToolbarButtonState.Offline:
iconName = "collaborate-offline";
tooltip = "Please check your network connection.";
break;
default:
throw new ArgumentOutOfRangeException();
}
// Create icon with tooltip and cache.
content = new GUIContent(LoadIcon(iconName), $"Collaborate • {tooltip}");
m_IconCache[m_CurrentState] = content;
}
// Add error message tooltip if there's a message.
var icon = new GUIContent(content);
if (!string.IsNullOrEmpty(m_ErrorMessage))
{
icon.tooltip = $"Collaborate • {m_ErrorMessage}";
}
return icon;
}
public override void OnGUI(Rect rect)
{
GUIStyle collabButtonStyle = "AppCommand";
var disable = EditorApplication.isPlaying;
using (new EditorGUI.DisabledScope(disable))
{
var icon = GetIconForState();
EditorGUIUtility.SetIconSize(new Vector2(16, 16));
if (GUI.Button(rect, icon, collabButtonStyle))
{
CollaborateWindow.Init();
}
EditorGUIUtility.SetIconSize(Vector2.zero);
}
}
public void Update()
{
var currentState = GetCurrentState();
if (m_CurrentState == currentState) return;
m_CurrentState = currentState;
Toolbar.RepaintToolbar();
}
protected virtual ToolbarButtonState GetCurrentState()
{
var currentState = ToolbarButtonState.UpToDate;
var networkAvailable = UnityConnect.instance.connectInfo.online && UnityConnect.instance.connectInfo.loggedIn;
m_ErrorMessage = string.Empty;
if (UnityConnect.instance.isDisableCollabWindow)
{
currentState = ToolbarButtonState.Disabled;
}
else if (networkAvailable)
{
var collab = Collab.instance;
var currentInfo = collab.collabInfo;
if (!currentInfo.ready)
{
currentState = ToolbarButtonState.InProgress;
}
else if (collab.GetError(UnityConnect.UnityErrorFilter.ByContext | UnityConnect.UnityErrorFilter.ByChild, out var errInfo) &&
errInfo.priority <= (int)UnityConnect.UnityErrorPriority.Error)
{
currentState = ToolbarButtonState.OperationError;
m_ErrorMessage = errInfo.shortMsg;
}
else if (currentInfo.inProgress)
{
currentState = ToolbarButtonState.InProgress;
}
else
{
var collabEnabled = Collab.instance.IsCollabEnabledForCurrentProject();
if (UnityConnect.instance.projectInfo.projectBound == false || !collabEnabled)
{
currentState = ToolbarButtonState.NeedToEnableCollab;
}
else if (currentInfo.update)
{
currentState = ToolbarButtonState.ServerHasChanges;
}
else if (currentInfo.conflict)
{
currentState = ToolbarButtonState.Conflict;
}
else if (currentInfo.publish)
{
currentState = ToolbarButtonState.FilesToPush;
}
}
}
else
{
currentState = ToolbarButtonState.Offline;
}
return currentState;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 100f9184f13dd20448c29dc74f30dce1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using Unity.Cloud.Collaborate.Common;
using UnityEngine;
using UnityEngine.Serialization;
namespace Unity.Cloud.Collaborate.UserInterface
{
internal interface IWindowCache
{
void Clear();
SelectedItemsDictionary SimpleSelectedItems { get; set; }
string RevisionSummary { get; set; }
string ChangesSearchValue { get; set; }
string SelectedHistoryRevision { get; set; }
int HistoryPageNumber { get; set; }
int TabIndex { get; set; }
}
[Location("Cache/Window.yml", LocationAttribute.Location.LibraryFolder)]
internal class WindowCache : ScriptableObjectSingleton<WindowCache>, IWindowCache
{
public event Action<IWindowCache> BeforeSerialize;
public void Serialize()
{
BeforeSerialize?.Invoke(this);
Save();
}
public void Clear()
{
SimpleSelectedItems = default;
RevisionSummary = default;
ChangesSearchValue = default;
SelectedHistoryRevision = default;
HistoryPageNumber = default;
TabIndex = default;
}
SelectedItemsDictionary IWindowCache.SimpleSelectedItems
{
get => SimpleSelectedItems;
set => SimpleSelectedItems = value;
}
string IWindowCache.RevisionSummary
{
get => RevisionSummary;
set => RevisionSummary = value;
}
string IWindowCache.ChangesSearchValue
{
get => ChangesSearchValue;
set => ChangesSearchValue = value;
}
string IWindowCache.SelectedHistoryRevision
{
get => SelectedHistoryRevision;
set => SelectedHistoryRevision = value;
}
int IWindowCache.HistoryPageNumber
{
get => HistoryPageNumber;
set => HistoryPageNumber = value;
}
int IWindowCache.TabIndex
{
get => TabIndex;
set => TabIndex = value;
}
[SerializeField]
public SelectedItemsDictionary SimpleSelectedItems = new SelectedItemsDictionary();
[FormerlySerializedAs("CommitMessage")]
[SerializeField]
public string RevisionSummary;
[SerializeField]
public string ChangesSearchValue;
[SerializeField]
public string SelectedHistoryRevision;
[SerializeField]
public int HistoryPageNumber;
[SerializeField]
public int TabIndex;
}
[Serializable]
internal class SelectedItemsDictionary : SerializableDictionary<string, bool>
{
public SelectedItemsDictionary() { }
public SelectedItemsDictionary(IDictionary<string, bool> dictionary) : base(dictionary) { }
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ffe70aba2e7074048aca5de573b64a59
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: