hyprland-plugins/borders-plus-plus/borderDeco.cpp

174 lines
6.9 KiB
C++
Raw Permalink Normal View History

2023-02-27 15:24:28 +00:00
#include "borderDeco.hpp"
2023-02-27 19:54:53 +00:00
#include <hyprland/src/Compositor.hpp>
2025-12-08 15:22:46 +00:00
#include <hyprland/src/desktop/view/Window.hpp>
2024-12-25 15:58:23 +01:00
#include <hyprland/src/render/Renderer.hpp>
#include <hyprutils/memory/Casts.hpp>
using namespace Hyprutils::Memory;
2024-12-25 15:58:23 +01:00
#include "BorderppPassElement.hpp"
2023-02-27 19:54:53 +00:00
#include "globals.hpp"
2024-04-27 13:03:46 +01:00
CBordersPlusPlus::CBordersPlusPlus(PHLWINDOW pWindow) : IHyprWindowDecoration(pWindow), m_pWindow(pWindow) {
m_lastWindowPos = pWindow->m_realPosition->value();
m_lastWindowSize = pWindow->m_realSize->value();
2023-02-27 15:24:28 +00:00
}
2023-02-28 12:06:21 +00:00
CBordersPlusPlus::~CBordersPlusPlus() {
damageEntire();
}
2023-02-27 15:24:28 +00:00
2023-11-11 14:39:46 +00:00
SDecorationPositioningInfo CBordersPlusPlus::getPositioningInfo() {
2024-02-18 15:30:21 +00:00
static auto* const PBORDERS = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:borders-plus-plus:add_borders")->getDataStaticPtr();
2023-11-11 14:39:46 +00:00
2024-02-18 15:30:21 +00:00
static std::vector<Hyprlang::INT* const*> PSIZES;
2023-11-11 14:39:46 +00:00
for (size_t i = 0; i < 9; ++i) {
2024-02-18 15:30:21 +00:00
PSIZES.push_back((Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:borders-plus-plus:border_size_" + std::to_string(i + 1))->getDataStaticPtr());
2023-11-11 14:39:46 +00:00
}
SDecorationPositioningInfo info;
info.policy = DECORATION_POSITION_STICKY;
2023-11-11 14:39:46 +00:00
info.reserved = true;
info.priority = 9990;
2023-11-11 14:39:46 +00:00
info.edges = DECORATION_EDGE_BOTTOM | DECORATION_EDGE_LEFT | DECORATION_EDGE_RIGHT | DECORATION_EDGE_TOP;
if (m_fLastThickness == 0) {
double size = 0;
2024-02-18 15:30:21 +00:00
for (size_t i = 0; i < **PBORDERS; ++i) {
size += **PSIZES[i];
2023-11-11 14:39:46 +00:00
}
info.desiredExtents = {{size, size}, {size, size}};
m_fLastThickness = size;
2024-04-27 13:03:46 +01:00
} else
2023-11-11 14:39:46 +00:00
info.desiredExtents = {{m_fLastThickness, m_fLastThickness}, {m_fLastThickness, m_fLastThickness}};
return info;
}
void CBordersPlusPlus::onPositioningReply(const SDecorationPositioningReply& reply) {
m_bAssignedGeometry = reply.assignedGeometry;
2023-02-28 12:06:21 +00:00
}
2023-02-27 15:24:28 +00:00
2023-11-11 14:39:46 +00:00
uint64_t CBordersPlusPlus::getDecorationFlags() {
return DECORATION_PART_OF_MAIN_WINDOW;
2023-11-11 14:39:46 +00:00
}
eDecorationLayer CBordersPlusPlus::getDecorationLayer() {
return DECORATION_LAYER_OVER;
}
std::string CBordersPlusPlus::getDisplayName() {
return "Borders++";
}
2024-12-04 09:58:09 -05:00
void CBordersPlusPlus::draw(PHLMONITOR pMonitor, const float& a) {
2024-04-27 13:03:46 +01:00
if (!validMapped(m_pWindow))
2023-02-28 12:06:21 +00:00
return;
2023-02-27 15:24:28 +00:00
2024-04-27 13:03:46 +01:00
const auto PWINDOW = m_pWindow.lock();
2025-11-19 20:07:46 +01:00
if (!PWINDOW->m_ruleApplicator->decorate().valueOrDefault())
2023-02-28 12:06:21 +00:00
return;
2023-02-27 15:24:28 +00:00
2024-12-25 15:58:23 +01:00
CBorderPPPassElement::SBorderPPData data;
data.deco = this;
g_pHyprRenderer->m_renderPass.add(makeUnique<CBorderPPPassElement>(data));
2024-12-25 15:58:23 +01:00
}
void CBordersPlusPlus::drawPass(PHLMONITOR pMonitor, const float& a) {
const auto PWINDOW = m_pWindow.lock();
2024-02-18 15:30:21 +00:00
static std::vector<Hyprlang::INT* const*> PCOLORS;
static std::vector<Hyprlang::INT* const*> PSIZES;
for (size_t i = 0; i < 9; ++i) {
2024-02-18 15:30:21 +00:00
PCOLORS.push_back((Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:borders-plus-plus:col.border_" + std::to_string(i + 1))->getDataStaticPtr());
PSIZES.push_back((Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:borders-plus-plus:border_size_" + std::to_string(i + 1))->getDataStaticPtr());
}
2024-02-18 15:30:21 +00:00
static auto* const PBORDERS = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:borders-plus-plus:add_borders")->getDataStaticPtr();
static auto* const PNATURALROUND = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:borders-plus-plus:natural_rounding")->getDataStaticPtr();
static auto* const PROUNDING = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "decoration:rounding")->getDataStaticPtr();
static auto* const PBORDERSIZE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "general:border_size")->getDataStaticPtr();
2023-02-27 15:24:28 +00:00
2024-02-18 15:30:21 +00:00
if (**PBORDERS < 1)
2023-02-28 12:06:21 +00:00
return;
2023-02-27 15:24:28 +00:00
if (m_bAssignedGeometry.width < m_seExtents.topLeft.x + 1 || m_bAssignedGeometry.height < m_seExtents.topLeft.y + 1)
return;
2025-04-29 14:21:14 +01:00
const auto PWORKSPACE = PWINDOW->m_workspace;
const auto WORKSPACEOFFSET = PWORKSPACE && !PWINDOW->m_pinned ? PWORKSPACE->m_renderOffset->value() : Vector2D();
2025-05-01 18:04:34 +01:00
auto rounding = PWINDOW->rounding() == 0 ? 0 : (PWINDOW->rounding() + **PBORDERSIZE) * pMonitor->m_scale;
const auto ROUNDINGPOWER = PWINDOW->roundingPower();
2025-05-01 18:04:34 +01:00
const auto ORIGINALROUND = rounding == 0 ? 0 : (PWINDOW->rounding() + **PBORDERSIZE) * pMonitor->m_scale;
2024-10-21 19:17:02 +01:00
CBox fullBox = m_bAssignedGeometry;
fullBox.translate(g_pDecorationPositioner->getEdgeDefinedPoint(DECORATION_EDGE_BOTTOM | DECORATION_EDGE_LEFT | DECORATION_EDGE_RIGHT | DECORATION_EDGE_TOP, m_pWindow.lock()));
2023-02-27 15:24:28 +00:00
2025-05-01 18:04:34 +01:00
fullBox.translate(PWINDOW->m_floatingOffset - pMonitor->m_position + WORKSPACEOFFSET);
if (fullBox.width < 1 || fullBox.height < 1)
2024-10-21 19:17:02 +01:00
return;
2023-02-27 15:24:28 +00:00
double fullThickness = 0;
2023-02-27 15:24:28 +00:00
for (size_t i = 0; i < **PBORDERS; ++i) {
2024-10-21 19:17:02 +01:00
const int THISBORDERSIZE = **(PSIZES[i]) == -1 ? **PBORDERSIZE : (**PSIZES[i]);
fullThickness += THISBORDERSIZE;
}
2025-05-01 18:04:34 +01:00
fullBox.expand(-fullThickness).scale(pMonitor->m_scale).round();
2024-02-18 15:30:21 +00:00
for (size_t i = 0; i < **PBORDERS; ++i) {
2025-05-01 18:04:34 +01:00
const int PREVBORDERSIZESCALED = i == 0 ? 0 : (**PSIZES[i - 1] == -1 ? **PBORDERSIZE : **(PSIZES[i - 1])) * pMonitor->m_scale;
2024-02-18 15:30:21 +00:00
const int THISBORDERSIZE = **(PSIZES[i]) == -1 ? **PBORDERSIZE : (**PSIZES[i]);
2023-02-27 15:24:28 +00:00
if (i != 0) {
rounding += rounding == 0 ? 0 : PREVBORDERSIZESCALED;
fullBox.x -= PREVBORDERSIZESCALED;
fullBox.y -= PREVBORDERSIZESCALED;
fullBox.width += PREVBORDERSIZESCALED * 2;
fullBox.height += PREVBORDERSIZESCALED * 2;
}
2023-02-27 15:24:28 +00:00
if (fullBox.width < 1 || fullBox.height < 1)
break;
2023-02-27 15:24:28 +00:00
2025-01-26 15:08:45 +00:00
g_pHyprOpenGL->scissor(nullptr);
2023-02-27 15:24:28 +00:00
g_pHyprOpenGL->renderBorder(fullBox, CHyprColor{(uint64_t)**PCOLORS[i]},
{.round = **PNATURALROUND ? sc<int>(ORIGINALROUND) : sc<int>(rounding),
.roundingPower = ROUNDINGPOWER,
.borderSize = THISBORDERSIZE,
.a = a,
.outerRound = **PNATURALROUND ? sc<int>(ORIGINALROUND) : -1});
}
2023-02-27 15:24:28 +00:00
m_seExtents = {{fullThickness, fullThickness}, {fullThickness, fullThickness}};
2023-11-11 14:39:46 +00:00
m_bLastRelativeBox = CBox{0, 0, m_lastWindowSize.x, m_lastWindowSize.y}.addExtents(m_seExtents);
2024-04-27 13:03:46 +01:00
2023-11-11 14:39:46 +00:00
if (fullThickness != m_fLastThickness) {
m_fLastThickness = fullThickness;
g_pDecorationPositioner->repositionDeco(this);
}
2023-02-27 15:24:28 +00:00
}
2023-02-28 12:06:21 +00:00
eDecorationType CBordersPlusPlus::getDecorationType() {
return DECORATION_CUSTOM;
}
2023-02-27 15:24:28 +00:00
2024-04-27 13:03:46 +01:00
void CBordersPlusPlus::updateWindow(PHLWINDOW pWindow) {
m_lastWindowPos = pWindow->m_realPosition->value();
m_lastWindowSize = pWindow->m_realSize->value();
2023-02-27 15:24:28 +00:00
damageEntire();
}
void CBordersPlusPlus::damageEntire() {
CBox dm = m_bLastRelativeBox.copy().translate(m_lastWindowPos).expand(2);
2025-01-26 15:08:45 +00:00
g_pHyprRenderer->damageBox(dm);
2024-07-13 16:10:57 +03:00
}