Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Screenbox.Core/Helpers/WebView2Util.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.Web.WebView2.Core;
using Newtonsoft.Json;
using System;
using System;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Web.WebView2.Core;
using Windows.System;

namespace Screenbox.Core.Helpers;
Expand Down Expand Up @@ -52,7 +52,7 @@ public static async Task<string> ExecuteScriptFunctionAsync(this WebView2 webVie
script.Append("(");
for (int i = 0; i < parameters.Length; i++)
{
script.Append(JsonConvert.SerializeObject(parameters[i]));
script.Append(JsonSerializer.Serialize(parameters[i]));
if (i < parameters.Length - 1)
{
script.Append(", ");
Expand Down
10 changes: 5 additions & 5 deletions Screenbox.Core/Services/LivelyWallpaperService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#nullable enable

using Newtonsoft.Json;
using Screenbox.Core.Helpers;
using Screenbox.Core.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Screenbox.Core.Helpers;
using Screenbox.Core.Models;
using Windows.Storage;

namespace Screenbox.Core.Services;
Expand Down Expand Up @@ -109,7 +109,7 @@ await visualizersFolder.CreateFolderAsync(Path.GetRandomFileName(),
using var entryStream = livelyInfoEntry.Open();
using var streamReader = new StreamReader(entryStream);
var jsonContent = await streamReader.ReadToEndAsync();
var model = JsonConvert.DeserializeObject<LivelyInfoModel>(jsonContent);
var model = JsonSerializer.Deserialize<LivelyInfoModel>(jsonContent);
return model;
}
catch
Expand All @@ -124,7 +124,7 @@ await visualizersFolder.CreateFolderAsync(Path.GetRandomFileName(),
{
var modelFile = await wallpaperFolder.GetFileAsync("LivelyInfo.json");
var jsonContent = await FileIO.ReadTextAsync(modelFile);
var model = JsonConvert.DeserializeObject<LivelyInfoModel>(jsonContent);
var model = JsonSerializer.Deserialize<LivelyInfoModel>(jsonContent);
return model;
}
catch
Expand Down
61 changes: 36 additions & 25 deletions Screenbox.Core/ViewModels/LivelyWallpaperPlayerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#nullable enable

using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text.Json;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using CommunityToolkit.WinUI;
using Microsoft.UI.Xaml.Controls;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Screenbox.Core.Helpers;
using Screenbox.Core.Messages;
using Screenbox.Core.Models;
using Screenbox.Core.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System;
Expand Down Expand Up @@ -138,32 +136,45 @@ public async Task UpdateLivelyProperties(WebView2 webView)
return;
}

var jsonObject = JObject.Parse(jsonString);
foreach (KeyValuePair<string, JToken?> item in jsonObject)
var jsonOptions = new JsonDocumentOptions { AllowTrailingCommas = true };
using var jsonDocument = JsonDocument.Parse(jsonString, jsonOptions);
var jsonElement = jsonDocument.RootElement;

foreach (var item in jsonElement.EnumerateObject())
{
var typeToken = item.Value?["type"];
var valueToken = item.Value?["value"];
if (typeToken == null || valueToken == null) continue;
switch (typeToken.ToString())
if (!item.Value.TryGetProperty("type", out var typeToken) ||
!item.Value.TryGetProperty("value", out var valueToken))
continue;

if (typeToken.ValueKind == JsonValueKind.Null || valueToken.ValueKind == JsonValueKind.Null)
continue;

switch (typeToken.GetString())
{
case "slider":
await webView.ExecuteScriptFunctionAsync(functionName, item.Key, (double)valueToken);
await webView.ExecuteScriptFunctionAsync(functionName, item.Name, valueToken.GetDouble());
break;
case "dropdown":
await webView.ExecuteScriptFunctionAsync(functionName, item.Key, (int)valueToken);
await webView.ExecuteScriptFunctionAsync(functionName, item.Name, valueToken.GetInt32());
break;
case "checkbox":
await webView.ExecuteScriptFunctionAsync(functionName, item.Key, (bool)valueToken);
await webView.ExecuteScriptFunctionAsync(functionName, item.Name, valueToken.GetBoolean());
break;
case "color":
await webView.ExecuteScriptFunctionAsync(functionName, item.Key, valueToken.ToString());
await webView.ExecuteScriptFunctionAsync(functionName, item.Name, valueToken.GetString());
break;
//case "textbox":
// await webView.ExecuteScriptFunctionAsync(functionName, item.Name, valueToken.GetString());
// break;
case "folderDropdown":
var relativePath = Path.Combine(item.Value?["folder"]?.ToString() ?? string.Empty,
valueToken.ToString());
var filePath = Path.Combine(Source.Path, relativePath);
await webView.ExecuteScriptFunctionAsync(functionName, item.Key,
File.Exists(filePath) ? relativePath : null);
if (item.Value.TryGetProperty("folder", out var folderToken))
{
var relativePath = Path.Combine(folderToken.GetString() ?? string.Empty,
valueToken.GetString() ?? string.Empty);
var filePath = Path.Combine(Source.Path, relativePath);
await webView.ExecuteScriptFunctionAsync(functionName, item.Name,
File.Exists(filePath) ? relativePath : null);
}
break;
case "button":
case "label":
Expand Down Expand Up @@ -207,7 +218,7 @@ public async Task UpdateCurrentTrack(WebView2 webView)
model.Thumbnail = base64;
}

await webView.ExecuteScriptFunctionAsync("livelyCurrentTrack", JsonConvert.SerializeObject(model));
await webView.ExecuteScriptFunctionAsync("livelyCurrentTrack", JsonSerializer.Serialize(model));
}

private void LoadMedia()
Expand Down
Loading