Hyprland/src/managers/input/InputMethodPopup.cpp

159 lines
4.6 KiB
C++
Raw Normal View History

2024-03-24 16:08:25 +00:00
#include "InputMethodPopup.hpp"
2024-03-24 17:19:35 +00:00
#include "InputManager.hpp"
#include "../../Compositor.hpp"
2024-04-20 14:14:54 +01:00
#include "../../protocols/FractionalScale.hpp"
2024-05-01 16:41:17 +01:00
#include "../../protocols/InputMethodV2.hpp"
#include "../../protocols/core/Compositor.hpp"
2024-05-01 16:41:17 +01:00
CInputPopup::CInputPopup(SP<CInputMethodPopupV2> popup_) : popup(popup_) {
listeners.commit = popup_->events.commit.registerListener([this](std::any d) { onCommit(); });
listeners.map = popup_->events.map.registerListener([this](std::any d) { onMap(); });
listeners.unmap = popup_->events.unmap.registerListener([this](std::any d) { onUnmap(); });
listeners.destroy = popup_->events.destroy.registerListener([this](std::any d) { onDestroy(); });
surface = CWLSurface::create();
surface->assign(popup_->surface());
2024-03-24 16:08:25 +00:00
}
SP<CWLSurface> CInputPopup::queryOwner() {
2024-03-24 16:08:25 +00:00
const auto FOCUSED = g_pInputManager->m_sIMERelay.getFocusedTextInput();
if (!FOCUSED)
return nullptr;
return CWLSurface::fromResource(FOCUSED->focusedSurface());
2024-03-24 16:08:25 +00:00
}
void CInputPopup::onDestroy() {
g_pInputManager->m_sIMERelay.removePopup(this);
}
void CInputPopup::onMap() {
Debug::log(LOG, "Mapped an IME Popup");
updateBox();
damageEntire();
const auto PMONITOR = g_pCompositor->getMonitorFromVector(globalBox().middle());
if (!PMONITOR)
return;
PROTO::fractional->sendScale(surface->resource(), PMONITOR->scale);
2024-03-24 16:08:25 +00:00
}
void CInputPopup::onUnmap() {
Debug::log(LOG, "Unmapped an IME Popup");
damageEntire();
}
void CInputPopup::onCommit() {
updateBox();
}
void CInputPopup::damageEntire() {
const auto OWNER = queryOwner();
if (!OWNER) {
Debug::log(ERR, "BUG THIS: No owner in imepopup::damageentire");
return;
}
CBox box = globalBox();
g_pHyprRenderer->damageBox(&box);
2024-03-24 16:08:25 +00:00
}
void CInputPopup::damageSurface() {
const auto OWNER = queryOwner();
if (!OWNER) {
Debug::log(ERR, "BUG THIS: No owner in imepopup::damagesurface");
return;
}
Vector2D pos = globalBox().pos();
g_pHyprRenderer->damageSurface(surface->resource(), pos.x, pos.y);
2024-03-24 16:08:25 +00:00
}
void CInputPopup::updateBox() {
if (!popup->mapped)
2024-03-24 16:08:25 +00:00
return;
const auto OWNER = queryOwner();
const auto PFOCUSEDTI = g_pInputManager->m_sIMERelay.getFocusedTextInput();
if (!PFOCUSEDTI)
return;
bool cursorRect = PFOCUSEDTI->hasCursorRectangle();
CBox cursorBoxParent = PFOCUSEDTI->cursorBox();
2024-03-24 16:08:25 +00:00
CBox parentBox;
if (!OWNER)
parentBox = {0, 0, 500, 500};
else
parentBox = OWNER->getSurfaceBoxGlobal().value_or(CBox{0, 0, 500, 500});
if (!cursorRect) {
Vector2D coords = OWNER ? OWNER->getSurfaceBoxGlobal().value_or(CBox{0, 0, 500, 500}).pos() : Vector2D{0, 0};
parentBox = {coords, {500, 500}};
cursorBoxParent = {0, 0, (int)parentBox.w, (int)parentBox.h};
2024-03-24 16:08:25 +00:00
}
Vector2D currentPopupSize = surface->getViewporterCorrectedSize();
2024-03-24 16:08:25 +00:00
CMonitor* pMonitor = g_pCompositor->getMonitorFromVector(parentBox.middle());
Vector2D popupOffset(0, 0);
2024-03-24 16:08:25 +00:00
if (parentBox.y + cursorBoxParent.y + cursorBoxParent.height + currentPopupSize.y > pMonitor->vecPosition.y + pMonitor->vecSize.y)
popupOffset.y = -currentPopupSize.y;
else
popupOffset.y = cursorBoxParent.height;
2024-03-24 16:08:25 +00:00
double popupOverflow = parentBox.x + cursorBoxParent.x + currentPopupSize.x - (pMonitor->vecPosition.x + pMonitor->vecSize.x);
if (popupOverflow > 0)
popupOffset.x -= popupOverflow;
2024-03-24 16:08:25 +00:00
CBox cursorBoxLocal({-popupOffset.x, -popupOffset.y}, cursorBoxParent.size());
popup->sendInputRectangle(cursorBoxLocal);
2024-03-24 16:08:25 +00:00
CBox popupBoxParent(cursorBoxParent.pos() + popupOffset, currentPopupSize);
if (popupBoxParent != lastBoxLocal) {
damageEntire();
lastBoxLocal = popupBoxParent;
}
2024-03-24 16:08:25 +00:00
damageSurface();
if (const auto PM = g_pCompositor->getMonitorFromCursor(); PM && PM->ID != lastMonitor) {
const auto PML = g_pCompositor->getMonitorFromID(lastMonitor);
if (PML)
surface->resource()->leave(PML->self.lock());
2024-03-24 16:08:25 +00:00
surface->resource()->enter(PM->self.lock());
2024-03-24 16:08:25 +00:00
lastMonitor = PM->ID;
}
}
CBox CInputPopup::globalBox() {
const auto OWNER = queryOwner();
if (!OWNER) {
Debug::log(ERR, "BUG THIS: No owner in imepopup::globalbox");
return {};
}
CBox parentBox = OWNER->getSurfaceBoxGlobal().value_or(CBox{0, 0, 500, 500});
2024-03-24 16:08:25 +00:00
return lastBoxLocal.copy().translate(parentBox.pos());
2024-03-24 16:08:25 +00:00
}
bool CInputPopup::isVecInPopup(const Vector2D& point) {
return globalBox().containsPoint(point);
}
SP<CWLSurfaceResource> CInputPopup::getSurface() {
return surface->resource();
2024-03-24 16:08:25 +00:00
}