swayfx/sway/commands/scratchpad.c

73 lines
2.4 KiB
C
Raw Normal View History

2016-09-02 12:39:08 +10:00
#include <string.h>
2017-03-11 15:41:24 +11:00
#include <strings.h>
2016-09-02 12:39:08 +10:00
#include <wlc/wlc.h>
2016-09-03 05:11:48 +10:00
#include "sway/commands.h"
#include "sway/container.h"
#include "sway/focus.h"
#include "sway/layout.h"
2016-09-02 12:39:08 +10:00
static swayc_t *fetch_view_from_scratchpad() {
2017-10-09 02:26:45 +11:00
sp_index = (sp_index + 1) % scratchpad->length;
swayc_t *view = scratchpad->items[sp_index];
2016-09-02 12:39:08 +10:00
if (wlc_view_get_output(view->handle) != swayc_active_output()->handle) {
wlc_view_set_output(view->handle, swayc_active_output()->handle);
}
if (!view->is_floating) {
view->width = swayc_active_workspace()->width * 0.5;
view->height = swayc_active_workspace()->height * 0.75;
2016-09-02 12:39:08 +10:00
view->x = (swayc_active_workspace()->width - view->width)/2;
view->y = (swayc_active_workspace()->height - view->height)/2;
}
if (swayc_active_workspace()->width < view->x + 20 || view->x + view->width < 20) {
view->x = (swayc_active_workspace()->width - view->width)/2;
}
if (swayc_active_workspace()->height < view->y + 20 || view->y + view->height < 20) {
view->y = (swayc_active_workspace()->height - view->height)/2;
}
add_floating(swayc_active_workspace(), view);
wlc_view_set_mask(view->handle, VISIBLE);
view->visible = true;
arrange_windows(swayc_active_workspace(), -1, -1);
set_focused_container(view);
return view;
}
struct cmd_results *cmd_scratchpad(int argc, char **argv) {
struct cmd_results *error = NULL;
if (config->reading) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can't be used in config file.");
if (!config->active) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can only be used when sway is running.");
if ((error = checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1))) {
return error;
}
if (strcasecmp(argv[0], "show") == 0 && scratchpad->length > 0) {
if (!sp_view) {
2017-10-09 02:26:45 +11:00
if (current_container) {
// Haxor the scratchpad index if criteria'd
for (int i = 0; i < scratchpad->length; ++i) {
if (scratchpad->items[i] == current_container) {
sp_index = (i - 1) % scratchpad->length;
}
}
}
2016-09-02 12:39:08 +10:00
sp_view = fetch_view_from_scratchpad();
} else {
if (swayc_active_workspace() != sp_view->parent) {
hide_view_in_scratchpad(sp_view);
if (sp_index == 0) {
sp_index = scratchpad->length;
}
sp_index--;
sp_view = fetch_view_from_scratchpad();
} else {
hide_view_in_scratchpad(sp_view);
sp_view = NULL;
}
}
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
}
return cmd_results_new(CMD_FAILURE, "scratchpad", "Expected 'scratchpad show' when scratchpad is not empty.");
}