refactor(plan): 添加往 plan 中新增 effect 相关辅助函数以消除模板代码
This commit is contained in:
119
src/core/plan.c
119
src/core/plan.c
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "base/array.h"
|
#include "base/array.h"
|
||||||
#include "base/memory.h"
|
#include "base/memory.h"
|
||||||
|
#include "core/types.h"
|
||||||
|
|
||||||
static void free_memory_hold_by_effects(effect_t *effects, size_t count) {
|
static void free_memory_hold_by_effects(effect_t *effects, size_t count) {
|
||||||
for (size_t i = 0; i < count; ++i) {
|
for (size_t i = 0; i < count; ++i) {
|
||||||
@@ -43,3 +44,121 @@ void plan_push_effect(plan_t *plan, const effect_t *effect) {
|
|||||||
effect_t *eft = array_push(plan->effects, plan->count, plan->capacity);
|
effect_t *eft = array_push(plan->effects, plan->count, plan->capacity);
|
||||||
*eft = *effect;
|
*eft = *effect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void plan_push_map_effect(plan_t *plan, window_id_t window_id) {
|
||||||
|
if (window_id_invalid(window_id)) return;
|
||||||
|
|
||||||
|
effect_t effect = {
|
||||||
|
.type = ZDWM_EFFECT_MAP_WINDOW,
|
||||||
|
.as.map.window = window_id,
|
||||||
|
};
|
||||||
|
plan_push_effect(plan, &effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plan_push_unmap_effect(plan_t *plan, window_id_t window_id) {
|
||||||
|
if (window_id_invalid(window_id)) return;
|
||||||
|
|
||||||
|
effect_t effect = {
|
||||||
|
.type = ZDWM_EFFECT_UNMAP_WINDOW,
|
||||||
|
.as.unmap.window = window_id,
|
||||||
|
};
|
||||||
|
plan_push_effect(plan, &effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plan_push_focus_effect(plan_t *plan, window_id_t window_id) {
|
||||||
|
if (window_id_invalid(window_id)) return;
|
||||||
|
|
||||||
|
effect_t effect = {
|
||||||
|
.type = ZDWM_EFFECT_FOCUS_WINDOW,
|
||||||
|
.as.focus.window = window_id,
|
||||||
|
};
|
||||||
|
plan_push_effect(plan, &effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plan_push_kill_effect(plan_t *plan, window_id_t window_id) {
|
||||||
|
if (window_id_invalid(window_id)) return;
|
||||||
|
|
||||||
|
effect_t effect = {
|
||||||
|
.type = ZDWM_EFFECT_KILL_WINDOW,
|
||||||
|
.as.kill.window = window_id,
|
||||||
|
};
|
||||||
|
plan_push_effect(plan, &effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plan_push_withdraw_effect(plan_t *plan, window_id_t window_id) {
|
||||||
|
if (window_id_invalid(window_id)) return;
|
||||||
|
|
||||||
|
effect_t effect = {
|
||||||
|
.type = ZDWM_EFFECT_WITHDRAW_WINDOW,
|
||||||
|
.as.withdraw.window = window_id,
|
||||||
|
};
|
||||||
|
plan_push_effect(plan, &effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plan_push_fullscreen_effect(
|
||||||
|
plan_t *plan,
|
||||||
|
window_id_t window_id,
|
||||||
|
bool value
|
||||||
|
) {
|
||||||
|
if (window_id_invalid(window_id)) return;
|
||||||
|
|
||||||
|
effect_t effect = {
|
||||||
|
.type = ZDWM_EFFECT_FULLSCREEN_WINDOW,
|
||||||
|
.as.fullscreen = {
|
||||||
|
.window = window_id,
|
||||||
|
.value = value,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
plan_push_effect(plan, &effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plan_push_maximize_effect(
|
||||||
|
plan_t *plan,
|
||||||
|
window_id_t window_id,
|
||||||
|
bool value
|
||||||
|
) {
|
||||||
|
if (window_id_invalid(window_id)) return;
|
||||||
|
|
||||||
|
effect_t effect = {
|
||||||
|
.type = ZDWM_EFFECT_MAXIMIZE_WINDOW,
|
||||||
|
.as.maximize = {
|
||||||
|
.window = window_id,
|
||||||
|
.value = value,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
plan_push_effect(plan, &effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plan_push_minimize_effect(
|
||||||
|
plan_t *plan,
|
||||||
|
window_id_t window_id,
|
||||||
|
bool value
|
||||||
|
) {
|
||||||
|
if (window_id_invalid(window_id)) return;
|
||||||
|
|
||||||
|
effect_t effect = {
|
||||||
|
.type = ZDWM_EFFECT_MINIMIZE_WINDOW,
|
||||||
|
.as.minimize = {
|
||||||
|
.window = window_id,
|
||||||
|
.value = value,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
plan_push_effect(plan, &effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plan_push_change_border_color_effect(
|
||||||
|
plan_t *plan,
|
||||||
|
window_id_t window_id,
|
||||||
|
const color_t *color
|
||||||
|
) {
|
||||||
|
if (window_id_invalid(window_id) || color == nullptr) return;
|
||||||
|
|
||||||
|
effect_t effect = {
|
||||||
|
.type = ZDWM_EFFECT_CHANGE_BORDER_COLOR,
|
||||||
|
.as.change_border_color = {
|
||||||
|
.window = window_id,
|
||||||
|
.color = color,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
plan_push_effect(plan, &effect);
|
||||||
|
}
|
||||||
|
|||||||
@@ -82,3 +82,21 @@ typedef struct plan_t {
|
|||||||
void plan_reset(plan_t *plan);
|
void plan_reset(plan_t *plan);
|
||||||
void plan_cleanup(plan_t *plan);
|
void plan_cleanup(plan_t *plan);
|
||||||
void plan_push_effect(plan_t *plan, const effect_t *effect);
|
void plan_push_effect(plan_t *plan, const effect_t *effect);
|
||||||
|
|
||||||
|
void plan_push_map_effect(plan_t *plan, window_id_t window_id);
|
||||||
|
void plan_push_unmap_effect(plan_t *plan, window_id_t window_id);
|
||||||
|
void plan_push_focus_effect(plan_t *plan, window_id_t window_id);
|
||||||
|
void plan_push_kill_effect(plan_t *plan, window_id_t window_id);
|
||||||
|
void plan_push_withdraw_effect(plan_t *plan, window_id_t window_id);
|
||||||
|
void plan_push_fullscreen_effect(
|
||||||
|
plan_t *plan,
|
||||||
|
window_id_t window_id,
|
||||||
|
bool value
|
||||||
|
);
|
||||||
|
void plan_push_maximize_effect(plan_t *plan, window_id_t window_id, bool value);
|
||||||
|
void plan_push_minimize_effect(plan_t *plan, window_id_t window_id, bool value);
|
||||||
|
void plan_push_change_border_color_effect(
|
||||||
|
plan_t *plan,
|
||||||
|
window_id_t window_id,
|
||||||
|
const color_t *color
|
||||||
|
);
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <zdwm/types.h>
|
#include <zdwm/types.h>
|
||||||
|
|
||||||
#include "base/color.h"
|
|
||||||
#include "base/macros.h"
|
#include "base/macros.h"
|
||||||
#include "base/window_list.h"
|
#include "base/window_list.h"
|
||||||
#include "core/binding.h"
|
#include "core/binding.h"
|
||||||
@@ -222,18 +221,6 @@ static void adjust_layout_windows_border_width(
|
|||||||
window_list_reset(&list);
|
window_list_reset(&list);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void window_change_border_color(
|
|
||||||
window_id_t window_id,
|
|
||||||
const color_t *color,
|
|
||||||
plan_t *plan
|
|
||||||
) {
|
|
||||||
effect_t change_border_color_effect = {
|
|
||||||
.type = ZDWM_EFFECT_CHANGE_BORDER_COLOR,
|
|
||||||
.as.change_border_color = {.window = window_id, .color = color}
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &change_border_color_effect);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void set_foucs_window(
|
static void set_foucs_window(
|
||||||
const policy_context_t *ctx,
|
const policy_context_t *ctx,
|
||||||
workspace_id_t workspace_id,
|
workspace_id_t workspace_id,
|
||||||
@@ -250,18 +237,15 @@ static void set_foucs_window(
|
|||||||
if (window_id == old_focused_window_id) return;
|
if (window_id == old_focused_window_id) return;
|
||||||
|
|
||||||
state_workspace_set_focused_window(state, workspace_id, window_id);
|
state_workspace_set_focused_window(state, workspace_id, window_id);
|
||||||
effect_t focus_effect = {
|
plan_push_focus_effect(plan, window_id);
|
||||||
.type = ZDWM_EFFECT_FOCUS_WINDOW,
|
|
||||||
.as.focus.window = window_id,
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &focus_effect);
|
|
||||||
|
|
||||||
if (state_window_get(state, window_id)) {
|
if (state_window_get(state, window_id)) {
|
||||||
window_change_border_color(window_id, &border->focused_color, plan);
|
auto color = &border->focused_color;
|
||||||
|
plan_push_change_border_color_effect(plan, window_id, color);
|
||||||
}
|
}
|
||||||
if (state_window_get(state, old_focused_window_id)) {
|
if (state_window_get(state, old_focused_window_id)) {
|
||||||
auto color = &border->normal_color;
|
auto color = &border->normal_color;
|
||||||
window_change_border_color(old_focused_window_id, color, plan);
|
plan_push_change_border_color_effect(plan, old_focused_window_id, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,16 +271,8 @@ static void manage_window(
|
|||||||
|
|
||||||
if (!state_workspace_show(state, command->workspace)) return;
|
if (!state_workspace_show(state, command->workspace)) return;
|
||||||
|
|
||||||
effect_t map_effect = {
|
plan_push_map_effect(plan, window_id);
|
||||||
.type = ZDWM_EFFECT_MAP_WINDOW,
|
plan_push_focus_effect(plan, window_id);
|
||||||
.as.map.window = window_id,
|
|
||||||
};
|
|
||||||
effect_t focus_effect = {
|
|
||||||
.type = ZDWM_EFFECT_FOCUS_WINDOW,
|
|
||||||
.as.focus.window = window_id,
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &map_effect);
|
|
||||||
plan_push_effect(plan, &focus_effect);
|
|
||||||
|
|
||||||
if (window_need_layout(window)) {
|
if (window_need_layout(window)) {
|
||||||
plan->need_relayout = true;
|
plan->need_relayout = true;
|
||||||
@@ -333,28 +309,16 @@ static void add_switch_workspace_effects(
|
|||||||
if (window->sticky) continue;
|
if (window->sticky) continue;
|
||||||
|
|
||||||
if (window->workspace_id == old_workspace) {
|
if (window->workspace_id == old_workspace) {
|
||||||
effect_t unmap_effect = {
|
plan_push_unmap_effect(plan, window->id);
|
||||||
.type = ZDWM_EFFECT_UNMAP_WINDOW,
|
|
||||||
.as.unmap.window = window->id,
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &unmap_effect);
|
|
||||||
} else if (window->workspace_id == new_workspace) {
|
} else if (window->workspace_id == new_workspace) {
|
||||||
effect_t map_effect = {
|
plan_push_map_effect(plan, window->id);
|
||||||
.type = ZDWM_EFFECT_MAP_WINDOW,
|
|
||||||
.as.map.window = window->id,
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &map_effect);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const workspace_t *workspace = state_workspace_get(state, new_workspace);
|
const workspace_t *workspace = state_workspace_get(state, new_workspace);
|
||||||
if (!workspace) return;
|
if (!workspace) return;
|
||||||
|
|
||||||
effect_t focus_effect = {
|
plan_push_focus_effect(plan, workspace->focused_window_id);
|
||||||
.type = ZDWM_EFFECT_FOCUS_WINDOW,
|
|
||||||
.as.focus.window = workspace->focused_window_id
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &focus_effect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -378,11 +342,7 @@ unmanage_window(const policy_context_t *ctx, window_id_t window, plan_t *plan) {
|
|||||||
if (output->current_workspace_id != workspace_id) return;
|
if (output->current_workspace_id != workspace_id) return;
|
||||||
|
|
||||||
if (old_focused_window != workspace->focused_window_id) {
|
if (old_focused_window != workspace->focused_window_id) {
|
||||||
effect_t focus_effect = {
|
plan_push_focus_effect(plan, workspace->focused_window_id);
|
||||||
.type = ZDWM_EFFECT_FOCUS_WINDOW,
|
|
||||||
.as.focus.window = workspace->focused_window_id
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &focus_effect);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,33 +359,21 @@ focus_window(const policy_context_t *ctx, window_id_t window, plan_t *plan) {
|
|||||||
set_foucs_window(ctx, workspace_id, window, plan);
|
set_foucs_window(ctx, workspace_id, window, plan);
|
||||||
if (old_focused_window == workspace->focused_window_id) return;
|
if (old_focused_window == workspace->focused_window_id) return;
|
||||||
|
|
||||||
effect_t focus_effect = {
|
plan_push_focus_effect(plan, workspace->focused_window_id);
|
||||||
.type = ZDWM_EFFECT_FOCUS_WINDOW,
|
|
||||||
.as.focus.window = workspace->focused_window_id,
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &focus_effect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void kill_window(state_t *state, window_id_t window, plan_t *plan) {
|
static void kill_window(state_t *state, window_id_t window, plan_t *plan) {
|
||||||
auto win = state_window_get(state, window);
|
auto win = state_window_get(state, window);
|
||||||
if (!win) return;
|
if (!win) return;
|
||||||
|
|
||||||
effect_t kill_effect = {
|
plan_push_kill_effect(plan, window);
|
||||||
.type = ZDWM_EFFECT_KILL_WINDOW,
|
|
||||||
.as.kill.window = window,
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &kill_effect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void withdraw_window(state_t *state, window_id_t window, plan_t *plan) {
|
static void withdraw_window(state_t *state, window_id_t window, plan_t *plan) {
|
||||||
auto win = state_window_get(state, window);
|
auto win = state_window_get(state, window);
|
||||||
if (!win) return;
|
if (!win) return;
|
||||||
|
|
||||||
effect_t withdraw_window_effect = {
|
plan_push_withdraw_effect(plan, window);
|
||||||
.type = ZDWM_EFFECT_WITHDRAW_WINDOW,
|
|
||||||
.as.withdraw.window = window,
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &withdraw_window_effect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -494,11 +442,7 @@ static void fullscreen_window(
|
|||||||
}
|
}
|
||||||
|
|
||||||
state_window_set_border_width(state, window_id, 0);
|
state_window_set_border_width(state, window_id, 0);
|
||||||
effect_t fullscreen_window_effect = {
|
plan_push_fullscreen_effect(plan, window_id, value);
|
||||||
.type = ZDWM_EFFECT_FULLSCREEN_WINDOW,
|
|
||||||
.as.fullscreen = {.window = window_id, .value = value},
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &fullscreen_window_effect);
|
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
state_window_set_geometry_mode(state, window_id, ZDWM_GEOMETRY_FULLSCREEN);
|
state_window_set_geometry_mode(state, window_id, ZDWM_GEOMETRY_FULLSCREEN);
|
||||||
@@ -536,11 +480,7 @@ static void maximize_window(
|
|||||||
}
|
}
|
||||||
|
|
||||||
state_window_set_border_width(state, window_id, 0);
|
state_window_set_border_width(state, window_id, 0);
|
||||||
effect_t maximize_window_effect = {
|
plan_push_maximize_effect(plan, window_id, value);
|
||||||
.type = ZDWM_EFFECT_MAXIMIZE_WINDOW,
|
|
||||||
.as.maximize = {.window = window_id, .value = value},
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &maximize_window_effect);
|
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
state_window_set_geometry_mode(state, window_id, ZDWM_GEOMETRY_MAXIMIZED);
|
state_window_set_geometry_mode(state, window_id, ZDWM_GEOMETRY_MAXIMIZED);
|
||||||
@@ -576,48 +516,25 @@ static void minimize_window(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
effect_t minimize_window_effect = {
|
plan_push_minimize_effect(plan, window_id, value);
|
||||||
.type = ZDWM_EFFECT_MINIMIZE_WINDOW,
|
|
||||||
.as.minimize = {.window = window_id, .value = value},
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &minimize_window_effect);
|
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
effect_t unmap_effect = {
|
plan_push_unmap_effect(plan, window_id);
|
||||||
.type = ZDWM_EFFECT_UNMAP_WINDOW,
|
|
||||||
.as.unmap.window = window_id,
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &unmap_effect);
|
|
||||||
|
|
||||||
auto workspace = state_workspace_get(state, window->workspace_id);
|
auto workspace = state_workspace_get(state, window->workspace_id);
|
||||||
auto old_focused_window_id = workspace->focused_window_id;
|
auto old_focused_window_id = workspace->focused_window_id;
|
||||||
state_window_set_geometry_mode(state, window_id, ZDWM_GEOMETRY_MINIMIZED);
|
state_window_set_geometry_mode(state, window_id, ZDWM_GEOMETRY_MINIMIZED);
|
||||||
auto new_focused_window_id = workspace->focused_window_id;
|
auto new_focused_window_id = workspace->focused_window_id;
|
||||||
if (old_focused_window_id != new_focused_window_id) {
|
if (old_focused_window_id != new_focused_window_id) {
|
||||||
effect_t change_old_border_color_effect = {
|
auto color = &ctx->border->normal_color;
|
||||||
.type = ZDWM_EFFECT_CHANGE_BORDER_COLOR,
|
plan_push_change_border_color_effect(plan, old_focused_window_id, color);
|
||||||
.as.change_border_color = {
|
|
||||||
.window = old_focused_window_id,
|
color = &ctx->border->focused_color;
|
||||||
.color = &ctx->border->normal_color
|
plan_push_change_border_color_effect(plan, new_focused_window_id, color);
|
||||||
}
|
|
||||||
};
|
|
||||||
effect_t change_new_border_color_effect = {
|
|
||||||
.type = ZDWM_EFFECT_CHANGE_BORDER_COLOR,
|
|
||||||
.as.change_border_color = {
|
|
||||||
.window = new_focused_window_id,
|
|
||||||
.color = &ctx->border->normal_color
|
|
||||||
}
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &change_old_border_color_effect);
|
|
||||||
plan_push_effect(plan, &change_new_border_color_effect);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
state_window_set_geometry_mode(state, window_id, ZDWM_GEOMETRY_NORMAL);
|
state_window_set_geometry_mode(state, window_id, ZDWM_GEOMETRY_NORMAL);
|
||||||
effect_t map_effect = {
|
plan_push_map_effect(plan, window_id);
|
||||||
.type = ZDWM_EFFECT_MAP_WINDOW,
|
|
||||||
.as.map.window = window_id
|
|
||||||
};
|
|
||||||
plan_push_effect(plan, &map_effect);
|
|
||||||
set_foucs_window(ctx, window->workspace_id, window_id, plan);
|
set_foucs_window(ctx, window->workspace_id, window_id, plan);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -105,3 +105,7 @@ typedef struct border_config_t {
|
|||||||
typedef struct only_window_data_t {
|
typedef struct only_window_data_t {
|
||||||
window_id_t window;
|
window_id_t window;
|
||||||
} only_window_data_t;
|
} only_window_data_t;
|
||||||
|
|
||||||
|
static inline bool window_id_invalid(window_id_t window_id) {
|
||||||
|
return window_id == ZDWM_WINDOW_ID_INVALID;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user