From 120af1612f5a68bdbe57b7ee7701d64d6a56f598 Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Fri, 22 Oct 2021 15:42:42 +0200 Subject: [PATCH] Configurable colors --- src/bar.cpp | 19 ++++++++++++------- src/bar.hpp | 3 +++ src/common.hpp | 5 +++++ src/config.hpp | 6 ++++++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/bar.cpp b/src/bar.cpp index 30e8624..e3c1ce1 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -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; } diff --git a/src/bar.hpp b/src/bar.hpp index bbc97ee..9afd9a5 100644 --- a/src/bar.hpp +++ b/src/bar.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #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 _fontMetrics; std::optional _bufs; std::vector _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(); diff --git a/src/common.hpp b/src/common.hpp index 9e8149e..666a59d 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -3,9 +3,14 @@ #pragma once #include +#include #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; +}; diff --git a/src/config.hpp b/src/config.hpp index 9e3df03..6ff53e1 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -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)};