hypr-chrome v0.1
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
#include "ChromePassElement.hpp"
|
||||
#include "Globals.hpp"
|
||||
#include "ChromeDecoration.hpp"
|
||||
#include "ChromeDecorationGeometry.hpp"
|
||||
|
||||
#include <hyprland/src/Compositor.hpp>
|
||||
#include <hyprland/src/desktop/view/Window.hpp>
|
||||
#include <hyprland/src/render/Renderer.hpp>
|
||||
#include <hyprland/src/render/Texture.hpp>
|
||||
#include <hyprland/src/render/pass/TexPassElement.hpp>
|
||||
|
||||
ChromePassElement::ChromePassElement(const SData& data_) : data(data_) {}
|
||||
|
||||
std::vector<UP<IPassElement>> ChromePassElement::draw() {
|
||||
const auto monitor = g_pHyprRenderer->m_renderData.pMonitor.lock();
|
||||
if (!monitor)
|
||||
return {};
|
||||
|
||||
const auto window = data.decoration->GetOwner();
|
||||
if (!window)
|
||||
return {};
|
||||
|
||||
auto box = data.decoration->FullDecorationExtentGlobal();
|
||||
box.translate(-monitor->m_position).scale(monitor->m_scale).round();
|
||||
|
||||
if (box.w < 1 || box.h < 1)
|
||||
return {};
|
||||
|
||||
// Chamfer amount is inferred from the window's own border rounding, in
|
||||
// device pixels, so the frame's cut corners track the window's rounding
|
||||
// live (config reload, per-window rules, animations).
|
||||
const float chamferPx = window->rounding() * monitor->m_scale;
|
||||
const float extentPx = static_cast<float>(PluginState->config.extent->value()) * monitor->m_scale;
|
||||
const bool isActive = g_pCompositor->isWindowActive(window);
|
||||
// When follow_hyprland_border_color is set, track Hyprland's own
|
||||
// general:col.active_border / col.inactive_border (already resolved per
|
||||
// focus state and animated by the compositor) - gradients are collapsed
|
||||
// to their first stop, since the ring is a flat fill, not a shader.
|
||||
// Otherwise (or if Hyprland reports no border color at all) fall back to
|
||||
// our own active_color/inactive_color config.
|
||||
const auto& borderGradient = window->m_realBorderColor;
|
||||
const bool followHyprlandColor = PluginState->config.followHyprlandBorderColor->value() && !borderGradient.m_colors.empty();
|
||||
const uint64_t colorValue = followHyprlandColor
|
||||
? static_cast<uint64_t>(borderGradient.m_colors.front().getAsHex())
|
||||
: static_cast<uint64_t>(isActive ? PluginState->config.activeColor->value() : PluginState->config.inactiveColor->value());
|
||||
const float titleBarHeightPx = static_cast<float>(PluginState->config.titlebarHeight->value()) * monitor->m_scale;
|
||||
const float textSizePx = static_cast<float>(PluginState->config.titlebarTextSize->value()) * monitor->m_scale;
|
||||
const std::string fontFamily = PluginState->config.titlebarFont->value();
|
||||
|
||||
// Base geometry (everything but the title bar plateau's own width - that
|
||||
// depends on the rendered title text, which can only be measured by
|
||||
// actually rendering it). MaxTitleBarWidth() bounds that render so a long
|
||||
// title gets ellipsized rather than growing the plateau into the
|
||||
// self-intersection GetBorderTexture's cairo path would otherwise risk.
|
||||
const auto baseGeo = ChromeDecorationGeometry::ComputeBase({box.w, box.h}, extentPx, chamferPx, titleBarHeightPx);
|
||||
// Padding around the title text, inside the plateau.
|
||||
const float titlePadding = textSizePx * 1.2F;
|
||||
const int measureMaxWidthPx = static_cast<int>(baseGeo.MaxTitleBarWidth() - baseGeo.barX0 - titlePadding * 2.F);
|
||||
|
||||
const auto titleTex = measureMaxWidthPx > 0
|
||||
? data.decoration->GetTitleTexture(window->m_title, textSizePx, colorValue, fontFamily, measureMaxWidthPx, isActive)
|
||||
: nullptr;
|
||||
const float texW = (titleTex && titleTex->ok()) ? static_cast<float>(titleTex->m_size.x) : 0.F;
|
||||
|
||||
// Plateau grows to fit the measured text + padding, floored at 20% of the
|
||||
// total width regardless of how short (or absent) the title is.
|
||||
const float desiredTitleBarWidthPx = baseGeo.barX0 + texW + titlePadding * 2.F;
|
||||
const auto geo = baseGeo.WithTitleBarWidth(desiredTitleBarWidthPx);
|
||||
|
||||
// In fullSpan mode the outer path's shape is fixed regardless of text
|
||||
// width (see GetBorderTexture), so pass a value that doesn't fluctuate
|
||||
// with title length - otherwise every title change would needlessly
|
||||
// regenerate an identical border texture.
|
||||
const float borderTitleBarWidthPx = geo.fullSpan ? geo.MaxTitleBarWidth() : geo.titleBarWidth;
|
||||
const auto tex = data.decoration->GetBorderTexture({box.w, box.h}, extentPx, chamferPx, colorValue, titleBarHeightPx, borderTitleBarWidthPx);
|
||||
if (!tex || !tex->ok())
|
||||
return {};
|
||||
|
||||
CTexPassElement::SRenderData texData;
|
||||
texData.tex = tex;
|
||||
texData.box = box;
|
||||
texData.a = data.alpha;
|
||||
|
||||
std::vector<UP<IPassElement>> children;
|
||||
children.emplace_back(makeUnique<CTexPassElement>(texData));
|
||||
|
||||
if (titleTex && titleTex->ok() && titleTex->m_size.x > 0 && titleTex->m_size.y > 0) {
|
||||
const float texH = static_cast<float>(titleTex->m_size.y);
|
||||
const float localX = geo.barX0 + titlePadding;
|
||||
const float localY = (geo.barBottom - texH) / 2.F;
|
||||
|
||||
CTexPassElement::SRenderData titleData;
|
||||
titleData.tex = titleTex;
|
||||
titleData.box = CBox{box.x + localX, box.y + localY, texW, texH};
|
||||
titleData.a = data.alpha;
|
||||
children.emplace_back(makeUnique<CTexPassElement>(titleData));
|
||||
}
|
||||
|
||||
return children;
|
||||
}
|
||||
|
||||
bool ChromePassElement::needsLiveBlur() { return false; }
|
||||
|
||||
bool ChromePassElement::needsPrecomputeBlur() { return false; }
|
||||
|
||||
std::optional<CBox> ChromePassElement::boundingBox() {
|
||||
const auto monitor = g_pHyprRenderer->m_renderData.pMonitor.lock();
|
||||
if (!monitor)
|
||||
return std::nullopt;
|
||||
|
||||
return data.decoration->FullDecorationExtentGlobal()
|
||||
.translate(-monitor->m_position)
|
||||
.expand(4);
|
||||
}
|
||||
Reference in New Issue
Block a user