MCPcopy
hub / github.com/paperwm/PaperWM / findTargetWindow

Function findTargetWindow

gestures.js:450–510  ·  view source on GitHub ↗
(space, direction)

Source from the content-addressed store, hash-verified

448 * @returns
449 */
450export function findTargetWindow(space, direction) {
451 let selected = space.selectedWindow?.clone;
452 if (!selected) {
453 return;
454 }
455
456 if (selected.x + space.targetX >= 0 &&
457 selected.x + selected.width + space.targetX <= space.width) {
458 return selected.meta_window;
459 }
460 selected = selected && space.selectedWindow;
461 let workArea = space.workArea();
462 let min = workArea.x;
463
464 let windows = space.getWindows().filter(w => {
465 let clone = w.clone;
466 let x = clone.targetX + space.targetX;
467 return !(x + clone.width < min ||
468 x > min + workArea.width);
469 });
470 if (!direction) // scroll left
471 windows.reverse();
472 let visible = windows.filter(w => {
473 let clone = w.clone;
474 let x = clone.targetX + space.targetX;
475 return x >= 0 &&
476 x + clone.width <= min + workArea.width;
477 });
478 if (visible.length > 0) {
479 return visible[0];
480 }
481
482 if (windows.length === 0) {
483 let first = space.getWindow(0, 0);
484 let last = space.getWindow(space.length - 1, 0);
485 if (direction) {
486 return last;
487 } else {
488 return first;
489 }
490 }
491
492 if (windows.length === 1)
493 return windows[0];
494
495 let closest = windows[0].clone;
496 let next = windows[1].clone;
497 let r1, r2;
498 if (direction) { // ->
499 r1 = Math.abs(closest.targetX + closest.width + space.targetX) / closest.width;
500 r2 = Math.abs(next.targetX + space.targetX - space.width) / next.width;
501 } else {
502 r1 = Math.abs(closest.targetX + space.targetX - space.width) / closest.width;
503 r2 = Math.abs(next.targetX + next.width + space.targetX) / next.width;
504 }
505 // Choose the window the most visible width (as a ratio)
506 if (r1 > r2)
507 return closest.meta_window;

Callers 2

updateFunction · 0.85
doneFunction · 0.85

Calls 3

workAreaMethod · 0.80
getWindowsMethod · 0.80
getWindowMethod · 0.80

Tested by

no test coverage detected