Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature - selection #510

Merged
merged 13 commits into from
May 3, 2023
4 changes: 2 additions & 2 deletions src/NuGetForUnity/Editor/NugetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,9 @@ private static void DeleteAllFiles(string directoryPath, string extension)
/// <summary>
/// Uninstalls all of the currently installed packages.
/// </summary>
internal static void UninstallAll()
internal static void UninstallAll(List<NugetPackage> packagesToUninstall)
{
foreach (var package in InstalledPackages.ToList())
foreach (var package in packagesToUninstall)
{
Uninstall(package);
}
Expand Down
186 changes: 102 additions & 84 deletions src/NuGetForUnity/Editor/NugetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class NugetWindow : EditorWindow
/// </summary>
private readonly HashSet<NugetPackage> openCloneWindows = new HashSet<NugetPackage>();

/// <summary>
/// Used to keep track of which packages are selected for uninstalling or updating.
/// </summary>
private readonly HashSet<NugetPackage> selectedPackages = new HashSet<NugetPackage>();

/// <summary>
/// The titles of the tabs in the window.
/// </summary>
Expand All @@ -62,7 +67,21 @@ public class NugetWindow : EditorWindow
/// <summary>
/// The filtered list of package updates available.
/// </summary>
private List<NugetPackage> filteredUpdatePackages = new List<NugetPackage>();
private List<NugetPackage> FilteredUpdatePackages
{
get
{
if (string.IsNullOrWhiteSpace(updatesSearchTerm) || updatesSearchTerm == "Search")
{
return updatePackages;
}

return updatePackages
.Where(x => x.Id.ToLower().Contains(updatesSearchTerm) || x.Title.ToLower().Contains(updatesSearchTerm))
.ToList();
}
}


/// <summary>
/// True when the NugetWindow has initialized. This is used to skip time-consuming reloading operations when the assembly is reloaded.
Expand All @@ -75,13 +94,6 @@ public class NugetWindow : EditorWindow
/// </summary>
private string installedSearchTerm = "Search";

/// <summary>
/// The search term in progress while it is being typed into the search box.
/// We wait until the Enter key or Search button is pressed before searching in order
/// to match the way that the Online and Updates searches work.
/// </summary>
private string installedSearchTermEditBox = "Search";

/// <summary>
/// The number of packages to skip when requesting a list of packages from the server. This is used to get a new group of packages.
/// </summary>
Expand Down Expand Up @@ -133,14 +145,13 @@ private IEnumerable<NugetPackage> FilteredInstalledPackages
{
get
{
if (installedSearchTerm == "Search")
if (string.IsNullOrWhiteSpace(installedSearchTerm) || installedSearchTerm == "Search")
{
return NugetHelper.InstalledPackages;
}

return NugetHelper.InstalledPackages
.Where(x => x.Id.ToLower().Contains(installedSearchTerm) || x.Title.ToLower().Contains(installedSearchTerm))
.ToList();
.Where(x => x.Id.ToLower().Contains(installedSearchTerm) || x.Title.ToLower().Contains(installedSearchTerm));
}
}

Expand Down Expand Up @@ -357,14 +368,6 @@ private void UpdateUpdatePackages()
{
// get any available updates for the installed packages
updatePackages = NugetHelper.GetUpdates(NugetHelper.InstalledPackages, showPrereleaseUpdates, showAllUpdateVersions);
filteredUpdatePackages = updatePackages;

if (updatesSearchTerm != "Search")
{
filteredUpdatePackages = updatePackages
.Where(x => x.Id.ToLower().Contains(updatesSearchTerm) || x.Title.ToLower().Contains(updatesSearchTerm))
.ToList();
}
}

/// <summary>
Expand Down Expand Up @@ -420,6 +423,7 @@ protected void OnGUI()

private void OnTabChanged()
{
selectedPackages.Clear();
openCloneWindows.Clear();
}

Expand Down Expand Up @@ -458,9 +462,10 @@ private void DrawUpdates()
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
EditorGUILayout.BeginVertical();

var filteredUpdatePackages = FilteredUpdatePackages;
if (filteredUpdatePackages != null && filteredUpdatePackages.Count > 0)
{
DrawPackages(filteredUpdatePackages);
DrawPackages(filteredUpdatePackages, true);
}
else
{
Expand Down Expand Up @@ -489,7 +494,7 @@ private void DrawInstalled()
var filteredInstalledPackages = FilteredInstalledPackages.ToList();
if (filteredInstalledPackages != null && filteredInstalledPackages.Count > 0)
{
DrawPackages(filteredInstalledPackages);
DrawPackages(filteredInstalledPackages, true);
}
else
{
Expand Down Expand Up @@ -551,15 +556,15 @@ private void DrawOnline()
EditorGUILayout.EndScrollView();
}

private void DrawPackages(List<NugetPackage> packages)
private void DrawPackages(List<NugetPackage> packages, bool canBeSelected = false)
{
var backgroundStyle = GetBackgroundStyle();
var contrastStyle = GetContrastStyle();

for (var i = 0; i < packages.Count; i++)
{
EditorGUILayout.BeginVertical(backgroundStyle);
DrawPackage(packages[i], backgroundStyle, contrastStyle);
DrawPackage(packages[i], backgroundStyle, contrastStyle, canBeSelected);
EditorGUILayout.EndVertical();

// swap styles
Expand Down Expand Up @@ -595,16 +600,7 @@ private void DrawOnlineHeader()
UpdateOnlinePackages();
}

if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
Refresh(true);
}

if (GUILayout.Button("Preferences", GUILayout.Width(80)))
{
SettingsService.OpenUserPreferences("Preferences/NuGet For Unity");
GetWindow<NugetWindow>().Close();
}
DrawMandatoryButtons();
}
EditorGUILayout.EndHorizontal();

Expand Down Expand Up @@ -661,39 +657,42 @@ private void DrawInstalledHeader()

EditorGUILayout.BeginVertical(headerStyle);
{
var enterPressed = Event.current.Equals(Event.KeyboardEvent("return"));

EditorGUILayout.BeginHorizontal();
{
if (GUILayout.Button("Preferences", GUILayout.Width(80)))
GUILayout.FlexibleSpace();
if (NugetHelper.InstalledPackages.Count() > 0)
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
{
SettingsService.OpenUserPreferences("Preferences/NuGet For Unity");
GetWindow<NugetWindow>().Close();
if (GUILayout.Button("Uninstall All", EditorStyles.miniButtonRight, GUILayout.Width(150)))
{
NugetHelper.UninstallAll(NugetHelper.InstalledPackages.ToList());
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}
}

if (NugetHelper.InstalledPackages.Any(selectedPackages.Contains))
{
if (GUILayout.Button("Uninstall Selected", EditorStyles.miniButtonRight, GUILayout.Width(150)))
{
NugetHelper.UninstallAll(NugetHelper.InstalledPackages.Where(p => selectedPackages.Contains(p)).ToList());
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}
}

DrawMandatoryButtons();
}
EditorGUILayout.EndHorizontal();

EditorGUILayout.BeginHorizontal();
{
var oldFontSize = GUI.skin.textField.fontSize;
GUI.skin.textField.fontSize = 25;
installedSearchTermEditBox = EditorGUILayout.TextField(installedSearchTermEditBox, GUILayout.Height(30));

if (GUILayout.Button("Search", GUILayout.Width(100), GUILayout.Height(28)))
{
// the search button emulates the Enter key
enterPressed = true;
}
installedSearchTerm = EditorGUILayout.TextField(installedSearchTerm, GUILayout.Height(30));

GUI.skin.textField.fontSize = oldFontSize;
}
EditorGUILayout.EndHorizontal();

// search only if the enter key is pressed
if (enterPressed)
{
installedSearchTerm = installedSearchTermEditBox;
}
}
EditorGUILayout.EndVertical();
}
Expand Down Expand Up @@ -724,23 +723,27 @@ private void DrawUpdatesHeader()
UpdateUpdatePackages();
}

