From 6f9e75b6f26922132713162cfe858c9e7f19385d Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Sat, 6 Jan 2024 19:40:18 +0100 Subject: [PATCH] boxes: Move Rectangle.is_adjacent_to to Mtk Part-of: --- mtk/mtk/mtk-rectangle.c | 23 ++++++++++++++++ mtk/mtk/mtk-rectangle.h | 4 +++ src/backends/meta-monitor-config-manager.c | 4 +-- src/core/boxes-private.h | 4 --- src/core/boxes.c | 25 +---------------- src/tests/mtk/rectangle-tests.c | 31 +++++++++++++++++++++ src/tests/unit-tests.c | 32 ---------------------- src/wayland/meta-wayland-xdg-shell.c | 2 +- 8 files changed, 62 insertions(+), 63 deletions(-) diff --git a/mtk/mtk/mtk-rectangle.c b/mtk/mtk/mtk-rectangle.c index 76cdedca3..55626d8e2 100644 --- a/mtk/mtk/mtk-rectangle.c +++ b/mtk/mtk/mtk-rectangle.c @@ -381,3 +381,26 @@ mtk_rectangle_scale_double (const MtkRectangle *rect, graphene_rect_scale (&tmp, scale, scale, &tmp); mtk_rectangle_from_graphene_rect (&tmp, rounding_strategy, dest); } + +gboolean +mtk_rectangle_is_adjacent_to (const MtkRectangle *rect, + const MtkRectangle *other) +{ + int rect_x1 = rect->x; + int rect_y1 = rect->y; + int rect_x2 = rect->x + rect->width; + int rect_y2 = rect->y + rect->height; + int other_x1 = other->x; + int other_y1 = other->y; + int other_x2 = other->x + other->width; + int other_y2 = other->y + other->height; + + if ((rect_x1 == other_x2 || rect_x2 == other_x1) && + !(rect_y2 <= other_y1 || rect_y1 >= other_y2)) + return TRUE; + else if ((rect_y1 == other_y2 || rect_y2 == other_y1) && + !(rect_x2 <= other_x1 || rect_x1 >= other_x2)) + return TRUE; + else + return FALSE; +} diff --git a/mtk/mtk/mtk-rectangle.h b/mtk/mtk/mtk-rectangle.h index 91a8757bf..5354cd94b 100644 --- a/mtk/mtk/mtk-rectangle.h +++ b/mtk/mtk/mtk-rectangle.h @@ -136,3 +136,7 @@ void mtk_rectangle_scale_double (const MtkRectangle *rect, double scale, MtkRoundingStrategy rounding_strategy, MtkRectangle *dest); + +MTK_EXPORT +gboolean mtk_rectangle_is_adjacent_to (const MtkRectangle *rect, + const MtkRectangle *other); diff --git a/src/backends/meta-monitor-config-manager.c b/src/backends/meta-monitor-config-manager.c index d5c86be5d..8f6b25a9c 100644 --- a/src/backends/meta-monitor-config-manager.c +++ b/src/backends/meta-monitor-config-manager.c @@ -1898,8 +1898,8 @@ has_adjacent_neighbour (MetaMonitorsConfig *config, if (logical_monitor_config == other_logical_monitor_config) continue; - if (meta_rectangle_is_adjacent_to (&logical_monitor_config->layout, - &other_logical_monitor_config->layout)) + if (mtk_rectangle_is_adjacent_to (&logical_monitor_config->layout, + &other_logical_monitor_config->layout)) return TRUE; } diff --git a/src/core/boxes-private.h b/src/core/boxes-private.h index d39a2f876..8e9a4350e 100644 --- a/src/core/boxes-private.h +++ b/src/core/boxes-private.h @@ -237,10 +237,6 @@ GList* meta_rectangle_find_nonintersected_monitor_edges ( const GList *monitor_rects, const GSList *all_struts); -META_EXPORT_TEST -gboolean meta_rectangle_is_adjacent_to (MtkRectangle *rect, - MtkRectangle *other); - META_EXPORT_TEST void meta_rectangle_transform (const MtkRectangle *rect, MetaMonitorTransform transform, diff --git a/src/core/boxes.c b/src/core/boxes.c index 90f6ae66c..dd77e9482 100644 --- a/src/core/boxes.c +++ b/src/core/boxes.c @@ -703,7 +703,7 @@ meta_rectangle_is_adjacent_to_any_in_region (const GList *spanning_rects, if (rect == other || mtk_rectangle_equal (rect, other)) continue; - if (meta_rectangle_is_adjacent_to (rect, other)) + if (mtk_rectangle_is_adjacent_to (rect, other)) return TRUE; } @@ -1826,29 +1826,6 @@ meta_rectangle_find_nonintersected_monitor_edges ( return ret; } -gboolean -meta_rectangle_is_adjacent_to (MtkRectangle *rect, - MtkRectangle *other) -{ - int rect_x1 = rect->x; - int rect_y1 = rect->y; - int rect_x2 = rect->x + rect->width; - int rect_y2 = rect->y + rect->height; - int other_x1 = other->x; - int other_y1 = other->y; - int other_x2 = other->x + other->width; - int other_y2 = other->y + other->height; - - if ((rect_x1 == other_x2 || rect_x2 == other_x1) && - !(rect_y2 <= other_y1 || rect_y1 >= other_y2)) - return TRUE; - else if ((rect_y1 == other_y2 || rect_y2 == other_y1) && - !(rect_x2 <= other_x1 || rect_x1 >= other_x2)) - return TRUE; - else - return FALSE; -} - /** * meta_rectangle_transform: * @rect: the #MtkRectangle to be transformed diff --git a/src/tests/mtk/rectangle-tests.c b/src/tests/mtk/rectangle-tests.c index e430de890..66937793e 100644 --- a/src/tests/mtk/rectangle-tests.c +++ b/src/tests/mtk/rectangle-tests.c @@ -170,6 +170,36 @@ test_basic_fitting (void) g_assert (!mtk_rectangle_could_fit_rect (&temp3, &temp2)); } +static void +test_adjacent_to (void) +{ + MtkRectangle base = { .x = 10, .y = 10, .width = 10, .height = 10 }; + MtkRectangle adjacent[] = { + { .x = 20, .y = 10, .width = 10, .height = 10 }, + { .x = 0, .y = 10, .width = 10, .height = 10 }, + { .x = 0, .y = 1, .width = 10, .height = 10 }, + { .x = 20, .y = 19, .width = 10, .height = 10 }, + { .x = 10, .y = 20, .width = 10, .height = 10 }, + { .x = 10, .y = 0, .width = 10, .height = 10 }, + }; + MtkRectangle not_adjacent[] = { + { .x = 0, .y = 0, .width = 10, .height = 10 }, + { .x = 20, .y = 20, .width = 10, .height = 10 }, + { .x = 21, .y = 10, .width = 10, .height = 10 }, + { .x = 10, .y = 21, .width = 10, .height = 10 }, + { .x = 10, .y = 5, .width = 10, .height = 10 }, + { .x = 11, .y = 10, .width = 10, .height = 10 }, + { .x = 19, .y = 10, .width = 10, .height = 10 }, + }; + unsigned int i; + + for (i = 0; i < G_N_ELEMENTS (adjacent); i++) + g_assert (mtk_rectangle_is_adjacent_to (&base, &adjacent[i])); + + for (i = 0; i < G_N_ELEMENTS (not_adjacent); i++) + g_assert (!mtk_rectangle_is_adjacent_to (&base, ¬_adjacent[i])); +} + int main (int argc, char **argv) @@ -184,6 +214,7 @@ main (int argc, g_test_add_func ("/mtk/rectangle/equal", test_equal); 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); return g_test_run (); } diff --git a/src/tests/unit-tests.c b/src/tests/unit-tests.c index fae808114..055f6ad9c 100644 --- a/src/tests/unit-tests.c +++ b/src/tests/unit-tests.c @@ -200,36 +200,6 @@ meta_test_util_later_schedule_from_later (void) g_assert_cmpint (data.state, ==, META_TEST_LATER_FINISHED); } -static void -meta_test_adjacent_to (void) -{ - MtkRectangle base = { .x = 10, .y = 10, .width = 10, .height = 10 }; - MtkRectangle adjacent[] = { - { .x = 20, .y = 10, .width = 10, .height = 10 }, - { .x = 0, .y = 10, .width = 10, .height = 10 }, - { .x = 0, .y = 1, .width = 10, .height = 10 }, - { .x = 20, .y = 19, .width = 10, .height = 10 }, - { .x = 10, .y = 20, .width = 10, .height = 10 }, - { .x = 10, .y = 0, .width = 10, .height = 10 }, - }; - MtkRectangle not_adjacent[] = { - { .x = 0, .y = 0, .width = 10, .height = 10 }, - { .x = 20, .y = 20, .width = 10, .height = 10 }, - { .x = 21, .y = 10, .width = 10, .height = 10 }, - { .x = 10, .y = 21, .width = 10, .height = 10 }, - { .x = 10, .y = 5, .width = 10, .height = 10 }, - { .x = 11, .y = 10, .width = 10, .height = 10 }, - { .x = 19, .y = 10, .width = 10, .height = 10 }, - }; - unsigned int i; - - for (i = 0; i < G_N_ELEMENTS (adjacent); i++) - g_assert (meta_rectangle_is_adjacent_to (&base, &adjacent[i])); - - for (i = 0; i < G_N_ELEMENTS (not_adjacent); i++) - g_assert (!meta_rectangle_is_adjacent_to (&base, ¬_adjacent[i])); -} - static void init_tests (void) { @@ -237,8 +207,6 @@ init_tests (void) g_test_add_func ("/util/meta-later/schedule-from-later", meta_test_util_later_schedule_from_later); - g_test_add_func ("/core/boxes/adjacent-to", meta_test_adjacent_to); - init_monitor_store_tests (); init_monitor_config_migration_tests (); init_boxes_tests (); diff --git a/src/wayland/meta-wayland-xdg-shell.c b/src/wayland/meta-wayland-xdg-shell.c index cee1dc980..00dc9c752 100644 --- a/src/wayland/meta-wayland-xdg-shell.c +++ b/src/wayland/meta-wayland-xdg-shell.c @@ -1359,7 +1359,7 @@ meta_wayland_xdg_popup_post_apply_state (MetaWaylandSurfaceRole *surface_role, meta_window_get_buffer_rect (window, &buffer_rect); meta_window_get_buffer_rect (parent_window, &parent_buffer_rect); if (!mtk_rectangle_overlap (&buffer_rect, &parent_buffer_rect) && - !meta_rectangle_is_adjacent_to (&buffer_rect, &parent_buffer_rect)) + !mtk_rectangle_is_adjacent_to (&buffer_rect, &parent_buffer_rect)) { g_warning ("Buggy client caused popup to be placed outside of " "parent window");