Finish forward-porting @acrisci's positioner work

This commit is contained in:
Drew DeVault 2018-04-14 21:34:50 -04:00 committed by Guido Günther
parent 2e3d901ac5
commit 0a0627f5d0
4 changed files with 360 additions and 3 deletions

View file

@ -1608,6 +1608,268 @@ struct wlr_surface *wlr_xdg_surface_surface_at(
return wlr_surface_surface_at(surface->surface, sx, sy, sub_x, sub_y);
}
void wlr_xdg_popup_get_anchor_point(struct wlr_xdg_popup *popup,
int *root_sx, int *root_sy) {
struct wlr_box rect = popup->positioner.anchor_rect;
enum xdg_positioner_anchor anchor = popup->positioner.anchor;
int sx = 0, sy = 0;
if (anchor == XDG_POSITIONER_ANCHOR_NONE) {
sx = (rect.x + rect.width) / 2;
sy = (rect.y + rect.height) / 2;
} else if (anchor == XDG_POSITIONER_ANCHOR_TOP) {
sx = (rect.x + rect.width) / 2;
sy = rect.y;
} else if (anchor == XDG_POSITIONER_ANCHOR_BOTTOM) {
sx = (rect.x + rect.width) / 2;
sy = rect.y + rect.height;
} else if (anchor == XDG_POSITIONER_ANCHOR_LEFT) {
sx = rect.x;
sy = (rect.y + rect.height) / 2;
} else if (anchor == XDG_POSITIONER_ANCHOR_RIGHT) {
sx = rect.x + rect.width;
sy = (rect.y + rect.height) / 2;
} else if (anchor == (XDG_POSITIONER_ANCHOR_TOP |
XDG_POSITIONER_ANCHOR_LEFT)) {
sx = rect.x;
sy = rect.y;
} else if (anchor == (XDG_POSITIONER_ANCHOR_TOP |
XDG_POSITIONER_ANCHOR_RIGHT)) {
sx = rect.x + rect.width;
sy = rect.y;
} else if (anchor == (XDG_POSITIONER_ANCHOR_BOTTOM |
XDG_POSITIONER_ANCHOR_LEFT)) {
sx = rect.x;
sy = rect.y + rect.height;
} else if (anchor == (XDG_POSITIONER_ANCHOR_BOTTOM |
XDG_POSITIONER_ANCHOR_RIGHT)) {
sx = rect.x + rect.width;
sy = rect.y + rect.height;
}
*root_sx = sx;
*root_sy = sy;
}
void wlr_xdg_popup_get_toplevel_coords(struct wlr_xdg_popup *popup,
int popup_sx, int popup_sy, int *toplevel_sx, int *toplevel_sy) {
assert(strcmp(popup->parent->role, wlr_desktop_xdg_toplevel_role) == 0
|| strcmp(popup->parent->role, wlr_desktop_xdg_popup_role) == 0);
struct wlr_xdg_surface *parent = popup->parent->role_data;
while (parent != NULL && parent->role == WLR_XDG_SURFACE_ROLE_POPUP) {
popup_sx += parent->popup->geometry.x;
popup_sy += parent->popup->geometry.y;
parent = parent->popup->parent->role_data;
}
assert(parent);
*toplevel_sx = popup_sx + parent->geometry.x;
*toplevel_sy = popup_sy + parent->geometry.y;
}
static void wlr_xdg_popup_box_constraints(struct wlr_xdg_popup *popup,
struct wlr_box *toplevel_sx_box, int *offset_x, int *offset_y) {
int popup_width = popup->geometry.width;
int popup_height = popup->geometry.height;
int anchor_sx = 0, anchor_sy = 0;
wlr_xdg_popup_get_anchor_point(popup, &anchor_sx, &anchor_sy);
int popup_sx = 0, popup_sy = 0;
wlr_xdg_popup_get_toplevel_coords(popup, popup->geometry.x,
popup->geometry.y, &popup_sx, &popup_sy);
*offset_x = 0, *offset_y = 0;
if (popup_sx < toplevel_sx_box->x) {
*offset_x = toplevel_sx_box->x - popup_sx;
} else if (popup_sx + popup_width >
toplevel_sx_box->x + toplevel_sx_box->width) {
*offset_x = toplevel_sx_box->x + toplevel_sx_box->width -
(popup_sx + popup_width);
}
if (popup_sy < toplevel_sx_box->y) {
*offset_y = toplevel_sx_box->y - popup_sy;
} else if (popup_sy + popup_height >
toplevel_sx_box->y + toplevel_sx_box->height) {
*offset_y = toplevel_sx_box->y + toplevel_sx_box->height -
(popup_sy + popup_height);
}
}
static bool wlr_xdg_popup_unconstrain_flip(struct wlr_xdg_popup *popup,
struct wlr_box *toplevel_sx_box) {
int offset_x = 0, offset_y = 0;
wlr_xdg_popup_box_constraints(popup, toplevel_sx_box,
&offset_x, &offset_y);
if (!offset_x && !offset_y) {
return true;
}
bool flip_x = offset_x &&
(popup->positioner.constraint_adjustment &
XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X);
bool flip_y = offset_y &&
(popup->positioner.constraint_adjustment &
XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y);
if (flip_x) {
wlr_positioner_invert_x(&popup->positioner);
}
if (flip_y) {
wlr_positioner_invert_y(&popup->positioner);
}
popup->geometry =
wlr_xdg_positioner_get_geometry(&popup->positioner);
wlr_xdg_popup_box_constraints(popup, toplevel_sx_box,
&offset_x, &offset_y);
if (!offset_x && !offset_y) {
// no longer constrained
return true;
}
// revert the positioner back if it didn't fix it and go to the next part
if (flip_x) {
wlr_positioner_invert_x(&popup->positioner);
}
if (flip_y) {
wlr_positioner_invert_y(&popup->positioner);
}
popup->geometry =
wlr_xdg_positioner_get_geometry(&popup->positioner);
return false;
}
static bool wlr_xdg_popup_unconstrain_slide(struct wlr_xdg_popup *popup,
struct wlr_box *toplevel_sx_box) {
int offset_x = 0, offset_y = 0;
wlr_xdg_popup_box_constraints(popup, toplevel_sx_box,
&offset_x, &offset_y);
if (!offset_x && !offset_y) {
return true;
}
bool slide_x = offset_x &&
(popup->positioner.constraint_adjustment &
XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X);
bool slide_y = offset_x &&
(popup->positioner.constraint_adjustment &
XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y);
if (slide_x) {
popup->geometry.x += offset_x;
}
if (slide_y) {
popup->geometry.y += offset_y;
}
int toplevel_x = 0, toplevel_y = 0;
wlr_xdg_popup_get_toplevel_coords(popup, popup->geometry.x,
popup->geometry.y, &toplevel_x, &toplevel_y);
if (slide_x && toplevel_x < toplevel_sx_box->x) {
popup->geometry.x += toplevel_sx_box->x - toplevel_x;
}
if (slide_y && toplevel_y < toplevel_sx_box->y) {
popup->geometry.y += toplevel_sx_box->y - toplevel_y;
}
wlr_xdg_popup_box_constraints(popup, toplevel_sx_box,
&offset_x, &offset_y);
return !offset_x && !offset_y;
}
static bool wlr_xdg_popup_unconstrain_resize(struct wlr_xdg_popup *popup,
struct wlr_box *toplevel_sx_box) {
int offset_x, offset_y;
wlr_xdg_popup_box_constraints(popup, toplevel_sx_box,
&offset_x, &offset_y);
if (!offset_x && !offset_y) {
return true;
}
bool resize_x = offset_x &&
(popup->positioner.constraint_adjustment &
XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X);
bool resize_y = offset_x &&
(popup->positioner.constraint_adjustment &
XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y);
if (resize_x) {
popup->geometry.width -= offset_x;
}
if (resize_y) {
popup->geometry.height -= offset_y;
}
wlr_xdg_popup_box_constraints(popup, toplevel_sx_box,
&offset_y, &offset_y);
return !offset_x && !offset_y;
}
void wlr_xdg_popup_unconstrain_from_box(struct wlr_xdg_popup *popup,
struct wlr_box *toplevel_sx_box) {
if (wlr_xdg_popup_unconstrain_flip(popup, toplevel_sx_box)) {
return;
}
if (wlr_xdg_popup_unconstrain_slide(popup, toplevel_sx_box)) {
return;
}
if (wlr_xdg_popup_unconstrain_resize(popup, toplevel_sx_box)) {
return;
}
}
void wlr_positioner_invert_x(struct wlr_xdg_positioner *positioner) {
if (positioner->anchor & XDG_POSITIONER_ANCHOR_LEFT) {
positioner->anchor &= ~XDG_POSITIONER_ANCHOR_LEFT;
positioner->anchor |= XDG_POSITIONER_ANCHOR_RIGHT;
} else if (positioner->anchor & XDG_POSITIONER_ANCHOR_RIGHT) {
positioner->anchor &= ~XDG_POSITIONER_ANCHOR_RIGHT;
positioner->anchor |= XDG_POSITIONER_ANCHOR_LEFT;
}
if (positioner->gravity & XDG_POSITIONER_GRAVITY_RIGHT) {
positioner->gravity &= ~XDG_POSITIONER_GRAVITY_RIGHT;
positioner->gravity |= XDG_POSITIONER_GRAVITY_LEFT;
} else if (positioner->gravity & XDG_POSITIONER_GRAVITY_LEFT) {
positioner->gravity &= ~XDG_POSITIONER_GRAVITY_LEFT;
positioner->gravity |= XDG_POSITIONER_GRAVITY_RIGHT;
}
}
void wlr_positioner_invert_y(
struct wlr_xdg_positioner *positioner) {
if (positioner->anchor & XDG_POSITIONER_ANCHOR_TOP) {
positioner->anchor &= ~XDG_POSITIONER_ANCHOR_TOP;
positioner->anchor |= XDG_POSITIONER_ANCHOR_BOTTOM;
} else if (positioner->anchor & XDG_POSITIONER_ANCHOR_BOTTOM) {
positioner->anchor &= ~XDG_POSITIONER_ANCHOR_BOTTOM;
positioner->anchor |= XDG_POSITIONER_ANCHOR_TOP;
}
if (positioner->gravity & XDG_POSITIONER_GRAVITY_TOP) {
positioner->gravity &= ~XDG_POSITIONER_GRAVITY_TOP;
positioner->gravity |= XDG_POSITIONER_GRAVITY_BOTTOM;
} else if (positioner->gravity & XDG_POSITIONER_GRAVITY_BOTTOM) {
positioner->gravity &= ~XDG_POSITIONER_GRAVITY_BOTTOM;
positioner->gravity |= XDG_POSITIONER_GRAVITY_TOP;
}
}
struct xdg_surface_iterator_data {
wlr_surface_iterator_func_t user_iterator;