From 41e2b7286da7b097eff4f2fb5652a128bb8db8a8 Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Fri, 22 Oct 2021 15:49:09 +0200 Subject: [PATCH] Configurable font --- src/bar.cpp | 24 ++++++++++++++++-------- src/bar.hpp | 3 ++- src/config.hpp | 6 +++++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/bar.cpp b/src/bar.cpp index e3c1ce1..a9ff3b3 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -14,7 +14,16 @@ const zwlr_layer_surface_v1_listener Bar::_layerSurfaceListener = { } }; +static QFont getFont() +{ + QFont font {fontFamily, fontSizePt}; + font.setBold(fontBold); + return font; +} + Bar::Bar(const wl_output *output) + : _font {getFont()} + , _fontMetrics {_font} { _surface = wl_compositor_create_surface(compositor); _layerSurface = zwlr_layer_shell_v1_get_layer_surface(wlrLayerShell, @@ -23,6 +32,10 @@ Bar::Bar(const wl_output *output) auto anchor = topbar ? ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP : ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; zwlr_layer_surface_v1_set_anchor(_layerSurface, anchor | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); + + auto barSize = _fontMetrics.ascent() + _fontMetrics.descent() + paddingY * 2; + _textY = _fontMetrics.ascent() + paddingY; + zwlr_layer_surface_v1_set_size(_layerSurface, 0, barSize); zwlr_layer_surface_v1_set_exclusive_zone(_layerSurface, barSize); wl_surface_commit(_surface); @@ -56,15 +69,10 @@ void Bar::render() }; auto painter = QPainter {&img}; _painter = &painter; - auto font = painter.font(); - font.setBold(true); - font.setPixelSize(18); - painter.setFont(font); + painter.setFont(_font); setColorScheme(colorActive); painter.fillRect(0, 0, img.width(), img.height(), painter.brush()); - _fontMetrics.emplace(painter.font()); - _textY = _fontMetrics->ascent() + paddingY; renderTags(painter); _painter = nullptr; @@ -85,7 +93,7 @@ void Bar::renderTags(QPainter &painter) for (const auto &tag : _tags) { auto size = textWidth(tag.name) + paddingX*2; setColorScheme(tag.active ? colorActive : colorInactive); - painter.fillRect(x, 0, size, barSize, _painter->brush()); + painter.fillRect(x, 0, size, _bufs->height, _painter->brush()); painter.drawText(paddingX+x, _textY, tag.name); x += size; } @@ -93,5 +101,5 @@ void Bar::renderTags(QPainter &painter) int Bar::textWidth(const QString &text) { - return _fontMetrics->size(Qt::TextSingleLine, text).width(); + return _fontMetrics.size(Qt::TextSingleLine, text).width(); } diff --git a/src/bar.hpp b/src/bar.hpp index 9afd9a5..b394c08 100644 --- a/src/bar.hpp +++ b/src/bar.hpp @@ -23,7 +23,8 @@ class Bar { wl_surface *_surface {nullptr}; zwlr_layer_surface_v1 *_layerSurface {nullptr}; QPainter *_painter {nullptr}; - std::optional _fontMetrics; + QFont _font; + QFontMetrics _fontMetrics; std::optional _bufs; std::vector _tags; int _textY; diff --git a/src/config.hpp b/src/config.hpp index 6ff53e1..a38ce87 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -4,10 +4,14 @@ #pragma once #include "common.hpp" -constexpr int barSize = 26; constexpr bool topbar = 1; + constexpr int paddingX = 10; constexpr int paddingY = 3; +constexpr const char *fontFamily = "Source Sans Pro"; +constexpr int fontSizePt = 12; +constexpr bool fontBold = true; + constexpr ColorScheme colorInactive = {QColor(255, 255, 255), QColor(0, 0, 0)}; constexpr ColorScheme colorActive = {QColor(255, 255, 255), QColor(0, 0, 255)};