opt: better direction algorithm for client find

This commit is contained in:
DreamMaoMao 2026-06-16 08:08:30 +08:00
parent ce1879d417
commit 84a7d3d1e9
2 changed files with 104 additions and 125 deletions

View file

@ -130,8 +130,13 @@ int32_t exchange_stack_client(const Arg *arg) {
} }
int32_t focusdir(const Arg *arg) { int32_t focusdir(const Arg *arg) {
if(!selmon)
return 0;
Client *c = NULL; Client *c = NULL;
c = direction_select(arg); c = direction_select(arg);
if(!selmon->isoverview)
c = get_focused_stack_client(c, arg->tc); c = get_focused_stack_client(c, arg->tc);
if (c) { if (c) {
focusclient(c, 1); focusclient(c, 1);

View file

@ -164,43 +164,13 @@ Client *center_tiled_select(Monitor *m) {
return target_c; return target_c;
} }
Client *find_client_by_direction(Client *tc, const Arg *arg, Client *find_client_by_direction(Client *tc, const Arg *arg, bool findfloating) {
bool findfloating) {
Client *c = NULL; Client *c = NULL;
Client **tempClients = NULL; Client *tempFocusClients = NULL;
int32_t last = -1; Client *tempSameMonitorFocusClients = NULL;
int64_t distance = LLONG_MAX;
int64_t same_monitor_distance = LLONG_MAX;
// 第一次遍历,计算客户端数量
wl_list_for_each(c, &clients, link) {
if (c && (findfloating || !c->isfloating) && !c->isunglobal &&
(config.focus_cross_monitor || c->mon == tc->mon) &&
(c->tags & c->mon->tagset[c->mon->seltags])) {
last++;
}
}
if (last < 0) {
return NULL;
}
// 动态分配内存
tempClients = malloc((last + 1) * sizeof(Client *));
if (!tempClients) {
return NULL;
}
// 第二次遍历,填充 tempClients
last = -1;
wl_list_for_each(c, &clients, link) {
if (c && (findfloating || !c->isfloating) && !c->isunglobal &&
(config.focus_cross_monitor || c->mon == tc->mon) &&
(c->tags & c->mon->tagset[c->mon->seltags])) {
last++;
tempClients[last] = c;
}
}
// 获取当前窗口的四个边界及中心点
int32_t tc_l = tc->geom.x; int32_t tc_l = tc->geom.x;
int32_t tc_r = tc->geom.x + tc->geom.width; int32_t tc_r = tc->geom.x + tc->geom.width;
int32_t tc_t = tc->geom.y; int32_t tc_t = tc->geom.y;
@ -208,26 +178,25 @@ Client *find_client_by_direction(Client *tc, const Arg *arg,
int32_t tc_cx = tc_l + tc->geom.width / 2; int32_t tc_cx = tc_l + tc->geom.width / 2;
int32_t tc_cy = tc_t + tc->geom.height / 2; int32_t tc_cy = tc_t + tc->geom.height / 2;
int64_t distance = LLONG_MAX;
int64_t same_monitor_distance = LLONG_MAX;
Client *tempFocusClients = NULL;
Client *tempSameMonitorFocusClients = NULL;
for (int32_t step = 0; step < 2; step++) { for (int32_t step = 0; step < 2; step++) {
if (step == 1 && tempFocusClients) if (step == 1 && tempFocusClients)
break; break;
for (int32_t _i = 0; _i <= last; _i++) { wl_list_for_each(c, &clients, link) {
c = tempClients[_i]; if (!c || c == tc)
if (c == tc) continue;
if (!findfloating && c->isfloating)
continue;
if (c->isunglobal)
continue;
if (!config.focus_cross_monitor && c->mon != tc->mon)
continue;
if (!(c->tags & c->mon->tagset[c->mon->seltags]))
continue; continue;
if (step == 0 && if (step == 0 && ((!tc->mon->isoverview && !client_is_in_same_stack(tc, c, NULL)) || c->mon != tc->mon))
(!client_is_in_same_stack(tc, c, NULL) || c->mon != tc->mon)) {
continue; continue;
}
// 获取目标窗口的四个边界及中心点
int32_t c_l = c->geom.x; int32_t c_l = c->geom.x;
int32_t c_r = c->geom.x + c->geom.width; int32_t c_r = c->geom.x + c->geom.width;
int32_t c_t = c->geom.y; int32_t c_t = c->geom.y;
@ -241,46 +210,56 @@ Client *find_client_by_direction(Client *tc, const Arg *arg,
switch (arg->i) { switch (arg->i) {
case LEFT: case LEFT:
match_dir = (c_cx < tc_cx && c_l < tc_l); if (c_cx < tc_cx || (c_cx == tc_cx && c_l < tc_l)) {
if (match_dir) { match_dir = true;
main_dist = tc_l - c_r; main_dist = tc_l - c_r;
orth_dist = (c_b < tc_t) orth_dist = (c_b < tc_t) ? (tc_t - c_b) : ((c_t > tc_b) ? (c_t - tc_b) : 0);
? (tc_t - c_b)
: ((c_t > tc_b) ? (c_t - tc_b) : 0);
} }
break; break;
case RIGHT: case RIGHT:
match_dir = (c_cx > tc_cx && c_r > tc_r); if (c_cx > tc_cx || (c_cx == tc_cx && c_l > tc_l)) {
if (match_dir) { match_dir = true;
main_dist = c_l - tc_r; main_dist = c_l - tc_r;
orth_dist = (c_b < tc_t) orth_dist = (c_b < tc_t) ? (tc_t - c_b) : ((c_t > tc_b) ? (c_t - tc_b) : 0);
? (tc_t - c_b)
: ((c_t > tc_b) ? (c_t - tc_b) : 0);
} }
break; break;
case UP: case UP:
match_dir = (c_cy < tc_cy && c_t < tc_t); if (c_cy < tc_cy || (c_cy == tc_cy && c_t < tc_t)) {
if (match_dir) { match_dir = true;
main_dist = tc_t - c_b; main_dist = tc_t - c_b;
orth_dist = (c_r < tc_l) orth_dist = (c_r < tc_l) ? (tc_l - c_r) : ((c_l > tc_r) ? (c_l - tc_r) : 0);
? (tc_l - c_r)
: ((c_l > tc_r) ? (c_l - tc_r) : 0);
} }
break; break;
case DOWN: case DOWN:
match_dir = (c_cy > tc_cy && c_b > tc_b); if (c_cy > tc_cy || (c_cy == tc_cy && c_t > tc_t)) {
if (match_dir) { match_dir = true;
main_dist = c_t - tc_b; main_dist = c_t - tc_b;
orth_dist = (c_r < tc_l) orth_dist = (c_r < tc_l) ? (tc_l - c_r) : ((c_l > tc_r) ? (c_l - tc_r) : 0);
? (tc_l - c_r)
: ((c_l > tc_r) ? (c_l - tc_r) : 0);
} }
break; break;
default:
continue;
} }
if (match_dir) { if (!match_dir)
int64_t tmp_distance = continue;
main_dist * main_dist + 2 * orth_dist * orth_dist;
int64_t penalty = 0;
if (main_dist < 0) {
penalty = 10000000000LL; // 主方向重叠(反方向)的极大惩罚
main_dist = -main_dist;
}
// 正交方向无重叠惩罚,优先选择在同一行/列的窗口
int64_t no_overlap_penalty = 0;
if (orth_dist > 0) {
// LEFT/RIGHT 时 orth_dist 是垂直间距,>0 表示垂直无重叠
// UP/DOWN 时 orth_dist 是水平间距,>0 表示水平无重叠
no_overlap_penalty = 10000000LL;
}
int64_t tmp_distance = penalty + no_overlap_penalty
+ (main_dist * main_dist) + (orth_dist * orth_dist);
if (tmp_distance < distance) { if (tmp_distance < distance) {
distance = tmp_distance; distance = tmp_distance;
@ -292,15 +271,10 @@ Client *find_client_by_direction(Client *tc, const Arg *arg,
} }
} }
} }
}
free(tempClients); if (tempSameMonitorFocusClients)
if (tempSameMonitorFocusClients) {
return tempSameMonitorFocusClients; return tempSameMonitorFocusClients;
} else {
return tempFocusClients; return tempFocusClients;
}
} }
Client *direction_select(const Arg *arg) { Client *direction_select(const Arg *arg) {