Debian Codegraph @

hyprtester/src/tests/main/follow_mouse_shrink.cpp

raw
1 #include <chrono>
2 #include <thread>
3
4 #include "../../shared.hpp"
5 #include "../../hyprctlCompat.hpp"
6 #include "../shared.hpp"
7 #include "tests.hpp"
8
9 static bool isActiveWindow(const std::string& class_, char fullscreen = '0', bool log = true) {
10 std::string activeWin = getFromSocket("/activewindow");
11 auto winClass = Tests::getAttribute(activeWin, "class");
12 auto winFullscreen = Tests::getAttribute(activeWin, "fullscreen").back();
13 if (winClass == class_ && winFullscreen == fullscreen)
14 return true;
15 else {
16 if (log)
17 NLog::log("{}Wrong active window: expected class {} fullscreen '{}', found class {}, fullscreen '{}'", Colors::RED, class_, fullscreen, winClass, winFullscreen);
18 return false;
19 }
20 }
21
22 static bool waitForActiveWindow(const std::string& class_, char fullscreen = '0', bool logLastCheck = true, int maxTries = 50) {
23 int cnt = 0;
24 while (!isActiveWindow(class_, fullscreen, false)) {
25 ++cnt;
26 std::this_thread::sleep_for(std::chrono::milliseconds(100));
27 if (cnt > maxTries) {
28 return isActiveWindow(class_, fullscreen, logLastCheck);
29 }
30 }
31 return true;
32 }
33
34 // TODO: split this into multiple test cases
35 TEST_CASE(followMouseShrink) {
36 NLog::log("{}Testing follow_mouse_shrink", Colors::GREEN);
37
38 getFromSocket("/dispatch hl.dsp.focus({ workspace = 'name:follow_mouse_shrink' })");
39
40 // follow_mouse 2 so cursor position determines focus (mode 1's delta threshold
41 // is unreliable with movecursor/simulateMouseMovement). float_switch_override_focus 2
42 // enables focus switching between floating windows.
43 OK(getFromSocket("/eval hl.config({ input = { follow_mouse = 2 } })"));
44 OK(getFromSocket("/eval hl.config({ input = { float_switch_override_focus = 2 } })"));
45
46 // Spawn two floating windows with a 20px gap
47 // fms_a: position (100,100), size 400x400 -> hitbox [100,499] x [100,499]
48 if (!Tests::spawnKitty("fms_a"))
49 FAIL_TEST("Couldn't spawn kitty");
50 OK(getFromSocket("/dispatch hl.dsp.window.float({ action = 'set' })"));
51 OK(getFromSocket("/dispatch hl.dsp.window.resize({ x = 400, y = 400 })"));
52 OK(getFromSocket("/dispatch hl.dsp.window.move({ x = 100, y = 100 })"));
53
54 // fms_b: position (520,100), size 400x400 -> hitbox [520,919] x [100,499]
55 if (!Tests::spawnKitty("fms_b"))
56 FAIL_TEST("Couldn't spawn kitty");
57 OK(getFromSocket("/dispatch hl.dsp.window.float({ action = 'set' })"));
58 OK(getFromSocket("/dispatch hl.dsp.window.resize({ x = 400, y = 400 })"));
59 OK(getFromSocket("/dispatch hl.dsp.window.move({ x = 520, y = 100 })"));
60
61 // --- Test 1: Baseline shrink=0, edge focus works ---
62 NLog::log("{}Test 1: shrink=0, cursor at B's left edge focuses B", Colors::GREEN);
63 OK(getFromSocket("/eval hl.config({ input = { follow_mouse_shrink = 0 } })"));
64 // Focus A explicitly, then move cursor inside A so follow_mouse tracks it
65 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:fms_a' })"));
66 ASSERT(waitForActiveWindow("fms_a"), true);
67 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 300, y = 300 })"));
68 std::this_thread::sleep_for(std::chrono::milliseconds(200));
69 // Move to just inside B's left edge
70 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 521, y = 300 })"));
71 ASSERT(waitForActiveWindow("fms_b"), true);
72
73 // --- Test 2: Shrink=20, cursor in dead zone does NOT change focus ---
74 NLog::log("{}Test 2: shrink=20, cursor in B's dead zone stays on A", Colors::GREEN);
75 OK(getFromSocket("/eval hl.config({ input = { follow_mouse_shrink = 20 } })"));
76 // Focus A explicitly
77 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:fms_a' })"));
78 ASSERT(waitForActiveWindow("fms_a"), true);
79 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 300, y = 300 })"));
80 std::this_thread::sleep_for(std::chrono::milliseconds(200));
81 // Move to 530,300 -- 10px inside B, within 20px shrink zone (B's shrunk hitbox starts at 540)
82 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 530, y = 300 })"));
83 std::this_thread::sleep_for(std::chrono::milliseconds(500));
84 ASSERT(isActiveWindow("fms_a"), true);
85
86 // --- Test 3: Shrink=20, cursor well inside inactive window DOES focus it ---
87 NLog::log("{}Test 3: shrink=20, cursor at B's center focuses B", Colors::GREEN);
88 // Still focused on A from test 2
89 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 720, y = 300 })"));
90 ASSERT(waitForActiveWindow("fms_b"), true);
91
92 // --- Test 4: Focused window's hitbox is NOT shrunk ---
93 NLog::log("{}Test 4a: focused window hitbox is not shrunk", Colors::GREEN);
94 // Focus A explicitly, move cursor inside A, then move near A's right edge
95 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:fms_a' })"));
96 ASSERT(waitForActiveWindow("fms_a"), true);
97 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 300, y = 300 })"));
98 std::this_thread::sleep_for(std::chrono::milliseconds(200));
99 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 490, y = 300 })"));
100 std::this_thread::sleep_for(std::chrono::milliseconds(500));
101 ASSERT(isActiveWindow("fms_a"), true);
102
103 NLog::log("{}Test 4b: inactive window hitbox IS shrunk at same position", Colors::GREEN);
104 // Focus B explicitly, then move to (490,300). Now A is inactive with shrunk box ending at 479, so 490 is outside A.
105 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:fms_b' })"));
106 ASSERT(waitForActiveWindow("fms_b"), true);
107 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 720, y = 300 })"));
108 std::this_thread::sleep_for(std::chrono::milliseconds(200));
109 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 490, y = 300 })"));
110 std::this_thread::sleep_for(std::chrono::milliseconds(500));
111 ASSERT(isActiveWindow("fms_b"), true);
112 }