-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathOverviewInteraction.cpp
More file actions
616 lines (499 loc) · 18 KB
/
Copy pathOverviewInteraction.cpp
File metadata and controls
616 lines (499 loc) · 18 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include "HyprlandConfigCompat.hpp"
#define HyprlandAPI CompatHyprlandAPI
#include "OverviewInternal.hpp"
#include "HyprexpoLogic.hpp"
#include <hyprland/src/Compositor.hpp>
#include <hyprland/src/config/ConfigValue.hpp>
#include <hyprland/src/helpers/Monitor.hpp>
#include <hyprland/src/managers/cursor/CursorShapeOverrideController.hpp>
#include <hyprland/src/managers/eventLoop/EventLoopManager.hpp>
#include <algorithm>
#include <chrono>
#include <cmath>
using namespace std::chrono_literals;
void COverview::selectHoveredWorkspace() {
if (closing)
return;
updateHoveredFromMouse();
closeOnID = std::clamp(hoveredID, 0, SIDE_LENGTH * SIDE_LENGTH - 1);
}
int64_t COverview::selectedWorkspaceID() const {
const int id = closeOnID == -1 ? openedID : closeOnID;
if (id < 0 || id >= (int)images.size())
return WORKSPACE_INVALID;
return images[id].workspaceID;
}
bool COverview::selectWorkspaceByID(int64_t workspaceID) {
if (closing)
return false;
for (size_t i = 0; i < images.size(); ++i) {
if (images[i].workspaceID != workspaceID)
continue;
closeOnID = i;
return true;
}
return false;
}
bool COverview::selectVisibleIndex(size_t index) {
if (closing)
return false;
size_t visible = 0;
for (size_t i = 0; i < images.size(); ++i) {
if (images[i].workspaceID == WORKSPACE_INVALID)
continue;
if (visible == index) {
closeOnID = i;
return true;
}
++visible;
}
return false;
}
void COverview::updateHoveredFromMouse() {
const auto MON = pMonitor.lock();
if (!MON)
return;
const int newHoveredID = Hyprexpo::tileIndexFromPoint(lastMousePosLocal.x, lastMousePosLocal.y, MON->m_size.x, MON->m_size.y, SIDE_LENGTH);
if (newHoveredID == hoveredID)
return;
hoveredID = newHoveredID;
damage();
}
void COverview::ensureKbFocusInitialized() {
if (kbFocusID != -1)
return;
// try to set to current openedID
if (openedID != -1) {
kbFocusID = openedID;
return;
}
// fallback: first valid tile
for (size_t i = 0; i < images.size(); ++i) {
if (isTileValid(i)) {
kbFocusID = i;
return;
}
}
}
bool COverview::isTileValid(int id) const {
if (id < 0 || id >= (int)images.size())
return false;
return images[id].workspaceID != WORKSPACE_INVALID;
}
int COverview::tileForWorkspaceID(int wsid) const {
for (size_t i = 0; i < images.size(); ++i) {
if (images[i].workspaceID == wsid)
return (int)i;
}
return -1;
}
int COverview::tileForVisibleIndex(int vIdx) const {
if (vIdx < 0)
return -1;
int seen = 0;
for (size_t i = 0; i < images.size(); ++i) {
if (images[i].workspaceID == WORKSPACE_INVALID)
continue;
if (seen == vIdx)
return (int)i;
++seen;
}
return -1;
}
Vector2D COverview::tilePointToWorkspacePoint(int id, const Vector2D& localPoint) const {
const auto MON = pMonitor.lock();
if (!MON)
return {};
const Vector2D tileSize = MON->m_size / SIDE_LENGTH;
const Vector2D tilePos = tileSize * Vector2D{id % SIDE_LENGTH, id / SIDE_LENGTH};
const Vector2D inTile = localPoint - tilePos;
return MON->m_position + Vector2D{
std::clamp(inTile.x / tileSize.x, 0.0, 1.0) * MON->m_size.x,
std::clamp(inTile.y / tileSize.y, 0.0, 1.0) * MON->m_size.y,
};
}
PHLWINDOW COverview::windowAtTilePoint(int id, const Vector2D& localPoint) const {
if (!isTileValid(id))
return nullptr;
const auto WORKSPACE = images[id].pWorkspace ? images[id].pWorkspace : g_pCompositor->getWorkspaceByID(images[id].workspaceID);
if (!WORKSPACE)
return nullptr;
const auto POINT = tilePointToWorkspacePoint(id, localPoint);
for (auto it = g_pCompositor->m_windows.rbegin(); it != g_pCompositor->m_windows.rend(); ++it) {
const auto& window = *it;
if (!windowVisibleOnWorkspace(window, WORKSPACE))
continue;
if (window->getWindowMainSurfaceBox().containsPoint(POINT))
return window;
}
return nullptr;
}
void COverview::beginWindowDrag() {
updateHoveredFromMouse();
dragStartLocal = lastMousePosLocal;
dragSourceID = hoveredID;
dragMoved = false;
dragWindow = windowAtTilePoint(dragSourceID, dragStartLocal);
dragGrabOffset = Vector2D{};
dropIntent = {};
dropIntentTargetID = -1;
if (!dragWindow)
return;
const auto POINT = tilePointToWorkspacePoint(dragSourceID, dragStartLocal);
const auto BOX = dragWindow->getWindowMainSurfaceBox();
dragGrabOffset = POINT - Vector2D{BOX.x, BOX.y};
Cursor::overrideController->setOverride("grabbing", Cursor::CURSOR_OVERRIDE_UNKNOWN);
damage();
}
void COverview::updateWindowDrag() {
if (!dragWindow)
return;
const auto dx = lastMousePosLocal.x - dragStartLocal.x;
const auto dy = lastMousePosLocal.y - dragStartLocal.y;
if (!dragMoved && std::hypot(dx, dy) < 12.0)
return;
if (!dragMoved)
dragMoved = true;
damage();
}
PHLWORKSPACE COverview::ensureWorkspaceForTile(int id) {
if (!isTileValid(id))
return nullptr;
const auto MON = pMonitor.lock();
if (!MON)
return nullptr;
auto& image = images[id];
if (image.pWorkspace)
return image.pWorkspace;
auto workspace = g_pCompositor->getWorkspaceByID(image.workspaceID);
if (!workspace)
workspace = g_pCompositor->createNewWorkspace(image.workspaceID, MON->m_id, std::to_string(image.workspaceID), false);
image.pWorkspace = workspace;
return workspace;
}
bool COverview::finishWindowDrag() {
const auto WINDOW = dragWindow;
const int SOURCE = dragSourceID;
const bool MOVED = dragMoved;
dragWindow = nullptr;
dragSourceID = -1;
dragMoved = false;
dragGrabOffset = Vector2D{};
dropIntent = {};
dropIntentTargetID = -1;
Cursor::overrideController->setOverride("left_ptr", Cursor::CURSOR_OVERRIDE_UNKNOWN);
if (!WINDOW || !MOVED)
return false;
// Drag proxies are render-only; repaint even when release does not move workspace contents.
damage();
updateHoveredFromMouse();
const int TARGET = hoveredID;
if (!isTileValid(SOURCE) || !isTileValid(TARGET) || SOURCE == TARGET)
return true;
const auto SOURCEWS = images[SOURCE].pWorkspace ? images[SOURCE].pWorkspace : g_pCompositor->getWorkspaceByID(images[SOURCE].workspaceID);
const auto TARGETWS = ensureWorkspaceForTile(TARGET);
if (!windowVisibleOnWorkspace(WINDOW, SOURCEWS) || !TARGETWS || TARGETWS == SOURCEWS)
return true;
images[SOURCE].pWorkspace = SOURCEWS;
g_pCompositor->moveWindowToWorkspaceSafe(WINDOW, TARGETWS);
settleWorkspaceMoveAnimation(WINDOW);
redrawDraggedWindowTiles(SOURCE, TARGET);
return true;
}
bool COverview::moveWindowBetweenVisibleIndices(size_t sourceIndex, size_t targetIndex, const PHLWINDOW& requestedWindow) {
if (closing)
return false;
const int SOURCE = tileForVisibleIndex(sourceIndex);
const int TARGET = tileForVisibleIndex(targetIndex);
if (!isTileValid(SOURCE) || !isTileValid(TARGET) || SOURCE == TARGET)
return false;
const auto SOURCEWS = images[SOURCE].pWorkspace ? images[SOURCE].pWorkspace : g_pCompositor->getWorkspaceByID(images[SOURCE].workspaceID);
const auto TARGETWS = ensureWorkspaceForTile(TARGET);
if (!SOURCEWS || !TARGETWS || SOURCEWS == TARGETWS)
return false;
PHLWINDOW window = requestedWindow;
if (window) {
if (!windowVisibleOnWorkspace(window, SOURCEWS))
return false;
} else {
for (auto it = g_pCompositor->m_windows.rbegin(); it != g_pCompositor->m_windows.rend(); ++it) {
const auto& candidate = *it;
if (!windowVisibleOnWorkspace(candidate, SOURCEWS))
continue;
window = candidate;
break;
}
}
if (!window)
return false;
images[SOURCE].pWorkspace = SOURCEWS;
g_pCompositor->moveWindowToWorkspaceSafe(window, TARGETWS);
settleWorkspaceMoveAnimation(window);
redrawDraggedWindowTiles(SOURCE, TARGET);
return true;
}
void COverview::redrawDraggedWindowTiles(int source, int target) {
queueRedrawID(source);
queueRedrawID(target);
for (const auto id : queuedRedrawIDs) {
if (std::find(settlingRedrawIDs.begin(), settlingRedrawIDs.end(), id) == settlingRedrawIDs.end())
settlingRedrawIDs.push_back(id);
}
redrawSettleTicks = 4;
flushQueuedRedraws();
if (redrawSettleTimer)
return;
redrawSettleTimer = makeShared<CEventLoopTimer>(
75ms,
[this](SP<CEventLoopTimer> self, void*) {
if (!g_pOverview || g_pOverview.get() != this || closing) {
self->cancel();
redrawSettleTimer.reset();
return;
}
for (const auto id : settlingRedrawIDs)
queueRedrawID(id);
flushQueuedRedraws();
if (--redrawSettleTicks <= 0) {
settlingRedrawIDs.clear();
redrawSettleTimer.reset();
self->cancel();
return;
}
self->updateTimeout(100ms);
},
nullptr);
g_pEventLoopManager->addTimer(redrawSettleTimer);
}
void COverview::queueRedrawID(int id) {
if (!isTileValid(id))
return;
if (std::find(queuedRedrawIDs.begin(), queuedRedrawIDs.end(), id) == queuedRedrawIDs.end())
queuedRedrawIDs.push_back(id);
}
void COverview::flushQueuedRedraws() {
if (queuedRedrawIDs.empty())
return;
const auto ids = queuedRedrawIDs;
queuedRedrawIDs.clear();
for (const auto id : ids)
redrawID(id);
damage();
}
bool COverview::selectVisibleToken(const std::string& token) {
if (closing)
return false;
static auto* const* PSELECTLABEL = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:selection_label_enable")->getDataStaticPtr();
static auto const* PSELECTMAP = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:selection_label_token_map")->getDataStaticPtr();
const std::string normalized = lowerString(trimString(token));
if (normalized.empty())
return false;
if (**PSELECTLABEL) {
const auto tokens = splitCommaList(std::string{*PSELECTMAP});
for (size_t i = 0; i < tokens.size(); ++i) {
if (tokens[i].empty() || lowerString(tokens[i]) != normalized)
continue;
return selectVisibleIndex(i);
}
return false;
}
const int visibleIndex = fallbackTokenToVisibleIndex(normalized);
if (visibleIndex < 0)
return false;
return selectVisibleIndex(visibleIndex);
}
void COverview::moveFocus(int dx, int dy) {
ensureKbFocusInitialized();
if (kbFocusID == -1)
return;
int x = kbFocusID % SIDE_LENGTH;
int y = kbFocusID / SIDE_LENGTH;
static auto* const* PWRAPH = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:keynav_wrap_h")->getDataStaticPtr();
static auto* const* PWRAPV = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:keynav_wrap_v")->getDataStaticPtr();
if (dx != 0) {
static auto* const* PREADING = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:keynav_reading_order")->getDataStaticPtr();
int step = dx > 0 ? 1 : -1;
if (**PREADING) {
// reading-order scan: proceed linearly across the grid (row-major)
const int total = SIDE_LENGTH * SIDE_LENGTH;
int idx = kbFocusID;
for (int tries = 0; tries < total; ++tries) {
idx += step;
if (idx < 0 || idx >= total) {
// wrap only if both wraps are enabled (edge of grid)
if (**PWRAPH && **PWRAPV)
idx = (idx + total) % total;
else
break;
}
if (isTileValid(idx)) {
kbFocusID = idx;
return;
}
}
} else {
// in-row scan with optional horizontal wrap
int nx = x;
for (int tries = 0; tries < SIDE_LENGTH; ++tries) {
nx += step;
if (nx < 0 || nx >= SIDE_LENGTH) {
if (**PWRAPH)
nx = (nx + SIDE_LENGTH) % SIDE_LENGTH;
else
break;
}
const int nid = nx + y * SIDE_LENGTH;
if (isTileValid(nid)) {
kbFocusID = nid;
return;
}
}
}
}
if (dy != 0) {
int step = dy > 0 ? 1 : -1;
int ny = y;
for (int tries = 0; tries < SIDE_LENGTH; ++tries) {
ny += step;
if (ny < 0 || ny >= SIDE_LENGTH) {
if (**PWRAPV)
ny = (ny + SIDE_LENGTH) % SIDE_LENGTH;
else
break;
}
const int nid = x + ny * SIDE_LENGTH;
if (isTileValid(nid)) {
kbFocusID = nid;
return;
}
}
}
}
void COverview::onKbMoveFocus(const std::string& dir) {
if (closing)
return;
if (dir == "left")
moveFocus(-1, 0);
else if (dir == "right")
moveFocus(1, 0);
else if (dir == "up")
moveFocus(0, -1);
else if (dir == "down")
moveFocus(0, 1);
damage();
}
void COverview::onKbConfirm() {
if (closing)
return;
ensureKbFocusInitialized();
if (kbFocusID != -1)
closeOnID = kbFocusID;
close();
}
void COverview::onKbSelectNumber(int num) {
if (closing)
return;
if (num == 0)
num = 10;
if (selectWorkspaceByID(num))
close();
}
void COverview::onKbSelectToken(int visibleIdx) {
if (closing)
return;
if (visibleIdx < 0)
return;
if (selectVisibleIndex(visibleIdx))
close();
}
static float lerpFloat(const float& from, const float& to, const float perc) {
return (to - from) * perc + from;
}
static Vector2D lerp(const Vector2D& from, const Vector2D& to, const float perc) {
return Vector2D{lerpFloat(from.x, to.x, perc), lerpFloat(from.y, to.y, perc)};
}
void COverview::setClosing(bool closing_) {
closing = closing_;
}
void COverview::onWindowMoveToWorkspace(const PHLWINDOW& window, const PHLWORKSPACE& workspace) {
if (!closing || externalWorkspaceMoveDuringClose || !window)
return;
const auto monitor = pMonitor.lock();
if (!monitor)
return;
const bool movedOnOverviewMonitor = window->m_monitor == monitor || (window->m_workspace && window->m_workspace->m_monitor == monitor) || (workspace && workspace->m_monitor == monitor);
if (!movedOnOverviewMonitor)
return;
externalWorkspaceMoveDuringClose = true;
damage();
g_pCompositor->scheduleFrameForMonitor(monitor);
}
void COverview::resetSwipe() {
swipeWasCommenced = false;
}
void COverview::onSwipeUpdate(double delta) {
// Once the close animation is committed, ignore further swipe input so a
// re-grabbed gesture can't warp size/pos back and replay the close.
if (m_closeCommitted)
return;
m_isSwiping = true;
const auto MON = pMonitor.lock();
if (!MON) {
m_isSwiping = false;
return;
}
if (swipeWasCommenced)
return;
static auto* const* PDISTANCE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:gesture_distance")->getDataStaticPtr();
const double distance = std::max<Hyprlang::INT>(1, **PDISTANCE);
const float PERC = closing ? std::clamp(delta / distance, 0.0, 1.0) : 1.0 - std::clamp(delta / distance, 0.0, 1.0);
const auto WORKSPACE_FOCUS_ID = closing && closeOnID != -1 ? closeOnID : openedID;
Vector2D tileSize = (MON->m_size / SIDE_LENGTH);
const auto SIZEMAX = MON->m_size * MON->m_size / tileSize;
const auto POSMAX = (-((MON->m_size / (double)SIDE_LENGTH) * Vector2D{WORKSPACE_FOCUS_ID % SIDE_LENGTH, WORKSPACE_FOCUS_ID / SIDE_LENGTH}) * MON->m_scale) *
(MON->m_size / tileSize);
const auto SIZEMIN = MON->m_size;
const auto POSMIN = Vector2D{0, 0};
size->setCallbackOnEnd(nullptr);
pos->setCallbackOnEnd(nullptr);
size->setValueAndWarp(lerp(SIZEMIN, SIZEMAX, PERC));
pos->setValueAndWarp(lerp(POSMIN, POSMAX, PERC));
}
void COverview::onSwipeEnd() {
if (m_closeCommitted)
return;
const auto MON = pMonitor.lock();
if (!MON) {
m_isSwiping = false;
swipeWasCommenced = false;
closing = true;
g_pOverview.reset();
return;
}
const auto SIZEMIN = MON->m_size;
const auto SIZEMAX = MON->m_size * MON->m_size / (MON->m_size / SIDE_LENGTH);
const auto PERC = (size->value() - SIZEMIN).x / (SIZEMAX - SIZEMIN).x;
if (PERC > 0.5) {
close();
return;
}
*size = MON->m_size;
*pos = {0, 0};
size->setCallbackOnEnd([this](WP<Hyprutils::Animation::CBaseAnimatedVariable> thisptr) { redrawAll(true); });
swipeWasCommenced = true;
m_isSwiping = false;
}
void COverview::enterSubmapIfEnabled() {
static auto* const* PKEYNAV = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:keynav_enable")->getDataStaticPtr();
if (**PKEYNAV && !submapActive) {
// switch to a dedicated submap for hyprexpo navigation
g_pKeybindManager->m_dispatchers["submap"]("hyprexpo");
submapActive = true;
}
}
void COverview::resetSubmapIfNeeded() {
if (submapActive) {
g_pKeybindManager->m_dispatchers["submap"]("reset");
submapActive = false;
}
}