From 56f8380028b9106bbfec004b97a1b667f61f6aba Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Thu, 7 May 2026 03:21:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(action):=20=E5=AE=9E=E7=8E=B0=20switch=5Fw?= =?UTF-8?q?orkspace=20cycle=5Flayout=20=E5=92=8C=20cycle=5Foutput=20?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/action.c | 26 ++++++++++++++++++++++++++ src/core/state.c | 23 +++++++++++++++++------ src/core/state.h | 6 +++++- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/core/action.c b/src/core/action.c index fa3a1a3..111c4a5 100644 --- a/src/core/action.c +++ b/src/core/action.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -151,3 +152,28 @@ void toggle_sticky(runtime_t *runtime) { }; 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); +} diff --git a/src/core/state.c b/src/core/state.c index 532940c..ab37056 100644 --- a/src/core/state.c +++ b/src/core/state.c @@ -145,24 +145,35 @@ static void state_workspace_adjust_focused_window( 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 *)state_workspace_get(state, workspace_id); if (!workspace) return false; - size_t next_index = 0; + int32_t index = 0; 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]) { - next_index = (i + 1) % workspace->layout_count; - matched = true; + index = i; + matched = true; break; } } 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; return true; } diff --git a/src/core/state.h b/src/core/state.h index 6dc17de..4ed76dd 100644 --- a/src/core/state.h +++ b/src/core/state.h @@ -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_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( state_t *state, workspace_id_t workspace_id,