From 4992ee09c4a8d956bda505e65e57c0766a410feb Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 16 Nov 2021 06:20:19 +0500 Subject: [PATCH] Move border and gap code to separate module "helpers" --- Makefile | 4 +-- dwm.c | 1 + dwm/layouts.c | 80 ++++----------------------------------------------- helpers.c | 41 ++++++++++++++++++++++++++ helpers.h | 9 ++++++ 5 files changed, 59 insertions(+), 76 deletions(-) create mode 100644 helpers.c create mode 100644 helpers.h diff --git a/Makefile b/Makefile index 36def58..b76a0cc 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include config.mk -SRC = atoms.c drw.c dwm.c layouts.c settings.c spawn.c tags.c util.c +SRC = atoms.c drw.c dwm.c helpers.c layouts.c settings.c spawn.c tags.c util.c OBJ = ${SRC:.c=.o} all: options polytreewm @@ -18,7 +18,7 @@ options: ${CC} -c $< -o $@ ${CFLAGS} dwm.o: dwm/layouts.c dwm/swallow.c dwm/systray.c -${OBJ}: atoms.h drw.h config.def.h config.mk layouts.h settings.h spawn.h tags.h util.h +${OBJ}: atoms.h drw.h config.def.h config.mk helpers.h layouts.h settings.h spawn.h tags.h util.h polytreewm: ${OBJ} ${CC} -o $@ ${OBJ} ${LDFLAGS} diff --git a/dwm.c b/dwm.c index a1a0ce7..3988c2c 100644 --- a/dwm.c +++ b/dwm.c @@ -48,6 +48,7 @@ #include "atoms.h" #include "drw.h" +#include "helpers.h" #include "layouts.h" #include "settings.h" #include "spawn.h" diff --git a/dwm/layouts.c b/dwm/layouts.c index d364aff..070d312 100644 --- a/dwm/layouts.c +++ b/dwm/layouts.c @@ -20,32 +20,8 @@ centeredmaster(Monitor *m) } } - const SettingsForSingleWindow gap_for_single_window = settings_get_gap_for_single_window(); - const SettingsForSingleWindow border_for_single_window = settings_get_border_for_single_window(); - - const int gap_size = ( - n > 1 - || - gap_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_ALWAYS - || - ( - gap_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_NOT_IN_FULLSCREEN - && - !m->sel->isfullscreen - ) - ) ? settings_get_gap_size() : 0; - - const int border_width = ( - n > 1 - || - border_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_ALWAYS - || - ( - border_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_NOT_IN_FULLSCREEN - && - !m->sel->isfullscreen - ) - ) ? settings_get_border_width() : 0; + const int gap_size = helpers_gap_size(n, m->sel->isfullscreen); + const int border_width = helpers_border_width(n, m->sel->isfullscreen); const int top_left_half_gap = gap_size / 2; const int bottom_right_half_gap = gap_size - top_left_half_gap; @@ -123,28 +99,8 @@ centeredmaster(Monitor *m) void monocle(Monitor *m) { - const SettingsForSingleWindow gap_for_single_window = settings_get_gap_for_single_window(); - const SettingsForSingleWindow border_for_single_window = settings_get_border_for_single_window(); - - const int gap_size = ( - gap_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_ALWAYS - || - ( - gap_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_NOT_IN_FULLSCREEN - && - !m->sel->isfullscreen - ) - ) ? settings_get_gap_size() : 0; - - const int border_width = ( - border_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_ALWAYS - || - ( - border_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_NOT_IN_FULLSCREEN - && - !m->sel->isfullscreen - ) - ) ? settings_get_border_width() : 0; + const int gap_size = helpers_gap_size(1, m->sel->isfullscreen); + const int border_width = helpers_border_width(1, m->sel->isfullscreen); for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next)) { resize( @@ -166,32 +122,8 @@ tile(Monitor *m) for (Client *c = nexttiled(m->clients); c; c = nexttiled(c->next), ++n); if (n == 0) return; - const SettingsForSingleWindow gap_for_single_window = settings_get_gap_for_single_window(); - const SettingsForSingleWindow border_for_single_window = settings_get_border_for_single_window(); - - const int gap_size = ( - n > 1 - || - gap_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_ALWAYS - || - ( - gap_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_NOT_IN_FULLSCREEN - && - !m->sel->isfullscreen - ) - ) ? settings_get_gap_size() : 0; - - const int border_width = ( - n > 1 - || - border_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_ALWAYS - || - ( - border_for_single_window == SETTINGS_FOR_SINGLE_WINDOW_NOT_IN_FULLSCREEN - && - !m->sel->isfullscreen - ) - ) ? settings_get_border_width() : 0; + const int gap_size = helpers_gap_size(n, m->sel->isfullscreen); + const int border_width = helpers_border_width(n, m->sel->isfullscreen); const int top_left_half_gap = gap_size / 2; const int bottom_right_half_gap = gap_size - top_left_half_gap; diff --git a/helpers.c b/helpers.c new file mode 100644 index 0000000..8f4b33e --- /dev/null +++ b/helpers.c @@ -0,0 +1,41 @@ +#include "helpers.h" + +#include "settings.h" + +int helpers_gap_size( + const unsigned int displayed_clients, + const bool selected_is_fullscreen +) { + const SettingsForSingleWindow gap_for_single_window = settings_get_gap_for_single_window(); + const int gap_size = settings_get_gap_size(); + + if (displayed_clients > 1) return gap_size; + + switch (gap_for_single_window) { + case SETTINGS_FOR_SINGLE_WINDOW_NEVER: + return 0; + case SETTINGS_FOR_SINGLE_WINDOW_ALWAYS: + return gap_size; + case SETTINGS_FOR_SINGLE_WINDOW_NOT_IN_FULLSCREEN: + return selected_is_fullscreen ? 0 : gap_size; + } +} + +int helpers_border_width( + const unsigned int displayed_clients, + const bool selected_is_fullscreen +) { + const SettingsForSingleWindow border_for_single_window = settings_get_border_for_single_window(); + const int border_width = settings_get_border_width(); + + if (displayed_clients > 1) return border_width; + + switch (border_for_single_window) { + case SETTINGS_FOR_SINGLE_WINDOW_NEVER: + return 0; + case SETTINGS_FOR_SINGLE_WINDOW_ALWAYS: + return border_width; + case SETTINGS_FOR_SINGLE_WINDOW_NOT_IN_FULLSCREEN: + return selected_is_fullscreen ? 0 : border_width; + } +} diff --git a/helpers.h b/helpers.h new file mode 100644 index 0000000..9ace611 --- /dev/null +++ b/helpers.h @@ -0,0 +1,9 @@ +#ifndef _HELPERS_H +#define _HELPERS_H + +#include + +int helpers_gap_size(unsigned int displayed_clients, bool selected_is_fullscreen); +int helpers_border_width(unsigned int displayed_clients, bool selected_is_fullscreen); + +#endif // _HELPERS_H