feat(action): 实现切换当前 output 工作区功能
This commit is contained in:
@@ -34,6 +34,14 @@ typedef struct zdwm_action_api_t {
|
||||
zdwm_runtime_t *runtime,
|
||||
zdwm_workspace_id_t workspace_id
|
||||
);
|
||||
void (*switch_workspace_in_current_output)(
|
||||
zdwm_runtime_t *runtime,
|
||||
zdwm_workspace_id_t workspace_id
|
||||
);
|
||||
void (*switch_workspace_by_index_in_current_output)(
|
||||
zdwm_runtime_t *runtime,
|
||||
uint32_t index
|
||||
);
|
||||
void (*cycle_layout)(zdwm_runtime_t *runtime, int32_t delta);
|
||||
void (*cycle_current_output)(zdwm_runtime_t *runtime, int32_t delta);
|
||||
void (*focus_window)(zdwm_runtime_t *runtime, int32_t delta);
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#include "config/defaults.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <zdwm/action.h>
|
||||
#include <zdwm/types.h>
|
||||
|
||||
#include "base/log.h"
|
||||
#include "base/macros.h"
|
||||
#include "core/types.h"
|
||||
|
||||
static constexpr char launcher[] =
|
||||
"rofi -show combi -modes combi -combi-modes window,drun,run,ssh,windowcd";
|
||||
@@ -67,6 +72,14 @@ static void toggle_floating(
|
||||
api->toggle_floating(runtime);
|
||||
}
|
||||
|
||||
static void switch_workspace(
|
||||
zdwm_runtime_t *runtime,
|
||||
const zdwm_action_api_t *api,
|
||||
const zdwm_action_arg_t *arg
|
||||
) {
|
||||
api->switch_workspace_by_index_in_current_output(runtime, arg->ui);
|
||||
}
|
||||
|
||||
static void focus_window(
|
||||
zdwm_runtime_t *runtime,
|
||||
const zdwm_action_api_t *api,
|
||||
@@ -119,17 +132,26 @@ bool config_defaults_build(
|
||||
fullscreen_id,
|
||||
floating_id,
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < output_count; i++) {
|
||||
if (api->define_workspace(
|
||||
builder,
|
||||
i,
|
||||
"main",
|
||||
layout_ids,
|
||||
countof(layout_ids),
|
||||
fair_id
|
||||
) == ZDWM_WORKSPACE_ID_INVALID) {
|
||||
return false;
|
||||
#define DEF_WORKSPACE(NAME) \
|
||||
api->define_workspace( \
|
||||
builder, \
|
||||
i, \
|
||||
(NAME), \
|
||||
layout_ids, \
|
||||
countof(layout_ids), \
|
||||
fair_id \
|
||||
)
|
||||
char *workspace_names[] = {"1 terminal", "2 Editor", "3 Browser"};
|
||||
for (size_t j = 0; j < countof(workspace_names); ++j) {
|
||||
auto workspace = DEF_WORKSPACE(workspace_names[j]);
|
||||
if (workspace_id_invalid(workspace)) {
|
||||
logger("== define workspace \"%s\" failed\n", workspace_names[j]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#undef DEF_WORKSPACE
|
||||
}
|
||||
|
||||
auto default_mode = api->add_mode(builder, "default");
|
||||
@@ -154,11 +176,17 @@ bool config_defaults_build(
|
||||
BIND(default_mode, Alt "+j", focus_window, {.i = 1});
|
||||
BIND(default_mode, Alt "+k", focus_window, {.i = -1});
|
||||
|
||||
char buffer[64] = {0};
|
||||
for (uint32_t i = 0; i < 9; ++i) {
|
||||
snprintf(buffer, sizeof(buffer), "%s+%d", Super, i + 1);
|
||||
BIND(default_mode, buffer, switch_workspace, {.ui = i});
|
||||
}
|
||||
|
||||
#undef BIND
|
||||
|
||||
api->set_default_mode(builder, default_mode);
|
||||
api->set_initial_mode(builder, default_mode);
|
||||
api->set_border_config(builder, 2, "#444444", "#005577");
|
||||
api->set_border_config(builder, 2, "#1c2022", "#606060");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -164,6 +164,45 @@ void switch_workspace(runtime_t *runtime, workspace_id_t workspace_id) {
|
||||
command_buffer_push(&runtime->command_buffer, &switch_workspace_cmd);
|
||||
}
|
||||
|
||||
void switch_workspace_in_current_output(
|
||||
runtime_t *runtime,
|
||||
workspace_id_t workspace_id
|
||||
) {
|
||||
auto state = &runtime->state;
|
||||
auto output = state_output_at(state, state->current_output_index);
|
||||
auto workspace = state_workspace_get(state, workspace_id);
|
||||
if (workspace->output_id != output->id) 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 switch_workspace_by_index_in_current_output(
|
||||
runtime_t *runtime,
|
||||
uint32_t index
|
||||
) {
|
||||
auto state = &runtime->state;
|
||||
auto output = state_output_at(state, state->current_output_index);
|
||||
uint32_t count = 0;
|
||||
for (size_t i = 0; i < state_window_count(state); ++i) {
|
||||
auto workspace = state_workspace_at(state, i);
|
||||
if (workspace->output_id != output->id) continue;
|
||||
|
||||
if (count == index) {
|
||||
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);
|
||||
return;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
void cycle_layout(runtime_t *runtime, int32_t delta) {
|
||||
auto state = &runtime->state;
|
||||
auto output = state_output_at(state, state->current_output_index);
|
||||
|
||||
@@ -19,6 +19,14 @@ void switch_workspace(
|
||||
zdwm_runtime_t *runtime,
|
||||
zdwm_workspace_id_t workspace_id
|
||||
);
|
||||
void switch_workspace_in_current_output(
|
||||
zdwm_runtime_t *runtime,
|
||||
zdwm_workspace_id_t workspace_id
|
||||
);
|
||||
void switch_workspace_by_index_in_current_output(
|
||||
zdwm_runtime_t *runtime,
|
||||
uint32_t index
|
||||
);
|
||||
void cycle_layout(zdwm_runtime_t *runtime, int32_t delta);
|
||||
void cycle_current_output(zdwm_runtime_t *runtime, int32_t delta);
|
||||
void focus_window(zdwm_runtime_t *runtime, int32_t delta);
|
||||
|
||||
@@ -257,7 +257,7 @@ static void runtime_arrange(runtime_t *runtime) {
|
||||
}
|
||||
}
|
||||
|
||||
policy_context_t policy_context_init(runtime_t *runtime) {
|
||||
static policy_context_t policy_context_init(runtime_t *runtime) {
|
||||
policy_context_t ctx = {
|
||||
.bind_table = (runtime)->binding_table,
|
||||
.state = &(runtime)->state,
|
||||
@@ -266,14 +266,19 @@ policy_context_t policy_context_init(runtime_t *runtime) {
|
||||
.layouts = &(runtime)->layouts,
|
||||
.runtime = (runtime),
|
||||
.action_api = {
|
||||
.spawn = spawn,
|
||||
.quit = quit,
|
||||
.raise_or_run = raise_or_run,
|
||||
.toggle_fullscreen = toggle_fullscreen,
|
||||
.toggle_maximize = toggle_maximize,
|
||||
.toggle_floating = toggle_floating,
|
||||
.toggle_sticky = toggle_sticky,
|
||||
.switch_workspace = switch_workspace,
|
||||
.spawn = spawn,
|
||||
.quit = quit,
|
||||
.raise_or_run = raise_or_run,
|
||||
.toggle_fullscreen = toggle_fullscreen,
|
||||
.toggle_maximize = toggle_maximize,
|
||||
.toggle_floating = toggle_floating,
|
||||
.toggle_sticky = toggle_sticky,
|
||||
|
||||
.switch_workspace = switch_workspace,
|
||||
.switch_workspace_in_current_output = switch_workspace_in_current_output,
|
||||
.switch_workspace_by_index_in_current_output =
|
||||
switch_workspace_by_index_in_current_output,
|
||||
|
||||
.cycle_layout = cycle_layout,
|
||||
.cycle_current_output = cycle_current_output,
|
||||
.focus_window = focus_window,
|
||||
|
||||
Reference in New Issue
Block a user