Files
msime-windows/server/src/utils/surface_theme_config.cpp
T
Evan 42238a2af6 style: 用 .clang-format 重排 server / windows / ui / log
一次性重排,无任何行为改动。四个目录各自的 .clang-format 放在仓库里已久,但全组织只有
MSIME-Linux 在 CI 里执行 clang-format 检查,这边 149 个文件里有 149 个不符合自己声明的风格。

已逐文件核对语义等价:去掉空白、注释与相邻字符串字面量拆分之后,全部 149 个文件与重排前逐字节
相同。差异只有三类——缩进与换行、clang-format 补的 } // namespace 结尾注释、以及长字符串被拆成
相邻字面量(C++ 保证自动拼接)。

这个提交会写进 .git-blame-ignore-revs。
2026-09-06 12:13:47 +09:00

91 lines
2.9 KiB
C++

#include "surface_theme_config.h"
#include "utils/common_utils.h"
#include <Windows.h>
#include <filesystem>
#include <fstream>
#include <string>
#include <unordered_map>
namespace
{
std::string Trim(std::string value)
{
const size_t first = value.find_first_not_of(" \t\r");
if (first == std::string::npos)
return {};
const size_t last = value.find_last_not_of(" \t\r");
return value.substr(first, last - first + 1);
}
std::string Unquote(std::string value)
{
value = Trim(std::move(value));
if (value.size() >= 2 && (value.front() == '"' || value.front() == '\'') && value.back() == value.front())
return value.substr(1, value.size() - 2);
return value;
}
std::unordered_map<std::string, std::string> ReadAppearance()
{
std::unordered_map<std::string, std::string> values;
const std::wstring ime_data = CommonUtils::get_ime_data_path_w();
if (ime_data.empty())
return values;
std::ifstream input(std::filesystem::path(ime_data) / L"config.toml");
bool in_appearance = false;
for (std::string line; std::getline(input, line);)
{
const std::string trimmed = Trim(line);
if (!trimmed.empty() && trimmed.front() == '[')
{
in_appearance = trimmed == "[appearance]";
continue;
}
if (!in_appearance || trimmed.empty() || trimmed.front() == '#')
continue;
const size_t equals = trimmed.find('=');
if (equals == std::string::npos)
continue;
std::string value = trimmed.substr(equals + 1);
const size_t comment = value.find('#');
if (comment != std::string::npos)
value.resize(comment);
values[Trim(trimmed.substr(0, equals))] = Unquote(std::move(value));
}
return values;
}
bool IsWindowsAppsLight()
{
HKEY key = nullptr;
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 0,
KEY_READ, &key) != ERROR_SUCCESS)
return false;
DWORD value = 0;
DWORD size = sizeof(value);
const LONG result =
RegQueryValueExW(key, L"AppsUseLightTheme", nullptr, nullptr, reinterpret_cast<LPBYTE>(&value), &size);
RegCloseKey(key);
return result == ERROR_SUCCESS && value != 0;
}
} // namespace
bool SurfaceThemeConfig::IsLight(const char *surface_key)
{
const auto values = ReadAppearance();
const auto surface_it = values.find(surface_key);
const std::string surface = surface_it == values.end() ? "follow" : surface_it->second;
if (surface == "light")
return true;
if (surface == "dark")
return false;
const auto mode_it = values.find("theme_mode");
const std::string mode = mode_it == values.end() ? "dark" : mode_it->second;
if (mode == "light")
return true;
return (mode == "system" || mode == "auto") && IsWindowsAppsLight();
}