mirror of
https://github.com/DreamMaoMao/maomaowm.git
synced 2026-06-16 14:33:36 -04:00
opt: better direction algorithm for client find
This commit is contained in:
parent
ce1879d417
commit
84a7d3d1e9
2 changed files with 104 additions and 125 deletions
|
|
@ -130,9 +130,14 @@ 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);
|
||||||
c = get_focused_stack_client(c, arg->tc);
|
if(!selmon->isoverview)
|
||||||
|
c = get_focused_stack_client(c, arg->tc);
|
||||||
if (c) {
|
if (c) {
|
||||||
focusclient(c, 1);
|
focusclient(c, 1);
|
||||||
if (config.warpcursor)
|
if (config.warpcursor)
|
||||||
|
|
|
||||||
|
|
@ -164,143 +164,117 @@ 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 *tempFocusClients = NULL;
|
||||||
Client **tempClients = NULL;
|
Client *tempSameMonitorFocusClients = NULL;
|
||||||
int32_t last = -1;
|
int64_t distance = LLONG_MAX;
|
||||||
|
int64_t same_monitor_distance = LLONG_MAX;
|
||||||
|
|
||||||
// 第一次遍历,计算客户端数量
|
int32_t tc_l = tc->geom.x;
|
||||||
wl_list_for_each(c, &clients, link) {
|
int32_t tc_r = tc->geom.x + tc->geom.width;
|
||||||
if (c && (findfloating || !c->isfloating) && !c->isunglobal &&
|
int32_t tc_t = tc->geom.y;
|
||||||
(config.focus_cross_monitor || c->mon == tc->mon) &&
|
int32_t tc_b = tc->geom.y + tc->geom.height;
|
||||||
(c->tags & c->mon->tagset[c->mon->seltags])) {
|
int32_t tc_cx = tc_l + tc->geom.width / 2;
|
||||||
last++;
|
int32_t tc_cy = tc_t + tc->geom.height / 2;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (last < 0) {
|
for (int32_t step = 0; step < 2; step++) {
|
||||||
return NULL;
|
if (step == 1 && tempFocusClients)
|
||||||
}
|
break;
|
||||||
|
|
||||||
// 动态分配内存
|
wl_list_for_each(c, &clients, link) {
|
||||||
tempClients = malloc((last + 1) * sizeof(Client *));
|
if (!c || c == tc)
|
||||||
if (!tempClients) {
|
continue;
|
||||||
return NULL;
|
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;
|
||||||
|
|
||||||
// 第二次遍历,填充 tempClients
|
if (step == 0 && ((!tc->mon->isoverview && !client_is_in_same_stack(tc, c, NULL)) || c->mon != tc->mon))
|
||||||
last = -1;
|
continue;
|
||||||
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 c_l = c->geom.x;
|
||||||
int32_t tc_l = tc->geom.x;
|
int32_t c_r = c->geom.x + c->geom.width;
|
||||||
int32_t tc_r = tc->geom.x + tc->geom.width;
|
int32_t c_t = c->geom.y;
|
||||||
int32_t tc_t = tc->geom.y;
|
int32_t c_b = c->geom.y + c->geom.height;
|
||||||
int32_t tc_b = tc->geom.y + tc->geom.height;
|
int32_t c_cx = c_l + c->geom.width / 2;
|
||||||
int32_t tc_cx = tc_l + tc->geom.width / 2;
|
int32_t c_cy = c_t + c->geom.height / 2;
|
||||||
int32_t tc_cy = tc_t + tc->geom.height / 2;
|
|
||||||
|
|
||||||
int64_t distance = LLONG_MAX;
|
int64_t main_dist = 0;
|
||||||
int64_t same_monitor_distance = LLONG_MAX;
|
int64_t orth_dist = 0;
|
||||||
Client *tempFocusClients = NULL;
|
bool match_dir = false;
|
||||||
Client *tempSameMonitorFocusClients = NULL;
|
|
||||||
|
|
||||||
for (int32_t step = 0; step < 2; step++) {
|
switch (arg->i) {
|
||||||
if (step == 1 && tempFocusClients)
|
case LEFT:
|
||||||
break;
|
if (c_cx < tc_cx || (c_cx == tc_cx && c_l < tc_l)) {
|
||||||
|
match_dir = true;
|
||||||
|
main_dist = tc_l - c_r;
|
||||||
|
orth_dist = (c_b < tc_t) ? (tc_t - c_b) : ((c_t > tc_b) ? (c_t - tc_b) : 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case RIGHT:
|
||||||
|
if (c_cx > tc_cx || (c_cx == tc_cx && c_l > tc_l)) {
|
||||||
|
match_dir = true;
|
||||||
|
main_dist = c_l - tc_r;
|
||||||
|
orth_dist = (c_b < tc_t) ? (tc_t - c_b) : ((c_t > tc_b) ? (c_t - tc_b) : 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case UP:
|
||||||
|
if (c_cy < tc_cy || (c_cy == tc_cy && c_t < tc_t)) {
|
||||||
|
match_dir = true;
|
||||||
|
main_dist = tc_t - c_b;
|
||||||
|
orth_dist = (c_r < tc_l) ? (tc_l - c_r) : ((c_l > tc_r) ? (c_l - tc_r) : 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DOWN:
|
||||||
|
if (c_cy > tc_cy || (c_cy == tc_cy && c_t > tc_t)) {
|
||||||
|
match_dir = true;
|
||||||
|
main_dist = c_t - tc_b;
|
||||||
|
orth_dist = (c_r < tc_l) ? (tc_l - c_r) : ((c_l > tc_r) ? (c_l - tc_r) : 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for (int32_t _i = 0; _i <= last; _i++) {
|
if (!match_dir)
|
||||||
c = tempClients[_i];
|
continue;
|
||||||
if (c == tc)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (step == 0 &&
|
int64_t penalty = 0;
|
||||||
(!client_is_in_same_stack(tc, c, NULL) || c->mon != tc->mon)) {
|
if (main_dist < 0) {
|
||||||
continue;
|
penalty = 10000000000LL; // 主方向重叠(反方向)的极大惩罚
|
||||||
}
|
main_dist = -main_dist;
|
||||||
|
}
|
||||||
|
|
||||||
// 获取目标窗口的四个边界及中心点
|
// 正交方向无重叠惩罚,优先选择在同一行/列的窗口
|
||||||
int32_t c_l = c->geom.x;
|
int64_t no_overlap_penalty = 0;
|
||||||
int32_t c_r = c->geom.x + c->geom.width;
|
if (orth_dist > 0) {
|
||||||
int32_t c_t = c->geom.y;
|
// LEFT/RIGHT 时 orth_dist 是垂直间距,>0 表示垂直无重叠
|
||||||
int32_t c_b = c->geom.y + c->geom.height;
|
// UP/DOWN 时 orth_dist 是水平间距,>0 表示水平无重叠
|
||||||
int32_t c_cx = c_l + c->geom.width / 2;
|
no_overlap_penalty = 10000000LL;
|
||||||
int32_t c_cy = c_t + c->geom.height / 2;
|
}
|
||||||
|
|
||||||
int64_t main_dist = 0;
|
int64_t tmp_distance = penalty + no_overlap_penalty
|
||||||
int64_t orth_dist = 0;
|
+ (main_dist * main_dist) + (orth_dist * orth_dist);
|
||||||
bool match_dir = false;
|
|
||||||
|
|
||||||
switch (arg->i) {
|
if (tmp_distance < distance) {
|
||||||
case LEFT:
|
distance = tmp_distance;
|
||||||
match_dir = (c_cx < tc_cx && c_l < tc_l);
|
tempFocusClients = c;
|
||||||
if (match_dir) {
|
}
|
||||||
main_dist = tc_l - c_r;
|
if (c->mon == tc->mon && tmp_distance < same_monitor_distance) {
|
||||||
orth_dist = (c_b < tc_t)
|
same_monitor_distance = tmp_distance;
|
||||||
? (tc_t - c_b)
|
tempSameMonitorFocusClients = c;
|
||||||
: ((c_t > tc_b) ? (c_t - tc_b) : 0);
|
}
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
case RIGHT:
|
|
||||||
match_dir = (c_cx > tc_cx && c_r > tc_r);
|
|
||||||
if (match_dir) {
|
|
||||||
main_dist = c_l - tc_r;
|
|
||||||
orth_dist = (c_b < tc_t)
|
|
||||||
? (tc_t - c_b)
|
|
||||||
: ((c_t > tc_b) ? (c_t - tc_b) : 0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case UP:
|
|
||||||
match_dir = (c_cy < tc_cy && c_t < tc_t);
|
|
||||||
if (match_dir) {
|
|
||||||
main_dist = tc_t - c_b;
|
|
||||||
orth_dist = (c_r < tc_l)
|
|
||||||
? (tc_l - c_r)
|
|
||||||
: ((c_l > tc_r) ? (c_l - tc_r) : 0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case DOWN:
|
|
||||||
match_dir = (c_cy > tc_cy && c_b > tc_b);
|
|
||||||
if (match_dir) {
|
|
||||||
main_dist = c_t - tc_b;
|
|
||||||
orth_dist = (c_r < tc_l)
|
|
||||||
? (tc_l - c_r)
|
|
||||||
: ((c_l > tc_r) ? (c_l - tc_r) : 0);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (match_dir) {
|
if (tempSameMonitorFocusClients)
|
||||||
int64_t tmp_distance =
|
return tempSameMonitorFocusClients;
|
||||||
main_dist * main_dist + 2 * orth_dist * orth_dist;
|
return tempFocusClients;
|
||||||
|
|
||||||
if (tmp_distance < distance) {
|
|
||||||
distance = tmp_distance;
|
|
||||||
tempFocusClients = c;
|
|
||||||
}
|
|
||||||
if (c->mon == tc->mon && tmp_distance < same_monitor_distance) {
|
|
||||||
same_monitor_distance = tmp_distance;
|
|
||||||
tempSameMonitorFocusClients = c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(tempClients);
|
|
||||||
|
|
||||||
if (tempSameMonitorFocusClients) {
|
|
||||||
return tempSameMonitorFocusClients;
|
|
||||||
} else {
|
|
||||||
return tempFocusClients;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Client *direction_select(const Arg *arg) {
|
Client *direction_select(const Arg *arg) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue