From 90edcafd4e02146de6b94efeaa0d194b3daed24a Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Mon, 5 May 2025 11:09:31 +0800 Subject: [PATCH] feat: add toggle_named_scratch --- src/config/parse_config.h | 4 ++ src/dispatch/dispatch.h | 1 + src/maomao.c | 87 ++++++++++++++++++++++++++++++++------- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/src/config/parse_config.h b/src/config/parse_config.h index 307c264..953f543 100644 --- a/src/config/parse_config.h +++ b/src/config/parse_config.h @@ -553,6 +553,10 @@ FuncType parse_func_name(char *func_name, Arg *arg, char *arg_value, char *arg_v (*arg).ui2 = parse_num_type(arg_value2); (*arg).i = (*arg).ui == NUM_TYPE_DEFAULT ? atoi(arg_value) : atoi(arg_value+1); (*arg).i2 = (*arg).ui2 == NUM_TYPE_DEFAULT ? atoi(arg_value2) : atoi(arg_value2+1); + } else if (strcmp(func_name, "toggle_named_scratch") == 0) { + func = toggle_named_scratch; + (*arg).v = strdup(arg_value); + (*arg).v2 = strdup(arg_value2); } else { return NULL; } diff --git a/src/dispatch/dispatch.h b/src/dispatch/dispatch.h index 8125422..f378d48 100644 --- a/src/dispatch/dispatch.h +++ b/src/dispatch/dispatch.h @@ -53,3 +53,4 @@ void togglefakefullscreen(const Arg *arg); void toggleoverlay(const Arg *arg); void movewin(const Arg *arg); void resizewin(const Arg *arg); +void toggle_named_scratch(const Arg *arg); \ No newline at end of file diff --git a/src/maomao.c b/src/maomao.c index 4a0ed06..0faee45 100644 --- a/src/maomao.c +++ b/src/maomao.c @@ -1432,26 +1432,85 @@ void swallow(Client *c, Client *w) { } } -void toggle_scratchpad(const Arg *arg) { - Client *c; +bool switch_scratch_client_state(Client *c) { + if (c->is_in_scratchpad && c->is_scratchpad_show && + (selmon->tagset[selmon->seltags] & c->tags) == 0) { + unsigned int target = get_tags_first_tag(selmon->tagset[selmon->seltags]); + tag_client(&(Arg){.ui = target}, c); + return true; + } else if (c->is_in_scratchpad && c->is_scratchpad_show && + (selmon->tagset[selmon->seltags] & c->tags) != 0) { + set_minized(c); + return true; + } else if (c && c->is_in_scratchpad && !c->is_scratchpad_show) { + show_scratchpad(c); + return true; + } + + return false; +} + +void toggle_named_scratch(const Arg *arg) { + Client *c = NULL; + Client *target_client = NULL; + const char *appid, *title; + char *arg_id = arg->v; + char *arg_title = arg->v2; + wl_list_for_each(c, &clients, link) { if (c->mon != selmon) { continue; } - if (c->is_in_scratchpad && c->is_scratchpad_show && - (selmon->tagset[selmon->seltags] & c->tags) == 0) { - unsigned int target = get_tags_first_tag(selmon->tagset[selmon->seltags]); - tag_client(&(Arg){.ui = target}, c); - return; - } else if (c->is_in_scratchpad && c->is_scratchpad_show && - (selmon->tagset[selmon->seltags] & c->tags) != 0) { - set_minized(c); - return; - } else if (c && c->is_in_scratchpad && !c->is_scratchpad_show) { - show_scratchpad(c); - return; + + if (!(appid = client_get_appid(c))) + appid = broken; + if (!(title = client_get_title(c))) + title = broken; + + if (arg_id && strncmp(arg_id, "none", 4) == 0) + arg_id = NULL; + + if (arg_title && strncmp(arg_title, "none", 4) == 0) + arg_title = NULL; + + if ((arg_title && strstr(title, arg_title) && !arg_id) || + (arg_id && strstr(appid, arg_id) && !arg_title) || + (arg_id && strstr(appid, arg_id) && arg_title && + strstr(title, arg_title))) { + target_client = c; + break; } } + + if (!target_client) + return; + + wl_list_for_each(c, &clients, link) { + if (c->mon != selmon) { + continue; + } + if(c->is_in_scratchpad && c->is_scratchpad_show && c != target_client) { + set_minized(c); + } + } + + if(!target_client->is_in_scratchpad) + set_minized(target_client); + else + switch_scratch_client_state(target_client); +} + +void toggle_scratchpad(const Arg *arg) { + Client *c; + bool hit = false; + wl_list_for_each(c, &clients, link) { + if (c->mon != selmon) { + continue; + } + hit =switch_scratch_client_state(c); + if(hit) + break; + } } void gpureset(struct wl_listener *listener, void *data) {