]> git.mdlowis.com Git - proto/labwc.git/commitdiff
view: account for base size in resize indicator
authorJohn Lindgren <john@jlindgren.net>
Fri, 22 Sep 2023 05:22:19 +0000 (01:22 -0400)
committerJohn Lindgren <john@jlindgren.net>
Tue, 26 Sep 2023 05:24:02 +0000 (01:24 -0400)
For views with a non-pixel size increment (e.g. X11 terminals), it's
helpful to subtract the base size of the window (typically including
menu bar, scrollbars, etc.) before computing the number of size
increments (e.g. cells/characters). This way, the displayed size will
exactly match the terminal grid (e.g. 80x25 or whatever).

wlr_box isn't really the best fit for size hints, so let's define a
struct view_size_hints and a nice view_get_size_hints() function,
wrapping view->impl->get_size_hints().

This also seems like a great opportunity to make view_adjust_size()
window-system-agnostic and eliminate xwayland_apply_size_hints().

include/view.h
include/xwayland.h
src/ssd/resize_indicator.c
src/view.c
src/xwayland.c

index 411cbb0b8d9870ea6a6b2b50fb2c867f3bcda1a1..ce6a6ce9c8d626bebf1253be81724aa1d3c7f869 100644 (file)
@@ -37,6 +37,17 @@ enum view_edge {
 };
 
 struct view;
