Skip to content

Commit

Permalink
Send individual grids to UIs with event "grid_resize"
Browse files Browse the repository at this point in the history
The grids get allocated a handle (very naive at the moment) and these
handles are sent with every event to the external UIs.  Works for one
window at the moment.

Use neovim/python-gui#36 to test. It currently creates separate windows
for each grid. `:sp` seems to work, but `:vs` crashes.
  • Loading branch information
coditva committed Jul 28, 2018
1 parent 131fdfd commit f3b1c84
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/nvim/api/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ static void remote_ui_grid_resize(UI *ui, Integer grid,
Array args = ARRAY_DICT_INIT;
if (ui->ui_ext[kUINewgrid]) {
ADD(args, INTEGER_OBJ(grid));
} else if (grid != 1) {
// calling grid_resize with a grid other than the global when
// the remote ui is not managing grids should not send the event
return;
}
ADD(args, INTEGER_OBJ(width));
ADD(args, INTEGER_OBJ(height));
Expand Down
50 changes: 36 additions & 14 deletions src/nvim/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ StlClickDefinition *tab_page_click_defs = NULL;

long tab_page_click_defs_size = 0;

/// The last handle that was assigned to a ScreenGrid. 1 is reserved for
/// the default_grid.
/// TODO(utkarshme): Numbers can be recycled after grid destruction.
static int last_handle = 1;

/// Whether to call "ui_call_grid_resize" in win_grid_alloc
static int send_grid_resize;

#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "screen.c.generated.h"
#endif
Expand Down Expand Up @@ -422,6 +430,7 @@ void update_screen(int type)
win_redr_status(wp);
}
}
send_grid_resize = false;
end_search_hl();
// May need to redraw the popup menu.
if (pum_drawn()) {
Expand Down Expand Up @@ -654,7 +663,7 @@ static void win_update(win_T *wp)

type = wp->w_redr_type;

window_grid_alloc(wp, false);
win_grid_alloc(wp, false);

if (type == NOT_VALID) {
wp->w_redr_status = TRUE;
Expand Down Expand Up @@ -2221,7 +2230,7 @@ win_line (
row = startrow;

// allocate window grid if not already
window_grid_alloc(wp, true);
win_grid_alloc(wp, true);

/*
* To speed up the loop below, set extra_check when there is linebreak,
Expand Down Expand Up @@ -5831,18 +5840,28 @@ int screen_valid(int doclear)
/// (re)allocate a window grid if size changed
/// If "doclear" is true, clear the screen if resized.
// TODO(utkarshme): Think of a better name, place
void window_grid_alloc(win_T *wp, int doclear)
void win_grid_alloc(win_T *wp, int doclear)
{
if (wp->w_grid.ScreenLines != NULL
&& wp->w_grid.Rows == wp->w_height
&& wp->w_grid.Columns == wp->w_width) {
return;
}
if (wp->w_grid.ScreenLines == NULL
|| wp->w_grid.Rows != wp->w_height
|| wp->w_grid.Columns != wp->w_width) {
grid_alloc(&wp->w_grid, wp->w_height, wp->w_width, doclear);

// only assign a grid handle if not already
if (wp->w_grid.handle == 0) {
wp->w_grid.handle = ++last_handle;
}

wp->w_grid.OffsetRow = wp->w_winrow;
wp->w_grid.OffsetColumn = wp->w_wincol;

grid_alloc(&wp->w_grid, wp->w_height, wp->w_width, doclear);
wp->w_grid.was_resized = true;
}

wp->w_grid.OffsetRow = wp->w_winrow;
wp->w_grid.OffsetColumn = wp->w_wincol;
if (send_grid_resize || wp->w_grid.was_resized) {
ui_call_grid_resize(wp->w_grid.handle, wp->w_grid.Columns, wp->w_grid.Rows);
wp->w_grid.was_resized = false;
}
}

/*
Expand Down Expand Up @@ -5936,6 +5955,7 @@ void screenalloc(bool doclear)

default_grid.OffsetRow = 0;
default_grid.OffsetColumn = 0;
default_grid.handle = 1;

must_redraw = CLEAR; /* need to clear the screen later */
if (doclear)
Expand All @@ -5960,7 +5980,7 @@ void screenalloc(bool doclear)
void grid_alloc(ScreenGrid *grid, int rows, int columns, bool copy)
{
int new_row, old_row;
ScreenGrid new = { 0 };
ScreenGrid new = *grid;

size_t ncells = (size_t)((rows+1) * columns);
new.ScreenLines = xmalloc(ncells * sizeof(schar_T));
Expand Down Expand Up @@ -6247,7 +6267,7 @@ int grid_ins_lines(ScreenGrid *grid, int row, int line_count, int end,
}
}

ui_call_grid_scroll(1, row, end, col, col+width, -line_count, 0);
ui_call_grid_scroll(grid->handle, row, end, col, col+width, -line_count, 0);

return OK;
}
Expand Down Expand Up @@ -6299,7 +6319,7 @@ int grid_del_lines(ScreenGrid *grid, int row, int line_count, int end,
}
}

ui_call_grid_scroll(1, row, end, col, col+width, line_count, 0);
ui_call_grid_scroll(grid->handle, row, end, col, col+width, line_count, 0);

return OK;
}
Expand Down Expand Up @@ -7052,6 +7072,8 @@ void screen_resize(int width, int height)
default_grid.Rows = screen_Rows;
default_grid.Columns = screen_Columns;

