Skip to content
This repository was archived by the owner on Jan 25, 2026. It is now read-only.

Commit 79ed887

Browse files
committed
feat(theme): 添加 GrayProfile 相关模型和 gray.json 文件加载
1 parent 7aec562 commit 79ed887

File tree

7 files changed

+137
-13
lines changed

7 files changed

+137
-13
lines changed

Model/Files/FileProcess.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
25

36
namespace PCL.Core.Model.Files;
47

@@ -12,11 +15,43 @@ namespace PCL.Core.Model.Files;
1215

1316
public static class FileProcesses
1417
{
18+
/// <summary>
19+
/// Result: text content of the file.
20+
/// </summary>
1521
public static readonly FileProcess ReadText = ((_, path) =>
1622
{
1723
if (path == null) return null;
1824
var fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
1925
var sr = new StreamReader(fs);
2026
return sr.ReadToEnd();
2127
});
28+
29+
private static readonly JsonSerializerOptions _ParseJsonCreateDefaultOptions = new()
30+
{
31+
WriteIndented = true,
32+
AllowOutOfOrderMetadataProperties = true,
33+
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
34+
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
35+
};
36+
37+
/// <summary>
38+
/// Result: parsed object as the specified type (nullable)
39+
/// </summary>
40+
/// <typeparam name="TValue">the specified type (must be serializable and have a public default constructor)</typeparam>
41+
/// <param name="createDefault">whether create the file with default values if not exist</param>
42+
public static FileProcess ParseJson<TValue>(bool createDefault = true) => ((_, path) =>
43+
{
44+
if (path == null) return null;
45+
var exist = File.Exists(path);
46+
using var fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
47+
if (exist)
48+
{
49+
var result = JsonSerializer.Deserialize<TValue>(fs);
50+
return result;
51+
}
52+
if (!createDefault) return null;
53+
var d = Activator.CreateInstance(typeof(TValue));
54+
JsonSerializer.Serialize(fs, d, _ParseJsonCreateDefaultOptions);
55+
return d;
56+
});
2257
}

Model/Files/FileTransfer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23

34
namespace PCL.Core.Model.Files;
45