if (GUILayout.Button("Install All Updates", GUILayout.Width(150)))
if (updatePackages.Count > 0)
{
NugetHelper.UpdateAll(updatePackages, NugetHelper.InstalledPackages);
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}
if (GUILayout.Button("Update All", GUILayout.Width(150)))
{
NugetHelper.UpdateAll(updatePackages, NugetHelper.InstalledPackages);
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}

if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
Refresh(true);
if (updatePackages.Any(selectedPackages.Contains))
{
if (GUILayout.Button("Update Selected", GUILayout.Width(200)))
{
NugetHelper.UpdateAll(updatePackages.Where(p => selectedPackages.Contains(p)), NugetHelper.InstalledPackages);
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}
}
}

if (GUILayout.Button("Preferences", GUILayout.Width(80)))
{
SettingsService.OpenUserPreferences("Preferences/NuGet For Unity");
GetWindow<NugetWindow>().Close();
}
DrawMandatoryButtons();
}
EditorGUILayout.EndHorizontal();

Expand All @@ -751,47 +754,62 @@ private void DrawUpdatesHeader()
UpdateUpdatePackages();
}

var enterPressed = Event.current.Equals(Event.KeyboardEvent("return"));

EditorGUILayout.BeginHorizontal();
{
var oldFontSize = GUI.skin.textField.fontSize;
GUI.skin.textField.fontSize = 25;
updatesSearchTerm = EditorGUILayout.TextField(updatesSearchTerm, GUILayout.Height(30));

if (GUILayout.Button("Search", GUILayout.Width(100), GUILayout.Height(28)))
{
// the search button emulates the Enter key
enterPressed = true;
}

GUI.skin.textField.fontSize = oldFontSize;
}
EditorGUILayout.EndHorizontal();

