// somebar - dwl bar // See LICENSE file for copyright and license details. #include #include #include #include "bar.hpp" #include "config.hpp" const zwlr_layer_surface_v1_listener Bar::_layerSurfaceListener = { [](void *owner, zwlr_layer_surface_v1*, uint32_t serial, uint32_t width, uint32_t height) { static_cast(owner)->layerSurfaceConfigure(serial, width, height); } }; const wl_callback_listener Bar::_frameListener = { [](void *owner, wl_callback *cb, uint32_t) { static_cast(owner)->render(); wl_callback_destroy(cb); } }; static QFont getFont() { QFont font {fontFamily, fontSizePt}; font.setBold(fontBold); return font; } static QFont font = getFont(); static QFontMetrics fontMetrics = QFontMetrics {font}; const wl_surface* Bar::surface() const { return _surface.get(); } Bar::Bar(Monitor *mon) { _mon = mon; for (auto i=0u; i _statusX) { control = ClkStatusText; } else if (x > _titleX) { control = ClkWinTitle; } else if (x > _layoutX) { control = ClkLayoutSymbol; } else for (auto tag = _tags.size()-1; tag >= 0; tag--) { if (x > _tags[tag].x) { control = ClkTagBar; arg.ui = 1<data(), _bufs->width, _bufs->height, _bufs->stride, QImage::Format_ARGB32 }; auto painter = QPainter {&img}; _painter = &painter; _x = 0; painter.setFont(font); renderTags(); setColorScheme(_selected ? colorActive : colorInactive); _layoutX = _x; renderText(layoutNames[_layout]); _titleX = _x; renderText(_title); renderStatus(); _painter = nullptr; wl_surface_attach(_surface.get(), _bufs->buffer(), 0, 0); wl_surface_damage(_surface.get(), 0, 0, INT_MAX, INT_MAX); wl_surface_commit(_surface.get()); _bufs->flip(); _invalid = false; } void Bar::setColorScheme(const ColorScheme &scheme, bool invert) { _painter->setBrush(QBrush {invert ? scheme.fg : scheme.bg}); _painter->setPen(QPen {QBrush {invert ? scheme.bg : scheme.fg}, 1}); } void Bar::renderTags() { for (auto i=0u; i<_tags.size(); i++) { auto& tag = _tags[i]; tag.x = _x; setColorScheme( tag.state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_ACTIVE ? colorActive : colorInactive, tag.state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_URGENT); renderText(tagNames[i]); auto indicators = qMin(tag.numClients, _bufs->height/2); for (auto ind = 0; ind < indicators; ind++) { if (ind == tag.focusedClient) { _painter->drawLine(tag.x, ind*2, tag.x+5, ind*2); } else { _painter->drawPoint(tag.x, ind*2); } } } } 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() { _painter->fillRect(_x, 0, _bufs->width-_x, _bufs->height, _painter->brush()); auto size = textWidth(_status) + paddingX; _statusX = _bufs->width - size; _painter->drawText(paddingX+_statusX, _textY, _status); _x = _bufs->width; } int Bar::textWidth(const QString &text) { return fontMetrics.size(Qt::TextSingleLine, text).width(); }