Support mouse on preview in tree mode.

This commit is contained in:
nicm 2017-11-03 17:02:33 +00:00
parent ba93a647f1
commit 50a5f84cb4
7 changed files with 140 additions and 35 deletions

2
grid.c
View File

@ -182,7 +182,7 @@ grid_clear_cell(struct grid *gd, u_int px, u_int py, u_int bg)
static int static int
grid_check_y(struct grid *gd, u_int py) grid_check_y(struct grid *gd, u_int py)
{ {
if ((py) >= (gd)->hsize + (gd)->sy) { if (py >= gd->hsize + gd->sy) {
log_debug("y out of range: %u", py); log_debug("y out of range: %u", py);
return (-1); return (-1);
} }

View File

@ -190,27 +190,6 @@ mode_tree_clear_tagged(struct mode_tree_list *mtl)
} }
} }
static void
mode_tree_set_current(struct mode_tree_data *mtd, uint64_t tag)
{
u_int i;
for (i = 0; i < mtd->line_size; i++) {
if (mtd->line_list[i].item->tag == tag)
break;
}
if (i != mtd->line_size) {
mtd->current = i;
if (mtd->current > mtd->height - 1)
mtd->offset = mtd->current - mtd->height + 1;
else
mtd->offset = 0;
} else {
mtd->current = 0;
mtd->offset = 0;
}
}
void void
mode_tree_up(struct mode_tree_data *mtd, int wrap) mode_tree_up(struct mode_tree_data *mtd, int wrap)
{ {
@ -248,6 +227,36 @@ mode_tree_get_current(struct mode_tree_data *mtd)
return (mtd->line_list[mtd->current].item->itemdata); return (mtd->line_list[mtd->current].item->itemdata);
} }
void
mode_tree_expand_current(struct mode_tree_data *mtd)
{
if (!mtd->line_list[mtd->current].item->expanded) {
mtd->line_list[mtd->current].item->expanded = 1;
mode_tree_build(mtd);
}
}
void
mode_tree_set_current(struct mode_tree_data *mtd, uint64_t tag)
{
u_int i;
for (i = 0; i < mtd->line_size; i++) {
if (mtd->line_list[i].item->tag == tag)
break;
}
if (i != mtd->line_size) {
mtd->current = i;
if (mtd->current > mtd->height - 1)
mtd->offset = mtd->current - mtd->height + 1;
else
mtd->offset = 0;
} else {
mtd->current = 0;
mtd->offset = 0;
}
}
u_int u_int
mode_tree_count_tagged(struct mode_tree_data *mtd) mode_tree_count_tagged(struct mode_tree_data *mtd)
{ {
@ -718,7 +727,7 @@ mode_tree_filter_free(void *data)
int int
mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key, mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key,
struct mouse_event *m) struct mouse_event *m, u_int *xp, u_int *yp)
{ {
struct mode_tree_line *line; struct mode_tree_line *line;
struct mode_tree_item *current, *parent; struct mode_tree_item *current, *parent;
@ -731,8 +740,13 @@ mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key,
*key = KEYC_NONE; *key = KEYC_NONE;
return (0); return (0);
} }
if (xp != NULL)
*xp = x;
if (yp != NULL)
*yp = y;
if (x > mtd->width || y > mtd->height) { if (x > mtd->width || y > mtd->height) {
*key = KEYC_NONE; if (!mtd->preview || y < mtd->height)
*key = KEYC_NONE;
return (0); return (0);
} }
if (mtd->offset + y < mtd->line_size) { if (mtd->offset + y < mtd->line_size) {

View File

@ -403,6 +403,8 @@ screen_write_fast_copy(struct screen_write_ctx *ctx, struct screen *src,
cy = s->cy; cy = s->cy;
for (yy = py; yy < py + ny; yy++) { for (yy = py; yy < py + ny; yy++) {
if (yy >= gd->hsize + gd->sy)
break;
cx = s->cx; cx = s->cx;
for (xx = px; xx < px + nx; xx++) { for (xx = px; xx < px + nx; xx++) {
if (xx >= gd->linedata[yy].cellsize) if (xx >= gd->linedata[yy].cellsize)

4
tmux.h
View File

@ -2232,6 +2232,8 @@ typedef int (*mode_tree_search_cb)(void *, void *, const char *);
typedef void (*mode_tree_each_cb)(void *, void *, struct client *, key_code); typedef void (*mode_tree_each_cb)(void *, void *, struct client *, key_code);
u_int mode_tree_count_tagged(struct mode_tree_data *); u_int mode_tree_count_tagged(struct mode_tree_data *);
void *mode_tree_get_current(struct mode_tree_data *); void *mode_tree_get_current(struct mode_tree_data *);
void mode_tree_expand_current(struct mode_tree_data *);
void mode_tree_set_current(struct mode_tree_data *, uint64_t);
void mode_tree_each_tagged(struct mode_tree_data *, mode_tree_each_cb, void mode_tree_each_tagged(struct mode_tree_data *, mode_tree_each_cb,
struct client *, key_code, int); struct client *, key_code, int);
void mode_tree_up(struct mode_tree_data *, int); void mode_tree_up(struct mode_tree_data *, int);
@ -2248,7 +2250,7 @@ struct mode_tree_item *mode_tree_add(struct mode_tree_data *,
void mode_tree_remove(struct mode_tree_data *, struct mode_tree_item *); void mode_tree_remove(struct mode_tree_data *, struct mode_tree_item *);
void mode_tree_draw(struct mode_tree_data *); void mode_tree_draw(struct mode_tree_data *);
int mode_tree_key(struct mode_tree_data *, struct client *, key_code *, int mode_tree_key(struct mode_tree_data *, struct client *, key_code *,
struct mouse_event *); struct mouse_event *, u_int *, u_int *);
void mode_tree_run_command(struct client *, struct cmd_find_state *, void mode_tree_run_command(struct client *, struct cmd_find_state *,
const char *, const char *); const char *, const char *);

View File

@ -344,7 +344,7 @@ window_buffer_key(struct window_pane *wp, struct client *c,
struct window_buffer_itemdata *item; struct window_buffer_itemdata *item;
int finished; int finished;
finished = mode_tree_key(mtd, c, &key, m); finished = mode_tree_key(mtd, c, &key, m, NULL, NULL);
switch (key) { switch (key) {
case 'd': case 'd':
item = mode_tree_get_current(mtd); item = mode_tree_get_current(mtd);

View File

@ -318,7 +318,7 @@ window_client_key(struct window_pane *wp, struct client *c,
struct window_client_itemdata *item; struct window_client_itemdata *item;
int finished; int finished;
finished = mode_tree_key(mtd, c, &key, m); finished = mode_tree_key(mtd, c, &key, m, NULL, NULL);
switch (key) { switch (key) {
case 'd': case 'd':
case 'x': case 'x':

View File

@ -104,6 +104,12 @@ struct window_tree_modedata {
enum window_tree_type type; enum window_tree_type type;
int offset; int offset;
int left;
int right;
u_int start;
u_int end;
u_int each;
}; };
static void static void
@ -545,17 +551,25 @@ window_tree_draw_session(struct window_tree_modedata *data, struct session *s,
return; return;
if (left) { if (left) {
data->left = cx + 2;
screen_write_cursormove(ctx, cx + 2, cy); screen_write_cursormove(ctx, cx + 2, cy);
screen_write_vline(ctx, sy, 0, 0); screen_write_vline(ctx, sy, 0, 0);
screen_write_cursormove(ctx, cx, cy + sy / 2); screen_write_cursormove(ctx, cx, cy + sy / 2);
screen_write_puts(ctx, &grid_default_cell, "<"); screen_write_puts(ctx, &grid_default_cell, "<");
} } else
data->left = -1;
if (right) { if (right) {
data->right = cx + sx - 3;
screen_write_cursormove(ctx, cx + sx - 3, cy); screen_write_cursormove(ctx, cx + sx - 3, cy);
screen_write_vline(ctx, sy, 0, 0); screen_write_vline(ctx, sy, 0, 0);
screen_write_cursormove(ctx, cx + sx - 1, cy + sy / 2); screen_write_cursormove(ctx, cx + sx - 1, cy + sy / 2);
screen_write_puts(ctx, &grid_default_cell, ">"); screen_write_puts(ctx, &grid_default_cell, ">");
} } else
data->right = -1;
data->start = start;
data->end = end;
data->each = each;
i = loop = 0; i = loop = 0;
RB_FOREACH(wl, winlinks, &s->windows) { RB_FOREACH(wl, winlinks, &s->windows) {
@ -670,17 +684,25 @@ window_tree_draw_window(struct window_tree_modedata *data, struct session *s,
return; return;
if (left) { if (left) {
data->left = cx + 2;
screen_write_cursormove(ctx, cx + 2, cy); screen_write_cursormove(ctx, cx + 2, cy);
screen_write_vline(ctx, sy, 0, 0); screen_write_vline(ctx, sy, 0, 0);
screen_write_cursormove(ctx, cx, cy + sy / 2); screen_write_cursormove(ctx, cx, cy + sy / 2);
screen_write_puts(ctx, &grid_default_cell, "<"); screen_write_puts(ctx, &grid_default_cell, "<");
} } else
data->left = -1;
if (right) { if (right) {
data->right = cx + sx - 3;
screen_write_cursormove(ctx, cx + sx - 3, cy); screen_write_cursormove(ctx, cx + sx - 3, cy);
screen_write_vline(ctx, sy, 0, 0); screen_write_vline(ctx, sy, 0, 0);
screen_write_cursormove(ctx, cx + sx - 1, cy + sy / 2); screen_write_cursormove(ctx, cx + sx - 1, cy + sy / 2);
screen_write_puts(ctx, &grid_default_cell, ">"); screen_write_puts(ctx, &grid_default_cell, ">");
} } else
data->right = -1;
data->start = start;
data->end = end;
data->each = each;
i = loop = 0; i = loop = 0;
TAILQ_FOREACH(wp, &w->panes, entry) { TAILQ_FOREACH(wp, &w->panes, entry) {
@ -963,21 +985,86 @@ window_tree_command_free(void *modedata)
window_tree_destroy(data); window_tree_destroy(data);
} }
static key_code
window_tree_mouse(struct window_tree_modedata *data, key_code key, u_int x,
struct window_tree_itemdata *item)
{
struct session *s;
struct winlink *wl;
struct window_pane *wp;
u_int loop;
if (key != KEYC_MOUSEDOWN1_PANE)
return (KEYC_NONE);
if (data->left != -1 && x <= (u_int)data->left)
return ('<');
if (data->right != -1 && x >= (u_int)data->right)
return ('>');
if (data->left != -1)
x -= data->left;
else if (x != 0)
x--;
if (x == 0 || data->end == 0)
x = 0;
else {
x = x / data->each;
if (data->start + x >= data->end)
x = data->end - 1;
}
window_tree_pull_item(item, &s, &wl, &wp);
if (item->type == WINDOW_TREE_SESSION) {
if (s == NULL)
return (KEYC_NONE);
mode_tree_expand_current(data->data);
loop = 0;
RB_FOREACH(wl, winlinks, &s->windows) {
if (loop == data->start + x)
break;
loop++;
}
if (wl != NULL)
mode_tree_set_current(data->data, (uint64_t)wl);
return ('\r');
}
if (item->type == WINDOW_TREE_WINDOW) {
if (wl == NULL)
return (KEYC_NONE);
mode_tree_expand_current(data->data);
loop = 0;
TAILQ_FOREACH(wp, &wl->window->panes, entry) {
if (loop == data->start + x)
break;
loop++;
}
if (wp != NULL)
mode_tree_set_current(data->data, (uint64_t)wp);
return ('\r');
}
return (KEYC_NONE);
}
static void static void
window_tree_key(struct window_pane *wp, struct client *c, window_tree_key(struct window_pane *wp, struct client *c,
__unused struct session *s, key_code key, struct mouse_event *m) __unused struct session *s, key_code key, struct mouse_event *m)
{ {
struct window_tree_modedata *data = wp->modedata; struct window_tree_modedata *data = wp->modedata;
struct window_tree_itemdata *item; struct window_tree_itemdata *item, *new_item;
char *name, *prompt; char *name, *prompt;
struct cmd_find_state fs; struct cmd_find_state fs;
int finished; int finished;
u_int tagged; u_int tagged, x, y;
item = mode_tree_get_current(data->data); item = mode_tree_get_current(data->data);
finished = mode_tree_key(data->data, c, &key, m); finished = mode_tree_key(data->data, c, &key, m, &x, &y);
if (item != mode_tree_get_current(data->data)) if (item != (new_item = mode_tree_get_current(data->data))) {
item = new_item;
data->offset = 0; data->offset = 0;
}
if (KEYC_IS_MOUSE(key))
key = window_tree_mouse(data, key, x, item);
switch (key) { switch (key) {
case '<': case '<':
data->offset--; data->offset--;