From 96a591297d8369adee1b1223e1721e5553d009c6 Mon Sep 17 00:00:00 2001 From: Consolatis <35009135+Consolatis@users.noreply.github.com> Date: Wed, 6 Jul 2022 08:06:48 +0200 Subject: [PATCH] SnapToRegion: Add SnapToRegion action --- include/regions.h | 2 ++ src/action.c | 27 +++++++++++++++++++++++++++ src/regions.c | 14 ++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/include/regions.h b/include/regions.h index 86bd570e..7b9b2241 100644 --- a/include/regions.h +++ b/include/regions.h @@ -30,4 +30,6 @@ void regions_init(struct server *server, struct seat *seat); void regions_update(struct output *output); void regions_destroy(struct wl_list *regions); +struct region *regions_from_name(const char *region_name, struct output *output); + #endif /* __LABWC_REGIONS_H */ diff --git a/src/action.c b/src/action.c index c191bdbd..09992ef1 100644 --- a/src/action.c +++ b/src/action.c @@ -13,6 +13,7 @@ #include "debug.h" #include "labwc.h" #include "menu/menu.h" +#include "regions.h" #include "ssd.h" #include "view.h" #include "workspaces.h" @@ -58,6 +59,7 @@ enum action_type { ACTION_TYPE_RESIZE, ACTION_TYPE_GO_TO_DESKTOP, ACTION_TYPE_SEND_TO_DESKTOP, + ACTION_TYPE_SNAP_TO_REGION }; const char *action_names[] = { @@ -85,6 +87,7 @@ const char *action_names[] = { "Resize", "GoToDesktop", "SendToDesktop", + "SnapToRegion", NULL }; @@ -111,6 +114,9 @@ action_arg_from_xml_node(struct action *action, char *nodename, char *content) } else if (!strcmp(nodename, "to.action")) { /* GoToDesktop, SendToDesktop */ action_arg_add_str(action, NULL, content); + } else if (!strcmp(nodename, "region.action")) { + /* SnapToRegion */ + action_arg_add_str(action, NULL, content); } } @@ -418,6 +424,27 @@ actions_run(struct view *activator, struct server *server, } } break; + case ACTION_TYPE_SNAP_TO_REGION: + if (!arg) { + wlr_log(WLR_ERROR, "Missing argument for SnapToRegion"); + break; + } + if (!view) { + break; + } + struct output *output = view->output; + if (!output) { + break; + } + const char *region_name = action_str_from_arg(arg); + struct region *region = regions_from_name(region_name, output); + if (region) { + view_snap_to_region(view, region, + /*store_natural_geometry*/ true); + } else { + wlr_log(WLR_ERROR, "Invalid SnapToRegion id: '%s'", region_name); + } + break; case ACTION_TYPE_NONE: break; case ACTION_TYPE_INVALID: diff --git a/src/regions.c b/src/regions.c index 52d2c5d3..4b299730 100644 --- a/src/regions.c +++ b/src/regions.c @@ -25,6 +25,20 @@ regions_init(struct server *server, struct seat *seat) /* To be filled later */ } +struct region * +regions_from_name(const char *region_name, struct output *output) +{ + assert(region_name); + assert(output); + struct region *region; + wl_list_for_each(region, &output->regions, link) { + if (!strcmp(region->name, region_name)) { + return region; + } + } + return NULL; +} + void regions_update(struct output *output) {