Improve code readability: refactor animation code and translate comments

Co-authored-by: squassina <8495707+squassina@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-02-18 08:54:36 +00:00
parent 09ea9d3b06
commit 73995e5828
5 changed files with 128 additions and 114 deletions

View file

@ -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 calculate_animation_curve_at(double t, int32_t type) {
struct dvec2 point; struct dvec2 point;
double *animation_curve; double *animation_curve = get_animation_curve_by_type(type);
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;
}
point.x = 3 * t * (1 - t) * (1 - t) * animation_curve[0] + point.x = 3 * t * (1 - t) * (1 - t) * animation_curve[0] +
3 * t * t * (1 - t) * animation_curve[2] + t * t * t; 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) { void init_baked_points(void) {
baked_points_move = calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_move)); /* Animation type to baked points mapping */
baked_points_open = calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_open)); struct {
baked_points_tag = calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_tag)); int32_t type;
baked_points_close = struct dvec2 **points;
calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_close)); } animation_types[] = {
baked_points_focus = {MOVE, &baked_points_move},
calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_focus)); {OPEN, &baked_points_open},
baked_points_opafadein = {TAG, &baked_points_tag},
calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_opafadein)); {CLOSE, &baked_points_close},
baked_points_opafadeout = {FOCUS, &baked_points_focus},
calloc(BAKED_POINTS_COUNT, sizeof(*baked_points_opafadeout)); {OPAFADEIN, &baked_points_opafadein},
{OPAFADEOUT, &baked_points_opafadeout},
};
for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { /* Allocate and calculate baked points for all animation types */
baked_points_move[i] = calculate_animation_curve_at( for (size_t j = 0; j < sizeof(animation_types) / sizeof(animation_types[0]);
(double)i / (BAKED_POINTS_COUNT - 1), MOVE); 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); /* Helper function to get baked points array by type */
} static struct dvec2 *get_baked_points_by_type(int32_t type) {
for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { switch (type) {
baked_points_tag[i] = calculate_animation_curve_at( case MOVE:
(double)i / (BAKED_POINTS_COUNT - 1), TAG); return baked_points_move;
} case OPEN:
for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { return baked_points_open;
baked_points_close[i] = calculate_animation_curve_at( case TAG:
(double)i / (BAKED_POINTS_COUNT - 1), CLOSE); return baked_points_tag;
} case CLOSE:
for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { return baked_points_close;
baked_points_focus[i] = calculate_animation_curve_at( case FOCUS:
(double)i / (BAKED_POINTS_COUNT - 1), FOCUS); return baked_points_focus;
} case OPAFADEIN:
for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { return baked_points_opafadein;
baked_points_opafadein[i] = calculate_animation_curve_at( case OPAFADEOUT:
(double)i / (BAKED_POINTS_COUNT - 1), OPAFADEIN); return baked_points_opafadeout;
} default:
for (int32_t i = 0; i < BAKED_POINTS_COUNT; i++) { return baked_points_move;
baked_points_opafadeout[i] = calculate_animation_curve_at(
(double)i / (BAKED_POINTS_COUNT - 1), OPAFADEOUT);
} }
} }
@ -76,24 +87,7 @@ double find_animation_curve_at(double t, int32_t type) {
int32_t up = BAKED_POINTS_COUNT - 1; int32_t up = BAKED_POINTS_COUNT - 1;
int32_t middle = (up + down) / 2; int32_t middle = (up + down) / 2;
struct dvec2 *baked_points; struct dvec2 *baked_points = get_baked_points_by_type(type);
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;
}
while (up - down != 1) { while (up - down != 1) {
if (baked_points[middle].x <= t) { if (baked_points[middle].x <= t) {

View file

@ -4,6 +4,8 @@
void die(const char *fmt, ...); void die(const char *fmt, ...);
void *ecalloc(size_t nmemb, size_t size); void *ecalloc(size_t nmemb, size_t size);
int32_t fd_set_nonblock(int32_t fd); 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); int32_t regex_match(const char *pattern_mb, const char *str_mb);
void wl_list_append(struct wl_list *list, struct wl_list *object); void wl_list_append(struct wl_list *list, struct wl_list *object);
uint32_t get_now_in_ms(void); uint32_t get_now_in_ms(void);

View file

@ -8,13 +8,13 @@
#define SYSCONFDIR "/etc" #define SYSCONFDIR "/etc"
#endif #endif
// 整数版本 - 截断小数部分 // Integer version - truncates decimal part
#define CLAMP_INT(x, min, max) \ #define CLAMP_INT(x, min, max) \
((int32_t)(x) < (int32_t)(min) \ ((int32_t)(x) < (int32_t)(min) \
? (int32_t)(min) \ ? (int32_t)(min) \
: ((int32_t)(x) > (int32_t)(max) ? (int32_t)(max) : (int32_t)(x))) : ((int32_t)(x) > (int32_t)(max) ? (int32_t)(max) : (int32_t)(x)))
// 浮点数版本 - 保留小数部分 // Floating point version - preserves decimal part
#define CLAMP_FLOAT(x, min, max) \ #define CLAMP_FLOAT(x, min, max) \
((x) < (min) ? (min) : ((x) > (max) ? (max) : (x))) ((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", "scroller_proportion_preset format: %s\n",
value); value);
free(value_copy); free(value_copy);
free(config->scroller_proportion_preset); // 释放已分配的内存 free(config->scroller_proportion_preset); // 释放已分配的内存
config->scroller_proportion_preset = NULL; // 防止野指针 config->scroller_proportion_preset = NULL; // 防止野指针
config->scroller_proportion_preset_count = 0; config->scroller_proportion_preset_count = 0;
return false; return false;
@ -3267,9 +3267,9 @@ void set_value_default() {
config.axis_bind_apply_timeout = config.axis_bind_apply_timeout =
axis_bind_apply_timeout; // 滚轮绑定动作的触发的时间间隔 axis_bind_apply_timeout; // 滚轮绑定动作的触发的时间间隔
config.focus_on_activate = config.focus_on_activate =
focus_on_activate; // 收到窗口激活请求是否自动跳转聚焦 focus_on_activate; // 收到窗口激活请求是否自动跳转聚焦
config.new_is_master = new_is_master; // 新窗口是否插在头部 config.new_is_master = new_is_master; // 新窗口是否插在头部
config.default_mfact = default_mfact; // master 窗口比例 config.default_mfact = default_mfact; // master 窗口比例
config.default_nmaster = default_nmaster; // 默认master数量 config.default_nmaster = default_nmaster; // 默认master数量
config.center_master_overspread = config.center_master_overspread =
center_master_overspread; // 中心master时是否铺满 center_master_overspread; // 中心master时是否铺满

View file

@ -4,21 +4,27 @@
/* speedie's mango config */ /* speedie's mango config */
#define COLOR(hex) \ #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 */ /* animation */
char *animation_type_open = "slide"; // 是否启用动画 //slide,zoom char *animation_type_open =
char *animation_type_close = "slide"; // 是否启用动画 //slide,zoom "slide"; // Animation type for window open: slide or zoom
char *layer_animation_type_open = "slide"; // 是否启用layer动画 //slide,zoom char *animation_type_close =
char *layer_animation_type_close = "slide"; // 是否启用layer动画 //slide,zoom "slide"; // Animation type for window close: slide or zoom
int32_t animations = 1; // 是否启用动画 char *layer_animation_type_open =
int32_t layer_animations = 0; // 是否启用layer动画 "slide"; // Animation type for layer open: slide or zoom
int32_t tag_animation_direction = HORIZONTAL; // 标签动画方向 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_in = 1; // Enable animation fade in
int32_t animation_fade_out = 1; // Enable animation fade out int32_t animation_fade_out = 1; // Enable animation fade out
float zoom_initial_ratio = 0.3; // 动画起始窗口比例 float zoom_initial_ratio = 0.3; // Initial window size ratio for zoom animation
float zoom_end_ratio = 0.8; // 动画结束窗口比例 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 fadein_begin_opacity = 0.5; // Begin opac window ratio for animations
float fadeout_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 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_tag = 300; // Animation tag speed
uint32_t animation_duration_close = 300; // Animation close speed uint32_t animation_duration_close = 300; // Animation close speed
uint32_t animation_duration_focus = 0; // Animation focus opacity 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_move[4] = {0.46, 1.0, 0.29,
double animation_curve_open[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 0.99}; // Animation curve for move
double animation_curve_tag[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 double animation_curve_open[4] = {0.46, 1.0, 0.29,
double animation_curve_close[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 0.99}; // Animation curve for open
double animation_curve_focus[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 double animation_curve_tag[4] = {0.46, 1.0, 0.29,
double animation_curve_opafadein[4] = {0.46, 1.0, 0.29, 0.99}; // 动画曲线 0.99}; // Animation curve for tag
double animation_curve_opafadeout[4] = {0.5, 0.5, 0.5, 0.5}; // 动画曲线 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 */ /* appearance */
uint32_t axis_bind_apply_timeout = 100; // 滚轮绑定动作的触发的时间间隔 uint32_t axis_bind_apply_timeout =
uint32_t focus_on_activate = 1; // 收到窗口激活请求是否自动跳转聚焦 100; // Timeout interval for mouse wheel binding actions
uint32_t new_is_master = 1; // 新窗口是否插在头部 uint32_t focus_on_activate =
double default_mfact = 0.55f; // master 窗口比例 1; // Auto-focus when receiving window activation request
uint32_t default_nmaster = 1; // 默认master数量 uint32_t new_is_master = 1; // Insert new windows at the head
int32_t center_master_overspread = 0; // 中心master时是否铺满 double default_mfact = 0.55f; // Master window proportion
int32_t center_when_single_stack = 1; // 单个stack时是否居中 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 */ /* logging */
int32_t log_level = WLR_ERROR; int32_t log_level = WLR_ERROR;
uint32_t numlockon = 0; // 是否打开右边小键盘 uint32_t numlockon = 0; // Enable numlock
uint32_t capslock = 0; // 是否启用快捷键 uint32_t capslock = 0; // Enable capslock
uint32_t ov_tab_mode = 0; // alt tab切换模式 uint32_t ov_tab_mode = 0; // Alt-tab switch mode
uint32_t hotarea_size = 10; // 热区大小,10x10 uint32_t hotarea_size = 10; // Hot corner size in pixels (10x10)
uint32_t hotarea_corner = BOTTOM_LEFT; 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 smartgaps = 0; /* 1 means no outer gap when there is only one window */
int32_t sloppyfocus = 1; /* focus follows mouse */ int32_t sloppyfocus = 1; /* focus follows mouse */
uint32_t gappih = 5; /* horiz inner gap between windows */ uint32_t gappih = 5; /* horiz inner gap between windows */
@ -95,8 +112,9 @@ float globalcolor[] = COLOR(0xb153a7ff);
float overlaycolor[] = COLOR(0x14a57cff); float overlaycolor[] = COLOR(0x14a57cff);
// char *cursor_theme = "Bibata-Modern-Ice"; // char *cursor_theme = "Bibata-Modern-Ice";
int32_t overviewgappi = 5; /* overview时 窗口与边缘 缝隙大小 */ int32_t overviewgappi =
int32_t overviewgappo = 30; /* overview时 窗口与窗口 缝隙大小 */ 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 /* To conform the xdg-protocol, set the alpha to zero to restore the old
* behavior */ * behavior */

View file

@ -259,7 +259,7 @@ typedef struct {
struct wlr_input_device *wlr_device; struct wlr_input_device *wlr_device;
struct libinput_device *libinput_device; struct libinput_device *libinput_device;
struct wl_listener destroy_listener; // 用于监听设备销毁事件 struct wl_listener destroy_listener; // 用于监听设备销毁事件
void *device_data; // 新增:指向设备特定数据(如 Switch void *device_data; // 新增:指向设备特定数据(如 Switch
} InputDevice; } InputDevice;
typedef struct { 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_begin(struct wl_listener *listener, void *data);
static void hold_end(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 checkidleinhibitor(struct wlr_surface *exclude);
static void cleanup(void); // 退出清理 static void cleanup(void); // 退出清理
static void cleanupmon(struct wl_listener *listener, void *data); // 退出清理 static void cleanupmon(struct wl_listener *listener, void *data); // 退出清理
static void closemon(Monitor *m); static void closemon(Monitor *m);
static void cleanuplisteners(void); static void cleanuplisteners(void);