Render status bar

This commit is contained in:
Raphael Robatsch 2021-10-22 15:55:29 +02:00
parent 41e2b7286d
commit a239c94598
2 changed files with 33 additions and 9 deletions

View file

@ -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();

View file

@ -26,12 +26,17 @@ class Bar {
QFont _font;
QFontMetrics _fontMetrics;
std::optional<ShmBuffer> _bufs;
int _textY, _x;
std::vector<Tag> _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: