Implement resize command
Syntax: resize <left|right|up|down> [+|-]<pixels>
This commit is contained in:
parent
7be41492c6
commit
a55d0b77fe
|
@ -24,5 +24,13 @@ typedef enum { O_HORIZONTAL, O_VERTICAL } resize_orientation_t;
|
||||||
int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first,
|
int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first,
|
||||||
int second, resize_orientation_t orientation,
|
int second, resize_orientation_t orientation,
|
||||||
xcb_button_press_event_t *event);
|
xcb_button_press_event_t *event);
|
||||||
|
/**
|
||||||
|
* Resizes a column/row by the given amount of pixels. Called by
|
||||||
|
* resize_graphical_handler (the user clicked) or parse_resize_command (the
|
||||||
|
* user issued the command)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
|
||||||
|
resize_orientation_t orientation, int pixels);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "workspace.h"
|
#include "workspace.h"
|
||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
|
#include "resize.h"
|
||||||
|
|
||||||
bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
|
bool focus_window_in_container(xcb_connection_t *conn, Container *container, direction_t direction) {
|
||||||
/* If this container is empty, we’re done */
|
/* If this container is empty, we’re done */
|
||||||
|
@ -810,6 +811,55 @@ static void next_previous_workspace(xcb_connection_t *conn, int direction) {
|
||||||
workspace_show(conn, i+1);
|
workspace_show(conn, i+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void parse_resize_command(xcb_connection_t *conn, Client *last_focused, const char *command) {
|
||||||
|
int first, second;
|
||||||
|
resize_orientation_t orientation = O_VERTICAL;
|
||||||
|
Container *con = last_focused->container;
|
||||||
|
|
||||||
|
if (STARTS_WITH(command, "left")) {
|
||||||
|
if (con->col == 0)
|
||||||
|
return;
|
||||||
|
first = con->col - 1;
|
||||||
|
second = con->col;
|
||||||
|
command += strlen("left");
|
||||||
|
} else if (STARTS_WITH(command, "right")) {
|
||||||
|
first = con->col + (con->colspan - 1);
|
||||||
|
LOG("column %d\n", first);
|
||||||
|
|
||||||
|
if (!cell_exists(first, con->row) ||
|
||||||
|
(first == (con->workspace->cols-1)))
|
||||||
|
return;
|
||||||
|
|
||||||
|
second = first + 1;
|
||||||
|
command += strlen("right");
|
||||||
|
} else if (STARTS_WITH(command, "top")) {
|
||||||
|
if (con->row == 0)
|
||||||
|
return;
|
||||||
|
first = con->row - 1;
|
||||||
|
second = con->row;
|
||||||
|
orientation = O_HORIZONTAL;
|
||||||
|
command += strlen("top");
|
||||||
|
} else if (STARTS_WITH(command, "bottom")) {
|
||||||
|
first = con->row + (con->rowspan - 1);
|
||||||
|
if (!cell_exists(con->col, first) ||
|
||||||
|
(first == (con->workspace->rows-1)))
|
||||||
|
return;
|
||||||
|
|
||||||
|
second = first + 1;
|
||||||
|
orientation = O_HORIZONTAL;
|
||||||
|
command += strlen("bottom");
|
||||||
|
} else {
|
||||||
|
LOG("Syntax: resize <left|right|up|down> [+|-]<pixels>\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pixels = atoi(command);
|
||||||
|
if (pixels == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
resize_container(conn, con->workspace, first, second, orientation, pixels);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parses a command, see file CMDMODE for more information
|
* Parses a command, see file CMDMODE for more information
|
||||||
*
|
*
|
||||||
|
@ -889,6 +939,14 @@ void parse_command(xcb_connection_t *conn, const char *command) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (STARTS_WITH(command, "resize ")) {
|
||||||
|
if (last_focused == NULL)
|
||||||
|
return;
|
||||||
|
const char *rest = command + strlen("resize ");
|
||||||
|
parse_resize_command(conn, last_focused, rest);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Is it an <exit>? */
|
/* Is it an <exit>? */
|
||||||
if (STARTS_WITH(command, "exit")) {
|
if (STARTS_WITH(command, "exit")) {
|
||||||
LOG("User issued exit-command, exiting without error.\n");
|
LOG("User issued exit-command, exiting without error.\n");
|
||||||
|
|
46
src/resize.c
46
src/resize.c
|
@ -126,14 +126,26 @@ int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, i
|
||||||
xcb_destroy_window(conn, grabwin);
|
xcb_destroy_window(conn, grabwin);
|
||||||
xcb_flush(conn);
|
xcb_flush(conn);
|
||||||
|
|
||||||
/* TODO: refactor this, both blocks are very identical */
|
int pixels;
|
||||||
if (orientation == O_VERTICAL) {
|
if (orientation == O_VERTICAL)
|
||||||
LOG("Resize was from X = %d to X = %d\n", event->root_x, new_position);
|
pixels = (new_position - event->root_x);
|
||||||
if (event->root_x == new_position) {
|
else pixels = (new_position - event->root_y);
|
||||||
LOG("Nothing changed, not updating anything\n");
|
resize_container(conn, ws, first, second, orientation, pixels);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Resizes a column/row by the given amount of pixels. Called by
|
||||||
|
* resize_graphical_handler (the user clicked) or parse_resize_command (the
|
||||||
|
* user issued the command)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
|
||||||
|
resize_orientation_t orientation, int pixels) {
|
||||||
|
|
||||||
|
/* TODO: refactor this, both blocks are very identical */
|
||||||
|
if (orientation == O_VERTICAL) {
|
||||||
int default_width = ws->rect.width / ws->cols;
|
int default_width = ws->rect.width / ws->cols;
|
||||||
int old_unoccupied_x = get_unoccupied_x(ws);
|
int old_unoccupied_x = get_unoccupied_x(ws);
|
||||||
|
|
||||||
|
@ -174,8 +186,8 @@ int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, i
|
||||||
|
|
||||||
LOG("middle = %f\n", ws->width_factor[first]);
|
LOG("middle = %f\n", ws->width_factor[first]);
|
||||||
int old_width = ws->width_factor[first] * old_unoccupied_x;
|
int old_width = ws->width_factor[first] * old_unoccupied_x;
|
||||||
LOG("first->width = %d, new_position = %d, event->root_x = %d\n", old_width, new_position, event->root_x);
|
LOG("first->width = %d, pixels = %d\n", pixels);
|
||||||
ws->width_factor[first] *= (float)(old_width + (new_position - event->root_x)) / old_width;
|
ws->width_factor[first] *= (float)(old_width + pixels) / old_width;
|
||||||
LOG("-> %f\n", ws->width_factor[first]);
|
LOG("-> %f\n", ws->width_factor[first]);
|
||||||
|
|
||||||
|
|
||||||
|
@ -184,20 +196,14 @@ int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, i
|
||||||
ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
|
ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
|
||||||
LOG("middle = %f\n", ws->width_factor[second]);
|
LOG("middle = %f\n", ws->width_factor[second]);
|
||||||
old_width = ws->width_factor[second] * old_unoccupied_x;
|
old_width = ws->width_factor[second] * old_unoccupied_x;
|
||||||
LOG("second->width = %d, new_position = %d, event->root_x = %d\n", old_width, new_position, event->root_x);
|
LOG("second->width = %d, pixels = %d\n", pixels);
|
||||||
ws->width_factor[second] *= (float)(old_width - (new_position - event->root_x)) / old_width;
|
ws->width_factor[second] *= (float)(old_width - pixels) / old_width;
|
||||||
LOG("-> %f\n", ws->width_factor[second]);
|
LOG("-> %f\n", ws->width_factor[second]);
|
||||||
|
|
||||||
LOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
|
LOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
|
||||||
|
|
||||||
LOG("\n\n\n");
|
LOG("\n\n\n");
|
||||||
} else {
|
} else {
|
||||||
LOG("Resize was from X = %d to X = %d\n", event->root_y, new_position);
|
|
||||||
if (event->root_y == new_position) {
|
|
||||||
LOG("Nothing changed, not updating anything\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int default_height = ws->rect.height / ws->rows;
|
int default_height = ws->rect.height / ws->rows;
|
||||||
int old_unoccupied_y = get_unoccupied_y(ws);
|
int old_unoccupied_y = get_unoccupied_y(ws);
|
||||||
|
|
||||||
|
@ -238,8 +244,8 @@ int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, i
|
||||||
|
|
||||||
LOG("middle = %f\n", ws->height_factor[first]);
|
LOG("middle = %f\n", ws->height_factor[first]);
|
||||||
int old_height = ws->height_factor[first] * old_unoccupied_y;
|
int old_height = ws->height_factor[first] * old_unoccupied_y;
|
||||||
LOG("first->width = %d, new_position = %d, event->root_x = %d\n", old_height, new_position, event->root_y);
|
LOG("first->width = %d, pixels = %d\n", pixels);
|
||||||
ws->height_factor[first] *= (float)(old_height + (new_position - event->root_y)) / old_height;
|
ws->height_factor[first] *= (float)(old_height + pixels) / old_height;
|
||||||
LOG("-> %f\n", ws->height_factor[first]);
|
LOG("-> %f\n", ws->height_factor[first]);
|
||||||
|
|
||||||
|
|
||||||
|
@ -248,8 +254,8 @@ int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, i
|
||||||
ws->height_factor[second] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
|
ws->height_factor[second] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
|
||||||
LOG("middle = %f\n", ws->height_factor[second]);
|
LOG("middle = %f\n", ws->height_factor[second]);
|
||||||
old_height = ws->height_factor[second] * old_unoccupied_y;
|
old_height = ws->height_factor[second] * old_unoccupied_y;
|
||||||
LOG("second->width = %d, new_position = %d, event->root_x = %d\n", old_height, new_position, event->root_y);
|
LOG("second->width = %d, pixels = %d\n", pixels);
|
||||||
ws->height_factor[second] *= (float)(old_height - (new_position - event->root_y)) / old_height;
|
ws->height_factor[second] *= (float)(old_height - pixels) / old_height;
|
||||||
LOG("-> %f\n", ws->height_factor[second]);
|
LOG("-> %f\n", ws->height_factor[second]);
|
||||||
|
|
||||||
LOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));
|
LOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));
|
||||||
|
@ -258,6 +264,4 @@ int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, i
|
||||||
}
|
}
|
||||||
|
|
||||||
render_layout(conn);
|
render_layout(conn);
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue