mouse: split out snap-to-corner logic

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Michael Gehring 2008-06-15 18:51:47 +02:00 committed by Julien Danjou
parent 4d2dcb4476
commit c8b0872828
1 changed files with 76 additions and 34 deletions

110
mouse.c
View File

@ -158,6 +158,75 @@ mouse_snapclient(client_t *c, area_t geometry, int snap)
return titlebar_geometry_remove(c->titlebar, geometry); return titlebar_geometry_remove(c->titlebar, geometry);
} }
/** Set coordinates to a corner of an area.
*
* \param a The area.
* \param[in,out] x The x coordinate.
* \param[in,out] y The y coordinate.
* \param corner The corner to snap to.
* \return The corner the coordinates have been set to. If corner != AutoCorner
* this is always equal to \em corner.
*
* \todo rename/move this is still awkward and might be useful somewhere else.
*/
static corner_t
mouse_snap_to_corner(area_t a, int *x, int *y, corner_t corner)
{
int top, bottom, left, right;
top = AREA_TOP(a);
bottom = AREA_BOTTOM(a);
left = AREA_LEFT(a);
right = AREA_RIGHT(a);
/* figure out the nearser corner */
if(corner == AutoCorner)
{
if(abs(top - *y) < abs(bottom - *y))
{
if(abs(left - *x) < abs(right - *x))
corner = TopLeftCorner;
else
corner = TopRightCorner;
}
else
{
if(abs(left - *x) < abs(right - *x))
corner = BottomLeftCorner;
else
corner = BottomRightCorner;
}
}
switch(corner)
{
case TopRightCorner:
*x = right;
*y = top;
break;
case TopLeftCorner:
*x = left;
*y = top;
break;
case BottomLeftCorner:
*x = left;
*y = bottom;
break;
case BottomRightCorner:
*x = right;
*y = bottom;
break;
default:
break;
}
return corner;
}
/** Redraw the infobox. /** Redraw the infobox.
* \param ctx Draw context. * \param ctx Draw context.
* \param sw The simple window. * \param sw The simple window.
@ -502,52 +571,25 @@ mouse_client_resize_floating(client_t *c, corner_t corner, bool infobox)
right = left + c->geometry.width; right = left + c->geometry.width;
/* figure out which corner to move */ /* figure out which corner to move */
if(corner == AutoCorner) corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
{
if(abs(top - mouse_y) < abs(bottom - mouse_y))
{
if(abs(left - mouse_x) < abs(right - mouse_x))
cursor = CurTopLeft;
else
corner = TopRightCorner;
}
else
{
if(abs(left - mouse_x) < abs(right - mouse_x))
corner = BottomLeftCorner;
else
corner = BottomRightCorner;
}
}
/* the opposite corner is fixed */
fixed_x = (mouse_x == left) ? right : left;
fixed_y = (mouse_y == top) ? bottom : top;
/* select cursor */
switch(corner) switch(corner)
{ {
default: default:
mouse_y = top; cursor = CurTopLeft;
fixed_y = bottom;
mouse_x = left;
fixed_x = right;
corner = TopLeftCorner;
break; break;
case TopRightCorner: case TopRightCorner:
mouse_y = top;
fixed_y = bottom;
mouse_x = right;
fixed_x = left;
cursor = CurTopRight; cursor = CurTopRight;
break; break;
case BottomLeftCorner: case BottomLeftCorner:
mouse_y = bottom;
fixed_y = top;
mouse_x = left;
fixed_x = right;
cursor = CurBotLeft; cursor = CurBotLeft;
break; break;
case BottomRightCorner: case BottomRightCorner:
mouse_y = bottom;
fixed_y = top;
mouse_x = right;
fixed_x = left;
cursor = CurBotRight; cursor = CurBotRight;
break; break;
} }