@@ -24,5 +25,11 @@ public class TransferFailedException(string reason, FileItem item, Exception? in
2425

2526
public static class FileTransfers
2627
{
27-
public static readonly FileTransfer Empty = ((item, callback) => callback(item.TargetPath));
28+
public static readonly FileTransfer DoNothing = ((item, callback) => callback(item.TargetPath));
29+
30+
public static readonly FileTransfer CreateIfNotExist = ((item, callback) =>
31+
{
32+
if (!File.Exists(item.TargetPath)) File.Create(item.TargetPath).Close();
33+
callback(item.TargetPath);
34+
});
2835
}

Model/GrayProfile.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Text.Json.Serialization;
3+
4+
namespace PCL.Core.Model;
5+
6+
[Serializable]
7+
public record GrayProfile
8+
{
9+
[JsonPropertyName("light_c1")] public int L1 { get; init; }
10+
[JsonPropertyName("light_c2")] public int L2 { get; init; }
11+
[JsonPropertyName("light_c3")] public int L3 { get; init; }
12+
[JsonPropertyName("light_c4")] public int L4 { get; init; }
13+
[JsonPropertyName("light_c5")] public int L5 { get; init; }
14+
[JsonPropertyName("light_c6")] public int L6 { get; init; }
15+
[JsonPropertyName("light_c7")] public int L7 { get; init; }
16+
[JsonPropertyName("light_c8")] public int L8 { get; init; }
17+
[JsonPropertyName("light_g1")] public int G1 { get; init; }
18+
[JsonPropertyName("light_g2")] public int G2 { get; init; }
19+
[JsonPropertyName("light_g3")] public int G3 { get; init; }
20+
21+
private int? _lb0; [JsonPropertyName("light_b0")] public int Lb0 { get => _lb0 ?? L5; init => _lb0 = value; }
22+
private int? _lb1; [JsonPropertyName("light_b1")] public int Lb1 { get => _lb1 ?? L7; init => _lb1 = value; }
23+
24+
[JsonPropertyName("lightadjust_positive")] public double LaP { get; init; } = 1;
25+
[JsonPropertyName("lightadjust_negative")] public double LaN { get; init; } = 1;
26+
27+
[JsonPropertyName("sat_0")] public double Sa0 { get; init; }
28+
[JsonPropertyName("sat_1")] public double Sa1 { get; init; }
29+
30+
[JsonPropertyName("alpha_background")] public double Ab { get; init; } = 0xBE;
31+
[JsonPropertyName("alpha_semi_transparent")] public double Ast { get; init; } = 0x1;
32+
[JsonPropertyName("alpha_transparent")] public double At { get; init; } = 0x0;
33+
[JsonPropertyName("alpha_half_white")] public double Ahw { get; init; } = 0x55;
34+
[JsonPropertyName("alpha_semi_white")] public double Asw { get; init; } = 0xDB;
35+
[JsonPropertyName("alpha_tooltip_background")] public double Atb { get; init; } = 0xE5;
36+
[JsonPropertyName("alpha_sidebar_background")] public double Asb { get; init; } = 0xD2;
37+
}

Model/GrayProfileConfig.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Text.Json.Serialization;
3+
4+
namespace PCL.Core.Model;
5+
6+
[Serializable]
7+
public record GrayProfileConfig
8+
{
9+
[JsonPropertyName("enabled")]
10+
public bool IsEnabled { get; init; } = false;
11+
12+
[JsonPropertyName("profile_light")]
13+
public GrayProfile Light { get; init; } = new()
14+
{
15+
L1 = 25, L2 = 45, L3 = 55, L4 = 65,
16+
L5 = 80, L6 = 91, L7 = 95, L8 = 97,
17+
G1 = 100, G2 = 98, G3 = 0,
18+
Sa0 = 1, Sa1 = 1, LaN = 0.5
19+
};
20+
21+
[JsonPropertyName("profile_dark")]
22+
public GrayProfile Dark { get; init; } = new()
23+
{
24+
L1 = 96, L2 = 75, L3 = 60, L4 = 65,
25+
L5 = 45, L6 = 25, L7 = 22, L8 = 20,
26+
G1 = 15, G2 = 20, G3 = 100,
27+
Sa0 = 1, Sa1 = 0.4, LaP = 0.75, LaN = 0.75
28+
};
29+
30+
public GrayProfile CurrentProfile(bool isDarkMode) => isDarkMode ? Dark : Light;
31+
}

PCL.Core.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@
167167
<Compile Include="Model\Files\FileProcess.cs" />
168168
<Compile Include="Model\Files\FileTransfer.cs" />
169169
<Compile Include="Model\Files\IFileTask.cs" />
170+
<Compile Include="Model\GrayProfile.cs" />
171+
<Compile Include="Model\GrayProfileConfig.cs" />
170172
<Compile Include="Model\Java.cs" />
171173
<Compile Include="Model\McPingResult.cs" />
172174
<Compile Include="Model\Net\HttpProxyManager.cs" />

Service/FileService.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading;
77
using PCL.Core.Helper;
88
using PCL.Core.LifecycleManagement;
9+
using PCL.Core.Model;
910
using PCL.Core.Model.Files;
1011
using PCL.Core.Utils;
1112
using PCL.Core.Utils.FileTask;
@@ -14,6 +15,22 @@
1415

1516
namespace PCL.Core.Service;
1617

18+
public static class PredefinedFileItems
19+
{
20+
public static readonly FileItem CacheInformation = FileItem.FromLocalFile("cache.txt", FileType.Temporary);
21+
public static readonly FileItem GrayProfile = FileItem.FromLocalFile("gray.json", FileType.Data);
22+
}
23+
24+
public static class PredefinedFileTasks
25+
{
26+
public static readonly IFileTask CacheInformation = FileTask.FromSingleFile(PredefinedFileItems.CacheInformation, FileTransfers.DoNothing);
27+
public static readonly IFileTask GrayProfile = FileTask.FromSingleFile(PredefinedFileItems.GrayProfile, FileTransfers.DoNothing, FileProcesses.ParseJson<GrayProfileConfig>());
28+
29+
internal static readonly IFileTask[] Preload = [
30+
CacheInformation, GrayProfile
31+
];
32+
}
33+
1734
/// <summary>
1835
/// Global file management service.
1936
/// </summary>
@@ -195,6 +212,7 @@ private static void _FileLoadCallback()
195212
{
196213
Context.Warn($"文件传输出错: {item}", ex);
197214
OnProcessFinished(item, ex);
215+
break;
198216
}
199217
}
200218
Context.Warn($"无支持的传输实现或全部失败: {item}");
@@ -323,16 +341,7 @@ private static void _Initialize()
323341
// TODO
324342

325343
// preload tasks
326-
QueueTask(new MatchableFileTask([
327-
PredefinedFileItems.CacheInformation
328-
], [
329-
FileMatches.Any.Pair(FileTransfers.Empty)
330-
]));
344+
QueueTask(PredefinedFileTasks.Preload);
331345
}
332346

333347
}
334-
335-
internal static class PredefinedFileItems
336-
{
337-
internal static readonly FileItem CacheInformation = FileItem.FromLocalFile("cache.txt", FileType.Temporary);
338-
}

Utils/ExpandoObjectConverter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
// 不知道 o4-mini 到底从哪找来的这段源码,据它所说是 .NET 8 标准库里面的
55
// 我找了半天没找到来源,但是这个 Converter 确实是能用的
6-
// .NET 标准库的源码以 MIT 许可证开源,所以先把它的许可丢在这里,等后面找到真的来源了再说
6+
// .NET 标准库的源码以 MIT 许可证开源,不论如何先把它的许可丢在这里
7+
// 注:这段源码有一小部分内容是经过修改的
78

89
using System;
910
using System.Collections.Generic;
@@ -19,6 +20,8 @@ namespace PCL.Core.Utils;
1920
public sealed class ExpandoObjectConverter : JsonConverter<ExpandoObject>
2021
{
2122
public ExpandoObjectConverter() { }
23+
24+
public static readonly ExpandoObjectConverter Default = new ExpandoObjectConverter();
2225

2326
public override ExpandoObject Read(
2427
ref Utf8JsonReader reader,

0 commit comments

Comments
 (0)