Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
koekeishiya committed Mar 29, 2024
1 parent b9e8e35 commit 8a8bd34
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/display_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ uint32_t display_manager_find_closest_display_in_direction(uint32_t source_did,
int best_distance = INT_MAX;

struct area source_area = area_from_cgrect(CGDisplayBounds(source_did));
CGPoint source_area_max = { source_area.x + source_area.w - 1, source_area.y + source_area.h - 1};
CGPoint source_area_max = area_max_point(source_area);

for (int i = 0; i < display_count; ++i) {
uint32_t did = display_list[i];
if (did == source_did) continue;

struct area target_area = area_from_cgrect(CGDisplayBounds(did));
CGPoint target_area_max = { target_area.x + target_area.w - 1, target_area.y + target_area.h - 1};
CGPoint target_area_max = area_max_point(target_area);

if (area_is_in_direction(&source_area, source_area_max, &target_area, target_area_max, direction)) {
int distance = area_distance_in_direction(&source_area, source_area_max, &target_area, target_area_max, direction);
Expand Down
10 changes: 7 additions & 3 deletions src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ static inline struct area area_from_cgrect(CGRect rect)
return (struct area) { rect.origin.x, rect.origin.y, rect.size.width, rect.size.height };
}

static inline CGPoint area_max_point(struct area area)
{
return (CGPoint) { area.x + area.w - 1, area.y + area.h - 1 };
}

static inline enum window_node_child window_node_get_child(struct window_node *node)
{
return node->child != CHILD_NONE ? node->child : g_space_manager.window_placement;
Expand Down Expand Up @@ -588,14 +593,13 @@ struct window_node *view_find_window_node_in_direction(struct view *view, struct
int best_distance = INT_MAX;
int best_rank = INT_MAX;
struct window_node *best_node = NULL;

CGPoint source_area_max = { source->area.x + source->area.w - 1, source->area.y + source->area.h - 1};
CGPoint source_area_max = area_max_point(source->area);

struct window_node *target = window_node_find_first_leaf(view->root);
while (target) {
if (source == target) goto next;

CGPoint target_area_max = { target->area.x + target->area.w - 1, target->area.y + target->area.h - 1 };
CGPoint target_area_max = area_max_point(target->area);
if (area_is_in_direction(&source->area, source_area_max, &target->area, target_area_max, direction)) {
int distance = area_distance_in_direction(&source->area, source_area_max, &target->area, target_area_max, direction);
int rank = window_manager_find_rank_of_window_in_list(target->window_order[0], window_list, window_count);
Expand Down
58 changes: 30 additions & 28 deletions tests/src/cardinal.c → tests/src/area.c
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
struct test_display
struct test_area
{
struct area area;
CGPoint area_max;
};

static inline void init_test_display_list(struct test_display display_list[3])
static inline void init_test_display_list(struct test_area display_list[3])
{
display_list[0].area.x = 0;
display_list[0].area.y = 0;
display_list[0].area.w = 2560;
display_list[0].area.h = 1440;
display_list[0].area_max.x = display_list[0].area.x + display_list[0].area.w - 1;
display_list[0].area_max.y = display_list[0].area.y + display_list[0].area.h - 1;

display_list[1].area.x = -1728;
display_list[1].area.y = 0;
display_list[1].area.w = 1728;
display_list[1].area.h = 1117;
display_list[1].area_max.x = display_list[1].area.x + display_list[1].area.w - 1;
display_list[1].area_max.y = display_list[1].area.y + display_list[1].area.h - 1;

display_list[2].area.x = 2560;
display_list[2].area.y = 0;
display_list[2].area.w = 1920;
display_list[2].area.h = 1080;
display_list[2].area_max.x = display_list[2].area.x + display_list[2].area.w - 1;
display_list[2].area_max.y = display_list[2].area.y + display_list[2].area.h - 1;
display_list[0].area.x = 0;
display_list[0].area.y = 0;
display_list[0].area.w = 2560;
display_list[0].area.h = 1440;
display_list[0].area_max = area_max_point(display_list[0].area);

display_list[1].area.x = -1728;
display_list[1].area.y = 0;
display_list[1].area.w = 1728;
display_list[1].area.h = 1117;
display_list[1].area_max = area_max_point(display_list[1].area);

display_list[2].area.x = 2560;
display_list[2].area.y = 0;
display_list[2].area.w = 1920;
display_list[2].area.h = 1080;
display_list[2].area_max = area_max_point(display_list[2].area);
}

TEST_FUNC(display_area_is_in_direction,
{
struct test_display display_list[3];
struct test_area display_list[3];
init_test_display_list(display_list);

bool t1 = area_is_in_direction(&display_list[0].area, display_list[0].area_max, &display_list[1].area, display_list[1].area_max, DIR_WEST);
TEST_CHECK(t1, true);

bool t2 = area_is_in_direction(&display_list[0].area, display_list[0].area_max, &display_list[2].area, display_list[2].area_max, DIR_WEST);
bool t2 = area_is_in_direction(&display_list[0].area, display_list[0].area_max, &display_list[1].area, display_list[1].area_max, DIR_EAST);
TEST_CHECK(t2, false);

bool t3 = area_is_in_direction(&display_list[0].area, display_list[0].area_max, &display_list[2].area, display_list[2].area_max, DIR_WEST);
TEST_CHECK(t3, false);

bool t4 = area_is_in_direction(&display_list[0].area, display_list[0].area_max, &display_list[2].area, display_list[2].area_max, DIR_EAST);
TEST_CHECK(t4, true);
});

static inline int test_display_in_direction(struct test_display *display_list, int display_count, int source, int direction)
static inline int test_display_in_direction(struct test_area *display_list, int display_count, int source, int direction)
{
int best_index = -1;
int best_distance = INT_MAX;

for (int i = 0; i < display_count; ++i) {
if (i == source) continue;

bool in_direction = area_is_in_direction(&display_list[source].area, display_list[source].area_max, &display_list[i].area, display_list[i].area_max, direction);
if (in_direction) {
if (area_is_in_direction(&display_list[source].area, display_list[source].area_max, &display_list[i].area, display_list[i].area_max, direction)) {
int distance = area_distance_in_direction(&display_list[source].area, display_list[source].area_max, &display_list[i].area, display_list[i].area_max, direction);
if (distance < best_distance) {
best_index = i;
Expand All @@ -64,7 +66,7 @@ static inline int test_display_in_direction(struct test_display *display_list, i
TEST_FUNC(display_area_distance_in_direction,
{
int best_index;
struct test_display display_list[3];
struct test_area display_list[3];
init_test_display_list(display_list);

best_index = test_display_in_direction(display_list, array_count(display_list), 0, DIR_WEST);
Expand Down
2 changes: 1 addition & 1 deletion tests/src/tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define TEST_FUNC(name, code) static TEST_SIG(name) { char *test_name = #name; bool result = true; {code} return result; }
#define TEST_CHECK(r, e) if ((r) != (e)) { printf(" \e[1;33m%s\e[m\e[1;31m#%d %s == %s\e[m \e[1;31m(%d == %d)\e[m\n", test_name, __LINE__, #r, #e, r, e); result = false; }

#include "cardinal.c"
#include "area.c"

#define TEST_ENTRY(name) { #name, test_##name },
#define TEST_LIST \
Expand Down

0 comments on commit 8a8bd34

Please sign in to comment.