From 213c3c3e53459a77c77166cda85ba619e7eb2eb1 Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Sat, 13 Dec 2014 12:58:06 -0300 Subject: [PATCH] ui: Fix ui resizing and change some method names --- src/nvim/msgpack_rpc/remote_ui.c | 32 ++++++++++++-- src/nvim/syntax.c | 6 ++- src/nvim/ui.c | 74 ++++++++++++-------------------- test/functional/ui/screen.lua | 4 +- 4 files changed, 63 insertions(+), 53 deletions(-) diff --git a/src/nvim/msgpack_rpc/remote_ui.c b/src/nvim/msgpack_rpc/remote_ui.c index e27d4383c0..a0bc573698 100644 --- a/src/nvim/msgpack_rpc/remote_ui.c +++ b/src/nvim/msgpack_rpc/remote_ui.c @@ -27,12 +27,15 @@ void remote_ui_init(void) { connected_uis = pmap_new(uint64_t)(); // Add handler for "attach_ui" - String method = cstr_as_string("attach_ui"); + String method = cstr_as_string("ui_attach"); MsgpackRpcRequestHandler handler = {.fn = remote_ui_attach, .defer = false}; msgpack_rpc_add_method_handler(method, handler); - method = cstr_as_string("detach_ui"); + method = cstr_as_string("ui_detach"); handler.fn = remote_ui_detach; msgpack_rpc_add_method_handler(method, handler); + method = cstr_as_string("ui_try_resize"); + handler.fn = remote_ui_try_resize; + msgpack_rpc_add_method_handler(method, handler); } void remote_ui_disconnect(uint64_t channel_id) @@ -95,7 +98,6 @@ static Object remote_ui_attach(uint64_t channel_id, uint64_t request_id, ui->suspend = remote_ui_suspend; pmap_put(uint64_t)(connected_uis, channel_id, ui); ui_attach(ui); - return NIL; } @@ -110,6 +112,30 @@ static Object remote_ui_detach(uint64_t channel_id, uint64_t request_id, return NIL; } +static Object remote_ui_try_resize(uint64_t channel_id, uint64_t request_id, + Array args, Error *error) +{ + if (!pmap_has(uint64_t)(connected_uis, channel_id)) { + api_set_error(error, Exception, _("UI is not attached for channel")); + } + + if (args.size != 2 || args.items[0].type != kObjectTypeInteger + || args.items[1].type != kObjectTypeInteger + || args.items[0].data.integer <= 0 || args.items[1].data.integer <= 0) { + api_set_error(error, Validation, + _("Arguments must be a pair of positive integers " + "representing the remote screen width/height")); + return NIL; + } + + UI *ui = pmap_get(uint64_t)(connected_uis, channel_id); + ui->width = (int)args.items[0].data.integer; + ui->height = (int)args.items[1].data.integer; + ui_refresh(); + return NIL; +} + + static void push_call(UI *ui, char *name, Array args) { Array call = ARRAY_DICT_INIT; diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index 68589522c4..c2fb34cde0 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -6529,7 +6529,6 @@ do_highlight ( if (is_normal_group) { normal_fg = HL_TABLE()[idx].sg_rgb_fg; - ui_fg_updated(); } } else if (STRCMP(key, "GUIBG") == 0) { if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI)) { @@ -6548,7 +6547,6 @@ do_highlight ( if (is_normal_group) { normal_bg = HL_TABLE()[idx].sg_rgb_bg; - ui_bg_updated(); } } else if (STRCMP(key, "GUISP") == 0) { // Ignored @@ -6645,6 +6643,10 @@ do_highlight ( if (is_normal_group) { HL_TABLE()[idx].sg_term_attr = 0; HL_TABLE()[idx].sg_cterm_attr = 0; + if (abstract_ui) { + // If the normal group has changed, it is simpler to refresh every UI + ui_refresh(); + } } else set_hl_attr(idx); HL_TABLE()[idx].sg_scriptID = current_SID; diff --git a/src/nvim/ui.c b/src/nvim/ui.c index cd8c3b73e6..de92079ada 100644 --- a/src/nvim/ui.c +++ b/src/nvim/ui.c @@ -64,7 +64,7 @@ static HlAttrs current_attrs = { false, false, false, false, false, false, -1, -1 }; static bool cursor_enabled = true; -static int height = INT_MAX, width = INT_MAX; +static int height, width; // This set of macros allow us to use UI_CALL to invoke any function on // registered UI instances. The functions can have 0-5 arguments(configurable @@ -113,18 +113,6 @@ void ui_write(uint8_t *s, int len) free(tofree); } -void ui_fg_updated(void) -{ - UI_CALL(update_fg, normal_fg); - UI_CALL(flush); -} - -void ui_bg_updated(void) -{ - UI_CALL(update_bg, normal_bg); - UI_CALL(flush); -} - /* * If the machine has job control, use it to suspend the program, * otherwise fake it by starting a new shell. @@ -177,10 +165,32 @@ void ui_cursor_shape(void) } } -void ui_resize(int width, int height) +void ui_refresh(void) { - ui_fg_updated(); - ui_bg_updated(); + width = height = INT_MAX; + + for (size_t i = 0; i < ui_count; i++) { + UI *ui = uis[i]; + width = ui->width < width ? ui->width : width; + height = ui->height < height ? ui->height : height; + } + + screen_resize(width, height, true); +} + +void ui_resize(int new_width, int new_height) +{ + width = new_width; + height = new_height; + + if (normal_fg != -1) { + UI_CALL(update_fg, normal_fg); + } + + if (normal_bg != -1) { + UI_CALL(update_bg, normal_bg); + } + sr.top = 0; sr.bot = height - 1; sr.left = 0; @@ -254,7 +264,7 @@ void ui_attach(UI *ui) } uis[ui_count++] = ui; - resized(ui); + ui_refresh(); } void ui_detach(UI *ui) @@ -281,17 +291,8 @@ void ui_detach(UI *ui) ui_count--; - if (ui->width == width || ui->height == height) { - // It is possible that the UI being detached had the smallest screen, - // so check for the new minimum dimensions - width = height = INT_MAX; - for (size_t i = 0; i < ui_count; i++) { - check_dimensions(uis[i]); - } - } - if (ui_count) { - screen_resize(width, height, true); + ui_refresh(); } } @@ -474,25 +475,6 @@ static void parse_abstract_ui_codes(uint8_t *ptr, int len) UI_CALL(flush); } -static void resized(UI *ui) -{ - check_dimensions(ui); - screen_resize(width, height, true); -} - -static void check_dimensions(UI *ui) -{ - // The internal screen dimensions are always the minimum required to fit on - // all connected screens - if (ui->width < width) { - width = ui->width; - } - - if (ui->height < height) { - height = ui->height; - } -} - static void ui_linefeed(void) { int new_col = 0; diff --git a/test/functional/ui/screen.lua b/test/functional/ui/screen.lua index c9c9d5a197..a965b3626a 100644 --- a/test/functional/ui/screen.lua +++ b/test/functional/ui/screen.lua @@ -115,12 +115,12 @@ function Screen:set_default_attr_ids(attr_ids) end function Screen:attach() - request('attach_ui', self._width, self._height) + request('ui_attach', self._width, self._height) self._suspended = false end function Screen:detach() - request('detach_ui') + request('ui_detach') self._suspended = true end