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

157 lines
6.1 KiB
C++
Raw Normal View History

2023-02-27 16:24:28 +01:00
#include "borderDeco.hpp"
2023-02-27 20:54:53 +01:00
#include <hyprland/src/Compositor.hpp>
2024-03-20 04:02:10 +01:00
#include <hyprland/src/desktop/Window.hpp>
2023-02-27 16:24:28 +01:00
2023-02-27 20:54:53 +01:00
#include "globals.hpp"
2024-04-27 14:03:46 +02:00
CBordersPlusPlus::CBordersPlusPlus(PHLWINDOW pWindow) : IHyprWindowDecoration(pWindow), m_pWindow(pWindow) {
2024-03-02 20:38:48 +01:00
m_vLastWindowPos = pWindow->m_vRealPosition.value();
m_vLastWindowSize = pWindow->m_vRealSize.value();
2023-02-27 16:24:28 +01:00
}
2023-02-28 13:06:21 +01:00
CBordersPlusPlus::~CBordersPlusPlus() {
damageEntire();
}
2023-02-27 16:24:28 +01:00
2023-11-11 15:39:46 +01:00
SDecorationPositioningInfo CBordersPlusPlus::getPositioningInfo() {
2024-02-18 16:30:21 +01:00
static auto* const PBORDERS = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:borders-plus-plus:add_borders")->getDataStaticPtr();
2023-11-11 15:39:46 +01:00
2024-02-18 16:30:21 +01:00
static std::vector<Hyprlang::INT* const*> PSIZES;
2023-11-11 15:39:46 +01:00
for (size_t i = 0; i < 9; ++i) {
2024-02-18 16:30:21 +01: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 15:39:46 +01:00
}
SDecorationPositioningInfo info;
info.policy = DECORATION_POSITION_STICKY;
2023-11-11 15:39:46 +01:00
info.reserved = true;
info.priority = 9990;
2023-11-11 15:39:46 +01: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 16:30:21 +01:00
for (size_t i = 0; i < **PBORDERS; ++i) {
size += **PSIZES[i];
2023-11-11 15:39:46 +01:00
}
info.desiredExtents = {{size, size}, {size, size}};
m_fLastThickness = size;
2024-04-27 14:03:46 +02:00
} else
2023-11-11 15:39:46 +01: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 13:06:21 +01:00
}
2023-02-27 16:24:28 +01:00
2023-11-11 15:39:46 +01:00
uint64_t CBordersPlusPlus::getDecorationFlags() {
return DECORATION_PART_OF_MAIN_WINDOW;
2023-11-11 15:39:46 +01:00
}
eDecorationLayer CBordersPlusPlus::getDecorationLayer() {
return DECORATION_LAYER_OVER;
}
std::string CBordersPlusPlus::getDisplayName() {
return "Borders++";
}
2024-12-04 15:58:09 +01:00
void CBordersPlusPlus::draw(PHLMONITOR pMonitor, const float& a) {
2024-04-27 14:03:46 +02:00
if (!validMapped(m_pWindow))
2023-02-28 13:06:21 +01:00
return;
2023-02-27 16:24:28 +01:00
2024-04-27 14:03:46 +02:00
const auto PWINDOW = m_pWindow.lock();
2024-07-13 15:10:57 +02:00
if (!PWINDOW->m_sWindowData.decorate.valueOrDefault())
2023-02-28 13:06:21 +01:00
return;
2023-02-27 16:24:28 +01:00
2024-02-18 16:30:21 +01: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 16:30:21 +01: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 16:30:21 +01: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 16:24:28 +01:00
2024-02-18 16:30:21 +01:00
if (**PBORDERS < 1)
2023-02-28 13:06:21 +01:00
return;
2023-02-27 16:24:28 +01:00
if (m_bAssignedGeometry.width < m_seExtents.topLeft.x + 1 || m_bAssignedGeometry.height < m_seExtents.topLeft.y + 1)
return;
2024-04-27 14:03:46 +02:00
const auto PWORKSPACE = PWINDOW->m_pWorkspace;
const auto WORKSPACEOFFSET = PWORKSPACE && !PWINDOW->m_bPinned ? PWORKSPACE->m_vRenderOffset.value() : Vector2D();
auto rounding = PWINDOW->rounding() == 0 ? 0 : (PWINDOW->rounding() + **PBORDERSIZE) * pMonitor->scale;
const auto ORIGINALROUND = rounding == 0 ? 0 : (PWINDOW->rounding() + **PBORDERSIZE) * pMonitor->scale;
2024-10-21 20:17:02 +02: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 16:24:28 +01:00
fullBox.translate(PWINDOW->m_vFloatingOffset - pMonitor->vecPosition + WORKSPACEOFFSET);
if (fullBox.width < 1 || fullBox.height < 1)
2024-10-21 20:17:02 +02:00
return;
2023-02-27 16:24:28 +01:00
double fullThickness = 0;
2023-02-27 16:24:28 +01:00
for (size_t i = 0; i < **PBORDERS; ++i) {
2024-10-21 20:17:02 +02:00
const int THISBORDERSIZE = **(PSIZES[i]) == -1 ? **PBORDERSIZE : (**PSIZES[i]);
fullThickness += THISBORDERSIZE;
}
fullBox.expand(-fullThickness).scale(pMonitor->scale).round();
2024-02-18 16:30:21 +01:00
for (size_t i = 0; i < **PBORDERS; ++i) {
const int PREVBORDERSIZESCALED = i == 0 ? 0 : (**PSIZES[i - 1] == -1 ? **PBORDERSIZE : **(PSIZES[i - 1])) * pMonitor->scale;
const int THISBORDERSIZE = **(PSIZES[i]) == -1 ? **PBORDERSIZE : (**PSIZES[i]);
2023-02-27 16:24:28 +01: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 16:24:28 +01:00
if (fullBox.width < 1 || fullBox.height < 1)
break;
2023-02-27 16:24:28 +01:00
2023-11-04 18:25:00 +01:00
g_pHyprOpenGL->scissor((CBox*)nullptr);
2023-02-27 16:24:28 +01:00
2024-12-04 15:58:09 +01:00
g_pHyprOpenGL->renderBorder(&fullBox, CHyprColor{(uint64_t)**PCOLORS[i]}, **PNATURALROUND ? ORIGINALROUND : rounding, THISBORDERSIZE, a,
2024-02-18 16:30:21 +01:00
**PNATURALROUND ? ORIGINALROUND : -1);
}
2023-02-27 16:24:28 +01:00
m_seExtents = {{fullThickness, fullThickness}, {fullThickness, fullThickness}};
2023-11-11 15:39:46 +01:00
m_bLastRelativeBox = CBox{0, 0, m_vLastWindowSize.x, m_vLastWindowSize.y}.addExtents(m_seExtents);
2024-04-27 14:03:46 +02:00
2023-11-11 15:39:46 +01:00
if (fullThickness != m_fLastThickness) {
m_fLastThickness = fullThickness;
g_pDecorationPositioner->repositionDeco(this);
}
2023-02-27 16:24:28 +01:00
}
2023-02-28 13:06:21 +01:00
eDecorationType CBordersPlusPlus::getDecorationType() {
return DECORATION_CUSTOM;
}
2023-02-27 16:24:28 +01:00
2024-04-27 14:03:46 +02:00
void CBordersPlusPlus::updateWindow(PHLWINDOW pWindow) {
2024-03-02 20:38:48 +01:00
m_vLastWindowPos = pWindow->m_vRealPosition.value();
m_vLastWindowSize = pWindow->m_vRealSize.value();
2023-02-27 16:24:28 +01:00
damageEntire();
}
void CBordersPlusPlus::damageEntire() {
2024-04-27 14:03:46 +02:00
CBox dm = m_bLastRelativeBox.copy().translate(m_vLastWindowPos).expand(2);
2023-02-27 16:24:28 +01:00
g_pHyprRenderer->damageBox(&dm);
2024-07-13 15:10:57 +02:00
}