1
0
Fork 0

mtk: Add Rectangle.contains_point

By turning a macro that exists in the codebase to a proper
function so that gnome-shell could make use of it as well
instead of using a region for it contains_point api...

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3801>
This commit is contained in:
Bilal Elmoussaoui 2024-06-11 00:40:10 +02:00
parent 2f8a598582
commit 6c972546f1
7 changed files with 39 additions and 11 deletions

View file

@ -285,6 +285,25 @@ mtk_rectangle_contains_rect (const MtkRectangle *outer_rect,
inner_rect->y + inner_rect->height <= outer_rect->y + outer_rect->height;
}
/**
* mtk_rectangle_contains_point:
* @rect: A rectangle
* @x: X coordinate of the point
* @y: Y coordinate of the point
*
* Returns: Whether the rectangle contains the point
*/
gboolean
mtk_rectangle_contains_point (const MtkRectangle *rect,
int x,
int y)
{
return (x >= rect->x &&
x < (rect->x + rect->width) &&
y >= rect->y &&
y < (rect->y + rect->height));
}
/**
* mtk_rectangle_to_graphene_rect:
* @rect: A rectangle

View file

@ -127,6 +127,10 @@ gboolean mtk_rectangle_could_fit_rect (const MtkRectangle *outer_rect,
MTK_EXPORT
gboolean mtk_rectangle_contains_rect (const MtkRectangle *outer_rect,
const MtkRectangle *inner_rect);
MTK_EXPORT
gboolean mtk_rectangle_contains_point (const MtkRectangle *rect,
int x,
int y);
MTK_EXPORT
graphene_rect_t mtk_rectangle_to_graphene_rect (const MtkRectangle *rect);

View file

@ -3403,7 +3403,7 @@ meta_monitor_manager_get_logical_monitor_at (MetaMonitorManager *manager,
{
MetaLogicalMonitor *logical_monitor = l->data;
if (META_POINT_IN_RECT (x, y, logical_monitor->rect))
if (mtk_rectangle_contains_point (&logical_monitor->rect, x, y))
return logical_monitor;
}
@ -3441,7 +3441,7 @@ meta_monitor_manager_get_logical_monitor_from_rect (MetaMonitorManager *manager,
MtkRectangle intersection;
int intersection_area;
if (META_POINT_IN_RECT (center_x, center_y, logical_monitor->rect))
if (mtk_rectangle_contains_point (&logical_monitor->rect, center_x, center_y))
return logical_monitor;
if (!mtk_rectangle_intersect (&logical_monitor->rect,

View file

@ -100,7 +100,7 @@ meta_viewport_info_get_view_at (MetaViewportInfo *viewport_info,
{
ViewInfo *info = &g_array_index (viewport_info->views, ViewInfo, i);
if (META_POINT_IN_RECT (x, y, info->rect))
if (mtk_rectangle_contains_point (&info->rect, x, y))
return i;
}

View file

@ -47,11 +47,5 @@ meta_timeval_to_microseconds (const struct timeval *tv)
return ((int64_t) tv->tv_sec) * G_USEC_PER_SEC + tv->tv_usec;
}
#define META_POINT_IN_RECT(xcoord, ycoord, rect) \
((xcoord) >= (rect).x && \
(xcoord) < ((rect).x + (rect).width) && \
(ycoord) >= (rect).y && \
(ycoord) < ((rect).y + (rect).height))
#define META_CONTAINER_OF(ptr, type, member) \
(type *) ((uint8_t *) (ptr) - G_STRUCT_OFFSET (type, member))

View file

@ -1456,7 +1456,7 @@ window_contains_point (MetaWindow *window,
meta_window_get_frame_rect (window, &rect);
return META_POINT_IN_RECT (root_x, root_y, rect);
return mtk_rectangle_contains_point (&rect, root_x, root_y);
}
MetaWindow *

View file

@ -200,6 +200,16 @@ test_adjacent_to (void)
g_assert (!mtk_rectangle_is_adjacent_to (&base, &not_adjacent[i]));
}
static void
test_contains_point (void)
{
MtkRectangle base = { .x = 0, .y = 0, .width = 10, .height = 10 };
g_assert (mtk_rectangle_contains_point (&base, 5, 5));
g_assert (!mtk_rectangle_contains_point (&base, -5, 5));
g_assert (!mtk_rectangle_contains_point (&base, 15, 2));
}
int
main (int argc,
char **argv)
@ -215,6 +225,7 @@ main (int argc,
g_test_add_func ("/mtk/rectangle/overlap", test_overlap_funcs);
g_test_add_func ("/mtk/rectangle/basic-fitting", test_basic_fitting);
g_test_add_func ("/mtk/rectangle/adjacent-to", test_adjacent_to);
g_test_add_func ("/mtk/rectangle/contains-point", test_contains_point);
return g_test_run ();
}