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: 3 additions & 1 deletion src/NuGetForUnity/Editor/NugetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,10 +718,12 @@ 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(bool onlySelected = false)
{
foreach (var package in InstalledPackages.ToList())
{
if (onlySelected && !package.IsSelected) continue;

Uninstall(package);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/NuGetForUnity/Editor/NugetPackage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

Expand Down Expand Up @@ -93,6 +94,11 @@ public class NugetPackage : NugetPackageIdentifier, IEquatable<NugetPackage>, IE
/// </summary>
public string Title;

/// <summary>
/// Gets or sets if the package is selected to be batch uninstalled or updated.
/// </summary>
public bool IsSelected;

/// <summary>
/// Gets the icon for the package as a task returning a <see cref="Texture2D" />.
/// </summary>
Expand Down
182 changes: 100 additions & 82 deletions src/NuGetForUnity/Editor/NugetWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,22 @@ 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 (updatesSearchTerm == "Search")
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
{
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 +90,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 @@ -357,14 +365,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,9 +420,22 @@ protected void OnGUI()

private void OnTabChanged()
{
UnselectPackages(NugetHelper.InstalledPackages.ToList());
UnselectPackages(updatePackages);
openCloneWindows.Clear();
}

/// <summary>
/// Unselects all selected packages.
/// </summary>
private void UnselectPackages(List<NugetPackage> packages)
{
foreach (var package in packages)
{
package.IsSelected = false;
}
}

/// <summary>
/// Creates a GUI style with a contrasting background color based upon if the Unity Editor is the free (light) skin or the Pro (dark) skin.
/// </summary>
Expand Down Expand Up @@ -458,9 +471,9 @@ private void DrawUpdates()
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
EditorGUILayout.BeginVertical();

if (filteredUpdatePackages != null && filteredUpdatePackages.Count > 0)
if (FilteredUpdatePackages != null && FilteredUpdatePackages.Count > 0)
{
DrawPackages(filteredUpdatePackages);
DrawPackages(FilteredUpdatePackages, true);
}
else
{
Expand Down Expand Up @@ -489,7 +502,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 +564,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 +608,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 +665,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
{
if (GUILayout.Button("Uninstall All", EditorStyles.miniButtonRight, GUILayout.Width(150)))
{
NugetHelper.UninstallAll();
NugetHelper.UpdateInstalledPackages();
UpdateUpdatePackages();
}
}

if (NugetHelper.InstalledPackages.Where(p => p.IsSelected).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 Selected", EditorStyles.miniButtonRight, GUILayout.Width(150)))
{
NugetHelper.UninstallAll(true);
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 +731,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.Where(p => p.IsSelected).Count() > 0)
JoC0de marked this conversation as resolved.
Show resolved Hide resolved
{
if (GUILayout.Button("Update Selected", GUILayout.Width(200)))
{
NugetHelper.UpdateAll(updatePackages.Where(p => p.IsSelected), 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 +762,54 @@ 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 isSelectedTemp = EditorGUILayout.Toggle(package.IsSelected);
if (isSelectedTemp != package.IsSelected)
{
package.IsSelected = isSelectedTemp;
}
}

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