297 lines
10 KiB
C++
297 lines
10 KiB
C++
// P4-43 / P4-59 官方 UI 主题插件(ABI 1.8;纯 C ABI,不链接内核)。
|
||
// options: {"theme":"dark|light|high_contrast|custom",
|
||
// "colors":{"background":"#RRGGBB","foreground":"#RRGGBB",
|
||
// "accent":"#RRGGBB","border":"#RRGGBB","selection":"#RRGGBB"}}
|
||
// custom 时 colors 至少提供一个合法 hex 颜色;未提供的键回退 dark 主题值。
|
||
// 输出:{"ui_id","version","theme","colors",{"fonts"},"line_widths"},确定性字段顺序。
|
||
#include <cctype>
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <map>
|
||
#include <string>
|
||
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
|
||
namespace {
|
||
|
||
constexpr const char* kUiId = "ui.theme";
|
||
constexpr int kThemeVersion = 1;
|
||
|
||
struct ThemeColors {
|
||
std::string background;
|
||
std::string foreground;
|
||
std::string accent;
|
||
std::string border;
|
||
std::string selection;
|
||
};
|
||
|
||
struct ThemeChoice {
|
||
std::string theme;
|
||
ThemeColors colors;
|
||
};
|
||
|
||
ThemeColors dark_colors() {
|
||
return {"#1E1E1E", "#E6E6E6", "#64B5F6", "#3C3C3C", "#264F78"};
|
||
}
|
||
ThemeColors light_colors() {
|
||
return {"#FFFFFF", "#1A1A1A", "#1565C0", "#C8C8C8", "#BBDEFB"};
|
||
}
|
||
ThemeColors high_contrast_colors() {
|
||
return {"#000000", "#FFFFFF", "#FFFF00", "#FFFFFF", "#00FFFF"};
|
||
}
|
||
|
||
std::string trim_ascii(const std::string& text) {
|
||
std::size_t begin = 0, end = text.size();
|
||
while (begin < end && std::isspace(static_cast<unsigned char>(text[begin]))) ++begin;
|
||
while (end > begin && std::isspace(static_cast<unsigned char>(text[end - 1]))) --end;
|
||
return text.substr(begin, end - begin);
|
||
}
|
||
|
||
/// 在简单 JSON 对象中查字符串字段(不处理转义;值仅用于主题/语言短串)。
|
||
bool find_string_field(const std::string& json, const std::string& key, std::string* value) {
|
||
const std::string needle = "\"" + key + "\"";
|
||
std::size_t pos = json.find(needle);
|
||
if (pos == std::string::npos) return false;
|
||
pos = json.find(':', pos + needle.size());
|
||
if (pos == std::string::npos) return false;
|
||
++pos;
|
||
while (pos < json.size() && std::isspace(static_cast<unsigned char>(json[pos]))) ++pos;
|
||
if (pos >= json.size() || json[pos] != '"') return false;
|
||
++pos;
|
||
const std::size_t end = json.find('"', pos);
|
||
if (end == std::string::npos) return false;
|
||
*value = json.substr(pos, end - pos);
|
||
return true;
|
||
}
|
||
|
||
/// 取 key 对应的对象文本(大括号内),括号配对考虑引号。
|
||
bool find_object_field(const std::string& json, const std::string& key, std::string* object) {
|
||
const std::string needle = "\"" + key + "\"";
|
||
std::size_t pos = json.find(needle);
|
||
if (pos == std::string::npos) return false;
|
||
pos = json.find('{', pos + needle.size());
|
||
if (pos == std::string::npos) return false;
|
||
const std::size_t begin = pos + 1;
|
||
int depth = 1;
|
||
bool in_string = false;
|
||
for (++pos; pos < json.size(); ++pos) {
|
||
const char c = json[pos];
|
||
if (in_string) {
|
||
if (c == '\\') {
|
||
++pos;
|
||
} else if (c == '"') {
|
||
in_string = false;
|
||
}
|
||
continue;
|
||
}
|
||
if (c == '"') {
|
||
in_string = true;
|
||
} else if (c == '{') {
|
||
++depth;
|
||
} else if (c == '}') {
|
||
--depth;
|
||
if (depth == 0) {
|
||
*object = json.substr(begin, pos - begin);
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool is_hex_color(const std::string& text) {
|
||
if (text.size() != 7 || text[0] != '#') return false;
|
||
for (std::size_t i = 1; i < text.size(); ++i) {
|
||
if (!std::isxdigit(static_cast<unsigned char>(text[i]))) return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// 解析 colors 对象中的键值(只接受顶层 "key":"value" 字符串)。
|
||
bool parse_colors_object(const std::string& object, std::map<std::string, std::string>* colors,
|
||
std::string* error) {
|
||
std::size_t pos = 0;
|
||
while (true) {
|
||
const std::size_t key_begin = object.find('"', pos);
|
||
if (key_begin == std::string::npos) break;
|
||
const std::size_t key_end = object.find('"', key_begin + 1);
|
||
if (key_end == std::string::npos) break;
|
||
const std::string key = object.substr(key_begin + 1, key_end - key_begin - 1);
|
||
pos = key_end + 1;
|
||
const std::size_t colon = object.find(':', pos);
|
||
if (colon == std::string::npos) break;
|
||
pos = colon + 1;
|
||
while (pos < object.size() && std::isspace(static_cast<unsigned char>(object[pos])))
|
||
++pos;
|
||
if (pos >= object.size() || object[pos] != '"') break;
|
||
const std::size_t value_end = object.find('"', pos + 1);
|
||
if (value_end == std::string::npos) break;
|
||
const std::string value = trim_ascii(object.substr(pos + 1, value_end - pos - 1));
|
||
pos = value_end + 1;
|
||
if (!is_hex_color(value)) {
|
||
if (error) *error = "ui_theme: invalid hex color for '" + key + "': " + value;
|
||
return false;
|
||
}
|
||
(*colors)[key] = value;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// 解析 options → (theme, colors);返回 false 时写 error(INVALID_ARG)。
|
||
bool resolve_theme(const char* options_json, ThemeChoice* choice, std::string* error) {
|
||
choice->theme = "dark";
|
||
choice->colors = dark_colors();
|
||
if (options_json == nullptr || *options_json == '\0') return true;
|
||
const std::string json(options_json);
|
||
std::string theme;
|
||
if (find_string_field(json, "theme", &theme) && !trim_ascii(theme).empty()) {
|
||
choice->theme = trim_ascii(theme);
|
||
}
|
||
if (choice->theme == "dark") {
|
||
choice->colors = dark_colors();
|
||
return true;
|
||
}
|
||
if (choice->theme == "light") {
|
||
choice->colors = light_colors();
|
||
return true;
|
||
}
|
||
if (choice->theme == "high_contrast") {
|
||
choice->colors = high_contrast_colors();
|
||
return true;
|
||
}
|
||
if (choice->theme != "custom") {
|
||
if (error) *error = "ui_theme: unknown theme '" + choice->theme +
|
||
"' (expect dark|light|high_contrast|custom)";
|
||
return false;
|
||
}
|
||
// custom:colors 对象可选键叠加在 dark 基础上
|
||
std::string colors_text;
|
||
if (!find_object_field(json, "colors", &colors_text)) {
|
||
if (error) *error = "ui_theme: custom theme requires non-empty colors object";
|
||
return false;
|
||
}
|
||
std::map<std::string, std::string> colors;
|
||
if (!parse_colors_object(colors_text, &colors, error)) return false;
|
||
if (colors.empty()) {
|
||
if (error) *error = "ui_theme: custom theme requires at least one color";
|
||
return false;
|
||
}
|
||
for (const auto& entry : colors) {
|
||
if (entry.first == "background") choice->colors.background = entry.second;
|
||
else if (entry.first == "foreground") choice->colors.foreground = entry.second;
|
||
else if (entry.first == "accent") choice->colors.accent = entry.second;
|
||
else if (entry.first == "border") choice->colors.border = entry.second;
|
||
else if (entry.first == "selection") choice->colors.selection = entry.second;
|
||
// 未知颜色键忽略(向前兼容)
|
||
}
|
||
return true;
|
||
}
|
||
|
||
std::string build_theme_json(const ThemeChoice& choice) {
|
||
std::string json;
|
||
json.reserve(512);
|
||
json += "{\"ui_id\":\"";
|
||
json += kUiId;
|
||
json += "\",\"version\":";
|
||
json += std::to_string(kThemeVersion);
|
||
json += ",\"theme\":\"";
|
||
json += choice.theme;
|
||
json += "\",\"colors\":{\"background\":\"";
|
||
json += choice.colors.background;
|
||
json += "\",\"foreground\":\"";
|
||
json += choice.colors.foreground;
|
||
json += "\",\"accent\":\"";
|
||
json += choice.colors.accent;
|
||
json += "\",\"border\":\"";
|
||
json += choice.colors.border;
|
||
json += "\",\"selection\":\"";
|
||
json += choice.colors.selection;
|
||
json += "\"},\"fonts\":{\"ui_family\":\"Segoe UI\",\"mono_family\":\"Consolas\","
|
||
"\"size_pt\":10},\"line_widths\":{\"border\":1,\"focus\":2,\"grid\":1}}";
|
||
return json;
|
||
}
|
||
|
||
int get_theme_json(const VdePluginHost* host, const char* options_json,
|
||
VdeBuffer* result_json) {
|
||
if (result_json) {
|
||
result_json->data = nullptr;
|
||
result_json->size = 0;
|
||
}
|
||
if (!host || !host->alloc || !host->set_error || !result_json)
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
ThemeChoice choice;
|
||
std::string error;
|
||
if (!resolve_theme(options_json, &choice, &error)) {
|
||
host->set_error(error.c_str());
|
||
return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
}
|
||
const std::string json = build_theme_json(choice);
|
||
void* out = host->alloc(json.size() + 1);
|
||
if (!out) {
|
||
host->set_error("ui_theme: alloc failed");
|
||
return VDE_PLUGIN_ERR_BUFFER;
|
||
}
|
||
std::memcpy(out, json.data(), json.size() + 1);
|
||
result_json->data = out;
|
||
result_json->size = json.size();
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
const char* describe() {
|
||
return "VDE official UI theme (dark/light/high_contrast/custom; ABI 1.8)";
|
||
}
|
||
|
||
const VdeUiPlugin kUi = [] {
|
||
VdeUiPlugin plugin{};
|
||
plugin.struct_size = sizeof(VdeUiPlugin);
|
||
plugin.ui_id = kUiId;
|
||
plugin.capabilities_json =
|
||
"{\"themes\":[\"dark\",\"light\",\"high_contrast\",\"custom\"],"
|
||
"\"custom_colors\":[\"background\",\"foreground\",\"accent\",\"border\","
|
||
"\"selection\"]}";
|
||
plugin.get_theme_json = &get_theme_json;
|
||
plugin.describe = &describe;
|
||
return plugin;
|
||
}();
|
||
|
||
} // namespace
|
||
|
||
extern "C" {
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_query(std::uint32_t host_abi_major, VdePluginInfo* info) {
|
||
if (!info || info->struct_size < sizeof(VdePluginInfo)) return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
if (host_abi_major != VDE_PLUGIN_ABI_MAJOR) return VDE_PLUGIN_ERR_ABI;
|
||
info->id = "com.vde.official.ui.theme";
|
||
info->name = "VDE Official UI Theme";
|
||
info->version = "1.0.0";
|
||
info->kind = VDE_PLUGIN_KIND_UI;
|
||
info->abi_major = VDE_PLUGIN_ABI_MAJOR;
|
||
info->abi_minor = VDE_PLUGIN_ABI_MINOR;
|
||
info->capabilities = "ui:theme";
|
||
info->license = "Apache-2.0";
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT int vde_plugin_init(const VdePluginHost* host, VdePluginRegistration* reg) {
|
||
(void)host;
|
||
if (!reg) return VDE_PLUGIN_ERR_INVALID_ARG;
|
||
if (reg->struct_size < sizeof(VdePluginRegistration)) return VDE_PLUGIN_ERR_ABI;
|
||
reg->kind = VDE_PLUGIN_KIND_UI;
|
||
reg->format = nullptr;
|
||
reg->generic = nullptr;
|
||
reg->cae_solver = nullptr;
|
||
reg->cam_strategy = nullptr;
|
||
reg->gcs_solver = nullptr;
|
||
reg->mesher = nullptr;
|
||
reg->optimizer = nullptr;
|
||
reg->io_batch = nullptr;
|
||
reg->geom_op = nullptr;
|
||
reg->ui = &kUi;
|
||
return VDE_PLUGIN_OK;
|
||
}
|
||
|
||
VDE_PLUGIN_EXPORT void vde_plugin_shutdown() {}
|
||
|
||
} // extern "C"
|