Configurable font

This commit is contained in:
Raphael Robatsch 2021-10-22 15:49:09 +02:00
parent 120af1612f
commit 41e2b7286d
3 changed files with 23 additions and 10 deletions

View File

@ -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) Bar::Bar(const wl_output *output)
: _font {getFont()}
, _fontMetrics {_font}
{ {
_surface = wl_compositor_create_surface(compositor); _surface = wl_compositor_create_surface(compositor);
_layerSurface = zwlr_layer_shell_v1_get_layer_surface(wlrLayerShell, _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; auto anchor = topbar ? ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP : ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
zwlr_layer_surface_v1_set_anchor(_layerSurface, zwlr_layer_surface_v1_set_anchor(_layerSurface,
anchor | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); 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_size(_layerSurface, 0, barSize);
zwlr_layer_surface_v1_set_exclusive_zone(_layerSurface, barSize); zwlr_layer_surface_v1_set_exclusive_zone(_layerSurface, barSize);
wl_surface_commit(_surface); wl_surface_commit(_surface);
@ -56,15 +69,10 @@ void Bar::render()
}; };
auto painter = QPainter {&img}; auto painter = QPainter {&img};
_painter = &painter; _painter = &painter;
auto font = painter.font(); painter.setFont(_font);
font.setBold(true);
font.setPixelSize(18);
painter.setFont(font);
setColorScheme(colorActive); setColorScheme(colorActive);
painter.fillRect(0, 0, img.width(), img.height(), painter.brush()); painter.fillRect(0, 0, img.width(), img.height(), painter.brush());
_fontMetrics.emplace(painter.font());
_textY = _fontMetrics->ascent() + paddingY;
renderTags(painter); renderTags(painter);
_painter = nullptr; _painter = nullptr;
@ -85,7 +93,7 @@ void Bar::renderTags(QPainter &painter)
for (const auto &tag : _tags) { for (const auto &tag : _tags) {
auto size = textWidth(tag.name) + paddingX*2; auto size = textWidth(tag.name) + paddingX*2;
setColorScheme(tag.active ? colorActive : colorInactive); 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); painter.drawText(paddingX+x, _textY, tag.name);
x += size; x += size;
} }
@ -93,5 +101,5 @@ void Bar::renderTags(QPainter &painter)
int Bar::textWidth(const QString &text) int Bar::textWidth(const QString &text)
{ {
return _fontMetrics->size(Qt::TextSingleLine, text).width(); return _fontMetrics.size(Qt::TextSingleLine, text).width();
} }

View File

@ -23,7 +23,8 @@ class Bar {
wl_surface *_surface {nullptr}; wl_surface *_surface {nullptr};
zwlr_layer_surface_v1 *_layerSurface {nullptr}; zwlr_layer_surface_v1 *_layerSurface {nullptr};
QPainter *_painter {nullptr}; QPainter *_painter {nullptr};
std::optional<QFontMetrics> _fontMetrics; QFont _font;
QFontMetrics _fontMetrics;
std::optional<ShmBuffer> _bufs; std::optional<ShmBuffer> _bufs;
std::vector<Tag> _tags; std::vector<Tag> _tags;
int _textY; int _textY;

View File

@ -4,10 +4,14 @@
#pragma once #pragma once
#include "common.hpp" #include "common.hpp"
constexpr int barSize = 26;
constexpr bool topbar = 1; constexpr bool topbar = 1;
constexpr int paddingX = 10; constexpr int paddingX = 10;
constexpr int paddingY = 3; 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 colorInactive = {QColor(255, 255, 255), QColor(0, 0, 0)};
constexpr ColorScheme colorActive = {QColor(255, 255, 255), QColor(0, 0, 255)}; constexpr ColorScheme colorActive = {QColor(255, 255, 255), QColor(0, 0, 255)};