A game about forced loneliness, made by TACStudios
1using System.Linq;
2
3using UnityEngine;
4using UnityEngine.UIElements;
5
6using Codice.Client.Common;
7using Codice.Client.Common.EventTracking;
8using Codice.CM.Common;
9using PlasticGui;
10using Unity.PlasticSCM.Editor.Tool;
11using Unity.PlasticSCM.Editor.UI;
12using Unity.PlasticSCM.Editor.UI.UIElements;
13using Unity.PlasticSCM.Editor.Views.Welcome;
14
15namespace Unity.PlasticSCM.Editor.Views
16{
17 internal class DownloadPlasticExeDialog : PlasticDialog, DownloadAndInstallOperation.INotify
18 {
19 internal bool IsPlasticInstalling { get { return mIsPlasticInstalling; } }
20
21 protected override Rect DefaultRect
22 {
23 get
24 {
25 var baseRect = base.DefaultRect;
26 return new Rect(baseRect.x, baseRect.y, DIALOG_WIDTH, DIALOG_HEIGHT);
27 }
28 }
29
30 internal static ProgressControlsForDialogs.Data Show(
31 RepositorySpec repSpec,
32 bool isGluonMode,
33 string installCloudFrom,
34 string installEnterpriseFrom,
35 string cancelInstallFrom,
36 ProgressControlsForDialogs.Data progressData)
37 {
38 DownloadPlasticExeDialog dialog;
39
40 if (HasOpenInstances<DownloadPlasticExeDialog>())
41 {
42 dialog = GetWindow<DownloadPlasticExeDialog>(true);
43 }
44 else
45 {
46 dialog = Create(repSpec, isGluonMode, installCloudFrom, installEnterpriseFrom, cancelInstallFrom, progressData);
47
48 dialog.RunUtility(focusedWindow);
49 }
50
51 return progressData != null ? progressData : dialog.mProgressControls.ProgressData;
52 }
53
54 static DownloadPlasticExeDialog Create(
55 RepositorySpec repSpec,
56 bool isGluonMode,
57 string installCloudFrom,
58 string installEnterpriseFrom,
59 string cancelInstallFrom,
60 ProgressControlsForDialogs.Data progressData)
61 {
62 var instance = CreateInstance<DownloadPlasticExeDialog>();
63 instance.SetSizeToContent(SizeToContent.Manual);
64 instance.mRepSpec = repSpec;
65 instance.mInstallCloudFrom = installCloudFrom;
66 instance.mInstallEnterpriseFrom = installEnterpriseFrom;
67 instance.mCancelInstallFrom = cancelInstallFrom;
68 instance.mIsGluonMode = isGluonMode;
69 instance.mProgressData = progressData;
70 instance.mInstallerFile = GetInstallerTmpFileName.ForPlatform();
71 instance.mIsCloudEdition = EditionToken.IsCloudEdition();
72 instance.mTitle = PlasticLocalization.Name.UnityVersionControl.GetString();
73 return instance;
74 }
75
76 protected override string GetTitle()
77 {
78 return mTitle;
79 }
80
81 void OnEnable()
82 {
83 BuildComponents();
84 }
85
86 void OnDestroy()
87 {
88 Dispose();
89 }
90
91 protected override void OnModalGUI()
92 {
93 if (InstallationFinished())
94 {
95 mMessageLabel.text = PlasticLocalization.Name.UnityVersionControlInstalled.GetString();
96 mCancelButton.text = PlasticLocalization.Name.CloseButton.GetString();
97 mConfirmMessageLabel.Collapse();
98 mDownloadButton.Collapse();
99
100 maxSize = new Vector2(DIALOG_WIDTH, REDUCED_DIALOG_HEIGHT);
101 minSize = maxSize;
102
103 return;
104 }
105
106 if (mProgressData != null)
107 {
108 if (mProgressControlsContainer.Children().OfType<ProgressControlsForDialogs>().Any())
109 {
110 mProgressControlsContainer.RemoveAt(0);
111 mProgressLabel = new Label(GetMessageFromProgressData(mProgressData));
112 mProgressControlsContainer.Add(mProgressLabel);
113 }
114
115 mProgressLabel.text = GetMessageFromProgressData(mProgressData);
116 UpdateButtonsStatuses();
117 }
118 }
119
120 void CancelButton_Clicked()
121 {
122 if (!IsExeAvailable.ForMode(mIsGluonMode))
123 TrackFeatureUseEvent.For(mRepSpec, mCancelInstallFrom);
124
125 Close();
126 }
127
128 void DownloadButton_Clicked()
129 {
130 if (mIsCloudEdition)
131 {
132 TrackFeatureUseEvent.For(mRepSpec, mInstallCloudFrom);
133
134 DownloadAndInstallOperation.Run(
135 Edition.Cloud, mInstallerFile, mProgressControls, this);
136 }
137 else
138 {
139 TrackFeatureUseEvent.For(mRepSpec, mInstallEnterpriseFrom);
140
141 DownloadAndInstallOperation.Run(
142 Edition.Enterprise, mInstallerFile, mProgressControls, this);
143 }
144 }
145
146 void DownloadAndInstallOperation.INotify.InstallationStarted()
147 {
148 mIsPlasticInstalling = true;
149 }
150
151 void DownloadAndInstallOperation.INotify.InstallationFinished()
152 {
153 mIsPlasticInstalling = false;
154 }
155
156 bool InstallationFinished()
157 {
158 return mCancelButton.enabledSelf && IsExeAvailable.ForMode(mIsGluonMode);
159 }
160
161 void UpdateButtonsStatuses()
162 {
163 if (!string.IsNullOrEmpty(mProgressData.StatusMessage))
164 {
165 mCancelButton.SetEnabled(true);
166 mCancelButton.text = PlasticLocalization.Name.CloseButton.GetString();
167 mDownloadButton.Collapse();
168 return;
169 }
170
171 if (IsExeAvailable.ForMode(mIsGluonMode))
172 {
173 mCancelButton.SetEnabled(true);
174 return;
175 }
176
177 mDownloadButton.SetEnabled(false);
178 mCancelButton.SetEnabled(false);
179 }
180
181 static string GetMessageFromProgressData(ProgressControlsForDialogs.Data data)
182 {
183 if (!string.IsNullOrEmpty(data.StatusMessage))
184 {
185 return data.StatusMessage;
186 }
187 else
188 {
189 if (data.ProgressPercent >= 0)
190 return string.Format("{0} ({1}%)", data.ProgressMessage, (int)(data.ProgressPercent * 100));
191 else
192 return data.ProgressMessage;
193 }
194 }
195
196 void Dispose()
197 {
198 mDownloadButton.clicked -= DownloadButton_Clicked;
199 mCancelButton.clicked -= CancelButton_Clicked;
200 }
201
202 void BuildComponents()
203 {
204 VisualElement root = rootVisualElement;
205 root.Clear();
206
207 InitializeLayoutAndStyles();
208
209 mMessageLabel = root.Q<Label>("title");
210 mConfirmMessageLabel = root.Q<Label>("message");
211 mDownloadButton = root.Q<Button>("download");
212 mCancelButton = root.Q<Button>("cancel");
213 mProgressControlsContainer = root.Q<VisualElement>("progressControlsContainer");
214
215 mMessageLabel.text = PlasticLocalization.Name.InstallUnityVersionControl.GetString();
216 mConfirmMessageLabel.text = PlasticLocalization.Name.InstallConfirmationMessage.GetString();
217 mDownloadButton.text = PlasticLocalization.Name.YesButton.GetString();
218 mDownloadButton.clicked += DownloadButton_Clicked;
219 mCancelButton.text = PlasticLocalization.Name.NoButton.GetString();
220 mCancelButton.clicked += CancelButton_Clicked;
221
222 mProgressControls = new ProgressControlsForDialogs(
223 new VisualElement[] {
224 mDownloadButton,
225 mCancelButton
226 });
227
228 mProgressControlsContainer.Add(mProgressControls);
229 }
230
231 void InitializeLayoutAndStyles()
232 {
233 rootVisualElement.LoadLayout(typeof(DownloadPlasticExeDialog).Name);
234 rootVisualElement.LoadStyle(typeof(DownloadPlasticExeDialog).Name);
235 }
236
237 string mTitle;
238
239 Label mMessageLabel;
240 Label mConfirmMessageLabel;
241 Label mProgressLabel;
242 Button mDownloadButton;
243 Button mCancelButton;
244 ProgressControlsForDialogs mProgressControls;
245 VisualElement mProgressControlsContainer;
246
247 RepositorySpec mRepSpec;
248 string mInstallCloudFrom;
249 string mInstallEnterpriseFrom;
250 string mCancelInstallFrom;
251 string mInstallerFile;
252 bool mIsCloudEdition;
253 bool mIsGluonMode;
254 bool mIsPlasticInstalling;
255 ProgressControlsForDialogs.Data mProgressData;
256
257 const float DIALOG_WIDTH = 500;
258 const float DIALOG_HEIGHT = 250;
259 const float REDUCED_DIALOG_HEIGHT = 125;
260 }
261}