label: add relative values for font_size

This commit is contained in:
NotLebedev 2025-02-02 22:29:16 +03:00
parent c976b6a1d1
commit f0141ebf0d
No known key found for this signature in database
GPG key ID: E44DA505D03C700E
3 changed files with 92 additions and 3 deletions

View file

@ -14,6 +14,7 @@ enum eConfigValueDataTypes {
CVD_TYPE_INVALID = -1,
CVD_TYPE_LAYOUT = 0,
CVD_TYPE_GRADIENT = 1,
CVD_TYPE_FONT_SIZE = 2,
};
class ICustomConfigValueData {
@ -131,3 +132,57 @@ class CGradientValueData : public ICustomConfigValueData {
return P;
}
};
enum eFontRelativeTypes {
FR_TYPE_ABSOLUTE = 0,
FR_TYPE_VIEW_WIDTH = 1,
FR_TYPE_VIEW_HEIGHT = 2,
};
class CFontSizeValueData : public ICustomConfigValueData {
public:
CFontSizeValueData() {}
virtual ~CFontSizeValueData() {};
virtual eConfigValueDataTypes getDataType() {
return CVD_TYPE_FONT_SIZE;
}
static CFontSizeValueData* fromAnyPv(const std::any& v) {
RASSERT(v.type() == typeid(void*), "Invalid config value type");
const auto P = (CFontSizeValueData*)std::any_cast<void*>(v);
RASSERT(P, "Empty config value");
return P;
}
virtual std::string toString() {
return std::format("{}{}", m_size, valueSuffix());
}
const char *valueSuffix() {
switch (m_relativeTo) {
case FR_TYPE_VIEW_WIDTH:
return "vw";
case FR_TYPE_VIEW_HEIGHT:
return "vh";
case FR_TYPE_ABSOLUTE:
default:
return "px";
}
}
int getAbsolute(const Hyprutils::Math::Vector2D& viewport) {
switch (m_relativeTo) {
case FR_TYPE_VIEW_WIDTH:
return std::round((m_size / 100) * viewport.x);
case FR_TYPE_VIEW_HEIGHT:
return std::round((m_size / 100) * viewport.y);
case FR_TYPE_ABSOLUTE:
default:
return std::round(m_size);
}
}
float m_size;
eFontRelativeTypes m_relativeTo = FR_TYPE_ABSOLUTE;
};

View file

@ -95,11 +95,41 @@ static Hyprlang::CParseResult configHandleLayoutOption(const char* v, void** dat
return result;
}
static Hyprlang::CParseResult configHandleFontSizeOption(const char* v, void** data) {
const std::string VALUE = v;
Hyprlang::CParseResult result;
if (!*data)
*data = new CFontSizeValueData();
const auto DATA = (CFontSizeValueData*)(*data);
std::string stripped;
if (VALUE.ends_with("vw")) {
DATA->m_relativeTo = FR_TYPE_VIEW_WIDTH;
stripped = VALUE.substr(0, VALUE.size() - 3);
} else if (VALUE.ends_with("vh")) {
DATA->m_relativeTo = FR_TYPE_VIEW_HEIGHT;
stripped = VALUE.substr(0, VALUE.size() - 3);
} else {
stripped = VALUE;
}
DATA->m_size = std::stof(VALUE);
return result;
}
static void configHandleLayoutOptionDestroy(void** data) {
if (*data)
delete reinterpret_cast<CLayoutValueData*>(*data);
}
static void configHandleFontSizeOptionDestroy(void** data) {
if (*data)
delete reinterpret_cast<CFontSizeValueData*>(*data);
}
static Hyprlang::CParseResult configHandleGradientSet(const char* VALUE, void** data) {
const std::string V = VALUE;
@ -203,6 +233,10 @@ inline static constexpr auto LAYOUTCONFIG = [](const char* default_value) -> Hyp
return Hyprlang::CUSTOMTYPE{&configHandleLayoutOption, configHandleLayoutOptionDestroy, default_value};
};
inline static constexpr auto FONTSIZECONFIG = [](const char* default_value) -> Hyprlang::CUSTOMTYPE {
return Hyprlang::CUSTOMTYPE{&configHandleFontSizeOption, configHandleFontSizeOptionDestroy, default_value};
};
void CConfigManager::init() {
#define SHADOWABLE(name) \
@ -312,7 +346,7 @@ void CConfigManager::init() {
m_config.addSpecialConfigValue("label", "monitor", Hyprlang::STRING{""});
m_config.addSpecialConfigValue("label", "position", LAYOUTCONFIG("0,0"));
m_config.addSpecialConfigValue("label", "color", Hyprlang::INT{0xFFFFFFFF});
m_config.addSpecialConfigValue("label", "font_size", Hyprlang::INT{16});
m_config.addSpecialConfigValue("label", "font_size", FONTSIZECONFIG("16"));
m_config.addSpecialConfigValue("label", "text", Hyprlang::STRING{"Sample Text"});
m_config.addSpecialConfigValue("label", "font_family", Hyprlang::STRING{"Sans"});
m_config.addSpecialConfigValue("label", "halign", Hyprlang::STRING{"none"});

View file

@ -82,8 +82,8 @@ CLabel::CLabel(const Vector2D& viewport_, const std::unordered_map<std::string,
std::string textAlign = std::any_cast<Hyprlang::STRING>(props.at("text_align"));
std::string fontFamily = std::any_cast<Hyprlang::STRING>(props.at("font_family"));
CHyprColor labelColor = std::any_cast<Hyprlang::INT>(props.at("color"));
int fontSize = std::any_cast<Hyprlang::INT>(props.at("font_size"));
CHyprColor labelColor = std::any_cast<Hyprlang::INT>(props.at("color"));
int fontSize = CFontSizeValueData::fromAnyPv(props.at("font_size"))->getAbsolute(viewport_);
label = formatString(labelPreFormat);