From 5159508141148a1b764a21485de383d881e867ba Mon Sep 17 00:00:00 2001 From: Maxim Mikhaylov Date: Sun, 22 May 2022 00:57:20 +0300 Subject: [PATCH] Added font size to bar --- src/bar/Bar.cpp | 25 ++++++++++++++----------- src/bar/Bar.hpp | 4 ++-- src/config/ConfigManager.cpp | 1 + 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/bar/Bar.cpp b/src/bar/Bar.cpp index 3f3fe2c..6549086 100644 --- a/src/bar/Bar.cpp +++ b/src/bar/Bar.cpp @@ -413,9 +413,9 @@ void CStatusBar::destroy() { m_bIsDestroyed = true; } -int CStatusBar::getTextWidth(std::string text, std::string font) { +int CStatusBar::getTextWidth(std::string text, std::string font, double size) { cairo_select_font_face(m_pCairo, font.c_str(), CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_font_size(m_pCairo, 12); + cairo_set_font_size(m_pCairo, size); cairo_text_extents_t textextents; cairo_text_extents(m_pCairo, text.c_str(), &textextents); @@ -423,9 +423,9 @@ int CStatusBar::getTextWidth(std::string text, std::string font) { return textextents.width + 1 /* pad */; } -void CStatusBar::drawText(Vector2D pos, std::string text, uint32_t color, std::string font) { +void CStatusBar::drawText(Vector2D pos, std::string text, uint32_t color, std::string font, double size) { cairo_select_font_face(m_pCairo, font.c_str(), CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_font_size(m_pCairo, 12); + cairo_set_font_size(m_pCairo, size); cairo_set_source_rgba(m_pCairo, RED(color), GREEN(color), BLUE(color), ALPHA(color)); cairo_move_to(m_pCairo, pos.x, pos.y); cairo_show_text(m_pCairo, text.c_str()); @@ -447,7 +447,8 @@ void CStatusBar::drawErrorScreen() { else drawCairoRectangle(Vector2D(0, 0), m_vecSize, 0xFFaa1111); - drawText(Vector2D(1, getTextHalfY()), ConfigManager::parseError, 0xff000000, ConfigManager::getString("bar:font.main")); + drawText(Vector2D(1, getTextHalfY()), ConfigManager::parseError, 0xff000000, + ConfigManager::getString("bar:font.main"), ConfigManager::getFloat("bar:font.size")); // do all the drawing cuz we return later cairo_surface_flush(m_pCairoSurface); @@ -549,8 +550,8 @@ int CStatusBar::drawWorkspacesModule(SBarModule* mod, int off) { drawCairoRectangle(Vector2D(off + m_vecSize.y * drawnWorkspaces, 0), Vector2D(m_vecSize.y, m_vecSize.y), WORKSPACE == MOUSEWORKSPACEID ? ConfigManager::getInt("bar:col.high") : ConfigManager::getInt("bar:col.bg")); - drawText(Vector2D(off + m_vecSize.y * drawnWorkspaces + m_vecSize.y / 2.f - getTextWidth(workspaceName, ConfigManager::getString("bar:font.main")) / 2.f, getTextHalfY()), - workspaceName, WORKSPACE == MOUSEWORKSPACEID ? 0xFF111111 : 0xFFFFFFFF, ConfigManager::getString("bar:font.main")); + drawText(Vector2D(off + m_vecSize.y * drawnWorkspaces + m_vecSize.y / 2.f - getTextWidth(workspaceName, ConfigManager::getString("bar:font.main"), ConfigManager::getFloat("bar:font.size")) / 2.f, getTextHalfY()), + workspaceName, WORKSPACE == MOUSEWORKSPACEID ? 0xFF111111 : 0xFFFFFFFF, ConfigManager::getString("bar:font.main"), ConfigManager::getFloat("bar:font.size")); drawnWorkspaces++; } @@ -640,8 +641,8 @@ int CStatusBar::drawModule(SBarModule* mod, int off) { // We have the value, draw the module! - const auto MODULEWIDTH = getTextWidth(mod->valueCalculated, ConfigManager::getString("bar:font.main")) + PAD; - const auto ICONWIDTH = getTextWidth(mod->icon, ConfigManager::getString("bar:font.secondary")); + const auto MODULEWIDTH = getTextWidth(mod->valueCalculated, ConfigManager::getString("bar:font.main"), ConfigManager::getFloat("bar:font.size")) + PAD; + const auto ICONWIDTH = getTextWidth(mod->icon, ConfigManager::getString("bar:font.secondary"), ConfigManager::getFloat("bar:font.size")); if (!MODULEWIDTH || mod->accessValueCalculated(false) == "") return 0; // empty module @@ -661,8 +662,10 @@ int CStatusBar::drawModule(SBarModule* mod, int off) { drawCairoRectangle(position, Vector2D(MODULEWIDTH + ICONWIDTH, m_vecSize.y), mod->bgcolor); - drawText(position + Vector2D(PAD / 2, getTextHalfY()), mod->icon, mod->color, ConfigManager::getString("bar:font.secondary")); - drawText(position + Vector2D(PAD / 2 + ICONWIDTH, getTextHalfY()), mod->accessValueCalculated(false), mod->color, ConfigManager::getString("bar:font.main")); + drawText(position + Vector2D(PAD / 2, getTextHalfY()), mod->icon, mod->color, + ConfigManager::getString("bar:font.secondary"), ConfigManager::getFloat("bar:font.size")); + drawText(position + Vector2D(PAD / 2 + ICONWIDTH, getTextHalfY()), mod->accessValueCalculated(false), mod->color, + ConfigManager::getString("bar:font.main"), ConfigManager::getFloat("bar:font.size")); return MODULEWIDTH + ICONWIDTH; } diff --git a/src/bar/Bar.hpp b/src/bar/Bar.hpp index 68079bc..c497f39 100644 --- a/src/bar/Bar.hpp +++ b/src/bar/Bar.hpp @@ -96,9 +96,9 @@ private: cairo_surface_t* m_pCairoSurface = nullptr; cairo_t* m_pCairo = nullptr; - void drawText(Vector2D, std::string, uint32_t, std::string); + void drawText(Vector2D, std::string, uint32_t, std::string, double); void drawCairoRectangle(Vector2D, Vector2D, uint32_t); - int getTextWidth(std::string, std::string); + int getTextWidth(std::string, std::string, double); int drawModule(SBarModule*, int); int drawWorkspacesModule(SBarModule*, int); int getTextHalfY(); diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index bae5bb7..1dfa0b0 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -34,6 +34,7 @@ void ConfigManager::init() { configValues["bar:height"].intValue = 15; configValues["bar:col.bg"].intValue = 0xFF111111; configValues["bar:col.high"].intValue = 0xFFFF3333; + configValues["bar:font.size"].floatValue = 12; configValues["bar:font.main"].strValue = "Noto Sans"; configValues["bar:font.secondary"].strValue = "Noto Sans"; configValues["bar:mod_pad_in"].intValue = 4;