view: implement cascade placement policy

Adds following settings:
<placement>
  <policy>cascade</policy>
  <cascadeOffset x="40" y="30" />
</placement>

"Cascade" policy places a new window at the center of the screen like
"center" policy, but possibly shifts its position to bottom-right so the
new window doesn't cover existing windows.

The algorithm is copied from KWin's implementation:
df9f8f8346/src/placement.cpp (L589)

Also added some helper functions to manipulate `wlr_box`.
This commit is contained in:
tokyo4j 2024-05-04 06:26:27 +09:00 committed by Johan Malm
parent 3be8fe25f3
commit 46ec513630
10 changed files with 199 additions and 17 deletions

49
src/common/box.c Normal file
View file

@ -0,0 +1,49 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <assert.h>
#include "common/box.h"
#include "common/macros.h"
bool
box_contains(struct wlr_box *box_super, struct wlr_box *box_sub)
{
if (wlr_box_empty(box_super) || wlr_box_empty(box_sub)) {
return false;
}
return box_super->x <= box_sub->x
&& box_super->x + box_super->width >= box_sub->x + box_sub->width
&& box_super->y <= box_sub->y
&& box_super->y + box_super->height >= box_sub->y + box_sub->height;
}
bool
box_intersects(struct wlr_box *box_a, struct wlr_box *box_b)
{
if (wlr_box_empty(box_a) || wlr_box_empty(box_b)) {
return false;
}
return box_a->x < box_b->x + box_b->width
&& box_b->x < box_a->x + box_a->width
&& box_a->y < box_b->y + box_b->height
&& box_b->y < box_a->y + box_a->height;
}
void
box_union(struct wlr_box *box_dest, struct wlr_box *box_a, struct wlr_box *box_b)
{
if (wlr_box_empty(box_a)) {
*box_dest = *box_b;
return;
}
if (wlr_box_empty(box_b)) {
*box_dest = *box_a;
return;
}
int x1 = MIN(box_a->x, box_b->x);
int y1 = MIN(box_a->y, box_b->y);
int x2 = MAX(box_a->x + box_a->width, box_b->x + box_b->width);
int y2 = MAX(box_a->y + box_a->height, box_b->y + box_b->height);
box_dest->x = x1;
box_dest->y = y1;
box_dest->width = x2 - x1;
box_dest->height = y2 - y1;
}

View file

@ -1,4 +1,5 @@
labwc_sources += files(
'box.c',
'buf.c',
'dir.c',
'fd-util.c',