Configurable colors

This commit is contained in:
Raphael Robatsch 2021-10-22 15:42:42 +02:00
parent 4c0b1f31fe
commit 120af1612f
4 changed files with 26 additions and 7 deletions

View file

@ -44,9 +44,6 @@ void Bar::layerSurfaceConfigure(uint32_t serial, uint32_t width, uint32_t height
_bufs.emplace(width, height, WL_SHM_FORMAT_XRGB8888);
render();
}
static QBrush inactiveBg = {QColor::fromRgb(0, 0, 0)};
static QBrush activeBg = QBrush {QColor::fromRgb(0, 0, 255)};
static QPen fg = QPen {QBrush {QColor::fromRgb(255, 255, 255)}, 1};
void Bar::render()
{
@ -58,29 +55,37 @@ void Bar::render()
QImage::Format_ARGB32
};
auto painter = QPainter {&img};
_painter = &painter;
auto font = painter.font();
font.setBold(true);
font.setPixelSize(18);
painter.setFont(font);
painter.setPen(fg);
painter.fillRect(0, 0, img.width(), img.height(), activeBg);
setColorScheme(colorActive);
painter.fillRect(0, 0, img.width(), img.height(), painter.brush());
_fontMetrics.emplace(painter.font());
_textY = _fontMetrics->ascent() + paddingY;
renderTags(painter);
_painter = nullptr;
wl_surface_attach(_surface, _bufs->buffer(), 0, 0);
wl_surface_commit(_surface);
_bufs->flip();
}
void Bar::setColorScheme(const ColorScheme &scheme)
{
_painter->setBrush(QBrush {scheme.bg});
_painter->setPen(QPen {QBrush {scheme.fg}, 1});
}
void Bar::renderTags(QPainter &painter)
{
auto x = 0;
for (const auto &tag : _tags) {
auto size = textWidth(tag.name) + paddingX*2;
auto& bg = tag.active ? activeBg : inactiveBg;
painter.fillRect(x, 0, size, barSize, bg);
setColorScheme(tag.active ? colorActive : colorInactive);
painter.fillRect(x, 0, size, barSize, _painter->brush());
painter.drawText(paddingX+x, _textY, tag.name);
x += size;
}

View file

@ -7,6 +7,7 @@
#include <wayland-client.h>
#include <QString>
#include <QFontMetrics>
#include <QPainter>
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "common.hpp"
#include "shm_buffer.hpp"
@ -21,6 +22,7 @@ class Bar {
wl_surface *_surface {nullptr};
zwlr_layer_surface_v1 *_layerSurface {nullptr};
QPainter *_painter {nullptr};
std::optional<QFontMetrics> _fontMetrics;
std::optional<ShmBuffer> _bufs;
std::vector<Tag> _tags;
@ -30,6 +32,7 @@ class Bar {
void render();
void renderTags(QPainter &painter);
int textWidth(const QString &text);
void setColorScheme(const ColorScheme &scheme);
public:
explicit Bar(const wl_output *output);
~Bar();

View file

@ -3,9 +3,14 @@
#pragma once
#include <wayland-client.h>
#include <QColor>
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
extern wl_display *display;
extern wl_compositor *compositor;
extern wl_shm *shm;
extern zwlr_layer_shell_v1 *wlrLayerShell;
struct ColorScheme {
QColor fg, bg;
};

View file

@ -1,7 +1,13 @@
// somebar - dwl bar
// See LICENSE file for copyright and license details.
#pragma once
#include "common.hpp"
constexpr int barSize = 26;
constexpr bool topbar = 1;
constexpr int paddingX = 10;
constexpr int paddingY = 3;
constexpr ColorScheme colorInactive = {QColor(255, 255, 255), QColor(0, 0, 0)};
constexpr ColorScheme colorActive = {QColor(255, 255, 255), QColor(0, 0, 255)};