mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-08-31 06:26:18 +00:00
Move some style code to lib_ui.
This commit is contained in:
@@ -53,6 +53,11 @@ CppFile &CppFile::include(const QString &header) {
|
||||
return newline();
|
||||
}
|
||||
|
||||
CppFile &CppFile::includeFromLibrary(const QString &header) {
|
||||
stream() << "#include <" << header << ">";
|
||||
return newline();
|
||||
}
|
||||
|
||||
CppFile &CppFile::pushNamespace(const QString &name) {
|
||||
namespaces_.push_back(name);
|
||||
|
||||
|
@@ -36,6 +36,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
CppFile &include(const QString &header);
|
||||
CppFile &includeFromLibrary(const QString &header);
|
||||
|
||||
// Empty name adds anonymous namespace.
|
||||
CppFile &pushNamespace(const QString &name = QString());
|
||||
|
@@ -396,6 +396,22 @@ void Init() {\n\
|
||||
|
||||
bool Generator::writeHeader() {
|
||||
auto header = std::make_unique<common::CppFile>(outputPath_ + ".h", project_);
|
||||
header->includeFromLibrary("QtCore/QChar");
|
||||
header->includeFromLibrary("QtCore/QString");
|
||||
header->includeFromLibrary("QtCore/QVector");
|
||||
header->newline();
|
||||
header->includeFromLibrary("vector");
|
||||
header->newline();
|
||||
|
||||
header->pushNamespace("Ui").pushNamespace("Emoji");
|
||||
header->stream() << "class One;\n";
|
||||
header->popNamespace().popNamespace().newline();
|
||||
|
||||
header->stream() << "\
|
||||
using EmojiPtr = const Ui::Emoji::One*;\n\
|
||||
using EmojiPack = QVector<EmojiPtr>;\n\
|
||||
\n";
|
||||
|
||||
header->pushNamespace("Ui").pushNamespace("Emoji").pushNamespace("internal");
|
||||
header->stream() << "\
|
||||
\n\
|
||||
@@ -406,20 +422,6 @@ EmojiPtr ByIndex(int index);\n\
|
||||
\n\
|
||||
EmojiPtr Find(const QChar *ch, const QChar *end, int *outLength = nullptr);\n\
|
||||
\n\
|
||||
inline bool IsReplaceEdge(const QChar *ch) {\n\
|
||||
return true;\n\
|
||||
\n\
|
||||
// switch (ch->unicode()) {\n\
|
||||
// case '.': case ',': case ':': case ';': case '!': case '?': case '#': case '@':\n\
|
||||
// case '(': case ')': case '[': case ']': case '{': case '}': case '<': case '>':\n\
|
||||
// case '+': case '=': case '-': case '_': case '*': case '/': case '\\\\': case '^': case '$':\n\
|
||||
// case '\"': case '\\'':\n\
|
||||
// case 8212: case 171: case 187: // --, <<, >>\n\
|
||||
// return true;\n\
|
||||
// }\n\
|
||||
// return false;\n\
|
||||
}\n\
|
||||
\n\
|
||||
const std::vector<std::pair<QString, int>> GetReplacementPairs();\n\
|
||||
EmojiPtr FindReplace(const QChar *ch, const QChar *end, int *outLength = nullptr);\n\
|
||||
\n";
|
||||
@@ -439,7 +441,7 @@ enum class Section {\n\
|
||||
};\n\
|
||||
\n\
|
||||
int GetSectionCount(Section section);\n\
|
||||
EmojiPack GetSection(Section section);\n\
|
||||
QVector<const One*> GetSection(Section section);\n\
|
||||
\n";
|
||||
return header->finalize();
|
||||
}
|
||||
@@ -558,8 +560,9 @@ bool Generator::writeGetSections() {
|
||||
source_->stream() << "\
|
||||
\n\
|
||||
int GetSectionCount(Section section) {\n\
|
||||
switch (section) {\n\
|
||||
case Section::Recent: return GetRecent().size();\n";
|
||||
Expects(section != Section::Recent);\n\
|
||||
\n\
|
||||
switch (section) {\n";
|
||||
auto countIndex = 0;
|
||||
for (auto name : sectionNames) {
|
||||
if (countIndex >= int(data_.categories.size())) {
|
||||
@@ -575,15 +578,9 @@ int GetSectionCount(Section section) {\n\
|
||||
}\n\
|
||||
\n\
|
||||
EmojiPack GetSection(Section section) {\n\
|
||||
switch (section) {\n\
|
||||
case Section::Recent: {\n\
|
||||
auto result = EmojiPack();\n\
|
||||
result.reserve(GetRecent().size());\n\
|
||||
for (auto &item : GetRecent()) {\n\
|
||||
result.push_back(item.first);\n\
|
||||
}\n\
|
||||
return result;\n\
|
||||
} break;\n";
|
||||
Expects(section != Section::Recent);\n\
|
||||
\n\
|
||||
switch (section) {\n";
|
||||
auto index = 0;
|
||||
auto offset = 0;
|
||||
for (auto name : sectionNames) {
|
||||
@@ -614,7 +611,7 @@ bool Generator::writeFindReplace() {
|
||||
const std::vector<std::pair<QString, int>> ReplacementPairs = {\n";
|
||||
for (const auto &[what, index] : data_.replaces) {
|
||||
source_->stream() << "\
|
||||
{ qsl(\"" << what << "\"), " << index << " },\n";
|
||||
{ \"" << what << "\", " << index << " },\n";
|
||||
}
|
||||
source_->stream() << "\
|
||||
};\n\
|
||||
@@ -766,10 +763,6 @@ bool Generator::writeFindFromDictionary(
|
||||
source_->stream() << tabs(tabsUsed) << "if (outLength) *outLength = (ch - start);\n";
|
||||
}
|
||||
|
||||
// While IsReplaceEdge() currently is always true we just return the value.
|
||||
//source_->stream() << tabs(1 + chars.size()) << "if (ch + " << chars.size() << " == end || IsReplaceEdge(*(ch + " << chars.size() << ")) || (ch + " << chars.size() << ")->unicode() == ' ') {\n";
|
||||
//source_->stream() << tabs(1 + chars.size()) << "\treturn &Items[" << item.second << "];\n";
|
||||
//source_->stream() << tabs(1 + chars.size()) << "}\n";
|
||||
source_->stream() << tabs(tabsUsed) << "return " << (item.second + 1) << ";\n";
|
||||
}
|
||||
finishChecksTillKey(QString());
|
||||
|
@@ -210,6 +210,9 @@ bool Generator::writeHeader() {
|
||||
|
||||
header_->include("ui/style/style_core.h").newline();
|
||||
|
||||
if (!writeHeaderRequiredIncludes()) {
|
||||
return false;
|
||||
}
|
||||
if (!writeHeaderStyleNamespace()) {
|
||||
return false;
|
||||
}
|
||||
@@ -233,12 +236,9 @@ bool inited = false;\n\
|
||||
class Module_" << baseName_ << " : public style::internal::ModuleBase {\n\
|
||||
public:\n\
|
||||
Module_" << baseName_ << "() { style::internal::registerModule(this); }\n\
|
||||
~Module_" << baseName_ << "() { style::internal::unregisterModule(this); }\n\
|
||||
\n\
|
||||
void start() override {\n\
|
||||
style::internal::init_" << baseName_ << "();\n\
|
||||
}\n\
|
||||
void stop() override {\n\
|
||||
void start(int scale) override {\n\
|
||||
style::internal::init_" << baseName_ << "(scale);\n\
|
||||
}\n\
|
||||
};\n\
|
||||
Module_" << baseName_ << " registrator;\n";
|
||||
@@ -397,6 +397,50 @@ QString Generator::valueAssignmentCode(structure::Value value) const {
|
||||
return QString();
|
||||
}
|
||||
|
||||
bool Generator::writeHeaderRequiredIncludes() {
|
||||
std::function<QString(const Module&, structure::FullName)> findInIncludes = [&](const Module &module, const structure::FullName &name) {
|
||||
auto result = QString();
|
||||
module.enumIncludes([&](const Module &included) {
|
||||
if (Module::findStructInModule(name, included)) {
|
||||
result = moduleBaseName(included);
|
||||
return false;
|
||||
}
|
||||
result = findInIncludes(included, name);
|
||||
return true;
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
auto includes = QStringList();
|
||||
const auto written = module_.enumStructs([&](const Struct &value) -> bool {
|
||||
for (const auto &field : value.fields) {
|
||||
if (field.type.tag == structure::TypeTag::Struct) {
|
||||
const auto name = field.type.name;
|
||||
if (!module_.findStructInModule(name, module_)) {
|
||||
const auto base = findInIncludes(module_, name);
|
||||
if (base.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (!includes.contains(base)) {
|
||||
includes.push_back(base);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (!written) {
|
||||
return false;
|
||||
} else if (includes.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
for (const auto base : includes) {
|
||||
header_->include(base + ".h");
|
||||
}
|
||||
header_->newline();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Generator::writeHeaderStyleNamespace() {
|
||||
if (!module_.hasStructs() && !module_.hasVariables()) {
|
||||
return true;
|
||||
@@ -405,7 +449,7 @@ bool Generator::writeHeaderStyleNamespace() {
|
||||
|
||||
if (module_.hasVariables()) {
|
||||
header_->pushNamespace("internal").newline();
|
||||
header_->stream() << "void init_" << baseName_ << "();\n\n";
|
||||
header_->stream() << "void init_" << baseName_ << "(int scale);\n\n";
|
||||
header_->popNamespace();
|
||||
}
|
||||
bool wroteForwardDeclarations = writeStructsForwardDeclarations();
|
||||
@@ -1076,7 +1120,7 @@ bool Generator::writeVariableInit() {
|
||||
}
|
||||
|
||||
source_->stream() << "\
|
||||
void init_" << baseName_ << "() {\n\
|
||||
void init_" << baseName_ << "(int scale) {\n\
|
||||
if (inited) return;\n\
|
||||
inited = true;\n\n";
|
||||
|
||||
@@ -1084,7 +1128,7 @@ void init_" << baseName_ << "() {\n\
|
||||
bool writtenAtLeastOne = false;
|
||||
bool result = module_.enumIncludes([&](const Module &module) -> bool {
|
||||
if (module.hasVariables()) {
|
||||
source_->stream() << "\tinit_" + moduleBaseName(module) + "();\n";
|
||||
source_->stream() << "\tinit_" + moduleBaseName(module) + "(scale);\n";
|
||||
writtenAtLeastOne = true;
|
||||
}
|
||||
return true;
|
||||
@@ -1099,7 +1143,7 @@ void init_" << baseName_ << "() {\n\
|
||||
|
||||
if (!pxValues_.isEmpty() || !fontFamilies_.isEmpty()) {
|
||||
if (!pxValues_.isEmpty()) {
|
||||
source_->stream() << "\tinitPxValues();\n";
|
||||
source_->stream() << "\tinitPxValues(scale);\n";
|
||||
}
|
||||
if (!fontFamilies_.isEmpty()) {
|
||||
source_->stream() << "\tinitFontFamilies();\n";
|
||||
@@ -1134,8 +1178,7 @@ bool Generator::writePxValuesInit() {
|
||||
source_->stream() << "int " << pxValueName(i.key()) << " = " << i.key() << ";\n";
|
||||
}
|
||||
source_->stream() << "\
|
||||
void initPxValues() {\n\
|
||||
const auto scale = cScale();\n";
|
||||
void initPxValues(int scale) {\n";
|
||||
for (auto it = pxValues_.cbegin(), e = pxValues_.cend(); it != e; ++it) {
|
||||
auto value = it.key();
|
||||
source_->stream() << "\t" << pxValueName(value) << " = ConvertScale(" << value << ", scale);\n";
|
||||
|
@@ -35,6 +35,7 @@ private:
|
||||
QString typeToDefaultValue(structure::Type type) const;
|
||||
QString valueAssignmentCode(structure::Value value) const;
|
||||
|
||||
bool writeHeaderRequiredIncludes();
|
||||
bool writeHeaderStyleNamespace();
|
||||
bool writeStructsForwardDeclarations();
|
||||
bool writeStructsDefinitions();
|
||||
|
@@ -69,7 +69,7 @@ const Variable *Module::findVariable(const FullName &name, bool *outFromThisModu
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const Struct *Module::findStructInModule(const FullName &name, const Module &module) const {
|
||||
const Struct *Module::findStructInModule(const FullName &name, const Module &module) {
|
||||
auto index = module.structsByName_.value(fullNameKey(name), -1);
|
||||
if (index < 0) {
|
||||
return nullptr;
|
||||
@@ -77,7 +77,7 @@ const Struct *Module::findStructInModule(const FullName &name, const Module &mod
|
||||
return &module.structs_.at(index);
|
||||
}
|
||||
|
||||
const Variable *Module::findVariableInModule(const FullName &name, const Module &module) const {
|
||||
const Variable *Module::findVariableInModule(const FullName &name, const Module &module) {
|
||||
auto index = module.variablesByName_.value(fullNameKey(name), -1);
|
||||
if (index < 0) {
|
||||
return nullptr;
|
||||
|
@@ -81,8 +81,8 @@ public:
|
||||
return !fullpath_.isEmpty();
|
||||
}
|
||||
|
||||
const Struct *findStructInModule(const FullName &name, const Module &module) const;
|
||||
const Variable *findVariableInModule(const FullName &name, const Module &module) const;
|
||||
static const Struct *findStructInModule(const FullName &name, const Module &module);
|
||||
static const Variable *findVariableInModule(const FullName &name, const Module &module);
|
||||
|
||||
private:
|
||||
QString fullpath_;
|
||||
|
Reference in New Issue
Block a user