-
Notifications
You must be signed in to change notification settings - Fork 4
/
workspaceLayout.cpp
598 lines (494 loc) · 17.1 KB
/
workspaceLayout.cpp
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
#include "workspaceLayout.hpp"
SWorkspaceLayoutWindowData* CWorkspaceLayout::getDataFromWindow(PHLWINDOW pWindow, bool create) {
for (auto& nd : m_vWorkspaceWindowData) {
if (nd.pWindow.lock() == pWindow) {
return &nd;
}
}
//Create it if we don't have it, using the window's current workspaceID.
SWorkspaceLayoutWindowData *WINDOWDATA = nullptr;
if (create) {
WINDOWDATA = &m_vWorkspaceWindowData.emplace_back();
WINDOWDATA->pWindow = pWindow;
WINDOWDATA->workspaceID = pWindow->workspaceID();
}
return WINDOWDATA;
}
void CWorkspaceLayout::setupWorkspace(PHLWORKSPACE pWorkspace) {
if (!pWorkspace) {
//??
return;
}
IHyprLayout *wlret = nullptr;
wlret = findUserLayoutForWorkspace(pWorkspace);
bool isDefault = false;
if (!wlret) {
isDefault = true;
wlret = m_pDefaultLayout;
}
setLayoutForWorkspace(wlret, pWorkspace, isDefault);
}
void CWorkspaceLayout::onEnable() {
for (auto &wsp : g_pCompositor->m_vWorkspaces) {
setupWorkspace(wsp);
}
}
void CWorkspaceLayout::onDisable() {
for (auto &wsd : m_vWorkspacesData) {
if (wsd.layout) {
wsd.layout->onDisable();
}
}
m_vWorkspacesData.clear();
}
void CWorkspaceLayout::onWindowCreated(PHLWINDOW pWindow, eDirection direction) {
if (!pWindow) return; //??
auto WDATA = getDataFromWindow(pWindow);
if (!WDATA) return;
auto const WSID = WDATA->workspaceID;
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout) {
return layout->onWindowCreated(pWindow, direction);
}
}
void CWorkspaceLayout::onWindowCreatedTiling(PHLWINDOW pWindow, eDirection direction) {
if (!pWindow) return; //??
auto WDATA = getDataFromWindow(pWindow);
if (!WDATA) return;
auto const WSID = WDATA->workspaceID;
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout) {
layout->onWindowCreatedTiling(pWindow, direction);
}
}
void CWorkspaceLayout::onWindowCreatedFloating(PHLWINDOW pWindow) {
if (!pWindow) return; //??
auto WDATA = getDataFromWindow(pWindow);
if (!WDATA) return;
auto const WSID = WDATA->workspaceID;
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout) {
return layout->onWindowCreatedFloating(pWindow);
}
}
bool CWorkspaceLayout::isWindowTiled(PHLWINDOW pWindow) {
if (!pWindow) return false; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->isWindowTiled(pWindow);
return false;
}
void CWorkspaceLayout::onWindowRemoved(PHLWINDOW pWindow) {
if (!pWindow) return; //??
auto WDATA = getDataFromWindow(pWindow, false);
auto const WSID = WDATA ? WDATA->workspaceID : pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout) {
if (WDATA) //??
m_vWorkspaceWindowData.remove(*WDATA);
return layout->onWindowRemoved(pWindow);
}
}
void CWorkspaceLayout::onWindowRemovedTiling(PHLWINDOW pWindow) {
if (!pWindow) return; //??
auto WDATA = getDataFromWindow(pWindow, false);
auto const WSID = WDATA ? WDATA->workspaceID : pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
{
if (WDATA) //??
m_vWorkspaceWindowData.remove(*WDATA);
layout->onWindowRemovedTiling(pWindow);
}
}
void CWorkspaceLayout::onWindowRemovedFloating(PHLWINDOW pWindow) {
if (!pWindow) return; //??
auto WDATA = getDataFromWindow(pWindow, false);
if (WDATA) return;
auto const WSID = WDATA->workspaceID;
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout) {
if (WDATA) //??
m_vWorkspaceWindowData.remove(*WDATA);
return layout->onWindowRemovedFloating(pWindow);
}
}
void CWorkspaceLayout::recalculateMonitor(const MONITORID& monID) {
const auto PMONITOR = g_pCompositor->getMonitorFromID(monID);
if (!PMONITOR)
return;
if (PMONITOR->activeSpecialWorkspace) {
const auto PSPWS = PMONITOR->activeSpecialWorkspace;
if (PSPWS) {
IHyprLayout *layout = getLayoutForWorkspace(PSPWS->m_iID);
if (layout)
return layout->recalculateMonitor(monID);
}
}
const auto PWORKSPACE = PMONITOR->activeWorkspace;
if (!PWORKSPACE) return;
IHyprLayout *layout = getLayoutForWorkspace(PWORKSPACE->m_iID);
if (layout)
return layout->recalculateMonitor(monID);
}
void CWorkspaceLayout::recalculateWindow(PHLWINDOW pWindow) {
if (!pWindow) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->recalculateWindow(pWindow);
}
void CWorkspaceLayout::changeWindowFloatingMode(PHLWINDOW pWindow) {
if (!pWindow) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->changeWindowFloatingMode(pWindow);
}
void CWorkspaceLayout::onBeginDragWindow() {
const auto pWindow = g_pInputManager->currentlyDraggedWindow.lock();
if (!pWindow) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout) {
layout->onBeginDragWindow();
m_vBeginDragXY = layout->m_vBeginDragXY;
m_vBeginDragPositionXY = layout->m_vBeginDragPositionXY;
m_vBeginDragSizeXY = layout->m_vBeginDragSizeXY;
m_vLastDragXY = layout->m_vLastDragXY;
m_vDraggingWindowOriginalFloatSize = layout->m_vDraggingWindowOriginalFloatSize;
m_eGrabbedCorner = layout->m_eGrabbedCorner;
}
}
void CWorkspaceLayout::resizeActiveWindow(const Vector2D& vec, eRectCorner corner, PHLWINDOW pWindow) {
const auto PWINDOW = pWindow ? pWindow : g_pCompositor->m_pLastWindow.lock();
if (!validMapped(PWINDOW)) return; //??
auto const WSID = PWINDOW->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->resizeActiveWindow(vec, corner, PWINDOW);
}
void CWorkspaceLayout::moveActiveWindow(const Vector2D& vec, PHLWINDOW pWindow) {
const auto PWINDOW = pWindow ? pWindow : g_pCompositor->m_pLastWindow.lock();
if (!validMapped(PWINDOW)) return; //??
auto const WSID = PWINDOW->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->moveActiveWindow(vec, PWINDOW);
}
void CWorkspaceLayout::onEndDragWindow() {
const auto pWindow = g_pInputManager->currentlyDraggedWindow.lock();
if (!validMapped(pWindow)) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout) {
layout->m_vBeginDragXY = m_vBeginDragXY;
layout->m_vBeginDragPositionXY = m_vBeginDragPositionXY;
layout->m_vBeginDragSizeXY = m_vBeginDragSizeXY;
layout->m_vLastDragXY = m_vLastDragXY;
layout->m_vDraggingWindowOriginalFloatSize = m_vDraggingWindowOriginalFloatSize;
layout->m_eGrabbedCorner = m_eGrabbedCorner;
layout->onEndDragWindow();
auto WDATA = getDataFromWindow(pWindow);
if (WDATA) {
//The layout's onEndDragWindow called onWindowCreatedTiling internally. Update our map to reflect the new workspace for the window)
WDATA->workspaceID = WSID;
}
}
}
void CWorkspaceLayout::onMouseMove(const Vector2D& vec) {
const auto pWindow = g_pInputManager->currentlyDraggedWindow.lock();
if (!validMapped(pWindow)) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout) {
layout->m_vBeginDragXY = m_vBeginDragXY;
layout->m_vBeginDragPositionXY = m_vBeginDragPositionXY;
layout->m_vBeginDragSizeXY = m_vBeginDragSizeXY;
layout->m_vLastDragXY = m_vLastDragXY;
layout->m_vDraggingWindowOriginalFloatSize = m_vDraggingWindowOriginalFloatSize;
layout->m_eGrabbedCorner = m_eGrabbedCorner;
layout->onMouseMove(vec);
m_vLastDragXY = layout->m_vLastDragXY;
}
}
void CWorkspaceLayout::fullscreenRequestForWindow(PHLWINDOW pWindow, const eFullscreenMode CURRENT_EFFECTIVE_MODE, const eFullscreenMode EFFECTIVE_MODE) {
if (!pWindow) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->fullscreenRequestForWindow(pWindow, CURRENT_EFFECTIVE_MODE, EFFECTIVE_MODE);
}
std::any CWorkspaceLayout::layoutMessage(SLayoutMessageHeader header, std::string message) {
CVarList vars(message, 0, ' ');
if (vars.size() < 1 || vars[0].empty()) {
Debug::log(ERR, "layoutmsg called without params");
return 0;
}
auto command = vars[0];
const auto PWINDOW = header.pWindow;
auto const WSID = PWINDOW->workspaceID();
if (command == "setlayout" && (vars.size() == 2)) {
IHyprLayout *layout = getLayoutByName(vars[1]);
if (layout) {
setLayoutForWorkspace(layout, WSID);
}
return 0;
} else if (command == "cyclelayout") {
size_t layoutidx = 0;
IHyprLayout *wslayout = getLayoutForWorkspace(WSID);
for(size_t i = 0; i < m_vLayoutList.size(); i++) {
if (m_vLayoutList[i] == wslayout) {
layoutidx = i;
break;
}
}
if (vars.size() == 1 || vars[1].contains("next")) {
layoutidx++;
} else if (vars.size() == 2 && vars[1].contains("prev")) {
if (layoutidx == 0)
layoutidx = m_vLayoutList.size()-1;
else
layoutidx--;
}
if (layoutidx >= m_vLayoutList.size())
layoutidx = 0;
IHyprLayout *newLayout = m_vLayoutList[layoutidx];
if (newLayout)
setLayoutForWorkspace(newLayout, WSID);
} else if (command == "togglelayout") {
IHyprLayout *prevLayout = getPreviousLayoutForWorkspace(WSID);
IHyprLayout *currLayout = getLayoutForWorkspace(WSID);
IHyprLayout *newLayout = nullptr;
if (vars.size() == 2) {
newLayout = getLayoutByName(vars[1]);
if (newLayout) {
if (newLayout == currLayout)
setLayoutForWorkspace(prevLayout, WSID);
else
setLayoutForWorkspace(newLayout, WSID);
}
} else {
setLayoutForWorkspace(prevLayout, WSID);
}
} else {
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->layoutMessage(header, message);
}
return 0;
}
SWindowRenderLayoutHints CWorkspaceLayout::requestRenderHints(PHLWINDOW pWindow) {
SWindowRenderLayoutHints hints;
if (!pWindow) return hints; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->requestRenderHints(pWindow);
return hints;
}
Vector2D CWorkspaceLayout::predictSizeForNewWindowTiled() {
if (!g_pCompositor->m_pLastMonitor)
return {};
auto const WSID = g_pCompositor->m_pLastMonitor->activeWorkspace->m_iID;
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->predictSizeForNewWindowTiled();
return {};
}
void CWorkspaceLayout::switchWindows(PHLWINDOW pWindow, PHLWINDOW pWindow2) {
if (!pWindow || !pWindow2) return; //??
auto const WSID1 = pWindow->workspaceID();
auto const WSID2 = pWindow2->workspaceID();
IHyprLayout *layout1 = getLayoutForWorkspace(WSID1);
IHyprLayout *layout2 = getLayoutForWorkspace(WSID2);
if (layout1 == layout2)
return layout1->switchWindows(pWindow, pWindow2);
//Different layouts; hax
std::swap(pWindow2->m_pMonitor, pWindow->m_pMonitor);
std::swap(pWindow2->m_pWorkspace, pWindow->m_pWorkspace);
pWindow->setAnimationsToMove();
pWindow2->setAnimationsToMove();
layout1->replaceWindowDataWith(pWindow, pWindow2);
layout2->replaceWindowDataWith(pWindow2, pWindow);
recalculateMonitor(pWindow->monitorID());
recalculateMonitor(pWindow2->monitorID());
g_pHyprRenderer->damageWindow(pWindow);
g_pHyprRenderer->damageWindow(pWindow2);
}
void CWorkspaceLayout::moveWindowTo(PHLWINDOW pWindow, const std::string& direction, bool silent) {
if (!pWindow) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout) {
layout->moveWindowTo(pWindow, direction, silent);
auto const NEXTID = pWindow->workspaceID();
IHyprLayout *nextlayout = getLayoutForWorkspace(NEXTID);
// layout has changed -> readd
if (nextlayout && nextlayout != layout) {
layout->onWindowRemoved(pWindow);
nextlayout->onWindowCreated(pWindow);
}
}
}
void CWorkspaceLayout::alterSplitRatio(PHLWINDOW pWindow, float ratio, bool exact) {
if (!pWindow) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->alterSplitRatio(pWindow, ratio, exact);
}
std::string CWorkspaceLayout::getLayoutName() {
return "WSLayout";
}
PHLWINDOW CWorkspaceLayout::getNextWindowCandidate(PHLWINDOW pWindow) {
if (!pWindow) return nullptr; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->getNextWindowCandidate(pWindow);
return nullptr;
}
void CWorkspaceLayout::onWindowFocusChange(PHLWINDOW pWindow) {
if (!pWindow) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->onWindowFocusChange(pWindow);
}
void CWorkspaceLayout::replaceWindowDataWith(PHLWINDOW from, PHLWINDOW to) {
if (!from) return; //??
auto const WSID = from->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->replaceWindowDataWith(from, to);
}
bool CWorkspaceLayout::isWindowReachable(PHLWINDOW pWindow) {
if (!pWindow) return false; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->isWindowReachable(pWindow);
return false;
}
void CWorkspaceLayout::bringWindowToTop(PHLWINDOW pWindow) {
if (!pWindow) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->bringWindowToTop(pWindow);
}
void CWorkspaceLayout::requestFocusForWindow(PHLWINDOW pWindow) {
if (!pWindow) return; //??
auto const WSID = pWindow->workspaceID();
IHyprLayout *layout = getLayoutForWorkspace(WSID);
if (layout)
return layout->requestFocusForWindow(pWindow);
}
IHyprLayout *CWorkspaceLayout::findUserLayoutForWorkspace(PHLWORKSPACE pWorkspace) {
std::string retName = "";
const auto wsrule = g_pConfigManager->getWorkspaceRuleFor(pWorkspace);
const auto layoutopts = wsrule.layoutopts;
if (layoutopts.contains("wslayout-layout")) {
retName = layoutopts.at("wslayout-layout");
}
return getLayoutByName(retName);
}
IHyprLayout *CWorkspaceLayout::getPreviousLayoutForWorkspace(const int &ws) {
for (auto& w : m_vWorkspacesData) {
if (w.workspaceID == ws && w.layout)
{
return w.previousLayout;
}
}
return nullptr;
}
IHyprLayout *CWorkspaceLayout::getLayoutForWorkspace(const int &ws) {
for (auto& w : m_vWorkspacesData) {
if (w.workspaceID == ws && w.layout)
{
return w.layout;
}
}
return nullptr;
}
void CWorkspaceLayout::setLayoutForWorkspace(IHyprLayout *layout, PHLWORKSPACE pWorkspace, bool isDefault) {
setLayoutForWorkspace(layout, pWorkspace->m_iID, isDefault);
}
void CWorkspaceLayout::setLayoutForWorkspace(IHyprLayout *layout, const int& ws, bool isDefault) {
if (!layout)
return;
if (layout->getLayoutName() == getLayoutName())
return;
for (auto& w : m_vWorkspacesData) {
if (w.workspaceID == ws) {
if (w.layout) {
for (auto &win : g_pCompositor->m_vWindows) {
if ((win->workspaceID() != ws) || !win->m_bIsMapped || win->isHidden())
continue;
w.layout->onWindowRemoved(win);
}
}
if (w.layout)
w.previousLayout = w.layout;
w.layout = layout;
w.isDefault = isDefault;
for (auto &win : g_pCompositor->m_vWindows) {
if ((win->workspaceID() != ws) || !win->m_bIsMapped || win->isHidden())
continue;
onWindowCreated(win);
}
return;
}
}
//Not found, create a new entry
const auto WSENTRY = &m_vWorkspacesData.emplace_back();
WSENTRY->workspaceID = ws;
WSENTRY->layout = layout;
WSENTRY->isDefault = isDefault;
for (auto &win : g_pCompositor->m_vWindows) {
if ((win->workspaceID() != ws) || !win->m_bIsMapped || win->isHidden())
continue;
onWindowCreated(win);
}
}
void CWorkspaceLayout::setDefaultLayout(std::string name) {
IHyprLayout *layout = getLayoutByName(name);
if (layout)
{
m_pDefaultLayout = layout;
for (auto& w : m_vWorkspacesData) {
if (w.isDefault) {
setLayoutForWorkspace(m_pDefaultLayout, w.workspaceID, true);
}
}
}
}
IHyprLayout *CWorkspaceLayout::getLayoutByName(const std::string& name) {
for (auto &layoutp : g_pLayoutManager->m_vLayouts) {
if(layoutp.first == name) {
return layoutp.second;
}
}
return nullptr;
}
void CWorkspaceLayout::clearLayoutMaps() {
}
void CWorkspaceLayout::setupLayoutList() {
static auto* const LAYOUTS = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:wslayout:layouts")->getDataStaticPtr();
m_vLayoutList.clear();
CVarList layoutlist(*LAYOUTS, 0, 's');
for (size_t i = 0; i < layoutlist.size(); i++) {
auto layoutName = layoutlist[i];
IHyprLayout *layout = getLayoutByName(layoutName);
if (layout && layout->getLayoutName() != getLayoutName())
m_vLayoutList.push_back(layout);
}
if (!m_vLayoutList.size()) {
for (auto &layoutp : g_pLayoutManager->m_vLayouts) {
if (layoutp.second->getLayoutName() != getLayoutName())
m_vLayoutList.push_back(layoutp.second);
}
}
}