From 73995e58281198d2d892c14b45e59e7c6a0402c7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 18 Feb 2026 08:54:36 +0000 Subject: [PATCH] Improve code readability: refactor animation code and translate comments Co-authored-by: squassina <8495707+squassina@users.noreply.github.com> --- src/animation/common.h | 140 ++++++++++++++++++-------------------- src/common/util.h | 2 + src/config/parse_config.h | 12 ++-- src/config/preset.h | 84 ++++++++++++++--------- src/mango.c | 4 +- 5 files changed, 128 insertions(+), 114 deletions(-) diff --git a/src/animation/common.h b/src/animation/common.h index 9f022db2..ea12a3f9 100644 --- a/src/animation/common.h +++ b/src/animation/common.h @@ -1,23 +1,28 @@ +/* Helper function to get animation curve array by type */ +static double *get_animation_curve_by_type(int32_t type) { + switch (type) { + case MOVE: + return animation_curve_move; + case OPEN: + return animation_curve_open; + case TAG: + return animation_curve_tag; + case CLOSE: + return animation_curve_close; + case FOCUS: + return animation_curve_focus; + case OPAFADEIN: + return animation_curve_opafadein; + case OPAFADEOUT: + return animation_curve_opafadeout; + default: + return animation_curve_move; + } +} + struct dvec2 calculate_animation_curve_at(double t, int32_t type) { struct dvec2 point; - double *animation_curve; - if (type == MOVE) { - animation_curve = animation_curve_move; - } else if (type == OPEN) { - animation_curve = animation_curve_open; - } else if (type == TAG) { - animation_curve = animation_curve_tag; - } else if (type == CLOSE) { - animation_curve = animation_curve_close; - } else if (type == FOCUS) { - animation_curve = animation_curve_focus; - } else if (type == OPAFADEIN) { - animation_curve = animation_curve_opafadein; - } else if (type == OPAFADEOUT) { - animation_curve = animation_curve_opafadeout; - } else { - animation_curve = animation_curve_move; - } + double *animation_curve = get_animation_curve_by_type(type); point.x = 3 * t * (1 - t) * (1 - t) * animation_curve[0] + 3 * t * t * (1 - t) * animation_curve[2] + t * t * t; @@ -29,45 +34,51 @@ struct dvec2 calculate_animation_curve_at(double t, int32_t type) { } void init_baked_points(void) { - baked_points_move = calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_move)); - baked_points_open = calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_open)); - baked_points_tag = calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_tag)); - baked_points_close = - calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_close)); - baked_points_focus = - calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_focus)); - baked_points_opafadein = - calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_opafadein)); - baked_points_opafadeout = - calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_opafadeout)); + /* Animation type to baked points mapping */ + struct { + int32_t type; + struct dvec2 **points; + } animation_types[] = { + {MOVE, &baked_points_move}, + {OPEN, &baked_points_open}, + {TAG, &baked_points_tag}, + {CLOSE, &baked_points_close}, + {FOCUS, &baked_points_focus}, + {OPAFADEIN, &baked_points_opafadein}, + {OPAFADEOUT, &baked_points_opafadeout}, + }; - for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { - baked_points_move[i] = calculate_animation_curve_at( - (double)i / (BAKED_POINTS_COUNT - 1), MOVE); + /* Allocate and calculate baked points for all animation types */ + for (size_t j = 0; j < sizeof(animation_types) / sizeof(animation_types[0]); + j++) { + *animation_types[j].points = + calloc(BAKED_POINTS_COUNT, sizeof(struct dvec2)); + for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { + (*animation_types[j].points)[i] = calculate_animation_curve_at( + (double)i / (BAKED_POINTS_COUNT - 1), animation_types[j].type); + } } - for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { - baked_points_open[i] = calculate_animation_curve_at( - (double)i / (BAKED_POINTS_COUNT - 1), OPEN); - } - for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { - baked_points_tag[i] = calculate_animation_curve_at( - (double)i / (BAKED_POINTS_COUNT - 1), TAG); - } - for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { - baked_points_close[i] = calculate_animation_curve_at( - (double)i / (BAKED_POINTS_COUNT - 1), CLOSE); - } - for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { - baked_points_focus[i] = calculate_animation_curve_at( - (double)i / (BAKED_POINTS_COUNT - 1), FOCUS); - } - for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { - baked_points_opafadein[i] = calculate_animation_curve_at( - (double)i / (BAKED_POINTS_COUNT - 1), OPAFADEIN); - } - for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { - baked_points_opafadeout[i] = calculate_animation_curve_at( - (double)i / (BAKED_POINTS_COUNT - 1), OPAFADEOUT); +} + +/* Helper function to get baked points array by type */ +static struct dvec2 *get_baked_points_by_type(int32_t type) { + switch (type) { + case MOVE: + return baked_points_move; + case OPEN: + return baked_points_open; + case TAG: + return baked_points_tag; + case CLOSE: + return baked_points_close; + case FOCUS: + return baked_points_focus; + case OPAFADEIN: + return baked_points_opafadein; + case OPAFADEOUT: + return baked_points_opafadeout; + default: + return baked_points_move; } } @@ -76,24 +87,7 @@ double find_animation_curve_at(double t, int32_t type) { int32_t up = BAKED_POINTS_COUNT - 1; int32_t middle = (up + down) / 2; - struct dvec2 *baked_points; - if (type == MOVE) { - baked_points = baked_points_move; - } else if (type == OPEN) { - baked_points = baked_points_open; - } else if (type == TAG) { - baked_points = baked_points_tag; - } else if (type == CLOSE) { - baked_points = baked_points_close; - } else if (type == FOCUS) { - baked_points = baked_points_focus; - } else if (type == OPAFADEIN) { - baked_points = baked_points_opafadein; - } else if (type == OPAFADEOUT) { - baked_points = baked_points_opafadeout; - } else { - baked_points = baked_points_move; - } + struct dvec2 *baked_points = get_baked_points_by_type(type); while (up - down != 1) { if (baked_points[middle].x <= t) { diff --git a/src/common/util.h b/src/common/util.h index 8fb60338..b635c36a 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -4,6 +4,8 @@ void die(const char *fmt, ...); void *ecalloc(size_t nmemb, size_t size); int32_t fd_set_nonblock(int32_t fd); +/* Match string against regex pattern. Both pattern and string are multi-byte + * (mb) UTF-8 encoded. */ int32_t regex_match(const char *pattern_mb, const char *str_mb); void wl_list_append(struct wl_list *list, struct wl_list *object); uint32_t get_now_in_ms(void); diff --git a/src/config/parse_config.h b/src/config/parse_config.h index b7f89d59..6fff4027 100644 --- a/src/config/parse_config.h +++ b/src/config/parse_config.h @@ -8,13 +8,13 @@ #define SYSCONFDIR "/etc" #endif -// 整数版本 - 截断小数部分 +// Integer version - truncates decimal part #define CLAMP_INT(x, min, max) \ ((int32_t)(x) < (int32_t)(min) \ ? (int32_t)(min) \ : ((int32_t)(x) > (int32_t)(max) ? (int32_t)(max) : (int32_t)(x))) -// 浮点数版本 - 保留小数部分 +// Floating point version - preserves decimal part #define CLAMP_FLOAT(x, min, max) \ ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x))) @@ -1485,7 +1485,7 @@ bool parse_option(Config *config, char *key, char *value) { "scroller_proportion_preset format: %s\n", value); free(value_copy); - free(config->scroller_proportion_preset); // 释放已分配的内存 + free(config->scroller_proportion_preset); // 释放已分配的内存 config->scroller_proportion_preset = NULL; // 防止野指针 config->scroller_proportion_preset_count = 0; return false; @@ -3267,9 +3267,9 @@ void set_value_default() { config.axis_bind_apply_timeout = axis_bind_apply_timeout; // 滚轮绑定动作的触发的时间间隔 config.focus_on_activate = - focus_on_activate; // 收到窗口激活请求是否自动跳转聚焦 - config.new_is_master = new_is_master; // 新窗口是否插在头部 - config.default_mfact = default_mfact; // master 窗口比例 + focus_on_activate; // 收到窗口激活请求是否自动跳转聚焦 + config.new_is_master = new_is_master; // 新窗口是否插在头部 + config.default_mfact = default_mfact; // master 窗口比例 config.default_nmaster = default_nmaster; // 默认master数量 config.center_master_overspread = center_master_overspread; // 中心master时是否铺满 diff --git a/src/config/preset.h b/src/config/preset.h index ae4424f9..8522748f 100644 --- a/src/config/preset.h +++ b/src/config/preset.h @@ -4,21 +4,27 @@ /* speedie's mango config */ #define COLOR(hex) \ - {((hex >> 24) & 0xFF) / 255.0f, ((hex >> 16) & 0xFF) / 255.0f, \ - ((hex >> 8) & 0xFF) / 255.0f, (hex & 0xFF) / 255.0f} + { \ + ((hex >> 24) & 0xFF) / 255.0f, ((hex >> 16) & 0xFF) / 255.0f, \ + ((hex >> 8) & 0xFF) / 255.0f, (hex & 0xFF) / 255.0f \ + } -/* animaion */ -char *animation_type_open = "slide"; // 是否启用动画 //slide,zoom -char *animation_type_close = "slide"; // 是否启用动画 //slide,zoom -char *layer_animation_type_open = "slide"; // 是否启用layer动画 //slide,zoom -char *layer_animation_type_close = "slide"; // 是否启用layer动画 //slide,zoom -int32_t animations = 1; // 是否启用动画 -int32_t layer_animations = 0; // 是否启用layer动画 -int32_t tag_animation_direction = HORIZONTAL; // 标签动画方向 +/* animation */ +char *animation_type_open = + "slide"; // Animation type for window open: slide or zoom +char *animation_type_close = + "slide"; // Animation type for window close: slide or zoom +char *layer_animation_type_open = + "slide"; // Animation type for layer open: slide or zoom +char *layer_animation_type_close = + "slide"; // Animation type for layer close: slide or zoom +int32_t animations = 1; // Enable window animations +int32_t layer_animations = 0; // Enable layer animations +int32_t tag_animation_direction = HORIZONTAL; // Tag animation direction int32_t animation_fade_in = 1; // Enable animation fade in int32_t animation_fade_out = 1; // Enable animation fade out -float zoom_initial_ratio = 0.3; // 动画起始窗口比例 -float zoom_end_ratio = 0.8; // 动画结束窗口比例 +float zoom_initial_ratio = 0.3; // Initial window size ratio for zoom animation +float zoom_end_ratio = 0.8; // End window size ratio for zoom animation float fadein_begin_opacity = 0.5; // Begin opac window ratio for animations float fadeout_begin_opacity = 0.5; // Begin opac window ratio for animations uint32_t animation_duration_move = 500; // Animation move speed @@ -26,31 +32,42 @@ uint32_t animation_duration_open = 400; // Animation open speed uint32_t animation_duration_tag = 300; // Animation tag speed uint32_t animation_duration_close = 300; // Animation close speed uint32_t animation_duration_focus = 0; // Animation focus opacity speed -double animation_curve_move[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 -double animation_curve_open[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 -double animation_curve_tag[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 -double animation_curve_close[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 -double animation_curve_focus[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 -double animation_curve_opafadein[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 -double animation_curve_opafadeout[4] = {0.5, 0.5, 0.5, 0.5}; // 动画曲线 +double animation_curve_move[4] = {0.46, 1.0, 0.29, + 0.99}; // Animation curve for move +double animation_curve_open[4] = {0.46, 1.0, 0.29, + 0.99}; // Animation curve for open +double animation_curve_tag[4] = {0.46, 1.0, 0.29, + 0.99}; // Animation curve for tag +double animation_curve_close[4] = {0.46, 1.0, 0.29, + 0.99}; // Animation curve for close +double animation_curve_focus[4] = {0.46, 1.0, 0.29, + 0.99}; // Animation curve for focus +double animation_curve_opafadein[4] = { + 0.46, 1.0, 0.29, 0.99}; // Animation curve for opacity fade in +double animation_curve_opafadeout[4] = { + 0.5, 0.5, 0.5, 0.5}; // Animation curve for opacity fade out /* appearance */ -uint32_t axis_bind_apply_timeout = 100; // 滚轮绑定动作的触发的时间间隔 -uint32_t focus_on_activate = 1; // 收到窗口激活请求是否自动跳转聚焦 -uint32_t new_is_master = 1; // 新窗口是否插在头部 -double default_mfact = 0.55f; // master 窗口比例 -uint32_t default_nmaster = 1; // 默认master数量 -int32_t center_master_overspread = 0; // 中心master时是否铺满 -int32_t center_when_single_stack = 1; // 单个stack时是否居中 +uint32_t axis_bind_apply_timeout = + 100; // Timeout interval for mouse wheel binding actions +uint32_t focus_on_activate = + 1; // Auto-focus when receiving window activation request +uint32_t new_is_master = 1; // Insert new windows at the head +double default_mfact = 0.55f; // Master window proportion +uint32_t default_nmaster = 1; // Default number of master windows +int32_t center_master_overspread = + 0; // Whether to fill screen when center master +int32_t center_when_single_stack = + 1; // Whether to center when single stack window /* logging */ int32_t log_level = WLR_ERROR; -uint32_t numlockon = 0; // 是否打开右边小键盘 -uint32_t capslock = 0; // 是否启用快捷键 +uint32_t numlockon = 0; // Enable numlock +uint32_t capslock = 0; // Enable capslock -uint32_t ov_tab_mode = 0; // alt tab切换模式 -uint32_t hotarea_size = 10; // 热区大小,10x10 +uint32_t ov_tab_mode = 0; // Alt-tab switch mode +uint32_t hotarea_size = 10; // Hot corner size in pixels (10x10) uint32_t hotarea_corner = BOTTOM_LEFT; -uint32_t enable_hotarea = 1; // 是否启用鼠标热区 +uint32_t enable_hotarea = 1; // Enable mouse hot corner int32_t smartgaps = 0; /* 1 means no outer gap when there is only one window */ int32_t sloppyfocus = 1; /* focus follows mouse */ uint32_t gappih = 5; /* horiz inner gap between windows */ @@ -95,8 +112,9 @@ float globalcolor[] = COLOR(0xb153a7ff); float overlaycolor[] = COLOR(0x14a57cff); // char *cursor_theme = "Bibata-Modern-Ice"; -int32_t overviewgappi = 5; /* overview时 窗口与边缘 缝隙大小 */ -int32_t overviewgappo = 30; /* overview时 窗口与窗口 缝隙大小 */ +int32_t overviewgappi = + 5; /* Gap size between windows and edges in overview mode */ +int32_t overviewgappo = 30; /* Gap size between windows in overview mode */ /* To conform the xdg-protocol, set the alpha to zero to restore the old * behavior */ diff --git a/src/mango.c b/src/mango.c index 540395ae..e1f3c3a0 100644 --- a/src/mango.c +++ b/src/mango.c @@ -259,7 +259,7 @@ typedef struct { struct wlr_input_device *wlr_device; struct libinput_device *libinput_device; struct wl_listener destroy_listener; // 用于监听设备销毁事件 - void *device_data; // 新增:指向设备特定数据(如 Switch) + void *device_data; // 新增:指向设备特定数据(如 Switch) } InputDevice; typedef struct { @@ -570,7 +570,7 @@ static void pinch_end(struct wl_listener *listener, void *data); static void hold_begin(struct wl_listener *listener, void *data); static void hold_end(struct wl_listener *listener, void *data); static void checkidleinhibitor(struct wlr_surface *exclude); -static void cleanup(void); // 退出清理 +static void cleanup(void); // 退出清理 static void cleanupmon(struct wl_listener *listener, void *data); // 退出清理 static void closemon(Monitor *m); static void cleanuplisteners(void);