]> git.mdlowis.com Git - proto/labwc.git/commitdiff
src/action.c: add none branch to the ForEach action
authornicolas3121 <nicolasvrhulst@gmail.com>
Sat, 13 Apr 2024 19:24:04 +0000 (21:24 +0200)
committerConsolatis <35009135+Consolatis@users.noreply.github.com>
Sun, 14 Apr 2024 11:14:43 +0000 (13:14 +0200)
This allows implementing a run_or_raise behavior. For an example see
https://github.com/labwc/labwc/pull/1710#issuecomment-2054002641

Fixes: #1298
src/action.c
src/config/rcxml.c

index e268c3d352ecd7b228e6698554589827b889f81e..677e13da7e077eef6a8e1a7aaac13884f3cd2dfc 100644 (file)
@@ -515,7 +515,7 @@ action_is_valid(struct action *action)
        case ACTION_TYPE_IF:
        case ACTION_TYPE_FOR_EACH:
                ; /* works around "a label can only be part of a statement" */
-               static const char * const branches[] = { "then", "else" };
+               static const char * const branches[] = { "then", "else", "none" };
                for (size_t i = 0; i < ARRAY_SIZE(branches); i++) {
                        struct wl_list *children = action_get_actionlist(action, branches[i]);
                        if (children && !action_list_is_valid(children)) {
@@ -636,7 +636,7 @@ view_for_action(struct view *activator, struct server *server,
        }
 }
 
-static void
+static bool
 run_if_action(struct view *view, struct server *server, struct action *action)
 {
        struct view_query *query;
@@ -659,6 +659,7 @@ run_if_action(struct view *view, struct server *server, struct action *action)
        if (actions) {
                actions_run(view, server, actions, 0);
        }
+       return !strcmp(branch, "then");
 }
 
 void
@@ -984,12 +985,20 @@ actions_run(struct view *activator, struct server *server,
                        {
                                struct wl_array views;
                                struct view **item;
+                               bool matches = false;
                                wl_array_init(&views);
                                view_array_append(server, &views, LAB_VIEW_CRITERIA_NONE);
                                wl_array_for_each(item, &views) {
-                                       run_if_action(*item, server, action);
+                                       matches |= run_if_action(*item, server, action);
                                }
                                wl_array_release(&views);
+                               if (!matches) {
+                                       struct wl_list *actions;
+                                       actions = action_get_actionlist(action, "none");
+                                       if (actions) {
+                                               actions_run(view, server, actions, 0);
+                                       }
+                               }
                        }
                        break;
                case ACTION_TYPE_VIRTUAL_OUTPUT_ADD:
@@ -1052,4 +1061,3 @@ actions_run(struct view *activator, struct server *server,
                }
        }
 }
-
index 2b594a08975fc9ae62d9ebe8fce49f720f6a48d8..7fb0e80689dea3d52fd97bf78bc6656e0dd04224 100644 (file)
@@ -47,6 +47,7 @@ static bool in_window_rules;
 static bool in_action_query;
 static bool in_action_then_branch;
 static bool in_action_else_branch;
+static bool in_action_none_branch;
 
 static struct usable_area_override *current_usable_area_override;
 static struct keybind *current_keybind;
@@ -282,6 +283,7 @@ fill_child_action(char *nodename, char *content, struct action *parent,
        string_truncate_at_pattern(nodename, ".mousebind.context.mouse");
        string_truncate_at_pattern(nodename, ".then.action");
        string_truncate_at_pattern(nodename, ".else.action");
+       string_truncate_at_pattern(nodename, ".none.action");
 
        if (!strcasecmp(nodename, "action")) {
                current_child_action = NULL;
@@ -731,6 +733,9 @@ entry(xmlNode *node, char *nodename, char *content)
                } else if (in_action_else_branch) {
                        fill_child_action(nodename, content,
                                current_keybind_action, "else");
+               } else if (in_action_none_branch) {
+                       fill_child_action(nodename, content,
+                               current_keybind_action, "none");
                } else {
                        fill_keybind(nodename, content);
                }
@@ -745,6 +750,9 @@ entry(xmlNode *node, char *nodename, char *content)
                } else if (in_action_else_branch) {
                        fill_child_action(nodename, content,
                                current_mousebind_action, "else");
+               } else if (in_action_none_branch) {
+                       fill_child_action(nodename, content,
+                               current_mousebind_action, "none");
                } else {
                        fill_mousebind(nodename, content);
                }
@@ -1085,6 +1093,12 @@ xml_tree_walk(xmlNode *node)
                        in_action_else_branch = false;
                        continue;
                }
+               if (!strcasecmp((char *)n->name, "none")) {
+                       in_action_none_branch = true;
+                       traverse(n);
+                       in_action_none_branch = false;
+                       continue;
+               }
                traverse(n);
        }
 }