com.alicizax.unity.packagem.../Editor/PackageManager/Window/PackageInfoListItem.cs

117 lines
4.1 KiB
C#
Raw Normal View History

2025-02-20 10:48:57 +08:00
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace AlicizaX.PackageManager.Editor
{
internal class PackageInfoListItem : VisualElement
{
private RepositoryPackageData _repositoryPackageData;
private VisualElement m_MainItem;
private Label m_NameLabel;
private Label m_VersionLabel;
private VisualElement m_StateContainer;
private Image m_StateIcon;
private VisualElement m_LeftContainer;
private VisualElement m_RightContainer;
public PackageInfoListItem()
{
style.backgroundColor = new Color(0.2196078f, 0.2196078f, 0.2196078f);
BuildMainItem();
}
public void RefreshDataSource(RepositoryPackageData packageData)
{
_repositoryPackageData = packageData;
m_NameLabel.text = packageData.displayName;
if (packageData.PackageState == PackageState.Update)
{
m_StateIcon.image = EditorGUIUtility.IconContent("Update-Available").image;
}
else if (packageData.PackageState == PackageState.Install || packageData.PackageState == PackageState.InstallLocal)
{
m_StateIcon.image = EditorGUIUtility.IconContent("Installed").image;
}
else if (packageData.PackageState == PackageState.UnInstall)
{
m_StateIcon.image = EditorGUIUtility.IconContent("d_CreateAddNew").image;
}
m_VersionLabel.text = packageData.PackageState == PackageState.InstallLocal ? "[Local]" : packageData.version;
}
private void BuildMainItem()
{
style.flexShrink = 1;
style.borderBottomWidth = 1;
var borderColor = new Color(0.1372549f, 0.1372549f, 0.1372549f);
style.borderBottomColor = borderColor;
style.borderTopColor = borderColor;
style.borderLeftColor = borderColor;
style.borderRightColor = borderColor;
m_MainItem = new VisualElement();
m_MainItem.name = "MainItem";
m_MainItem.style.height = Length.Percent(100);
m_MainItem.style.flexDirection = FlexDirection.Row;
m_MainItem.style.alignItems = Align.Center;
Add(m_MainItem);
m_LeftContainer = new VisualElement();
m_LeftContainer.name = "leftContainer";
m_MainItem.Add(m_LeftContainer);
m_NameLabel = new Label();
m_NameLabel.name = "packageName";
m_LeftContainer.Add((VisualElement)m_NameLabel);
m_LeftContainer.style.marginLeft = new Length(20, LengthUnit.Pixel);
m_LeftContainer.style.justifyContent = Justify.Center;
m_LeftContainer.style.flexGrow = 1; // 让 Label 占据剩余空间
m_VersionLabel = new Label();
m_VersionLabel.name = "versionLabel";
m_MainItem.Add((VisualElement)m_VersionLabel);
m_RightContainer = new VisualElement()
{
name = "rightContainer",
};
m_RightContainer.AddToClassList("right");
m_MainItem.Add(m_RightContainer);
m_StateContainer = new VisualElement()
{
name = "statesContainer"
};
m_MainItem.Add(m_StateContainer);
m_StateContainer.style.justifyContent = Justify.Center;
m_StateContainer.style.marginRight = new Length(10, LengthUnit.Pixel);
m_StateIcon = new Image()
{
name = "stateIcon",
};
m_StateIcon.AddToClassList("status");
m_StateContainer.Add(m_StateIcon);
}
public void SetSelected(bool isSelected)
{
if (m_MainItem != null)
{
// 设置选中时的背景颜色为 #4D4D4DRGB: 77,77,77未选中时透明
m_MainItem.style.backgroundColor = isSelected
? new Color(77f / 255f, 77f / 255f, 77f / 255f)
: Color.clear;
}
}
}
}