summaryrefslogtreecommitdiff
path: root/src/managers/input/InputMethodPopup.cpp
blob: bc2876adfe0febe352fa832f50cc889e6eb6b5c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "InputMethodPopup.hpp"
#include "InputManager.hpp"
#include "../../Compositor.hpp"

CInputPopup::CInputPopup(wlr_input_popup_surface_v2* surf) : pWlr(surf) {
    surface.assign(surf->surface);
    initCallbacks();
}

static void onCommit(void* owner, void* data) {
    const auto PPOPUP = (CInputPopup*)owner;
    PPOPUP->onCommit();
}

static void onMap(void* owner, void* data) {
    const auto PPOPUP = (CInputPopup*)owner;
    PPOPUP->onMap();
}

static void onUnmap(void* owner, void* data) {
    const auto PPOPUP = (CInputPopup*)owner;
    PPOPUP->onUnmap();
}

static void onDestroy(void* owner, void* data) {
    const auto PPOPUP = (CInputPopup*)owner;
    PPOPUP->onDestroy();
}

void CInputPopup::initCallbacks() {
    hyprListener_commitPopup.initCallback(&pWlr->surface->events.commit, &::onCommit, this, "IME Popup");
    hyprListener_mapPopup.initCallback(&pWlr->surface->events.map, &::onMap, this, "IME Popup");
    hyprListener_unmapPopup.initCallback(&pWlr->surface->events.unmap, &::onUnmap, this, "IME Popup");
    hyprListener_destroyPopup.initCallback(&pWlr->events.destroy, &::onDestroy, this, "IME Popup");
}

CWLSurface* CInputPopup::queryOwner() {
    const auto FOCUSED = g_pInputManager->m_sIMERelay.getFocusedTextInput();

    if (!FOCUSED)
        return nullptr;

    return CWLSurface::surfaceFromWlr(FOCUSED->focusedSurface());
}

void CInputPopup::onDestroy() {
    hyprListener_commitPopup.removeCallback();
    hyprListener_destroyPopup.removeCallback();
    hyprListener_mapPopup.removeCallback();
    hyprListener_unmapPopup.removeCallback();

    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;

    g_pProtocolManager->m_pFractionalScaleProtocolManager->setPreferredScaleForSurface(surface.wlr(), PMONITOR->scale);
}

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;
    }

    Vector2D pos    = OWNER->getSurfaceBoxGlobal().value_or(CBox{0, 0, 0, 0}).pos() + lastBoxLocal.pos();
    CBox     global = {pos, lastPopupSize};

    g_pHyprRenderer->damageBox(&global);
}

void CInputPopup::damageSurface() {
    const auto OWNER = queryOwner();

    if (!OWNER) {
        Debug::log(ERR, "BUG THIS: No owner in imepopup::damagesurface");
        return;
    }

    Vector2D pos = OWNER->getSurfaceBoxGlobal().value_or(CBox{0, 0, 0, 0}).pos() + lastBoxLocal.pos();
    g_pHyprRenderer->damageSurface(surface.wlr(), pos.x, pos.y);
}

void CInputPopup::updateBox() {
    if (!surface.wlr()->mapped)
        return;

    const auto OWNER      = queryOwner();
    const auto PFOCUSEDTI = g_pInputManager->m_sIMERelay.getFocusedTextInput();

    if (!PFOCUSEDTI)
        return;

    bool cursorRect     = PFOCUSEDTI->hasCursorRectangle();
    CBox cursorBoxLocal = PFOCUSEDTI->cursorBox();

    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}};
        cursorBoxLocal  = {0, 0, (int)parentBox.w, (int)parentBox.h};
    }

    Vector2D currentPopupSize = surface.getViewporterCorrectedSize();

    if (cursorBoxLocal != lastBoxLocal || currentPopupSize != lastPopupSize)
        damageEntire();

    CMonitor* pMonitor = g_pCompositor->getMonitorFromVector(parentBox.middle());

    if (cursorBoxLocal.y + parentBox.y + currentPopupSize.y + cursorBoxLocal.height > pMonitor->vecPosition.y + pMonitor->vecSize.y)
        cursorBoxLocal.y -= currentPopupSize.y;
    else
        cursorBoxLocal.y += cursorBoxLocal.height;

    if (cursorBoxLocal.x + parentBox.x + currentPopupSize.x > pMonitor->vecPosition.x + pMonitor->vecSize.x)
        cursorBoxLocal.x -= (cursorBoxLocal.x + parentBox.x + currentPopupSize.x) - (pMonitor->vecPosition.x + pMonitor->vecSize.x);

    lastBoxLocal  = cursorBoxLocal;
    lastPopupSize = currentPopupSize;

    wlr_input_popup_surface_v2_send_text_input_rectangle(pWlr, cursorBoxLocal.pWlr());

    damageSurface();

    if (const auto PM = g_pCompositor->getMonitorFromCursor(); PM && PM->ID != lastMonitor) {
        const auto PML = g_pCompositor->getMonitorFromID(lastMonitor);

        if (PML)
            wlr_surface_send_leave(surface.wlr(), PML->output);

        wlr_surface_send_enter(surface.wlr(), PM->output);

        lastMonitor = PM->ID;
    }
}

CBox CInputPopup::globalBox() {
    const auto OWNER = queryOwner();

    if (!OWNER) {
        Debug::log(ERR, "BUG THIS: No owner in imepopup::globalbox");
        return {};
    }

    return lastBoxLocal.copy().translate(OWNER->getSurfaceBoxGlobal().value_or(CBox{0, 0, 0, 0}).pos());
}

bool CInputPopup::isVecInPopup(const Vector2D& point) {
    return globalBox().containsPoint(point);
}

wlr_surface* CInputPopup::getWlrSurface() {
    return surface.wlr();
}