summaryrefslogtreecommitdiff
path: root/schewm.c
diff options
context:
space:
mode:
authorSamuel Fadel <samuel@nihil.ws>2022-12-19 23:02:38 +0100
committerSamuel Fadel <samuel@nihil.ws>2022-12-19 23:02:38 +0100
commit291a0b72fbeaf757056fbb7fba0376262360d877 (patch)
tree6e5bee2e5f4dc2d8b0832f638f55ae52ba7bafab /schewm.c
parent86d7067f5d27d7c97e9a1de8c4103e139af66c40 (diff)
Refactoring to reduce repeated code, fix tiling bugs.
* Makefile: Add optimization flag * init.scm: Use correct indices for workspaces * schewm.c: Many lines of repeated code removed
Diffstat (limited to 'schewm.c')
-rw-r--r--schewm.c231
1 files changed, 109 insertions, 122 deletions
diff --git a/schewm.c b/schewm.c
index c2a4fda..e8d3151 100644
--- a/schewm.c
+++ b/schewm.c
@@ -1946,7 +1946,9 @@ wm_set_client_state(struct Client *client, enum WindowState state) {
dpy_unmap_window(client->id);
break;
case WS_MAXIMIZED:
- client_store_size(client);
+ if (!client->has_old_size) {
+ client_store_size(client);
+ }
{
struct Rect mon_rect;
wm_client_monitor_size(client, true, &mon_rect);
@@ -2463,184 +2465,169 @@ wm_client_ensure_state(struct Client *client, enum WindowState state) {
}
}
+enum TileType {
+ TILE_HALF_LEFT,
+ TILE_HALF_RIGHT,
+ TILE_HALF_TOP,
+ TILE_HALF_BOTTOM,
+ TILE_TOP_LEFT,
+ TILE_TOP_RIGHT,
+ TILE_BOTTOM_LEFT,
+ TILE_BOTTOM_RIGHT,
+};
+
void
-wm_toggle_half_left() {
+wm_toggle_tile(enum TileType tile_type) {
if (wm.focus == NULL) {
return;
}
wm_client_ensure_state(wm.focus, WS_NORMAL);
- // Start with monitor size, then reduce to appropriate target size
- // for half the screen, accounting for border sizes
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- rect.width = rect.width / 2 - cfg.inner_border_width * 2;
- rect.height -= cfg.inner_border_width * 2;
- wm_move_resize_client(wm.focus, &rect, false);
+ if (wm.focus->has_old_size) {
+ client_restore_size(wm.focus);
+ dpy_update_window_geometry(wm.focus);
+ } else {
+ struct Rect rect;
+ // Start with monitor size, then reduce to appropriate target size
+ wm_client_monitor_size(wm.focus, true, &rect);
+ switch (tile_type) {
+ case TILE_HALF_LEFT:
+ rect.width = rect.width / 2;
+ break;
+ case TILE_HALF_RIGHT:
+ rect.x += rect.width / 2;
+ rect.width = rect.width / 2;
+ break;
+ case TILE_HALF_TOP:
+ rect.height = rect.height / 2;
+ break;
+ case TILE_HALF_BOTTOM:
+ rect.y += rect.height / 2;
+ rect.height = rect.height / 2;
+ break;
+ case TILE_TOP_LEFT:
+ rect.width = rect.width / 2;
+ rect.height = rect.height / 2;
+ break;
+ case TILE_TOP_RIGHT:
+ rect.x += rect.width / 2;
+ rect.width = rect.width / 2;
+ rect.height = rect.height / 2;
+ break;
+ case TILE_BOTTOM_LEFT:
+ rect.y += rect.height / 2;
+ rect.width = rect.width / 2;
+ rect.height = rect.height / 2;
+ break;
+ case TILE_BOTTOM_RIGHT:
+ rect.x += rect.width / 2;
+ rect.y += rect.height / 2;
+ rect.width = rect.width / 2;
+ rect.height = rect.height / 2;
+ break;
+ }
+ // Correct width/height according to borders
+ rect.width -= cfg.inner_border_width * 2;
+ rect.height -= cfg.inner_border_width * 2;
+ // Stores current size so we can toggle it later
+ client_store_size(wm.focus);
+ wm_move_resize_client(wm.focus, &rect, false);
+ }
wm_raise_and_center_cursor(wm.focus);
}
void
-wm_toggle_half_right() {
- if (wm.focus == NULL) {
- return;
- }
+wm_toggle_half_left() {
+ wm_toggle_tile(TILE_HALF_LEFT);
+}
- wm_client_ensure_state(wm.focus, WS_NORMAL);
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- rect.x += rect.width / 2;
- rect.width = rect.width / 2 - cfg.inner_border_width * 2;
- rect.height -= cfg.inner_border_width * 2;
- wm_move_resize_client(wm.focus, &rect, false);
- wm_raise_and_center_cursor(wm.focus);
+void
+wm_toggle_half_right() {
+ wm_toggle_tile(TILE_HALF_RIGHT);
}
void
wm_toggle_half_top() {
- if (wm.focus == NULL) {
- return;
- }
-
- wm_client_ensure_state(wm.focus, WS_NORMAL);
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- rect.width -= cfg.inner_border_width * 2;
- rect.height = rect.height / 2 - cfg.inner_border_width * 2;
- wm_move_resize_client(wm.focus, &rect, false);
- wm_raise_and_center_cursor(wm.focus);
+ wm_toggle_tile(TILE_HALF_TOP);
}
void
wm_toggle_half_bottom() {
- if (wm.focus == NULL) {
- return;
- }
-
- wm_client_ensure_state(wm.focus, WS_NORMAL);
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- rect.y += rect.height / 2;
- rect.width -= cfg.inner_border_width * 2;
- rect.height = rect.height / 2 - cfg.inner_border_width * 2;
- wm_move_resize_client(wm.focus, &rect, false);
- wm_raise_and_center_cursor(wm.focus);
+ wm_toggle_tile(TILE_HALF_BOTTOM);
}
void
wm_toggle_top_left() {
- if (wm.focus == NULL) {
- return;
- }
-
- wm_client_ensure_state(wm.focus, WS_NORMAL);
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- rect.width = rect.width / 2 - cfg.inner_border_width * 2;
- rect.height = rect.height / 2 - cfg.inner_border_width * 2;
- wm_move_resize_client(wm.focus, &rect, false);
- wm_raise_and_center_cursor(wm.focus);
+ wm_toggle_tile(TILE_TOP_LEFT);
}
void
wm_toggle_top_right() {
- if (wm.focus == NULL) {
- return;
- }
-
- wm_client_ensure_state(wm.focus, WS_NORMAL);
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- rect.x += rect.width / 2;
- rect.width = rect.width / 2 - cfg.inner_border_width * 2;
- rect.height = rect.height / 2 - cfg.inner_border_width * 2;
- wm_move_resize_client(wm.focus, &rect, false);
- wm_raise_and_center_cursor(wm.focus);
+ wm_toggle_tile(TILE_TOP_RIGHT);
}
void
wm_toggle_bottom_left() {
- if (wm.focus == NULL) {
- return;
- }
-
- wm_client_ensure_state(wm.focus, WS_NORMAL);
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- rect.y += rect.height / 2;
- rect.width = rect.width / 2 - cfg.inner_border_width * 2;
- rect.height = rect.height / 2 - cfg.inner_border_width * 2;
- wm_move_resize_client(wm.focus, &rect, false);
- wm_raise_and_center_cursor(wm.focus);
+ wm_toggle_tile(TILE_BOTTOM_LEFT);
}
void
wm_toggle_bottom_right() {
- if (wm.focus == NULL) {
- return;
- }
-
- wm_client_ensure_state(wm.focus, WS_NORMAL);
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- rect.x += rect.width / 2;
- rect.y += rect.height / 2;
- rect.width = rect.width / 2 - cfg.inner_border_width * 2;
- rect.height = rect.height / 2 - cfg.inner_border_width * 2;
- wm_move_resize_client(wm.focus, &rect, false);
- wm_raise_and_center_cursor(wm.focus);
+ wm_toggle_tile(TILE_BOTTOM_RIGHT);
}
+enum PackType {
+ PACK_LEFT,
+ PACK_RIGHT,
+ PACK_TOP,
+ PACK_BOTTOM,
+};
+
void
-wm_pack_left() {
+wm_pack_client(enum PackType pack_type) {
if (wm.focus == NULL || wm.focus->state != WS_NORMAL) {
return;
}
struct Rect rect;
wm_client_monitor_size(wm.focus, true, &rect);
- int16_t dx = rect.x - wm.focus->x;
- wm_move_client(wm.focus, dx, 0, false);
+ int16_t dx = 0, dy = 0;
+ switch (pack_type) {
+ case PACK_LEFT:
+ dx = rect.x - wm.focus->x;
+ break;
+ case PACK_RIGHT:
+ dx = rect.width - (wm.focus->x - rect.x + wm.focus->width) - cfg.inner_border_width * 2;
+ break;
+ case PACK_TOP:
+ dy = rect.y - wm.focus->y;
+ break;
+ case PACK_BOTTOM:
+ dy = rect.height - (wm.focus->y - rect.y + wm.focus->height) - cfg.inner_border_width * 2;
+ break;
+ }
+ wm_move_client(wm.focus, dx, dy, false);
wm_raise_and_center_cursor(wm.focus);
}
void
-wm_pack_right() {
- if (wm.focus == NULL || wm.focus->state != WS_NORMAL) {
- return;
- }
+wm_pack_left() {
+ wm_pack_client(PACK_LEFT);
+}
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- int16_t dx = rect.width - (wm.focus->x - rect.x + wm.focus->width) - cfg.inner_border_width * 2;
- wm_move_client(wm.focus, dx, 0, false);
- wm_raise_and_center_cursor(wm.focus);
+void
+wm_pack_right() {
+ wm_pack_client(PACK_RIGHT);
}
void
wm_pack_top() {
- if (wm.focus == NULL || wm.focus->state != WS_NORMAL) {
- return;
- }
-
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- int16_t dy = rect.y - wm.focus->y;
- wm_move_client(wm.focus, 0, dy, false);
- wm_raise_and_center_cursor(wm.focus);
+ wm_pack_client(PACK_TOP);
}
void
wm_pack_bottom() {
- if (wm.focus == NULL || wm.focus->state != WS_NORMAL) {
- return;
- }
-
- struct Rect rect;
- wm_client_monitor_size(wm.focus, true, &rect);
- int16_t dy = rect.height - (wm.focus->y - rect.y + wm.focus->height) - cfg.inner_border_width * 2;
- wm_move_client(wm.focus, 0, dy, false);
- wm_raise_and_center_cursor(wm.focus);
+ wm_pack_client(PACK_BOTTOM);
}
void