refactored workspace_next/prev

master
minus 9 years ago
parent 2e755cf13f
commit 1efda79bf2

@ -0,0 +1,4 @@
/**
* Wrap i into the range [0, max[
*/
int wrap(int i, int max);

@ -0,0 +1,3 @@
int wrap(int i, int max) {
return ((i % max) + max) % max;
}

@ -11,6 +11,7 @@
#include "config.h" #include "config.h"
#include "stringop.h" #include "stringop.h"
#include "focus.h" #include "focus.h"
#include "util.h"
char *workspace_next_name(void) { char *workspace_next_name(void) {
sway_log(L_DEBUG, "Workspace: Generating new name"); sway_log(L_DEBUG, "Workspace: Generating new name");
@ -102,32 +103,28 @@ void workspace_output_next() {
} }
void workspace_next() { void workspace_next() {
// Get the index of the workspace in the current output, and change the view to index+1 workspace. // Get the index of the workspace in the current output, and change the focus to index+1 workspace.
// if we're currently focused on the last workspace in the output, change focus to there // if we're currently focused on the last workspace in the output, change focus to the next output
// and call workspace_output_next(), as long as another output actually exists // and call workspace_output_next()
swayc_t *current_output = swayc_active_workspace()->parent; swayc_t *current_output = swayc_active_workspace()->parent;
int i; int i;
for (i = 0; i < current_output->children->length - 1; i++) { for (i = 0; i < current_output->children->length - 1; i++) {
if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { if (current_output->children->items[i] == swayc_active_workspace()) {
workspace_switch(current_output->children->items[i + 1]); workspace_switch(current_output->children->items[i + 1]);
return; return;
} }
} }
if (root_container.children->length > 1) {
for (i = 0; i < root_container.children->length - 1; i++) { int num_outputs = root_container.children->length;
for (i = 0; i < num_outputs; i++) {
if (root_container.children->items[i] == current_output) { if (root_container.children->items[i] == current_output) {
workspace_switch(((swayc_t *)root_container.children->items[i + 1])->focused); swayc_t *next_output = root_container.children->items[wrap(++i, num_outputs)];
workspace_switch(next_output->focused);
workspace_output_next(); workspace_output_next();
return; return;
} }
} }
// If we're at the last output, then go to the first
workspace_switch(((swayc_t *)root_container.children->items[0])->focused);
workspace_output_next();
return;
} else {
workspace_switch(current_output->children->items[0]);
}
} }
void workspace_output_prev() { void workspace_output_prev() {
@ -145,34 +142,28 @@ void workspace_output_prev() {
} }
void workspace_prev() { void workspace_prev() {
// Get the index of the workspace in the current output, and change the view to index-1 workspace. // Get the index of the workspace in the current output, and change the focus to index-1 workspace.
// if we're currently focused on the last workspace in the output, change focus to there // if we're currently focused on the first workspace in the output, change focus to the previous output
// and call workspace_output_next(), as long as another output actually exists // and call workspace_output_prev()
swayc_t *current_output = swayc_active_workspace()->parent; swayc_t *current_output = swayc_active_workspace()->parent;
int i; int i;
for (i = 1; i < current_output->children->length; i++) { for (i = 1; i < current_output->children->length; i++) {
if (strcmp((((swayc_t *)current_output->children->items[i])->name), swayc_active_workspace()->name) == 0) { if (current_output->children->items[i] == swayc_active_workspace()) {
workspace_switch(current_output->children->items[i - 1]); workspace_switch(current_output->children->items[i - 1]);
return; return;
} }
} }
if (root_container.children->length > 1) {
for (i = 1; i < root_container.children->length; i++) { int num_outputs = root_container.children->length;
for (i = 0; i < num_outputs; i++) {
if (root_container.children->items[i] == current_output) { if (root_container.children->items[i] == current_output) {
workspace_switch(((swayc_t *)root_container.children->items[i - 1])->focused); swayc_t *prev_output = root_container.children->items[wrap(--i, num_outputs)];
workspace_output_next(); workspace_switch(prev_output->focused);
workspace_output_prev();
return; return;
} }
} }
// If we're at the first output, then go to the last
workspace_switch(((swayc_t *)root_container.children->items[root_container.children->length-1])->focused);
workspace_output_next();
return;
} else {
workspace_switch(current_output->children->items[current_output->children->length - 1]);
}
} }
void workspace_switch(swayc_t *workspace) { void workspace_switch(swayc_t *workspace) {

Loading…
Cancel
Save