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 Jun 16, 2018
1 parent 8c93a3e commit fe14bf3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 27 deletions.
4 changes: 4 additions & 0 deletions src/nvim/api/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,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 @@ -421,6 +429,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 @@ -653,7 +662,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 @@ -2219,7 +2228,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 @@ -5808,18 +5817,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 @@ -5913,6 +5932,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 @@ -5937,7 +5957,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 @@ -6224,7 +6244,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 @@ -6276,7 +6296,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 @@ -7019,6 +7039,8 @@ void screen_resize(int width, int height)
default_grid.Rows = screen_Rows;
default_grid.Columns = screen_Columns;

send_grid_resize = true;

// TODO(bfredl): update default colors when they changed, NOT on resize.
ui_default_colors_set();

Expand Down
5 changes: 5 additions & 0 deletions src/nvim/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ typedef char_u schar_T[(MAX_MCO+1) * 4 + 1];
typedef int16_t sattr_T;

// TODO(bfredl): find me a good home
typedef unsigned int GridHandle;
typedef struct {
GridHandle handle;

schar_T *ScreenLines;
sattr_T *ScreenAttrs;
unsigned *LineOffset;
Expand All @@ -36,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
21 changes: 10 additions & 11 deletions src/nvim/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,16 @@ 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,
grid->ScreenLines + off,
grid->ScreenAttrs + off);

size_t off = grid->LineOffset[row] + (size_t)startcol;
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, grid->ScreenLines + off, grid->ScreenAttrs + off);
if (p_wd) { // 'writedelay': flush & delay each time.
ui_flush();
uint64_t wd = (uint64_t)labs(p_wd);
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 fe14bf3

Please sign in to comment.