+
+/* Basic size hints (subset of XSizeHints from X11) */
+struct view_size_hints {
+       int min_width;
+       int min_height;
+       int width_inc;
+       int height_inc;
+       int base_width;
+       int base_height;
+};
+
 struct view_impl {
        void (*configure)(struct view *view, struct wlr_box geo);
        void (*close)(struct view *view);
@@ -57,7 +68,7 @@ struct view_impl {
        void (*move_to_back)(struct view *view);
        struct view *(*get_root)(struct view *self);
        void (*append_children)(struct view *self, struct wl_array *children);
-       void (*fill_size_hints)(struct view *self, struct wlr_box *box);
+       struct view_size_hints (*get_size_hints)(struct view *self);
 };
 
 struct view {
@@ -306,6 +317,7 @@ void view_update_title(struct view *view);
 void view_update_app_id(struct view *view);
 void view_reload_ssd(struct view *view);
 
+struct view_size_hints view_get_size_hints(struct view *view);
 void view_adjust_size(struct view *view, int *w, int *h);
 
 void view_evacuate_region(struct view *view);
index b586cd6b3ef529a7e7d95f5e3e648da4326c3f13..29c7da0c17e8c2a71c84e6bb956c8b70a9df3974 100644 (file)
@@ -48,8 +48,6 @@ void xwayland_view_create(struct server *server,
 
 struct wlr_xwayland_surface *xwayland_surface_from_view(struct view *view);
 
-bool xwayland_apply_size_hints(struct view *view, int *w, int *h);
-
 void xwayland_server_init(struct server *server,
        struct wlr_compositor *compositor);
 void xwayland_server_finish(struct server *server);
index 3a41323d9d35e7f25f300d9b8975afe823ed1a67..d972d470c3df33a47463f2747ad8caa59a1d1354 100644 (file)
@@ -49,18 +49,6 @@ resize_indicator_init(struct view *view)
        resize_indicator_reconfigure_view(indicator);
 }
 
-static struct wlr_box
-get_size_hints(struct view *view)
-{
-       assert(view);
-
-       struct wlr_box hints = { 0 };
-       if (view->impl->fill_size_hints) {
-               view->impl->fill_size_hints(view, &hints);
-       }
-       return hints;
-}
-
 static bool
 wants_indicator(struct view *view)
 {
@@ -70,8 +58,8 @@ wants_indicator(struct view *view)
                if (view->server->input_mode != LAB_INPUT_STATE_RESIZE) {
                        return false;
                }
-               struct wlr_box size_hints = get_size_hints(view);
-               if (size_hints.width && size_hints.height) {
+               struct view_size_hints hints = view_get_size_hints(view);
+               if (hints.width_inc && hints.height_inc) {
                        return true;
                }
        }
@@ -173,10 +161,12 @@ resize_indicator_update(struct view *view)
        switch (view->server->input_mode) {
        case LAB_INPUT_STATE_RESIZE:
                ; /* works around "a label can only be part of a statement" */
-               struct wlr_box size_hints = get_size_hints(view);
+               struct view_size_hints hints = view_get_size_hints(view);
                snprintf(text, sizeof(text), "%d x %d",
-                       view->current.width / MAX(1, size_hints.width),
-                       view->current.height / MAX(1, size_hints.height));
+                       MAX(0, view->current.width - hints.base_width)
+                               / MAX(1, hints.width_inc),
+                       MAX(0, view->current.height - hints.base_height)
+                               / MAX(1, hints.height_inc));
                break;
        case LAB_INPUT_STATE_MOVE:
                ; /* works around "a label can only be part of a statement" */
index 51798e949176c8576cdb40445e5567293a7fb9dd..d43aeafeb864b2cf651399ea4d40815baa48906d 100644 (file)
@@ -296,20 +296,68 @@ view_move_relative(struct view *view, int x, int y)
        view_move(view, view->pending.x + x, view->pending.y + y);
 }
 
+struct view_size_hints
+view_get_size_hints(struct view *view)
+{
+       if (view->impl->get_size_hints) {
+               return view->impl->get_size_hints(view);
+       }
+       return (struct view_size_hints){0};
+}
+
+static void
+substitute_nonzero(int *a, int *b)
+{
+       if (!(*a)) {
+               *a = *b;
+       } else if (!(*b)) {
+               *b = *a;
+       }
+}
+
+static int
+round_to_increment(int val, int base, int inc)
+{
+       if (base < 0 || inc <= 0) {
+               return val;
+       }
+       return base + (val - base + inc / 2) / inc * inc;
+}
+
 void
 view_adjust_size(struct view *view, int *w, int *h)
 {
        assert(view);
+       struct view_size_hints hints = view_get_size_hints(view);
 
-#if HAVE_XWAYLAND
-       if (xwayland_apply_size_hints(view, w, h)) {
-               /* We don't want to cap the size to keep the aspect ratio */
-               return;
-       }
-#endif
+       /*
+        * "If a base size is not provided, the minimum size is to be
+        * used in its place and vice versa." (ICCCM 4.1.2.3)
+        */
+       substitute_nonzero(&hints.min_width, &hints.base_width);
+       substitute_nonzero(&hints.min_height, &hints.base_height);
+
+       /*
+        * Snap width/height to requested size increments (if any).
+        * Typically, terminal emulators use these to make sure that the
+        * terminal is resized to a width/height evenly divisible by the
+        * cell (character) size.
+        */
+       *w = round_to_increment(*w, hints.base_width, hints.width_inc);
+       *h = round_to_increment(*h, hints.base_height, hints.height_inc);
 
-       *w = MAX(*w, LAB_MIN_VIEW_WIDTH);
-       *h = MAX(*h, LAB_MIN_VIEW_HEIGHT);
+       /*
+        * If a minimum width/height was not set, then use default.
+        * This is currently always the case for xdg-shell views.
+        */
+       if (hints.min_width < 1) {
+               hints.min_width = LAB_MIN_VIEW_WIDTH;
+       }
+       if (hints.min_height < 1) {
+               hints.min_height = LAB_MIN_VIEW_HEIGHT;
+       }
+       *w = MAX(*w, hints.min_width);
+       *h = MAX(*h, hints.min_height);
 }
 
 static void
index ae4053112e58f4e54ea2972d005fd818bb10c3f3..604edfad9646e93df3ddb6020c2a8c111eb2c7ea 100644 (file)
 #include "workspaces.h"
 #include "xwayland.h"
 
-static int
-round_to_increment(int val, int base, int inc)
+static struct view_size_hints
+xwayland_view_get_size_hints(struct view *view)
 {
-       if (base < 0 || inc <= 0) {
-               return val;
+       xcb_size_hints_t *hints = xwayland_surface_from_view(view)->size_hints;
+       if (!hints) {
+               return (struct view_size_hints){0};
        }
-       return base + (val - base + inc / 2) / inc * inc;
-}
-
-bool
-xwayland_apply_size_hints(struct view *view, int *w, int *h)
-{
-       assert(view);
-       if (view->type == LAB_XWAYLAND_VIEW) {
-               xcb_size_hints_t *hints =
-                       xwayland_surface_from_view(view)->size_hints;
-
-               /*
-                * Honor size increments from WM_SIZE_HINTS. Typically, X11
-                * terminal emulators will use WM_SIZE_HINTS to make sure that
-                * the terminal is resized to a width/height evenly divisible by
-                * the cell (character) size.
-                */
-               if (hints) {
-                       *w = round_to_increment(*w, hints->base_width,
-                               hints->width_inc);
-                       *h = round_to_increment(*h, hints->base_height,
-                               hints->height_inc);
-
-                       *w = MAX(*w, MAX(1, hints->min_width));
-                       *h = MAX(*h, MAX(1, hints->min_height));
-                       return true;
-               }
-       }
-       return false;
-}
-
-static void
-xwayland_view_fill_size_hints(struct view *view, struct wlr_box *box)
-{
-       if (view->type == LAB_XWAYLAND_VIEW) {
-               xcb_size_hints_t *hints = xwayland_surface_from_view(view)->size_hints;
-               if (hints) {
-                       box->width = hints->width_inc;
-                       box->height = hints->height_inc;
-                       return;
-               }
-       }
-       box->width = 0;
-       box->height = 0;
+       return (struct view_size_hints){
+               .min_width = hints->min_width,
+               .min_height = hints->min_height,
+               .width_inc = hints->width_inc,
+               .height_inc = hints->height_inc,
+               .base_width = hints->base_width,
+               .base_height = hints->base_height,
+       };
 }
 
 static struct wlr_xwayland_surface *
@@ -669,7 +634,7 @@ static const struct view_impl xwayland_view_impl = {
        .move_to_back = xwayland_view_move_to_back,
        .get_root = xwayland_view_get_root,
        .append_children = xwayland_view_append_children,
-       .fill_size_hints = xwayland_view_fill_size_hints,
+       .get_size_hints = xwayland_view_get_size_hints,
 };
 
 void