]> git.mdlowis.com Git - proto/labwc.git/commitdiff
box: factor out box_center()
authorJohn Lindgren <john@jlindgren.net>
Wed, 26 Nov 2025 04:25:47 +0000 (23:25 -0500)
committerHiroaki Yamamoto <hrak1529@gmail.com>
Tue, 2 Dec 2025 05:57:41 +0000 (14:57 +0900)
include/common/box.h
src/common/box.c
src/view.c

index 45c0fc4d14d2e9142d17234749cf1c99a135a7a4..9f18b45e0883f7aa437b9a8f061dbfc066beb7a1 100644 (file)
@@ -10,6 +10,17 @@ bool box_intersects(struct wlr_box *box_a, struct wlr_box *box_b);
 void box_union(struct wlr_box *box_dest, struct wlr_box *box_a,
        struct wlr_box *box_b);
 
+/*
+ * Centers a content box (width & height) within a reference box,
+ * limiting it (if possible) to not extend outside a bounding box.
+ *
+ * The reference box and bounding box are often the same but could be
+ * different (e.g. when centering a view within its parent but limiting
+ * to usable output area).
+ */
+void box_center(int width, int height, const struct wlr_box *ref,
+       const struct wlr_box *bound, int *x, int *y);
+
 /*
  * Fits and centers a content box (width & height) within a bounding box.
  * The content box is downscaled if necessary (preserving aspect ratio) but
index 2520f733daeb7176a32e261af68c3e2732d16af3..5cc91bf37a94f2fac0e5d535ea556266d946c7b2 100644 (file)
@@ -35,6 +35,25 @@ box_union(struct wlr_box *box_dest, struct wlr_box *box_a, struct wlr_box *box_b
        box_dest->height = y2 - y1;
 }
 
+void
+box_center(int width, int height, const struct wlr_box *ref,
+               const struct wlr_box *bound, int *x, int *y)
+{
+       *x = ref->x + (ref->width - width) / 2;
+       *y = ref->y + (ref->height - height) / 2;
+
+       if (*x < bound->x) {
+               *x = bound->x;
+       } else if (*x + width > bound->x + bound->width) {
+               *x = bound->x + bound->width - width;
+       }
+       if (*y < bound->y) {
+               *y = bound->y;
+       } else if (*y + height > bound->y + bound->height) {
+               *y = bound->y + bound->height - height;
+       }
+}
+
 struct wlr_box
 box_fit_within(int width, int height, struct wlr_box *bound)
 {
index cc16536ffd868bd222d5dfef760ad4e7b2ed6759..57014b8d6844fede4169a98b3b5240950f1065d3 100644 (file)
@@ -816,23 +816,7 @@ view_compute_centered_position(struct view *view, const struct wlr_box *ref,
        int height = h + margin.top + margin.bottom;
 
        /* If reference box is NULL then center to usable area */
-       if (!ref) {
-               ref = &usable;
-       }
-       *x = ref->x + (ref->width - width) / 2;
-       *y = ref->y + (ref->height - height) / 2;
-
-       /* Fit the view within the usable area */
-       if (*x < usable.x) {
-               *x = usable.x;
-       } else if (*x + width > usable.x + usable.width) {
-               *x = usable.x + usable.width - width;
-       }
-       if (*y < usable.y) {
-               *y = usable.y;
-       } else if (*y + height > usable.y + usable.height) {
-               *y = usable.y + usable.height - height;
-       }
+       box_center(width, height, ref ? ref : &usable, &usable, x, y);
 
        *x += margin.left;
        *y += margin.top;