package details v1
This commit is contained in:
25
src/SharpIDE.Godot/Features/Nuget/ImageTextureHelper.cs
Normal file
25
src/SharpIDE.Godot/Features/Nuget/ImageTextureHelper.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Godot;
|
||||
using SharpIDE.Application.Features.Nuget;
|
||||
|
||||
namespace SharpIDE.Godot.Features.Nuget;
|
||||
|
||||
public static class ImageTextureHelper
|
||||
{
|
||||
public static ImageTexture? GetImageTextureFromBytes(byte[]? imageBytes, NugetPackageIconFormat? format)
|
||||
{
|
||||
if (imageBytes is null || format is null) return null;
|
||||
var image = new Image();
|
||||
var error = format switch
|
||||
{
|
||||
NugetPackageIconFormat.Png => image.LoadPngFromBuffer(imageBytes),
|
||||
NugetPackageIconFormat.Jpg => image.LoadJpgFromBuffer(imageBytes),
|
||||
_ => Error.FileUnrecognized
|
||||
};
|
||||
if (error is Error.Ok)
|
||||
{
|
||||
image.Resize(32, 32, Image.Interpolation.Lanczos); // Probably should cache resized images instead
|
||||
return ImageTexture.CreateFromImage(image);
|
||||
}
|
||||
return null!;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://f62r1og43wye
|
||||
@@ -1,8 +1,36 @@
|
||||
using Godot;
|
||||
using SharpIDE.Application.Features.Nuget;
|
||||
|
||||
namespace SharpIDE.Godot.Features.Nuget;
|
||||
|
||||
public partial class NugetPackageDetails : VBoxContainer
|
||||
{
|
||||
private TextureRect _packageIconTextureRect = null!;
|
||||
private Label _packageNameLabel = null!;
|
||||
|
||||
private IdePackageResult? _package;
|
||||
|
||||
[Inject] private readonly NugetPackageIconCacheService _nugetPackageIconCacheService = null!;
|
||||
public override void _Ready()
|
||||
{
|
||||
_packageIconTextureRect = GetNode<TextureRect>("%PackageIconTextureRect");
|
||||
_packageNameLabel = GetNode<Label>("%PackageNameLabel");
|
||||
}
|
||||
|
||||
public async Task SetPackage(IdePackageResult package)
|
||||
{
|
||||
_package = package;
|
||||
var iconTask = _nugetPackageIconCacheService.GetNugetPackageIcon(_package.PackageId, _package.PackageFromSources.First().PackageSearchMetadata.IconUrl);
|
||||
await this.InvokeAsync(() =>
|
||||
{
|
||||
_packageNameLabel.Text = package.PackageId;
|
||||
Visible = true;
|
||||
});
|
||||
var (iconBytes, iconFormat) = await iconTask;
|
||||
var imageTexture = ImageTextureHelper.GetImageTextureFromBytes(iconBytes, iconFormat);
|
||||
if (imageTexture is not null)
|
||||
{
|
||||
await this.InvokeAsync(() => _packageIconTextureRect.Texture = imageTexture);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,13 +15,15 @@ script = ExtResource("1_c3hm2")
|
||||
layout_mode = 2
|
||||
theme_override_constants/separation = 8
|
||||
|
||||
[node name="TextureRect" type="TextureRect" parent="HBoxContainer"]
|
||||
[node name="PackageIconTextureRect" type="TextureRect" parent="HBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(32, 32)
|
||||
layout_mode = 2
|
||||
texture = ExtResource("1_jrdf0")
|
||||
expand_mode = 3
|
||||
|
||||
[node name="Label" type="Label" parent="HBoxContainer"]
|
||||
[node name="PackageNameLabel" type="Label" parent="HBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
theme_override_colors/font_color = Color(0.83137256, 0.83137256, 0.83137256, 1)
|
||||
theme_override_font_sizes/font_size = 18
|
||||
@@ -36,6 +38,7 @@ theme_override_colors/font_color = Color(0.83137256, 0.83137256, 0.83137256, 1)
|
||||
text = "Version"
|
||||
|
||||
[node name="VersionOptionButton" type="OptionButton" parent="HBoxContainer2"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
selected = 0
|
||||
@@ -44,6 +47,7 @@ popup/item_0/text = "1.13.0"
|
||||
popup/item_0/id = 0
|
||||
|
||||
[node name="NugetSourceOptionButton" type="OptionButton" parent="HBoxContainer2"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
selected = 0
|
||||
item_count = 1
|
||||
|
||||
@@ -51,9 +51,9 @@ public partial class NugetPanel : Control
|
||||
});
|
||||
}
|
||||
|
||||
private void OnPackageSelected(IdePackageResult packageResult)
|
||||
private async Task OnPackageSelected(IdePackageResult packageResult)
|
||||
{
|
||||
_selectedPackage = packageResult;
|
||||
_nugetPackageDetails.Visible = true;
|
||||
await _nugetPackageDetails.SetPackage(packageResult);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public partial class PackageEntry : MarginContainer
|
||||
Source_5_Color
|
||||
];
|
||||
|
||||
public event Action<IdePackageResult> PackageSelected = null!;
|
||||
public event Func<IdePackageResult, Task> PackageSelected = null!;
|
||||
|
||||
[Inject] private readonly NugetPackageIconCacheService _nugetPackageIconCacheService = null!;
|
||||
|
||||
@@ -42,7 +42,7 @@ public partial class PackageEntry : MarginContainer
|
||||
_sourceNamesContainer = GetNode<HBoxContainer>("%SourceNamesHBoxContainer");
|
||||
_packageIconTextureRect = GetNode<TextureRect>("%PackageIconTextureRect");
|
||||
ApplyValues();
|
||||
_button.Pressed += () => PackageSelected?.Invoke(PackageResult);
|
||||
_button.Pressed += async () => await PackageSelected.Invoke(PackageResult);
|
||||
}
|
||||
|
||||
private void ApplyValues()
|
||||
@@ -58,18 +58,10 @@ public partial class PackageEntry : MarginContainer
|
||||
_ = Task.GodotRun(async () =>
|
||||
{
|
||||
var (iconBytes, iconFormat) = await _nugetPackageIconCacheService.GetNugetPackageIcon(PackageResult.PackageId, PackageResult.PackageFromSources.First().PackageSearchMetadata.IconUrl);
|
||||
var image = new Image();
|
||||
var error = iconFormat switch
|
||||
var imageTexture = ImageTextureHelper.GetImageTextureFromBytes(iconBytes, iconFormat);
|
||||
if (imageTexture is not null)
|
||||
{
|
||||
NugetPackageIconFormat.Png => image.LoadPngFromBuffer(iconBytes),
|
||||
NugetPackageIconFormat.Jpg => image.LoadJpgFromBuffer(iconBytes),
|
||||
_ => Error.FileUnrecognized
|
||||
};
|
||||
if (error is Error.Ok)
|
||||
{
|
||||
image.Resize(32, 32, Image.Interpolation.Lanczos); // Probably should cache resized images instead
|
||||
var loadedImageTexture = ImageTexture.CreateFromImage(image);
|
||||
await this.InvokeAsync(() => _packageIconTextureRect.Texture = loadedImageTexture);
|
||||
await this.InvokeAsync(() => _packageIconTextureRect.Texture = imageTexture);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user