Files
hypr-chrome/src/ChromePassElement.cpp
T
darmanandClaude Opus 5 b9dafe2ce1 Render border gradients instead of a flat first stop
The frame collapsed Hyprland's border gradient to m_colors.front(), so a
gradient col.active_border showed up as a single flat color.

Snapshot the gradient (stops + angle) into a new header-only ChromeGradient
and fill the cairo path with a linear gradient built from it:

- AxisFor() reproduces the border shader's quadrant-folding progress
  function (y*sin(a) + x*(1-sin(a)), not a true rotation) as a cairo axis,
  so the frame's sweep stays in step with the window border it wraps.
  Checked against a port of the shader across all 360 degrees: cardinal
  angles exact, worst case 0.003 of the sweep (the shader folds on literal
  1.57/3.14/4.71 where this uses pi).
- SampleAt() interpolates in OkLab, where the shader interpolates, with
  each segment subdivided into sampled cairo stops so cairo's own sRGB lerp
  tracks that curve.

active_color/inactive_color become gradient config values, taking the same
syntax as general:col.active_border. Single-color configs are unaffected.

The cross-focus fade (m_realBorderColorPrevious + fade progress, lerped
between two gradients in-shader) is still not reproduced.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-29 20:36:24 +02:00

116 lines
5.3 KiB
C++

#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), stops and angle included.
// Otherwise (or if Hyprland reports no border color at all) fall back to
// our own active_color/inactive_color config, which take the same
// gradient syntax.
const auto& hyprlandBorder = window->m_realBorderColor;
const bool followHyprlandColor = PluginState->config.followHyprlandBorderColor->value() && !hyprlandBorder.m_colors.empty();
const auto gradient = ChromeGradient::From(followHyprlandColor
? hyprlandBorder
: (isActive ? PluginState->config.activeColor->value() : PluginState->config.inactiveColor->value()));
const float borderAlpha = gradient.FirstAlpha();
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, borderAlpha, 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, gradient, 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);
}