common: flesh out enum lab_edge and prefer over wlr_edges/wlr_direction

I like the new common/edge.h. I don't like how inconsistently we use it.

Current situation:

 - enum wlr_edges and wlr_direction are designed to be used as bitset,
   and are defined compatibly

 - enum lab_edge is *also* designed to be used as bitset, but
   incompatible with the others (LEFT/RIGHT come before UP/DOWN)

 - we use an inconsistent mix of all three *AND* uint32_t (usually with
   the WLR_EDGE constants rather than the LAB_EDGE constants), and
   convert between them on an ad-hoc basis, sometimes implicitly

Let's clean this up:

 - reorder enum lab_edge to be compatible with the two wlr enums
   (check this by static_assert)

 - use TOP/BOTTOM naming rather than UP/DOWN (matches wlr_edges)

 - add constants for the remaining possible combinations of the 4 edges

 - use lab_edge for all internal edge/direction fields, consistently

 - add lab_edge_is_cardinal() as a sanity check before casting to
   enum wlr_direction, and then eliminate all of direction.c/h

Instead of "enum wlr_edges direction", we now have
"enum lab_edge direction" which is not that much better. At least we
are now clear that we're overloading one enum with two meanings.
This commit is contained in:
John Lindgren 2025-08-26 20:27:34 -04:00
parent 4d1be7eada
commit ef766d16f0
26 changed files with 294 additions and 320 deletions

View file

@ -2,7 +2,6 @@
#include "overlay.h"
#include <assert.h>
#include <wlr/types/wlr_scene.h>
#include "common/direction.h"
#include "common/lab-scene-rect.h"
#include "config/rcxml.h"
#include "labwc.h"
@ -97,7 +96,7 @@ inactivate_overlay(struct overlay *overlay)
&overlay->edge_rect.tree->node, false);
}
overlay->active.region = NULL;
overlay->active.edge = LAB_EDGE_INVALID;
overlay->active.edge = LAB_EDGE_NONE;
overlay->active.output = NULL;
if (overlay->timer) {
wl_event_source_timer_update(overlay->timer, 0);
@ -120,7 +119,7 @@ show_region_overlay(struct seat *seat, struct region *region)
static struct wlr_box
get_edge_snap_box(enum lab_edge edge, struct output *output)
{
if (edge == LAB_EDGE_UP && rc.snap_top_maximize) {
if (edge == LAB_EDGE_TOP && rc.snap_top_maximize) {
return output_usable_area_in_layout_coords(output);
} else {
return view_get_edge_snap_box(NULL, output, edge);
@ -131,7 +130,7 @@ static int
handle_edge_overlay_timeout(void *data)
{
struct seat *seat = data;
assert(seat->overlay.active.edge != LAB_EDGE_INVALID
assert(seat->overlay.active.edge != LAB_EDGE_NONE
&& seat->overlay.active.output);
struct wlr_box box = get_edge_snap_box(seat->overlay.active.edge,
seat->overlay.active.output);
@ -143,12 +142,13 @@ static bool
edge_has_adjacent_output_from_cursor(struct seat *seat, struct output *output,
enum lab_edge edge)
{
enum wlr_direction dir;
if (!direction_from_edge(edge, &dir)) {
/* Allow only up/down/left/right */
if (!lab_edge_is_cardinal(edge)) {
return false;
}
/* Cast from enum lab_edge to enum wlr_direction is safe */
return wlr_output_layout_adjacent_output(
seat->server->output_layout, dir,
seat->server->output_layout, (enum wlr_direction)edge,
output->wlr_output, seat->cursor->x, seat->cursor->y);
}
@ -159,7 +159,7 @@ show_edge_overlay(struct seat *seat, enum lab_edge edge1, enum lab_edge edge2,
if (!rc.snap_overlay_enabled) {
return;
}
uint32_t edge = edge1 | edge2;
enum lab_edge edge = edge1 | edge2;
if (seat->overlay.active.edge == edge
&& seat->overlay.active.output == output) {
return;