Skip to content

Commit

Permalink
label: add relative values for font_size
Browse files Browse the repository at this point in the history
  • Loading branch information
NotLebedev committed Feb 2, 2025
1 parent c976b6a commit f0141eb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
55 changes: 55 additions & 0 deletions src/config/ConfigDataValues.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
};
36 changes: 35 additions & 1 deletion src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) \
Expand Down Expand Up @@ -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"});
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/widgets/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f0141eb

Please sign in to comment.