// search only if the enter key is pressed
if (enterPressed)
{
if (updatesSearchTerm != "Search")
{
filteredUpdatePackages = updatePackages.Where(
x => x.Id.ToLower().Contains(updatesSearchTerm) || x.Title.ToLower().Contains(updatesSearchTerm))
.ToList();
}
}
}
EditorGUILayout.EndVertical();
}

/// <summary>
/// Draws the "Refresh" and "Preferences" buttons in the upper right corner, which are visible in every tab.
/// </summary>
private void DrawMandatoryButtons()
{
if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
Refresh(true);
}

if (GUILayout.Button("Preferences", GUILayout.Width(80)))
{
SettingsService.OpenUserPreferences("Preferences/NuGet For Unity");
GetWindow<NugetWindow>().Close();
}
}

/// <summary>
/// Draws the given <see cref="NugetPackage" />.
/// </summary>
/// <param name="package">The <see cref="NugetPackage" /> to draw.</param>
private void DrawPackage(NugetPackage package, GUIStyle packageStyle, GUIStyle contrastStyle)
private void DrawPackage(NugetPackage package, GUIStyle packageStyle, GUIStyle contrastStyle, bool canBeSelected = false)
{
var installedPackages = NugetHelper.InstalledPackages;
var installed = installedPackages.FirstOrDefault(p => p.Id == package.Id);

if (canBeSelected)
{
var isSelected = selectedPackages.Contains(package);
var isSelectedTemp = EditorGUILayout.Toggle(isSelected);
if (isSelectedTemp != isSelected)
{
if (isSelectedTemp)
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
{
selectedPackages.Add(package);
}
else
{
selectedPackages.Remove(package);
}
}
}

EditorGUILayout.BeginHorizontal();
{
// The Unity GUI system (in the Editor) is terrible. This probably requires some explanation.
Expand Down