2024-02-29 15:26:02 +01:00
|
|
|
#include "Popup.hpp"
|
2024-04-03 11:09:42 +02:00
|
|
|
#include "../config/ConfigValue.hpp"
|
2024-03-01 21:07:36 +01:00
|
|
|
#include "../Compositor.hpp"
|
2024-05-09 22:47:21 +02:00
|
|
|
#include "../protocols/LayerShell.hpp"
|
2024-05-11 00:28:33 +02:00
|
|
|
#include "../protocols/XDGShell.hpp"
|
2024-05-09 22:47:21 +02:00
|
|
|
#include <ranges>
|
2024-02-29 15:26:02 +01:00
|
|
|
|
2024-04-27 13:43:12 +02:00
|
|
|
CPopup::CPopup(PHLWINDOW pOwner) : m_pWindowOwner(pOwner) {
|
2024-02-29 15:26:02 +01:00
|
|
|
initAllSignals();
|
|
|
|
}
|
|
|
|
|
2024-04-30 03:41:27 +02:00
|
|
|
CPopup::CPopup(PHLLS pOwner) : m_pLayerOwner(pOwner) {
|
2024-02-29 15:26:02 +01:00
|
|
|
initAllSignals();
|
|
|
|
}
|
|
|
|
|
2024-05-11 00:28:33 +02:00
|
|
|
CPopup::CPopup(SP<CXDGPopupResource> popup, CPopup* pOwner) : m_pParent(pOwner), m_pResource(popup) {
|
|
|
|
m_sWLSurface.assign(popup->surface->surface, this);
|
2024-02-29 15:26:02 +01:00
|
|
|
|
|
|
|
m_pLayerOwner = pOwner->m_pLayerOwner;
|
|
|
|
m_pWindowOwner = pOwner->m_pWindowOwner;
|
|
|
|
|
2024-05-11 00:28:33 +02:00
|
|
|
m_vLastSize = popup->surface->current.geometry.size();
|
2024-02-29 15:26:02 +01:00
|
|
|
unconstrain();
|
|
|
|
|
|
|
|
initAllSignals();
|
|
|
|
}
|
|
|
|
|
|
|
|
CPopup::~CPopup() {
|
|
|
|
m_sWLSurface.unassign();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPopup::initAllSignals() {
|
|
|
|
|
2024-05-11 00:28:33 +02:00
|
|
|
if (!m_pResource) {
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowOwner.expired())
|
2024-05-11 00:28:33 +02:00
|
|
|
listeners.newPopup = m_pWindowOwner->m_pXDGSurface->events.newPopup.registerListener([this](std::any d) { this->onNewPopup(std::any_cast<SP<CXDGPopupResource>>(d)); });
|
2024-05-05 00:46:10 +02:00
|
|
|
else if (!m_pLayerOwner.expired())
|
2024-05-11 00:28:33 +02:00
|
|
|
listeners.newPopup = m_pLayerOwner->layerSurface->events.newPopup.registerListener([this](std::any d) { this->onNewPopup(std::any_cast<SP<CXDGPopupResource>>(d)); });
|
2024-02-29 15:26:02 +01:00
|
|
|
else
|
|
|
|
ASSERT(false);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-11 00:28:33 +02:00
|
|
|
listeners.reposition = m_pResource->events.reposition.registerListener([this](std::any d) { this->onReposition(); });
|
|
|
|
listeners.map = m_pResource->surface->events.map.registerListener([this](std::any d) { this->onMap(); });
|
|
|
|
listeners.unmap = m_pResource->surface->events.unmap.registerListener([this](std::any d) { this->onUnmap(); });
|
2024-05-11 02:02:57 +02:00
|
|
|
listeners.dismissed = m_pResource->surface->events.unmap.registerListener([this](std::any d) { this->onUnmap(); });
|
2024-05-11 00:28:33 +02:00
|
|
|
listeners.destroy = m_pResource->surface->events.destroy.registerListener([this](std::any d) { this->onDestroy(); });
|
|
|
|
listeners.commit = m_pResource->surface->events.commit.registerListener([this](std::any d) { this->onCommit(); });
|
|
|
|
listeners.newPopup = m_pResource->surface->events.newPopup.registerListener([this](std::any d) { this->onNewPopup(std::any_cast<SP<CXDGPopupResource>>(d)); });
|
2024-02-29 15:26:02 +01:00
|
|
|
}
|
|
|
|
|
2024-05-11 00:28:33 +02:00
|
|
|
void CPopup::onNewPopup(SP<CXDGPopupResource> popup) {
|
2024-02-29 15:26:02 +01:00
|
|
|
const auto POPUP = m_vChildren.emplace_back(std::make_unique<CPopup>(popup, this)).get();
|
2024-05-11 00:28:33 +02:00
|
|
|
Debug::log(LOG, "New popup at {:x}", (uintptr_t)POPUP);
|
2024-02-29 15:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPopup::onDestroy() {
|
|
|
|
m_bInert = true;
|
|
|
|
|
|
|
|
if (!m_pParent)
|
|
|
|
return; // head node
|
|
|
|
|
|
|
|
std::erase_if(m_pParent->m_vChildren, [this](const auto& other) { return other.get() == this; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPopup::onMap() {
|
2024-05-11 02:02:57 +02:00
|
|
|
m_vLastSize = {m_pResource->surface->surface->current.width, m_pResource->surface->surface->current.height};
|
|
|
|
const auto COORDS = coordsGlobal();
|
|
|
|
const auto PMONITOR = g_pCompositor->getMonitorFromVector(COORDS);
|
2024-02-29 15:26:02 +01:00
|
|
|
|
2024-02-29 22:51:50 +01:00
|
|
|
CBox box;
|
|
|
|
wlr_surface_get_extends(m_sWLSurface.wlr(), box.pWlr());
|
2024-03-14 21:21:58 +01:00
|
|
|
box.applyFromWlr().translate(COORDS).expand(4);
|
2024-02-29 15:26:02 +01:00
|
|
|
g_pHyprRenderer->damageBox(&box);
|
|
|
|
|
|
|
|
m_vLastPos = coordsRelativeToParent();
|
|
|
|
|
|
|
|
g_pInputManager->simulateMouseMovement();
|
|
|
|
|
|
|
|
m_pSubsurfaceHead = std::make_unique<CSubsurface>(this);
|
|
|
|
|
2024-05-11 02:02:57 +02:00
|
|
|
//unconstrain();
|
2024-03-05 21:42:22 +01:00
|
|
|
sendScale();
|
2024-05-11 02:02:57 +02:00
|
|
|
wlr_surface_send_enter(m_pResource->surface->surface, PMONITOR->output);
|
2024-03-25 17:08:55 +01:00
|
|
|
|
2024-05-05 18:16:00 +02:00
|
|
|
if (!m_pLayerOwner.expired() && m_pLayerOwner->layer < ZWLR_LAYER_SHELL_V1_LAYER_TOP)
|
|
|
|
g_pHyprOpenGL->markBlurDirtyForMonitor(g_pCompositor->getMonitorFromID(m_pLayerOwner->layer));
|
2024-02-29 15:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPopup::onUnmap() {
|
2024-05-11 00:28:33 +02:00
|
|
|
if (!m_pResource || !m_pResource->surface)
|
|
|
|
return;
|
|
|
|
m_vLastSize = {m_pResource->surface->surface->current.width, m_pResource->surface->surface->current.height};
|
2024-02-29 15:26:02 +01:00
|
|
|
const auto COORDS = coordsGlobal();
|
|
|
|
|
2024-02-29 22:51:50 +01:00
|
|
|
CBox box;
|
|
|
|
wlr_surface_get_extends(m_sWLSurface.wlr(), box.pWlr());
|
2024-03-14 21:21:58 +01:00
|
|
|
box.applyFromWlr().translate(COORDS).expand(4);
|
2024-02-29 15:26:02 +01:00
|
|
|
g_pHyprRenderer->damageBox(&box);
|
|
|
|
|
|
|
|
m_pSubsurfaceHead.reset();
|
|
|
|
|
|
|
|
g_pInputManager->simulateMouseMovement();
|
2024-03-25 17:08:55 +01:00
|
|
|
|
2024-05-05 18:16:00 +02:00
|
|
|
if (!m_pLayerOwner.expired() && m_pLayerOwner->layer < ZWLR_LAYER_SHELL_V1_LAYER_TOP)
|
|
|
|
g_pHyprOpenGL->markBlurDirtyForMonitor(g_pCompositor->getMonitorFromID(m_pLayerOwner->layer));
|
2024-05-11 00:28:33 +02:00
|
|
|
|
|
|
|
// damage all children
|
|
|
|
breadthfirst(
|
|
|
|
[this](CPopup* p, void* data) {
|
|
|
|
if (!p->m_pResource)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto box = CBox{p->coordsGlobal(), p->size()};
|
|
|
|
g_pHyprRenderer->damageBox(&box);
|
|
|
|
},
|
|
|
|
nullptr);
|
2024-02-29 15:26:02 +01:00
|
|
|
}
|
|
|
|
|
2024-03-15 16:55:24 +01:00
|
|
|
void CPopup::onCommit(bool ignoreSiblings) {
|
2024-05-11 00:28:33 +02:00
|
|
|
if (m_pResource->surface->initialCommit) {
|
|
|
|
m_pResource->surface->scheduleConfigure();
|
2024-02-29 15:26:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-05 18:16:00 +02:00
|
|
|
if (!m_pWindowOwner.expired() && (!m_pWindowOwner->m_bIsMapped || !m_pWindowOwner->m_pWorkspace->m_bVisible)) {
|
2024-05-11 00:28:33 +02:00
|
|
|
m_vLastSize = {m_pResource->surface->surface->current.width, m_pResource->surface->surface->current.height};
|
2024-04-03 11:09:42 +02:00
|
|
|
|
|
|
|
static auto PLOGDAMAGE = CConfigValue<Hyprlang::INT>("debug:log_damage");
|
|
|
|
if (*PLOGDAMAGE)
|
2024-04-27 13:43:12 +02:00
|
|
|
Debug::log(LOG, "Refusing to commit damage from a subsurface of {} because it's invisible.", m_pWindowOwner.lock());
|
2024-04-03 11:09:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-11 00:28:33 +02:00
|
|
|
if (!m_pResource->surface->mapped)
|
2024-04-04 17:42:24 +02:00
|
|
|
return;
|
|
|
|
|
2024-02-29 15:26:02 +01:00
|
|
|
const auto COORDS = coordsGlobal();
|
|
|
|
const auto COORDSLOCAL = coordsRelativeToParent();
|
|
|
|
|
2024-05-11 00:28:33 +02:00
|
|
|
if (m_vLastSize != Vector2D{m_pResource->surface->surface->current.width, m_pResource->surface->surface->current.height} || m_bRequestedReposition ||
|
|
|
|
m_vLastPos != COORDSLOCAL) {
|
2024-02-29 15:26:02 +01:00
|
|
|
CBox box = {localToGlobal(m_vLastPos), m_vLastSize};
|
|
|
|
g_pHyprRenderer->damageBox(&box);
|
2024-05-11 00:28:33 +02:00
|
|
|
m_vLastSize = {m_pResource->surface->surface->current.width, m_pResource->surface->surface->current.height};
|
2024-02-29 15:26:02 +01:00
|
|
|
box = {COORDS, m_vLastSize};
|
|
|
|
g_pHyprRenderer->damageBox(&box);
|
|
|
|
|
|
|
|
m_vLastPos = COORDSLOCAL;
|
|
|
|
}
|
|
|
|
|
2024-04-04 17:42:24 +02:00
|
|
|
if (!ignoreSiblings && m_pSubsurfaceHead)
|
2024-03-15 16:55:24 +01:00
|
|
|
m_pSubsurfaceHead->recheckDamageForSubsurfaces();
|
2024-02-29 15:26:02 +01:00
|
|
|
|
|
|
|
g_pHyprRenderer->damageSurface(m_sWLSurface.wlr(), COORDS.x, COORDS.y);
|
|
|
|
|
|
|
|
m_bRequestedReposition = false;
|
2024-03-25 17:08:55 +01:00
|
|
|
|
2024-05-05 18:16:00 +02:00
|
|
|
if (!m_pLayerOwner.expired() && m_pLayerOwner->layer < ZWLR_LAYER_SHELL_V1_LAYER_TOP)
|
|
|
|
g_pHyprOpenGL->markBlurDirtyForMonitor(g_pCompositor->getMonitorFromID(m_pLayerOwner->layer));
|
2024-02-29 15:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPopup::onReposition() {
|
|
|
|
Debug::log(LOG, "Popup {:x} requests reposition", (uintptr_t)this);
|
|
|
|
|
|
|
|
m_bRequestedReposition = true;
|
|
|
|
|
|
|
|
m_vLastPos = coordsRelativeToParent();
|
|
|
|
|
|
|
|
unconstrain();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPopup::unconstrain() {
|
|
|
|
const auto COORDS = t1ParentCoords();
|
|
|
|
const auto PMONITOR = g_pCompositor->getMonitorFromVector(COORDS);
|
|
|
|
|
|
|
|
if (!PMONITOR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CBox box = {PMONITOR->vecPosition.x - COORDS.x, PMONITOR->vecPosition.y - COORDS.y, PMONITOR->vecSize.x, PMONITOR->vecSize.y};
|
2024-05-11 00:28:33 +02:00
|
|
|
m_pResource->applyPositioning(box, COORDS - PMONITOR->vecPosition);
|
2024-02-29 15:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CPopup::coordsRelativeToParent() {
|
|
|
|
Vector2D offset;
|
|
|
|
|
2024-05-11 00:28:33 +02:00
|
|
|
if (!m_pResource)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
CPopup* current = this;
|
|
|
|
offset -= current->m_pResource->surface->current.geometry.pos();
|
2024-02-29 15:26:02 +01:00
|
|
|
|
2024-05-11 00:28:33 +02:00
|
|
|
while (current->m_pParent && current->m_pResource) {
|
2024-02-29 15:26:02 +01:00
|
|
|
|
|
|
|
offset += {current->m_sWLSurface.wlr()->current.dx, current->m_sWLSurface.wlr()->current.dy};
|
2024-05-11 00:28:33 +02:00
|
|
|
offset += current->m_pResource->geometry.pos();
|
2024-02-29 15:26:02 +01:00
|
|
|
|
|
|
|
current = current->m_pParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CPopup::coordsGlobal() {
|
|
|
|
return localToGlobal(coordsRelativeToParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CPopup::localToGlobal(const Vector2D& rel) {
|
|
|
|
return t1ParentCoords() + rel;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CPopup::t1ParentCoords() {
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowOwner.expired())
|
2024-05-05 18:16:00 +02:00
|
|
|
return m_pWindowOwner->m_vRealPosition.value();
|
2024-05-05 00:46:10 +02:00
|
|
|
if (!m_pLayerOwner.expired())
|
2024-05-05 18:16:00 +02:00
|
|
|
return m_pLayerOwner->realPosition.value();
|
2024-02-29 15:26:02 +01:00
|
|
|
|
|
|
|
ASSERT(false);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPopup::recheckTree() {
|
|
|
|
CPopup* curr = this;
|
|
|
|
while (curr->m_pParent) {
|
|
|
|
curr = curr->m_pParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr->recheckChildrenRecursive();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CPopup::recheckChildrenRecursive() {
|
|
|
|
for (auto& c : m_vChildren) {
|
2024-03-15 16:55:24 +01:00
|
|
|
c->onCommit(true);
|
2024-02-29 15:26:02 +01:00
|
|
|
c->recheckChildrenRecursive();
|
|
|
|
}
|
2024-02-29 16:07:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2D CPopup::size() {
|
|
|
|
return m_vLastSize;
|
2024-03-01 21:07:36 +01:00
|
|
|
}
|
2024-03-05 21:42:22 +01:00
|
|
|
|
|
|
|
void CPopup::sendScale() {
|
2024-04-27 13:43:12 +02:00
|
|
|
if (!m_pWindowOwner.expired())
|
2024-05-05 18:16:00 +02:00
|
|
|
g_pCompositor->setPreferredScaleForSurface(m_sWLSurface.wlr(), m_pWindowOwner->m_pWLSurface.m_fLastScale);
|
2024-05-05 00:46:10 +02:00
|
|
|
else if (!m_pLayerOwner.expired())
|
2024-05-05 18:16:00 +02:00
|
|
|
g_pCompositor->setPreferredScaleForSurface(m_sWLSurface.wlr(), m_pLayerOwner->surface.m_fLastScale);
|
2024-03-05 21:42:22 +01:00
|
|
|
else
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2024-05-05 00:46:10 +02:00
|
|
|
|
|
|
|
bool CPopup::visible() {
|
|
|
|
if (!m_pWindowOwner.expired())
|
|
|
|
return g_pHyprRenderer->shouldRenderWindow(m_pWindowOwner.lock());
|
|
|
|
if (!m_pLayerOwner.expired())
|
|
|
|
return true;
|
|
|
|
if (m_pParent)
|
|
|
|
return m_pParent->visible();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-09 22:47:21 +02:00
|
|
|
|
|
|
|
void CPopup::bfHelper(std::vector<CPopup*> nodes, std::function<void(CPopup*, void*)> fn, void* data) {
|
|
|
|
for (auto& n : nodes) {
|
|
|
|
fn(n, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<CPopup*> nodes2;
|
|
|
|
|
|
|
|
for (auto& n : nodes) {
|
|
|
|
for (auto& c : n->m_vChildren) {
|
|
|
|
nodes2.push_back(c.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 03:28:21 +02:00
|
|
|
if (!nodes2.empty())
|
|
|
|
bfHelper(nodes2, fn, data);
|
2024-05-09 22:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CPopup::breadthfirst(std::function<void(CPopup*, void*)> fn, void* data) {
|
|
|
|
std::vector<CPopup*> popups;
|
|
|
|
popups.push_back(this);
|
2024-05-10 03:36:56 +02:00
|
|
|
bfHelper(popups, fn, data);
|
2024-05-09 22:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CPopup* CPopup::at(const Vector2D& globalCoords, bool allowsInput) {
|
|
|
|
std::vector<CPopup*> popups;
|
|
|
|
breadthfirst([](CPopup* popup, void* data) { ((std::vector<CPopup*>*)data)->push_back(popup); }, &popups);
|
|
|
|
|
|
|
|
for (auto& p : popups | std::views::reverse) {
|
2024-05-11 00:28:33 +02:00
|
|
|
if (!p->m_pResource)
|
2024-05-09 22:47:21 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!allowsInput) {
|
2024-05-11 02:02:57 +02:00
|
|
|
const Vector2D offset = p->m_pResource ? (p->size() - p->m_pResource->geometry.size()) / 2.F : Vector2D{};
|
|
|
|
const Vector2D size = p->m_pResource ? p->m_pResource->geometry.size() : p->size();
|
|
|
|
|
|
|
|
const auto BOX = CBox{p->coordsGlobal() + offset, size};
|
2024-05-09 22:47:21 +02:00
|
|
|
if (BOX.containsPoint(globalCoords))
|
|
|
|
return p;
|
|
|
|
} else {
|
2024-05-11 02:02:57 +02:00
|
|
|
const Vector2D offset = p->m_pResource ? (p->size() - p->m_pResource->geometry.size()) / 2.F : Vector2D{};
|
|
|
|
const auto REGION = CRegion{&p->m_sWLSurface.wlr()->current.input}.translate(p->coordsGlobal() + offset);
|
2024-05-09 22:47:21 +02:00
|
|
|
if (REGION.containsPoint(globalCoords))
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|