Rewrite main-vertical and horizontal to use the common spread out code and to

handle the case where the panes won't fit into the existing window size.
This commit is contained in:
Nicholas Marriott 2019-04-09 21:15:00 +01:00
parent 0cbccc90ab
commit 031e9bc854

View File

@ -184,9 +184,8 @@ static void
layout_set_main_h(struct window *w) layout_set_main_h(struct window *w)
{ {
struct window_pane *wp; struct window_pane *wp;
struct layout_cell *lc, *lcmain, *lcrow, *lcchild; struct layout_cell *lc, *lcmain, *lcother, *lcchild;
u_int n, mainheight, otherheight, width, height; u_int n, mainh, otherh, sx;
u_int used, i, j, columns, rows, totalrows;
layout_print_cell(w->layout_root, __func__, 1); layout_print_cell(w->layout_root, __func__, 1);
@ -196,103 +195,57 @@ layout_set_main_h(struct window *w)
return; return;
n--; /* take off main pane */ n--; /* take off main pane */
/* How many rows and columns will be needed, not counting main? */ /* Get the main pane height and work out the other pane height. */
columns = (w->sx + 1) / (PANE_MINIMUM + 1); /* maximum columns */ mainh = options_get_number(w->options, "main-pane-height");
if (columns == 0) if (mainh + PANE_MINIMUM + 1 >= w->sy) {
columns = 1; if (w->sy <= PANE_MINIMUM + 1 + PANE_MINIMUM)
rows = 1 + (n - 1) / columns; mainh = PANE_MINIMUM;
columns = 1 + (n - 1) / rows;
width = (w->sx - (n - 1)) / columns;
/* Get the main pane height and add one for separator line. */
mainheight = options_get_number(w->options, "main-pane-height") + 1;
/* Get the optional other pane height and add one for separator line. */
otherheight = options_get_number(w->options, "other-pane-height") + 1;
/*
* If an other pane height was specified, honour it so long as it
* doesn't shrink the main height to less than the main-pane-height
*/
if (otherheight > 1 && w->sy - otherheight > mainheight)
mainheight = w->sy - otherheight;
if (mainheight < PANE_MINIMUM + 1)
mainheight = PANE_MINIMUM + 1;
/* Try and make everything fit. */
totalrows = rows * (PANE_MINIMUM + 1) - 1;
if (mainheight + totalrows > w->sy) {
if (totalrows + PANE_MINIMUM + 1 > w->sy)
mainheight = PANE_MINIMUM + 2;
else else
mainheight = w->sy - totalrows; mainh = w->sy - (PANE_MINIMUM + 1);
height = PANE_MINIMUM; otherh = PANE_MINIMUM;
} else } else {
height = (w->sy - mainheight - (rows - 1)) / rows; otherh = options_get_number(w->options, "other-pane-height");
if (otherh == 0)
otherh = w->sy - mainh;
else if (otherh > w->sy || w->sy - otherh < mainh)
otherh = w->sy - mainh;
else
mainh = w->sy - otherh;
}
/* Work out what height is needed. */
sx = (n * (PANE_MINIMUM + 1)) - 1;
if (sx < w->sx)
sx = w->sx;
/* Free old tree and create a new root. */ /* Free old tree and create a new root. */
layout_free(w); layout_free(w);
lc = w->layout_root = layout_create_cell(NULL); lc = w->layout_root = layout_create_cell(NULL);
layout_set_size(lc, w->sx, mainheight + rows * (height + 1) - 1, 0, 0); layout_set_size(lc, sx, mainh + otherh + 1, 0, 0);
layout_make_node(lc, LAYOUT_TOPBOTTOM); layout_make_node(lc, LAYOUT_TOPBOTTOM);
/* Create the main pane. */ /* Create the main pane. */
lcmain = layout_create_cell(lc); lcmain = layout_create_cell(lc);
layout_set_size(lcmain, w->sx, mainheight - 1, 0, 0); layout_set_size(lcmain, sx, mainh, 0, 0);
layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes)); layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes));
TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry); TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry);
/* Create a grid of the remaining cells. */ /* Create the other pane. */
wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry); lcother = layout_create_cell(lc);
for (j = 0; j < rows; j++) { layout_set_size(lcother, sx, otherh, 0, 0);
/* If this is the last cell, all done. */ layout_make_node(lcother, LAYOUT_LEFTRIGHT);
if (wp == NULL) TAILQ_INSERT_TAIL(&lc->cells, lcother, entry);
break;
/* Create the new row. */ /* Add the remaining panes as children. */
lcrow = layout_create_cell(lc); TAILQ_FOREACH(wp, &w->panes, entry) {
layout_set_size(lcrow, w->sx, height, 0, 0); if (wp == TAILQ_FIRST(&w->panes))
TAILQ_INSERT_TAIL(&lc->cells, lcrow, entry);
/* If only one column, just use the row directly. */
if (columns == 1) {
layout_make_leaf(lcrow, wp);
wp = TAILQ_NEXT(wp, entry);
continue; continue;
} lcchild = layout_create_cell(lc);
layout_set_size(lcchild, PANE_MINIMUM, otherh, 0, 0);
/* Add in the columns. */ layout_make_leaf(lcchild, wp);
layout_make_node(lcrow, LAYOUT_LEFTRIGHT); TAILQ_INSERT_TAIL(&lcother->cells, lcchild, entry);
for (i = 0; i < columns; i++) {
/* Create and add a pane cell. */
lcchild = layout_create_cell(lcrow);
layout_set_size(lcchild, width, height, 0, 0);
layout_make_leaf(lcchild, wp);
TAILQ_INSERT_TAIL(&lcrow->cells, lcchild, entry);
/* Move to the next cell. */
if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
break;
}
/* Adjust the row to fit the full width if necessary. */
if (i == columns)
i--;
used = ((i + 1) * (width + 1)) - 1;
if (w->sx <= used)
continue;
lcchild = TAILQ_LAST(&lcrow->cells, layout_cells);
layout_resize_adjust(w, lcchild, LAYOUT_LEFTRIGHT,
w->sx - used);
}
/* Adjust the last row height to fit if necessary. */
used = mainheight + (rows * height) + rows - 1;
if (w->sy > used) {
lcrow = TAILQ_LAST(&lc->cells, layout_cells);
layout_resize_adjust(w, lcrow, LAYOUT_TOPBOTTOM,
w->sy - used);
} }
layout_spread_cell(w, lcother);
/* Fix cell offsets. */ /* Fix cell offsets. */
layout_fix_offsets(lc); layout_fix_offsets(lc);
@ -309,9 +262,8 @@ static void
layout_set_main_v(struct window *w) layout_set_main_v(struct window *w)
{ {
struct window_pane *wp; struct window_pane *wp;
struct layout_cell *lc, *lcmain, *lccolumn, *lcchild; struct layout_cell *lc, *lcmain, *lcother, *lcchild;
u_int n, mainwidth, otherwidth, width, height; u_int n, mainw, otherw, sy;
u_int used, i, j, columns, rows, totalcolumns;
layout_print_cell(w->layout_root, __func__, 1); layout_print_cell(w->layout_root, __func__, 1);
@ -321,103 +273,57 @@ layout_set_main_v(struct window *w)
return; return;
n--; /* take off main pane */ n--; /* take off main pane */
/* How many rows and columns will be needed, not counting main? */ /* Get the main pane width and work out the other pane width. */
rows = (w->sy + 1) / (PANE_MINIMUM + 1); /* maximum rows */ mainw = options_get_number(w->options, "main-pane-width");
if (rows == 0) if (mainw + PANE_MINIMUM + 1 >= w->sx) {
rows = 1; if (w->sx <= PANE_MINIMUM + 1 + PANE_MINIMUM)
columns = 1 + (n - 1) / rows; mainw = PANE_MINIMUM;
rows = 1 + (n - 1) / columns;
height = (w->sy - (n - 1)) / rows;
/* Get the main pane width and add one for separator line. */
mainwidth = options_get_number(w->options, "main-pane-width") + 1;
/* Get the optional other pane width and add one for separator line. */
otherwidth = options_get_number(w->options, "other-pane-width") + 1;
/*
* If an other pane width was specified, honour it so long as it
* doesn't shrink the main width to less than the main-pane-width
*/
if (otherwidth > 1 && w->sx - otherwidth > mainwidth)
mainwidth = w->sx - otherwidth;
if (mainwidth < PANE_MINIMUM + 1)
mainwidth = PANE_MINIMUM + 1;
/* Try and make everything fit. */
totalcolumns = columns * (PANE_MINIMUM + 1) - 1;
if (mainwidth + totalcolumns > w->sx) {
if (totalcolumns + PANE_MINIMUM + 1 > w->sx)
mainwidth = PANE_MINIMUM + 2;
else else
mainwidth = w->sx - totalcolumns; mainw = w->sx - (PANE_MINIMUM + 1);
width = PANE_MINIMUM; otherw = PANE_MINIMUM;
} else } else {
width = (w->sx - mainwidth - (columns - 1)) / columns; otherw = options_get_number(w->options, "other-pane-width");
if (otherw == 0)
otherw = w->sx - mainw;
else if (otherw > w->sx || w->sx - otherw < mainw)
otherw = w->sx - mainw;
else
mainw = w->sx - otherw;
}
/* Work out what height is needed. */
sy = (n * (PANE_MINIMUM + 1)) - 1;
if (sy < w->sy)
sy = w->sy;
/* Free old tree and create a new root. */ /* Free old tree and create a new root. */
layout_free(w); layout_free(w);
lc = w->layout_root = layout_create_cell(NULL); lc = w->layout_root = layout_create_cell(NULL);
layout_set_size(lc, mainwidth + columns * (width + 1) - 1, w->sy, 0, 0); layout_set_size(lc, mainw + otherw + 1, sy, 0, 0);
layout_make_node(lc, LAYOUT_LEFTRIGHT); layout_make_node(lc, LAYOUT_LEFTRIGHT);
/* Create the main pane. */ /* Create the main pane. */
lcmain = layout_create_cell(lc); lcmain = layout_create_cell(lc);
layout_set_size(lcmain, mainwidth - 1, w->sy, 0, 0); layout_set_size(lcmain, mainw, sy, 0, 0);
layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes)); layout_make_leaf(lcmain, TAILQ_FIRST(&w->panes));
TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry); TAILQ_INSERT_TAIL(&lc->cells, lcmain, entry);
/* Create a grid of the remaining cells. */ /* Create the other pane. */
wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry); lcother = layout_create_cell(lc);
for (j = 0; j < columns; j++) { layout_set_size(lcother, otherw, sy, 0, 0);
/* If this is the last cell, all done. */ layout_make_node(lcother, LAYOUT_TOPBOTTOM);
if (wp == NULL) TAILQ_INSERT_TAIL(&lc->cells, lcother, entry);
break;
/* Create the new column. */ /* Add the remaining panes as children. */
lccolumn = layout_create_cell(lc); TAILQ_FOREACH(wp, &w->panes, entry) {
layout_set_size(lccolumn, width, w->sy, 0, 0); if (wp == TAILQ_FIRST(&w->panes))
TAILQ_INSERT_TAIL(&lc->cells, lccolumn, entry);
/* If only one row, just use the row directly. */
if (rows == 1) {
layout_make_leaf(lccolumn, wp);
wp = TAILQ_NEXT(wp, entry);
continue; continue;
} lcchild = layout_create_cell(lc);
layout_set_size(lcchild, otherw, PANE_MINIMUM, 0, 0);
/* Add in the rows. */ layout_make_leaf(lcchild, wp);
layout_make_node(lccolumn, LAYOUT_TOPBOTTOM); TAILQ_INSERT_TAIL(&lcother->cells, lcchild, entry);
for (i = 0; i < rows; i++) {
/* Create and add a pane cell. */
lcchild = layout_create_cell(lccolumn);
layout_set_size(lcchild, width, height, 0, 0);
layout_make_leaf(lcchild, wp);
TAILQ_INSERT_TAIL(&lccolumn->cells, lcchild, entry);
/* Move to the next cell. */
if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
break;
}
/* Adjust the column to fit the full height if necessary. */
if (i == rows)
i--;
used = ((i + 1) * (height + 1)) - 1;
if (w->sy <= used)
continue;
lcchild = TAILQ_LAST(&lccolumn->cells, layout_cells);
layout_resize_adjust(w, lcchild, LAYOUT_TOPBOTTOM,
w->sy - used);
}
/* Adjust the last column width to fit if necessary. */
used = mainwidth + (columns * width) + columns - 1;
if (w->sx > used) {
lccolumn = TAILQ_LAST(&lc->cells, layout_cells);
layout_resize_adjust(w, lccolumn, LAYOUT_LEFTRIGHT,
w->sx - used);
} }
layout_spread_cell(w, lcother);
/* Fix cell offsets. */ /* Fix cell offsets. */
layout_fix_offsets(lc); layout_fix_offsets(lc);