using Newtonsoft.Json.Linq;
namespace XFEWarframe.Desktop.Editing;
public sealed record EditorCategory(string Key, string Label)
{
public string Label { get; init; } = UiText.Translate(Label);
}
public sealed record EditorItem(string Key, string Name, string Detail, string Tags = "", bool CanEdit = true);
public sealed record EditorOption(JToken Value, string Label)
{
public string Label { get; init; } = UiText.Translate(Label);
public string Icon { get; init; } = "";
}
public sealed record EditorAction(string Key, string Label, string Description, JArray Fields, string Preview)
{
public string Label { get; init; } = UiText.Translate(Label);
public string Description { get; init; } = UiText.Translate(Description);
}
public sealed class EditorField(JObject schema)
{
public string Key { get; } = schema.Value<string>("key") ?? "";
public string Label { get; } = UiText.Translate(schema.Value<string>("label") ?? "");
public string Kind { get; } = schema.Value<string>("type") ?? "text";
public double Minimum { get; } = schema.Value<double?>("min") ?? 0;
public double Maximum { get; } = schema.Value<double?>("max") ?? 1000000;
public double Step { get; } = schema.Value<double?>("step") ?? 1;
public bool Integer { get; } = schema.Value<bool?>("integer") ?? false;
public JArray Ranges { get; } = schema["ranges"] as JArray ?? [];
public IReadOnlyList<EditorOption> Options { get; } = (schema["options"] as JArray ?? []).Select(x => new EditorOption(x["value"]!.DeepClone(), x.Value<string>("label") ?? "") { Icon = x.Value<string>("icon") ?? "" }).ToArray();
private readonly JToken original = schema["value"]?.DeepClone() ?? JValue.CreateNull();
private JToken value = schema["value"]?.DeepClone() ?? JValue.CreateNull();
public JToken Value { get => value; set { if (JToken.DeepEquals(this.value, value)) return; this.value = value; ValueChanged?.Invoke(this, EventArgs.Empty); } }
public event EventHandler? ValueChanged;
public string ChoiceQuery { get; set; } = "";
private bool hasUnresolvedChoice;
public bool HasUnresolvedChoice { get => hasUnresolvedChoice; set { if (hasUnresolvedChoice == value) return; hasUnresolvedChoice = value; ValueChanged?.Invoke(this, EventArgs.Empty); } }
public bool Changed => HasUnresolvedChoice || !JToken.DeepEquals(Value, original);
public void Reset() { ChoiceQuery = ""; HasUnresolvedChoice = false; Value = original.DeepClone(); }
public string DisplayValue => Kind switch
{
"bool" => UiText.Translate(Value.Value<bool>() ? "开启" : "关闭"),
"choice" => Options.FirstOrDefault(x => JToken.DeepEquals(x.Value, Value))?.Label ?? Value.ToString(),
"date" => Value.Value<long>() == 0 ? UiText.Translate("未设置") : DateTimeOffset.FromUnixTimeMilliseconds(Value.Value<long>()).ToLocalTime().ToString("g"),
_ => Value.ToString(Newtonsoft.Json.Formatting.None).Trim('"')
};
public JToken Serialize()
{
if (Kind == "choice" && (HasUnresolvedChoice || !Options.Any(x => JToken.DeepEquals(x.Value, Value))))
throw new InvalidOperationException(Label + ":请选择列表中的选项。");
if (Kind is "number" or "date" or "stat")
{
var number = Value.Value<double>();
if (!double.IsFinite(number) || number < Minimum || number > Maximum || Integer && number != Math.Truncate(number))
throw new InvalidOperationException(Label + " 必须在允许范围内" + (Integer ? ",且为整数。" : "。"));
return Integer ? new JValue((long)number) : new JValue(number);
}
return Value.DeepClone();
}
}
using Newtonsoft.Json.Linq;
namespace XFEWarframe.Desktop.Editing;
public sealed record EditorCategory(string Key, string Label)
{
public string Label { get; init; } = UiText.Translate(Label);
}
public sealed record EditorItem(string Key, string Name, string Detail, string Tags = "", bool CanEdit = true);
public sealed record EditorOption(JToken Value, string Label)
{
public string Label { get; init; } = UiText.Translate(Label);
public string Icon { get; init; } = "";
}
public sealed record EditorAction(string Key, string Label, string Description, JArray Fields, string Preview)
{
public string Label { get; init; } = UiText.Translate(Label);
public string Description { get; init; } = UiText.Translate(Description);
}
public sealed class EditorField(JObject schema)
{
public string Key { get; } = schema.Value<string>("key") ?? "";
public string Label { get; } = UiText.Translate(schema.Value<string>("label") ?? "");
public string Kind { get; } = schema.Value<string>("type") ?? "text";
public double Minimum { get; } = schema.Value<double?>("min") ?? 0;
public double Maximum { get; } = schema.Value<double?>("max") ?? 1000000;
public double Step { get; } = schema.Value<double?>("step") ?? 1;
public bool Integer { get; } = schema.Value<bool?>("integer") ?? false;
public JArray Ranges { get; } = schema["ranges"] as JArray ?? [];
public IReadOnlyList<EditorOption> Options { get; } = (schema["options"] as JArray ?? []).Select(x => new EditorOption(x["value"]!.DeepClone(), x.Value<string>("label") ?? "") { Icon = x.Value<string>("icon") ?? "" }).ToArray();
private readonly JToken original = schema["value"]?.DeepClone() ?? JValue.CreateNull();
private JToken value = schema["value"]?.DeepClone() ?? JValue.CreateNull();
public JToken Value { get => value; set { if (JToken.DeepEquals(this.value, value)) return; this.value = value; ValueChanged?.Invoke(this, EventArgs.Empty); } }
public event EventHandler? ValueChanged;
public string ChoiceQuery { get; set; } = "";
private bool hasUnresolvedChoice;
public bool HasUnresolvedChoice { get => hasUnresolvedChoice; set { if (hasUnresolvedChoice == value) return; hasUnresolvedChoice = value; ValueChanged?.Invoke(this, EventArgs.Empty); } }
public bool Changed => HasUnresolvedChoice || !JToken.DeepEquals(Value, original);
public void Reset() { ChoiceQuery = ""; HasUnresolvedChoice = false; Value = original.DeepClone(); }
public string DisplayValue => Kind switch
{
"bool" => UiText.Translate(Value.Value<bool>() ? "开启" : "关闭"),
"choice" => Options.FirstOrDefault(x => JToken.DeepEquals(x.Value, Value))?.Label ?? Value.ToString(),
"date" => Value.Value<long>() == 0 ? UiText.Translate("未设置") : DateTimeOffset.FromUnixTimeMilliseconds(Value.Value<long>()).ToLocalTime().ToString("g"),
_ => Value.ToString(Newtonsoft.Json.Formatting.None).Trim('"')
};
public JToken Serialize()
{
if (Kind == "choice" && (HasUnresolvedChoice || !Options.Any(x => JToken.DeepEquals(x.Value, Value))))
throw new InvalidOperationException(Label + ":请选择列表中的选项。");
if (Kind is "number" or "date" or "stat")
{
var number = Value.Value<double>();
if (!double.IsFinite(number) || number < Minimum || number > Maximum || Integer && number != Math.Truncate(number))
throw new InvalidOperationException(Label + " 必须在允许范围内" + (Integer ? ",且为整数。" : "。"));
return Integer ? new JValue((long)number) : new JValue(number);
}
return Value.DeepClone();
}
}