]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Add MoveToCursor action
authorArnaudv6 <2178699-Arnaudv6@users.noreply.gitlab.com>
Sat, 14 Oct 2023 12:57:44 +0000 (14:57 +0200)
committerJohan Malm <johanmalm@users.noreply.github.com>
Sun, 15 Oct 2023 19:36:37 +0000 (20:36 +0100)
docs/labwc-actions.5.scd
include/view.h
src/action.c
src/view.c

index b5fc6ef46f3d4d6f2856dd347be219093b3b307e..99b869836c198a9606c1cc5d10f2f7674cac79af 100644 (file)
@@ -54,6 +54,10 @@ Actions are used in menus and keyboard/mouse bindings.
 *<action name="MoveTo" x="" y="" />*
        Move to position (x, y)
 
+*<action name="MoveToCursor" />*
+       Move to be centered on cursor.
+       Tries to prevent any part of the window from going off-screen.
+
 *<action name="MoveRelative" x="" y="" />*
        Move window relative to its current position. Positive value of x moves
        it right, negative left. Positive value of y moves it down, negative up.
index 9379d1e64db6486d769a431b8ec932df9562e412..0fb10a229985a507dbff0fbf458540d6225b327b 100644 (file)
@@ -306,6 +306,7 @@ void view_resize_relative(struct view *view,
        int left, int right, int top, int bottom);
 void view_move_relative(struct view *view, int x, int y);
 void view_move(struct view *view, int x, int y);
+void view_move_to_cursor(struct view *view);
 void view_moved(struct view *view);
 void view_minimize(struct view *view, bool minimized);
 void view_store_natural_geometry(struct view *view);
index 43c3ddece80b77a90306a1fe292c2c3846412e28..fe0a6438fa7791e76a299b8a59af44be6dab9c33 100644 (file)
@@ -84,6 +84,7 @@ enum action_type {
        ACTION_TYPE_RESIZE,
        ACTION_TYPE_RESIZE_RELATIVE,
        ACTION_TYPE_MOVETO,
+       ACTION_TYPE_MOVETO_CURSOR,
        ACTION_TYPE_MOVE_RELATIVE,
        ACTION_TYPE_SEND_TO_DESKTOP,
        ACTION_TYPE_GO_TO_DESKTOP,
@@ -122,6 +123,7 @@ const char *action_names[] = {
        "Resize",
        "ResizeRelative",
        "MoveTo",
+       "MoveToCursor",
        "MoveRelative",
        "SendToDesktop",
        "GoToDesktop",
@@ -746,6 +748,11 @@ actions_run(struct view *activator, struct server *server,
                                view_move_relative(view, x, y);
                        }
                        break;
+               case ACTION_TYPE_MOVETO_CURSOR:
+                       if (view) {
+                               view_move_to_cursor(view);
+                       }
+                       break;
                case ACTION_TYPE_SEND_TO_DESKTOP:
                        if (!view) {
                                break;
index 94f2d82adfecb5ad8f6bb7bc37d37b3c51694dc4..7a390d1148c1ec6759c2220374961eaba7978cee 100644 (file)
@@ -351,6 +351,50 @@ view_move_relative(struct view *view, int x, int y)
        view_move(view, view->pending.x + x, view->pending.y + y);
 }
 
+void
+view_move_to_cursor(struct view *view)
+{
+       assert(view);
+
+       struct output *pending_output = output_nearest_to_cursor(view->server);
+       if (!output_is_usable(pending_output)) {
+               return;
+       }
+       if (view->fullscreen) {
+               view_set_fullscreen(view, false);
+       }
+       if (view->maximized) {
+               view_maximize(view, false, /*store_natural_geometry*/ false);
+       }
+       if (view_is_tiled(view)) {
+               view_set_untiled(view);
+               view_restore_to(view, view->natural_geometry);
+       }
+
+       struct border margin = ssd_thickness(view);
+       struct wlr_box geo = view->pending;
+       geo.width += margin.left + margin.right;
+       geo.height += margin.top + margin.bottom;
+
+       int x = view->server->seat.cursor->x - (geo.width / 2);
+       int y = view->server->seat.cursor->y - (geo.height / 2);
+
+       struct wlr_box usable = output_usable_area_in_layout_coords(pending_output);
+       if (x + geo.width > usable.x + usable.width) {
+               x = usable.x + usable.width - geo.width;
+       }
+       x = MAX(x, usable.x);
+
+       if (y + geo.height > usable.y + usable.height) {
+               y = usable.y + usable.height - geo.height;
+       }
+       y = MAX(y, usable.y);
+
+       x += margin.left;
+       y += margin.top;
+       view_move(view, x, y);
+}
+
 struct view_size_hints
 view_get_size_hints(struct view *view)
 {