From a239c945981a334906b7ba45212b6441a7290e7e Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Fri, 22 Oct 2021 15:55:29 +0200 Subject: [PATCH] Render status bar --- src/bar.cpp | 33 ++++++++++++++++++++++++++------- src/bar.hpp | 9 +++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/bar.cpp b/src/bar.cpp index a9ff3b3..b76b569 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -43,6 +43,8 @@ Bar::Bar(const wl_output *output) for (auto i=1; i<=4; i++) { _tags.push_back({ QString::number(i), i%2 == 0 }); } + _windowTitle = "Window title"; + _status = "Status"; } Bar::~Bar() @@ -69,11 +71,15 @@ void Bar::render() }; auto painter = QPainter {&img}; _painter = &painter; + _x = 0; painter.setFont(_font); setColorScheme(colorActive); painter.fillRect(0, 0, img.width(), img.height(), painter.brush()); - renderTags(painter); + renderTags(); + setColorScheme(colorActive); + renderText(_windowTitle); + renderStatus(); _painter = nullptr; wl_surface_attach(_surface, _bufs->buffer(), 0, 0); @@ -87,18 +93,31 @@ void Bar::setColorScheme(const ColorScheme &scheme) _painter->setPen(QPen {QBrush {scheme.fg}, 1}); } -void Bar::renderTags(QPainter &painter) +void Bar::renderTags() { - auto x = 0; for (const auto &tag : _tags) { - auto size = textWidth(tag.name) + paddingX*2; setColorScheme(tag.active ? colorActive : colorInactive); - painter.fillRect(x, 0, size, _bufs->height, _painter->brush()); - painter.drawText(paddingX+x, _textY, tag.name); - x += size; + renderText(tag.name); } } +void Bar::renderText(const QString &text) +{ + auto size = textWidth(text) + paddingX*2; + _painter->fillRect(_x, 0, size, _bufs->height, _painter->brush()); + _painter->drawText(paddingX+_x, _textY, text); + _x += size; +} + +void Bar::renderStatus() +{ + auto size = textWidth(_status) + paddingX*2; + _x = _bufs->width - size; + _painter->fillRect(_x, 0, size, _bufs->height, _painter->brush()); + _painter->drawText(paddingX+_x, _textY, _status); + _x = _bufs->width; +} + int Bar::textWidth(const QString &text) { return _fontMetrics.size(Qt::TextSingleLine, text).width(); diff --git a/src/bar.hpp b/src/bar.hpp index b394c08..7a76ee8 100644 --- a/src/bar.hpp +++ b/src/bar.hpp @@ -26,12 +26,17 @@ class Bar { QFont _font; QFontMetrics _fontMetrics; std::optional _bufs; + int _textY, _x; + std::vector _tags; - int _textY; + QString _windowTitle; + QString _status; void layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height); void render(); - void renderTags(QPainter &painter); + void renderTags(); + void renderStatus(); + void renderText(const QString &text); int textWidth(const QString &text); void setColorScheme(const ColorScheme &scheme); public: