Add support for svgaplayerweb in SVGA viewer

This commit is contained in:
ema
2025-07-05 11:24:02 +08:00
parent efba55b8d5
commit f6ca3a62e2
9 changed files with 77 additions and 14 deletions

View File

@@ -74,6 +74,7 @@
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.2"> <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.2">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="System.Text.Json" Version="9.0.6" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(DefineConstants)' != '' and $(DefineConstants.Contains('USESVGSKIA')) "> <ItemGroup Condition=" '$(DefineConstants)' != '' and $(DefineConstants.Contains('USESVGSKIA')) ">

View File

@@ -9,12 +9,13 @@
* *
* Features: * Features:
* - Loads and plays SVGA animation files * - Loads and plays SVGA animation files
* - Uses SVGA.js library for parsing and playback * - Uses svga or svgaplayerweb library for parsing and playback
* - Automatically starts playback after loading * - Automatically starts playback after loading
* - Handles asynchronous loading and mounting of SVGA files * - Handles asynchronous loading and mounting of SVGA files
*/ */
class SvgaViewer { class SvgaViewer {
constructor() { constructor() {
this.useLite = false; // Use SVGA Lite version if true
} }
/** /**
@@ -23,6 +24,8 @@ class SvgaViewer {
*/ */
async play() { async play() {
const path = await chrome.webview.hostObjects.external.GetPath(); const path = await chrome.webview.hostObjects.external.GetPath();
if (this.useLite) {
const parser = new SVGA.Parser(); // Only SVGA 2.x supported const parser = new SVGA.Parser(); // Only SVGA 2.x supported
// Because the path is a local file, we need to convert it to a URL format // Because the path is a local file, we need to convert it to a URL format
@@ -32,6 +35,21 @@ class SvgaViewer {
player.start(); player.start();
}); });
}); });
} else {
const size = JSON.parse(await chrome.webview.hostObjects.external.GetSize());
const parser = new SVGA.Parser('#canvas'); // Only SVGA 2.x supported
const player = new SVGA.Player('#canvas');
const canvas = document.getElementById('canvas');
canvas.width = size.width;
canvas.height = size.height;
// Because the path is a local file, we need to convert it to a URL format
parser.load('https://' + path, function (videoItem) {
player.setVideoItem(videoItem);
player.startAnimation();
});
}
} }
} }

View File

@@ -19,7 +19,9 @@
<body> <body>
<canvas id="canvas"></canvas> <canvas id="canvas"></canvas>
<!--<script src="https://unpkg.com/svga/dist/index.min.js"></script>--> <!--<script src="https://unpkg.com/svga/dist/index.min.js"></script>-->
<script src="svga/index.min.js"></script> <!--<script src="svga/index.min.js"></script>-->
<!--<script src="https://unpkg.com/svgaplayerweb/build/svga.min.js"></script>-->
<script src="svgaplayerweb/svga.min.js"></script>
<script src="js/svga2html.js"></script> <script src="js/svga2html.js"></script>
</body> </body>
</html> </html>

File diff suppressed because one or more lines are too long

View File

@@ -19,7 +19,7 @@ using System;
namespace QuickLook.Plugin.ImageViewer.Webview; namespace QuickLook.Plugin.ImageViewer.Webview;
internal interface IWebImagePanel : IDisposable public interface IWebImagePanel : IDisposable
{ {
public void Preview(string path); public void Preview(string path);
} }

View File

@@ -19,7 +19,7 @@ using System.Windows;
namespace QuickLook.Plugin.ImageViewer.Webview; namespace QuickLook.Plugin.ImageViewer.Webview;
internal interface IWebMetaProvider public interface IWebMetaProvider
{ {
public Size GetSize(); public Size GetSize();
} }

View File

@@ -20,7 +20,9 @@ using QuickLook.Plugin.ImageViewer.Webview.Svg;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace QuickLook.Plugin.ImageViewer.Webview.Lottie; namespace QuickLook.Plugin.ImageViewer.Webview.Lottie;
@@ -76,3 +78,15 @@ public class LottieImagePanel : SvgImagePanel
base.WebView_WebResourceRequested(sender, args); base.WebView_WebResourceRequested(sender, args);
} }
} }
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public sealed class ScriptHandler(string path)
{
public string Path { get; } = path;
public async Task<string> GetPath()
{
return await Task.FromResult(new Uri(Path).AbsolutePath);
}
}

View File

@@ -15,21 +15,48 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using Google.Protobuf.Compiler;
using QuickLook.Plugin.ImageViewer.Webview.Svg; using QuickLook.Plugin.ImageViewer.Webview.Svg;
using System; using System;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
namespace QuickLook.Plugin.ImageViewer.Webview.Svga; namespace QuickLook.Plugin.ImageViewer.Webview.Svga;
public class SvgaImagePanel : SvgImagePanel public class SvgaImagePanel(IWebMetaProvider metaWeb) : SvgImagePanel()
{ {
private readonly IWebMetaProvider _metaWeb = metaWeb;
public override void Preview(string path) public override void Preview(string path)
{ {
FallbackPath = Path.GetDirectoryName(path); FallbackPath = Path.GetDirectoryName(path);
ObjectForScripting ??= new ScriptHandler(path); ObjectForScripting ??= new ScriptHandler(path, _metaWeb);
_homePage = _resources["/svga2html.html"]; _homePage = _resources["/svga2html.html"];
NavigateToUri(new Uri("file://quicklook/")); NavigateToUri(new Uri("file://quicklook/"));
} }
} }
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public sealed class ScriptHandler(string path, IWebMetaProvider metaWeb)
{
public string Path { get; } = path;
public IWebMetaProvider MetaWeb { get; } = metaWeb;
public async Task<string> GetPath()
{
return await Task.FromResult(new Uri(Path).AbsolutePath);
}
public async Task<string> GetSize()
{
var size = MetaWeb.GetSize();
string jsonString = JsonSerializer.Serialize(new { width = size.Width, height = size.Height });
return await Task.FromResult(jsonString);
}
}

View File

@@ -99,7 +99,7 @@ internal static class WebHandler
ipWeb = ext switch ipWeb = ext switch
{ {
".svg" => new SvgImagePanel(), ".svg" => new SvgImagePanel(),
".svga" => new SvgaImagePanel(), ".svga" => new SvgaImagePanel(metaWeb),
".lottie" or ".json" => new LottieImagePanel(), ".lottie" or ".json" => new LottieImagePanel(),
_ => throw new NotSupportedException($"Unsupported file type: {ext}") _ => throw new NotSupportedException($"Unsupported file type: {ext}")
}; };