send_grid_resize = true;

/* The window layout used to be adjusted here, but it now happens in
* screenalloc() (also invoked from screenclear()). That is because the
* "busy" check above may skip this, but not screenalloc(). */
Expand Down
2 changes: 2 additions & 0 deletions src/nvim/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ typedef struct {
// offsets for the grid relative to the screen
int OffsetRow;
int OffsetColumn;

int was_resized;
} ScreenGrid;

#endif // NVIM_TYPES_H
11 changes: 7 additions & 4 deletions src/nvim/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,13 @@ void ui_set_ext_option(UI *ui, UIExtension ext, bool active)
void ui_line(ScreenGrid *grid, int row, int startcol, int endcol, int clearcol, int clearattr)
{
size_t off = grid->LineOffset[row] + (size_t)startcol;
UI_CALL(raw_line, 1, grid->OffsetRow + row, grid->OffsetColumn + startcol,
grid->OffsetColumn + endcol, grid->OffsetColumn + clearcol,
clearattr, (const schar_T *)grid->ScreenLines + off,
(const schar_T *)grid->ScreenAttrs + off);
int row_off = ui_is_external(kUIMultigrid) ? 0 : grid->OffsetRow;
int col_off = ui_is_external(kUIMultigrid) ? 0 : grid->OffsetColumn;

UI_CALL(raw_line, grid->handle, row_off + row, col_off + startcol,
col_off + endcol, col_off + clearcol, clearattr,
(const schar_T *)grid->ScreenLines + off,
(const sattr_T *)grid->ScreenAttrs + off);

if (p_wd) { // 'writedelay': flush & delay each time.
int old_row = row, old_col = col;
Expand Down
2 changes: 2 additions & 0 deletions src/nvim/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef enum {
kUIWildmenu,
#define kUIGlobalCount (kUIWildmenu+1)
kUINewgrid,
kUIMultigrid,
kUIHlState,
kUIExtCount,
} UIExtension;
Expand All @@ -26,6 +27,7 @@ EXTERN const char *ui_ext_names[] INIT(= {
"ext_tabline",
"ext_wildmenu",
"ext_newgrid",
"ext_multigrid",
"ext_hlstate",
});

Expand Down
4 changes: 2 additions & 2 deletions src/nvim/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -4222,7 +4222,7 @@ void win_setheight_win(int height, win_T *win)
}

frame_setheight(win->w_frame, height + win->w_status_height);
window_grid_alloc(win, false);
win_grid_alloc(win, false);

/* recompute the window positions */
row = win_comp_pos();
Expand Down Expand Up @@ -4419,7 +4419,7 @@ void win_setwidth_win(int width, win_T *wp)
}

frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
window_grid_alloc(wp, false);
win_grid_alloc(wp, false);

/* recompute the window positions */
(void)win_comp_pos();
Expand Down

0 comments on commit f3b1c84

Please sign in to comment.