api/ui: simplify popup menu position get/set logic; fix test

This commit is contained in:
Yatao Li 2020-03-22 17:53:45 +08:00
parent d372c804aa
commit e34684b2ad
4 changed files with 40 additions and 79 deletions

View File

@ -349,15 +349,15 @@ void nvim_ui_pum_set_height(uint64_t channel_id, Integer height, Error *err)
ui->pum_nlines = (int)height;
}
/// Tells Nvim the geometry of the popumenu, to align floating
/// windows with an external popup menu. Note that this method
/// is not to be confused with |nvim_ui_pum_set_height()|, which
/// sets the number of visible items in the popup menu, while
/// this function sets the bounding box of the popup menu,
/// including visual decorations such as boarders and sliders.
/// Floats need not use the same font size, nor be anchored to
/// exact grid corners, so one can set floating-point numbers
/// to the popup menu geometry.
/// Tells Nvim the geometry of the popumenu, to align floating windows with an
/// external popup menu.
///
/// Note that this method is not to be confused with |nvim_ui_pum_set_height()|,
/// which sets the number of visible items in the popup menu, while this
/// function sets the bounding box of the popup menu, including visual
/// decorations such as boarders and sliders. Floats need not use the same font
/// size, nor be anchored to exact grid corners, so one can set floating-point
/// numbers to the popup menu geometry.
///
/// @param channel_id
/// @param width Popupmenu width.

View File

@ -902,18 +902,6 @@ int pum_get_height(void)
return pum_height;
}
/// Gets the internal pum geometry.
///
/// @return the internal pum geometry. Ignores UI external pum geometry.
/// Only valid when pum_visible() returns TRUE!
void pum_get_internal_pos(int *pwidth, int *pheight, int *prow, int *pcol)
{
*pwidth = pum_width;
*pheight = pum_height;
*prow = pum_row;
*pcol = pum_col;
}
/// Add size information about the pum to "dict".
void pum_set_event_info(dict_T *dict)
{
@ -921,7 +909,12 @@ void pum_set_event_info(dict_T *dict)
return;
}
double w, h, r, c;
ui_pum_get_pos(&w, &h, &r, &c);
if (!ui_pum_get_pos(&w, &h, &r, &c)) {
w = (double)pum_width;
h = (double)pum_height;
r = (double)pum_row;
c = (double)pum_col;
}
tv_dict_add_float(dict, S_LEN("height"), h);
tv_dict_add_float(dict, S_LEN("width"), w);
tv_dict_add_float(dict, S_LEN("row"), r);

View File

@ -235,34 +235,19 @@ int ui_pum_get_height(void)
return pum_height;
}
void ui_pum_get_pos(double *pwidth, double *pheight, double *prow, double *pcol)
bool ui_pum_get_pos(double *pwidth, double *pheight, double *prow, double *pcol)
{
double w = 0.0, h = 0.0, r = 0.0, c = 0.0;
bool found = false;
for (size_t i = 1; i < ui_count; i++) {
if (!uis[i]->pum_pos) {
continue;
}
w = uis[i]->pum_width;
h = uis[i]->pum_height;
r = uis[i]->pum_row;
c = uis[i]->pum_col;
found = true;
break;
}
if (found) {
*pwidth = w;
*pheight = h;
*prow = r;
*pcol = c;
} else {
int iw, ih, ir, ic;
pum_get_internal_pos(&iw, &ih, &ir, &ic);
*pwidth = (double)iw;
*pheight = (double)ih;
*prow = (double)ir;
*pcol = (double)ic;
*pwidth = uis[i]->pum_width;
*pheight = uis[i]->pum_height;
*prow = uis[i]->pum_row;
*pcol = uis[i]->pum_col;
return true;
}
return false;
}
static void ui_refresh_event(void **argv)

View File

@ -8,7 +8,7 @@ local command = helpers.command
local funcs = helpers.funcs
local get_pathsep = helpers.get_pathsep
local eq = helpers.eq
local matches = helpers.matches
local pcall_err = helpers.pcall_err
describe('ui/ext_popupmenu', function()
local screen
@ -421,22 +421,16 @@ describe('ui/ext_popupmenu', function()
end)
it('an error occurs if set 0 or less', function()
local ok, err, _
ok, _ = pcall(meths.ui_pum_set_height, 1)
eq(true, ok)
ok, err = pcall(meths.ui_pum_set_height, 0)
eq(false, ok)
matches('.*: Expected pum height > 0', err)
meths.ui_pum_set_height(1)
eq('Expected pum height > 0',
pcall_err(meths.ui_pum_set_height, 0))
end)
it('an error occurs when ext_popupmenu is false', function()
local ok, err, _
ok, _ = pcall(meths.ui_pum_set_height, 1)
eq(true, ok)
meths.ui_pum_set_height(1)
screen:set_option('ext_popupmenu', false)
ok, err = pcall(meths.ui_pum_set_height, 1)
eq(false, ok)
matches('.*: It must support the ext_popupmenu option', err)
eq('It must support the ext_popupmenu option',
pcall_err(meths.ui_pum_set_height, 1))
end)
end)
@ -482,35 +476,24 @@ describe('ui/ext_popupmenu', function()
end)
it('no error occurs if row or col set less than 0', function()
local ok, err, _
ok, _ = pcall(meths.ui_pum_set_bounds, 1.0, 1.0, 0.0, 1.5)
eq(true, ok)
ok, _ = pcall(meths.ui_pum_set_bounds, 1.0, 1.0, -1.0, 0.0)
eq(true, ok)
ok, _ = pcall(meths.ui_pum_set_bounds, 1.0, 1.0, 0.0, -1.0)
eq(true, ok)
meths.ui_pum_set_bounds(1.0, 1.0, 0.0, 1.5)
meths.ui_pum_set_bounds(1.0, 1.0, -1.0, 0.0)
meths.ui_pum_set_bounds(1.0, 1.0, 0.0, -1.0)
end)
it('an error occurs if width or height set 0 or less', function()
local ok, err, _
ok, _ = pcall(meths.ui_pum_set_bounds, 1.0, 1.0, 0.0, 1.5)
eq(true, ok)
ok, err = pcall(meths.ui_pum_set_bounds, 0.0, 1.0, 1.0, 0.0)
eq(false, ok)
matches('.*: Expected pumpos width > 0', err)
ok, err = pcall(meths.ui_pum_set_bounds, 1.0, 0.0, 1.0, 0.0)
eq(false, ok)
matches('.*: Expected pumpos height > 0', err)
meths.ui_pum_set_bounds(1.0, 1.0, 0.0, 1.5)
eq('Expected width > 0',
pcall_err(meths.ui_pum_set_bounds, 0.0, 1.0, 1.0, 0.0))
eq('Expected height > 0',
pcall_err(meths.ui_pum_set_bounds, 1.0, -1.0, 1.0, 0.0))
end)
it('an error occurs when ext_popupmenu is false', function()
local ok, err, _
ok, _ = pcall(meths.ui_pum_set_bounds, 1.0, 1.0, 0.0, 1.5)
eq(true, ok)
meths.ui_pum_set_bounds(1.0, 1.0, 0.0, 1.5)
screen:set_option('ext_popupmenu', false)
ok, err = pcall(meths.ui_pum_set_bounds, 1.0, 1.0, 0.0, 1.5)
eq(false, ok)
matches('.*: UI must support the ext_popupmenu option', err)
eq('UI must support the ext_popupmenu option',
pcall_err(meths.ui_pum_set_bounds, 1.0, 1.0, 0.0, 1.5))
end)
end)