Minor tiding of screen-redraw.c.

This commit is contained in:
Nicholas Marriott 2024-10-28 09:26:15 +00:00
parent 78b9096c38
commit fa007e43d5

View File

@ -114,81 +114,16 @@ screen_redraw_two_panes(struct window *w, int direction)
return (1); return (1);
} }
/*
* This is how panes are referenced in screen_redraw_pane_border:
*
* sb_pos == PANE_SCROLLBARS_RIGHT:
*
* .0................o..............X.
* :a zSSSBa zSSS: <- dotted border is outside of tmux,
* |___wp->sx___|_| |___wp->sx___|_| this is the terminal window
* | sb_w^ | sb_w^
* ^wp->xoff ^wp->xoff
*
* sb_pos == PANE_SCROLLBARS_LEFT:
*
* .0...................o...........X.
* :SSSa zBSSSa z: <- Notes, see below
* |_||___wp->sx___||_||___wp->sx___|
* sb_w^ | sb_w^ |
* ^wp->xoff ^wp->xoff
*
* Terminal window tmux runs inide is X+1 characters wide
* 0 is the first column of the terminal
* X is the last column of the terminal, so 0-79 is 80 chars wide (px is [0..X])
* o is the offset of righthand pane in diagram (not a variable)
* a is the first column of each pane
* z is the last column of each pane
* wp->xoff (rel to terminal) is character offset of the pane,
* it will be 0 for left pane and o for right pane above.
* wp->sx is size of pane (usable width of pane in characters)
* sb_w is the width of the scrollbar in characters
* S is the scrollbar, (in this example sb_w would be 3)
* B is the pane border, if there is a right and left and they are superimposed
*
* The vertical situation looks like this:
* .....
* :Border (if pane-border-status==top)
* 0: | <- wp->yoff
* : |
* : |
* : wp->sy
* : |
* : |
* :Border
* o: | <- wp->yoff
* : |
* : |
* : wp->sy
* : |
* : |
* :Border (if pane-border-status==bottom)
* Y:Status line(s)
* .....
*
* 0 is first line of terminal
* Y is last line of terminal (py is [0..Y])
* o is the offset of the lower window in diagram (not a variable)
* wp->yoff (rel to terminal) is first line of pane
* wp->sy is usable size of pane in lines (6 in above example)
* There can be zero to 5 status lines
* Panes can have an optional border status
*
*/
/* Check if cell is on the border of a pane. */ /* Check if cell is on the border of a pane. */
static enum screen_redraw_border_type static enum screen_redraw_border_type
screen_redraw_pane_border(struct screen_redraw_ctx *ctx, struct window_pane *wp, screen_redraw_pane_border(struct screen_redraw_ctx *ctx, struct window_pane *wp,
u_int px, u_int py) u_int px, u_int py)
{ {
struct options *oo = wp->window->options; struct options *oo = wp->window->options;
int hsplit = 0;
int vsplit = 0;
u_int ex = wp->xoff + wp->sx, ey = wp->yoff + wp->sy; u_int ex = wp->xoff + wp->sx, ey = wp->yoff + wp->sy;
int pane_status = ctx->pane_status; int hsplit = 0, vsplit = 0, pane_status = ctx->pane_status;
int pane_scrollbars = ctx->pane_scrollbars; int pane_scrollbars = ctx->pane_scrollbars, sb_w = 0;
int sb_pos = ctx->pane_scrollbars_pos; int sb_pos = ctx->pane_scrollbars_pos;
int sb_w = PANE_SCROLLBARS_WIDTH;
/* Inside pane. */ /* Inside pane. */
if (px >= wp->xoff && px < ex && py >= wp->yoff && py < ey) if (px >= wp->xoff && px < ex && py >= wp->yoff && py < ey)
@ -203,76 +138,53 @@ screen_redraw_pane_border(struct screen_redraw_ctx *ctx, struct window_pane *wp,
break; break;
} }
/* Are scrollbars enabled? */
if (pane_scrollbars == PANE_SCROLLBARS_ALWAYS || if (pane_scrollbars == PANE_SCROLLBARS_ALWAYS ||
(pane_scrollbars == PANE_SCROLLBARS_MODAL && (pane_scrollbars == PANE_SCROLLBARS_MODAL &&
window_pane_mode(wp) != WINDOW_PANE_NO_MODE)) { window_pane_mode(wp) != WINDOW_PANE_NO_MODE))
pane_scrollbars = 1; sb_w = PANE_SCROLLBARS_WIDTH;
} else {
sb_w = 0;
}
/* Left/right borders. /*
* * Left/right borders. The wp->sy / 2 test is to colour only half the
* The wp->sy / 2 test is used to show half the active * active window's border when there are two panes.
* window's border and the other half the other
* window. It's easy to do this when 1 window
* vertically, so this feature only works when there's
* exactly 2 side-by-side panes. This could be
* genralised in the future.
*/ */
if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= ey) { if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= ey) {
if (sb_pos == PANE_SCROLLBARS_LEFT) { if (sb_pos == PANE_SCROLLBARS_LEFT) {
if (wp->xoff - sb_w == 0 && if (wp->xoff - sb_w == 0 && px == wp->sx + sb_w)
px == wp->sx + sb_w)
if (!hsplit || (hsplit && py <= wp->sy / 2)) if (!hsplit || (hsplit && py <= wp->sy / 2))
return (SCREEN_REDRAW_BORDER_RIGHT); return (SCREEN_REDRAW_BORDER_RIGHT);
if (wp->xoff - sb_w != 0 && if (wp->xoff - sb_w != 0 && px == wp->xoff - sb_w - 1)
px == wp->xoff - sb_w - 1)
if (!hsplit || (hsplit && py > wp->sy / 2)) if (!hsplit || (hsplit && py > wp->sy / 2))
return (SCREEN_REDRAW_BORDER_LEFT); return (SCREEN_REDRAW_BORDER_LEFT);
} else { } else { /* sb_pos == PANE_SCROLLBARS_RIGHT */
/* sb_pos == PANE_SCROLLBARS_RIGHT */ if (wp->xoff == 0 && px == wp->sx + sb_w)
if (wp->xoff == 0 &&
px == wp->sx + sb_w)
if (!hsplit || (hsplit && py <= wp->sy / 2)) if (!hsplit || (hsplit && py <= wp->sy / 2))
return (SCREEN_REDRAW_BORDER_RIGHT); return (SCREEN_REDRAW_BORDER_RIGHT);
if (wp->xoff != 0 && if (wp->xoff != 0 && px == wp->xoff - 1)
px == wp->xoff - 1)
if (!hsplit || (hsplit && py > wp->sy / 2)) if (!hsplit || (hsplit && py > wp->sy / 2))
return (SCREEN_REDRAW_BORDER_LEFT); return (SCREEN_REDRAW_BORDER_LEFT);
} }
} }
/* Top/bottom borders. */ /* Top/bottom borders. */
if (vsplit && if (vsplit && pane_status == PANE_STATUS_OFF && sb_w == 0) {
pane_status == PANE_STATUS_OFF && if (wp->yoff == 0 && py == wp->sy && px <= wp->sx / 2)
pane_scrollbars == PANE_SCROLLBARS_OFF) {
if (wp->yoff == 0 &&
py == wp->sy &&
px <= wp->sx / 2)
return (SCREEN_REDRAW_BORDER_BOTTOM); return (SCREEN_REDRAW_BORDER_BOTTOM);
if (wp->yoff != 0 && if (wp->yoff != 0 && py == wp->yoff - 1 && px > wp->sx / 2)
py == wp->yoff - 1 &&
px > wp->sx / 2)
return (SCREEN_REDRAW_BORDER_TOP); return (SCREEN_REDRAW_BORDER_TOP);
} else { } else {
if (sb_pos == PANE_SCROLLBARS_LEFT) { if (sb_pos == PANE_SCROLLBARS_LEFT) {
if ((wp->xoff - sb_w == 0 || px >= wp->xoff - sb_w) && if ((wp->xoff - sb_w == 0 || px >= wp->xoff - sb_w) &&
(px <= ex || (px <= ex || (sb_w != 0 && px - 1 == ex))) {
(pane_scrollbars && px - 1 == ex))) { if (wp->yoff != 0 && py == wp->yoff - 1)
if (wp->yoff != 0 &&
py == wp->yoff - 1)
return (SCREEN_REDRAW_BORDER_TOP); return (SCREEN_REDRAW_BORDER_TOP);
if (py == ey) if (py == ey)
return (SCREEN_REDRAW_BORDER_BOTTOM); return (SCREEN_REDRAW_BORDER_BOTTOM);
} }
} else { } else { /* sb_pos == PANE_SCROLLBARS_RIGHT */
/* sb_pos == PANE_SCROLLBARS_RIGHT */
if ((wp->xoff == 0 || px >= wp->xoff) && if ((wp->xoff == 0 || px >= wp->xoff) &&
(px <= ex || (px <= ex || (sb_w != 0 && px - 1 == ex))) {
(pane_scrollbars && px - 1 == ex))) { if (wp->yoff != 0 && py == wp->yoff - 1)
if (wp->yoff != 0 &&
py == wp->yoff - 1)
return (SCREEN_REDRAW_BORDER_TOP); return (SCREEN_REDRAW_BORDER_TOP);
if (py == ey) if (py == ey)
return (SCREEN_REDRAW_BORDER_BOTTOM); return (SCREEN_REDRAW_BORDER_BOTTOM);
@ -413,9 +325,8 @@ screen_redraw_check_cell(struct screen_redraw_ctx *ctx, u_int px, u_int py,
struct window_pane *wp, *active; struct window_pane *wp, *active;
int pane_status = ctx->pane_status; int pane_status = ctx->pane_status;
u_int sx = w->sx, sy = w->sy; u_int sx = w->sx, sy = w->sy;
int border; int border, pane_scrollbars = ctx->pane_scrollbars;
u_int right, line; u_int right, line;
int pane_scrollbars = ctx->pane_scrollbars;
int sb_pos = ctx->pane_scrollbars_pos; int sb_pos = ctx->pane_scrollbars_pos;
int sb_w = PANE_SCROLLBARS_WIDTH; int sb_w = PANE_SCROLLBARS_WIDTH;
@ -464,24 +375,22 @@ screen_redraw_check_cell(struct screen_redraw_ctx *ctx, u_int px, u_int py,
else else
line = wp->yoff + wp->sy; line = wp->yoff + wp->sy;
/* check if py could lie within a scroller /*
* if pane at the top then py==0 included * Check if py could lie within a scrollbar. If the
* if pane not at the top, then yoff to yoff+sy * pane is at the top then py == 0 to sy; if the pane
* is not at the top, then yoff to yoff + sy.
*/ */
if ((pane_status && py != line) || if ((pane_status && py != line) ||
(wp->yoff == 0 && py < wp->sy) || (wp->yoff == 0 && py < wp->sy) ||
(py >= wp->yoff && py < wp->yoff + wp->sy)) { (py >= wp->yoff && py < wp->yoff + wp->sy)) {
/* Check if px lies within a scrollbar. */
/* check if px lies within a scroller
*/
if ((sb_pos == PANE_SCROLLBARS_RIGHT && if ((sb_pos == PANE_SCROLLBARS_RIGHT &&
(px >= wp->xoff + wp->sx && (px >= wp->xoff + wp->sx &&
px < wp->xoff + wp->sx + sb_w)) || px < wp->xoff + wp->sx + sb_w)) ||
(sb_pos == PANE_SCROLLBARS_LEFT && (sb_pos == PANE_SCROLLBARS_LEFT &&
(px >= wp->xoff - sb_w && (px >= wp->xoff - sb_w &&
px < wp->xoff))) { px < wp->xoff)))
return (CELL_SCROLLBAR); return (CELL_SCROLLBAR);
}
} }
} }
@ -702,8 +611,8 @@ screen_redraw_set_context(struct client *c, struct screen_redraw_ctx *ctx)
ctx->pane_lines = options_get_number(wo, "pane-border-lines"); ctx->pane_lines = options_get_number(wo, "pane-border-lines");
ctx->pane_scrollbars = options_get_number(wo, "pane-scrollbars"); ctx->pane_scrollbars = options_get_number(wo, "pane-scrollbars");
ctx->pane_scrollbars_pos = ctx->pane_scrollbars_pos = options_get_number(wo,
options_get_number(wo, "pane-scrollbars-position"); "pane-scrollbars-position");
tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy); tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy);
@ -760,9 +669,8 @@ screen_redraw_screen(struct client *c)
void void
screen_redraw_pane(struct client *c, struct window_pane *wp) screen_redraw_pane(struct client *c, struct window_pane *wp)
{ {
struct screen_redraw_ctx ctx; struct screen_redraw_ctx ctx;
int pane_scrollbars; int pane_scrollbars, mode;
int pane_mode;
if (!window_pane_visible(wp)) if (!window_pane_visible(wp))
return; return;
@ -773,24 +681,18 @@ screen_redraw_pane(struct client *c, struct window_pane *wp)
screen_redraw_draw_pane(&ctx, wp); screen_redraw_draw_pane(&ctx, wp);
/* Redraw scrollbar if needed. Always redraw scrollbar /*
* in a mode because if redrawing a pane, it's because * Redraw scrollbar if needed. Always redraw scrollbar in a mode because
* pane has scrolled. * if redrawing a pane, it's because pane has scrolled.
*/ */
pane_mode = window_pane_mode(wp); mode = window_pane_mode(wp);
if (wp->flags & PANE_REDRAWSCROLLBAR || mode != WINDOW_PANE_NO_MODE) {
if (wp->flags & PANE_REDRAWSCROLLBAR ||
pane_mode != WINDOW_PANE_NO_MODE) {
pane_scrollbars = ctx.pane_scrollbars; pane_scrollbars = ctx.pane_scrollbars;
if (pane_scrollbars == PANE_SCROLLBARS_MODAL && if (pane_scrollbars == PANE_SCROLLBARS_MODAL &&
pane_mode == WINDOW_PANE_NO_MODE) { mode == WINDOW_PANE_NO_MODE)
pane_scrollbars = 0; pane_scrollbars = PANE_SCROLLBARS_OFF;
} if (pane_scrollbars != PANE_SCROLLBARS_OFF)
if (pane_scrollbars != 0) {
screen_redraw_draw_pane_scrollbar(&ctx, wp); screen_redraw_draw_pane_scrollbar(&ctx, wp);
}
} }
tty_reset(&c->tty); tty_reset(&c->tty);
@ -813,13 +715,10 @@ screen_redraw_draw_borders_style(struct screen_redraw_ctx *ctx, u_int x,
wp->border_gc_set = 1; wp->border_gc_set = 1;
ft = format_create_defaults(NULL, c, s, s->curw, wp); ft = format_create_defaults(NULL, c, s, s->curw, wp);
if (screen_redraw_check_is(ctx, x, y, active)) { if (screen_redraw_check_is(ctx, x, y, active))
log_debug("%s: %s y:%u active", __func__, c->name, y); style_apply(&wp->border_gc, oo, "pane-active-border-style", ft);
style_apply(&wp->border_gc, oo, "pane-active-border-style", ft); else
} else {
log_debug("%s: %s y:%u", __func__, c->name, y);
style_apply(&wp->border_gc, oo, "pane-border-style", ft); style_apply(&wp->border_gc, oo, "pane-border-style", ft);
}
format_free(ft); format_free(ft);
return (&wp->border_gc); return (&wp->border_gc);
@ -840,8 +739,7 @@ screen_redraw_draw_borders_cell(struct screen_redraw_ctx *ctx, u_int i, u_int j)
const struct grid_cell *tmp; const struct grid_cell *tmp;
struct overlay_ranges r; struct overlay_ranges r;
u_int cell_type, x = ctx->ox + i, y = ctx->oy + j; u_int cell_type, x = ctx->ox + i, y = ctx->oy + j;
int arrows = 0, border; int arrows = 0, border, isolates;
int isolates;
if (c->overlay_check != NULL) { if (c->overlay_check != NULL) {
c->overlay_check(c, c->overlay_data, x, y, 1, &r); c->overlay_check(c, c->overlay_data, x, y, 1, &r);
@ -1051,7 +949,7 @@ screen_redraw_draw_pane_scrollbars(struct screen_redraw_ctx *ctx)
log_debug("%s: %s @%u", __func__, c->name, w->id); log_debug("%s: %s @%u", __func__, c->name, w->id);
TAILQ_FOREACH(wp, &w->panes, entry) { TAILQ_FOREACH(wp, &w->panes, entry) {
switch(ctx->pane_scrollbars) { switch (ctx->pane_scrollbars) {
case PANE_SCROLLBARS_OFF: case PANE_SCROLLBARS_OFF:
return; return;
case PANE_SCROLLBARS_MODAL: case PANE_SCROLLBARS_MODAL:
@ -1066,17 +964,7 @@ screen_redraw_draw_pane_scrollbars(struct screen_redraw_ctx *ctx)
} }
} }
/* Draw scrollbar /* Draw pane scrollbar. */
*
* sb_x and sb_y are the upper left character of the scrollbar
* sb_h is scrollbar height, the number of lines in the scrollbar
* slider_h is the height of the slider in lines
* slider_y is the line within sb_h of the current vertical position
* The height and position of the slider are proportional to the number of
* lines in the scroll back buffer + number of lines on the screen
* (total_height) and the percentage of the number of visible lines to the
* total height (percent_view).
*/
void void
screen_redraw_draw_pane_scrollbar(struct screen_redraw_ctx *ctx, screen_redraw_draw_pane_scrollbar(struct screen_redraw_ctx *ctx,
struct window_pane *wp) struct window_pane *wp)
@ -1084,38 +972,28 @@ screen_redraw_draw_pane_scrollbar(struct screen_redraw_ctx *ctx,
struct client *c = ctx->c; struct client *c = ctx->c;
struct screen *s = wp->screen; struct screen *s = wp->screen;
double percent_view; double percent_view;
u_int sb = ctx->pane_scrollbars; u_int sb = ctx->pane_scrollbars, total_height, sb_h = wp->sy;
u_int sb_pos = ctx->pane_scrollbars_pos; u_int sb_pos = ctx->pane_scrollbars_pos, slider_h, slider_y;
u_int sb_w = PANE_SCROLLBARS_WIDTH; u_int sb_w = PANE_SCROLLBARS_WIDTH, cm_y, cm_size;
u_int sb_x; u_int sb_x, sb_y = wp->yoff; /* upper left of scrollbar */
u_int sb_y = wp->yoff;
u_int sb_h = wp->sy;
u_int slider_h;
u_int slider_y;
u_int total_height;
if (window_pane_mode(wp) == WINDOW_PANE_NO_MODE) { if (window_pane_mode(wp) == WINDOW_PANE_NO_MODE) {
/* not in a mode */ if (sb == PANE_SCROLLBARS_MODAL)
if (sb == PANE_SCROLLBARS_MODAL) {
return; return;
} /* Show slider at the bottom of the scrollbar. */
/* show slider at the bottom of the scrollbar */
total_height = screen_size_y(s) + screen_hsize(s); total_height = screen_size_y(s) + screen_hsize(s);
percent_view = (double)sb_h / total_height; percent_view = (double)sb_h / total_height;
slider_h = (u_int)((double)sb_h * percent_view); slider_h = (double)sb_h * percent_view;
slider_y = sb_h - slider_h; slider_y = sb_h - slider_h;
} else { } else {
/* copy-mode or view-mode */ if (TAILQ_FIRST(&wp->modes) == NULL)
u_int cm_y_pos, cm_size; return;
if (window_copy_get_current_offset(wp, &cm_y, &cm_size) == 0)
if (TAILQ_FIRST(&wp->modes) == NULL ||
window_copy_get_current_offset(wp, &cm_y_pos, &cm_size) == 0)
return; return;
total_height = cm_size + sb_h; total_height = cm_size + sb_h;
percent_view = (double)sb_h / (cm_size + sb_h); percent_view = (double)sb_h / (cm_size + sb_h);
slider_h = (u_int)((double)sb_h * percent_view); slider_h = (double)sb_h * percent_view;
slider_y = (u_int)(sb_h + 1) * ((double)cm_y_pos / total_height); slider_y = (sb_h + 1) * ((double)cm_y / total_height);
} }
if (sb_pos == PANE_SCROLLBARS_LEFT) if (sb_pos == PANE_SCROLLBARS_LEFT)
@ -1139,36 +1017,30 @@ screen_redraw_draw_scrollbar(struct client *c, struct window_pane *wp,
struct window *w = wp->window; struct window *w = wp->window;
struct tty *tty = &c->tty; struct tty *tty = &c->tty;
struct grid_cell gc; struct grid_cell gc;
u_int i, j;
int fg, bg; int fg, bg;
u_int pad_col = 0; u_int i, j, pad_col = 0;
u_int sb_w = PANE_SCROLLBARS_WIDTH; u_int sb_w = PANE_SCROLLBARS_WIDTH;
u_int sb_pad = PANE_SCROLLBARS_PADDING; u_int sb_pad = PANE_SCROLLBARS_PADDING;
log_debug("%s: scrollbar pos:%d w:%u @%u,%u h:%u eh:%u ep:%u",
__func__, sb_pos, sb_w, px, py, sb_h, slider_h, slider_y);
/* Set up default colour. */ /* Set up default colour. */
style_apply(&gc, w->options, "pane-scrollbars-style", NULL); style_apply(&gc, w->options, "pane-scrollbars-style", NULL);
fg = gc.fg; fg = gc.fg;
bg = gc.bg; bg = gc.bg;
utf8_set(&gc.data, ' '); utf8_set(&gc.data, ' ');
if (sb_pad) { if (sb_pad != 0) {
if (sb_pos == PANE_SCROLLBARS_RIGHT) if (sb_pos == PANE_SCROLLBARS_RIGHT)
pad_col = 0; pad_col = 0;
else else
pad_col = sb_w - 1; pad_col = sb_w - 1;
} }
gc.bg = bg;
for (i = 0; i < sb_w; i++) { for (i = 0; i < sb_w; i++) {
for (j = 0; j < sb_h; j++) { for (j = 0; j < sb_h; j++) {
tty_cursor(tty, px+i, py+j); tty_cursor(tty, px + i, py + j);
if (sb_pad && i == pad_col) {
if (sb_pad && i==pad_col) {
tty_cell(tty, &grid_default_cell, tty_cell(tty, &grid_default_cell,
&grid_default_cell, NULL, NULL); &grid_default_cell, NULL, NULL);
} else { } else {
if (j >= slider_y && j < slider_y + slider_h) { if (j >= slider_y && j < slider_y + slider_h) {
gc.bg = fg; gc.bg = fg;
@ -1178,7 +1050,7 @@ screen_redraw_draw_scrollbar(struct client *c, struct window_pane *wp,
gc.fg = fg; gc.fg = fg;
} }
tty_cell(tty, &gc, &grid_default_cell, NULL, tty_cell(tty, &gc, &grid_default_cell, NULL,
NULL); NULL);
} }
} }
} }