feat(action): 实现 switch_workspace cycle_layout 和 cycle_output 操作

This commit is contained in:
2026-05-07 03:21:27 +08:00
parent 2c3c264ddd
commit 56f8380028
3 changed files with 48 additions and 7 deletions

View File

@@ -2,6 +2,7 @@
#include <paths.h> #include <paths.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
@@ -151,3 +152,28 @@ void toggle_sticky(runtime_t *runtime) {
}; };
command_buffer_push(&runtime->command_buffer, &window_set_sticky_cmd); command_buffer_push(&runtime->command_buffer, &window_set_sticky_cmd);
} }
void switch_workspace(runtime_t *runtime, workspace_id_t workspace_id) {
auto workspace = state_workspace_get(&runtime->state, workspace_id);
if (!workspace) return;
command_t switch_workspace_cmd = {
.type = ZDWM_COMMAND_SWITCH_WORKSPACE,
.as.switch_workspace.workspace = workspace_id
};
command_buffer_push(&runtime->command_buffer, &switch_workspace_cmd);
}
void cycle_layout(runtime_t *runtime, int32_t delta) {
auto state = &runtime->state;
auto output = state_output_at(state, state->current_output_index);
auto workspace = state_workspace_get(state, output->current_workspace_id);
if (!state_workspace_cycle_layout(state, workspace->id, delta)) return;
runtime->plan.need_relayout = true;
}
void cycle_current_output(zdwm_runtime_t *runtime, int32_t delta) {
auto state = &runtime->state;
state_cycle_current_output(state, delta);
}

View File

@@ -145,16 +145,21 @@ static void state_workspace_adjust_focused_window(
workspace->focused_window_id = ZDWM_WINDOW_ID_INVALID; workspace->focused_window_id = ZDWM_WINDOW_ID_INVALID;
} }
bool state_workspace_cycle_layout(state_t *state, workspace_id_t workspace_id) { bool state_workspace_cycle_layout(
state_t *state,
workspace_id_t workspace_id,
int32_t delta
) {
workspace_t *workspace = workspace_t *workspace =
(workspace_t *)state_workspace_get(state, workspace_id); (workspace_t *)state_workspace_get(state, workspace_id);
if (!workspace) return false; if (!workspace) return false;
size_t next_index = 0; int32_t index = 0;
bool matched = false; bool matched = false;
for (size_t i = 0; i < workspace->layout_count; i++) { auto layout_count = (int32_t)workspace->layout_count;
for (int32_t i = 0; i < layout_count; i++) {
if (workspace->layout_id == workspace->available_layouts[i]) { if (workspace->layout_id == workspace->available_layouts[i]) {
next_index = (i + 1) % workspace->layout_count; index = i;
matched = true; matched = true;
break; break;
} }
@@ -162,7 +167,13 @@ bool state_workspace_cycle_layout(state_t *state, workspace_id_t workspace_id) {
if (!matched) return false; if (!matched) return false;
auto next_layout_id = workspace->available_layouts[next_index]; auto new_index = index + delta;
new_index %= layout_count;
if (new_index < 0) new_index += layout_count;
if (new_index == index) return false;
auto next_layout_id = workspace->available_layouts[new_index];
workspace->layout_id = next_layout_id; workspace->layout_id = next_layout_id;
return true; return true;
} }

View File

@@ -81,7 +81,11 @@ void state_cleanup(state_t *state);
*/ */
const workspace_t *state_workspace_get(const state_t *state, workspace_id_t id); const workspace_t *state_workspace_get(const state_t *state, workspace_id_t id);
const workspace_t *state_workspace_at(const state_t *state, size_t index); const workspace_t *state_workspace_at(const state_t *state, size_t index);
bool state_workspace_cycle_layout(state_t *state, workspace_id_t workspace_id); bool state_workspace_cycle_layout(
state_t *state,
workspace_id_t workspace_id,
int32_t delta
);
bool state_workspace_set_layout_by_index( bool state_workspace_set_layout_by_index(
state_t *state, state_t *state,
workspace_id_t workspace_id, workspace_id_t workspace_id,