feat(float): support toggle show float window

This commit is contained in:
glepnir 2023-09-27 17:23:42 +08:00
parent dfa8b582a6
commit 4200a0f167
9 changed files with 156 additions and 14 deletions

View File

@ -3181,6 +3181,7 @@ nvim_open_win({buffer}, {enter}, {*config}) *nvim_open_win()*
fire from calling this function.
• fixed: If true when anchor is NW or SW, the float window
would be kept fixed even if the window would be truncated.
• hide: If true the floating window will be hidden.
Return: ~
Window handle, or 0 on error

View File

@ -65,6 +65,9 @@ The following changes may require adaptations in user config or plugins.
now requires an explicit range argument to be passed. If injections are
required, provide an explicit range via `parser:parse({ start_row, end_row })`.
• Float window support hide and show by setting `hide` on `nvim_open_win` and
`nvim_win_set_config`.
==============================================================================
NEW FEATURES *news-features*

View File

@ -1617,6 +1617,7 @@ function vim.api.nvim_open_term(buffer, opts) end
--- fire from calling this function.
--- • fixed: If true when anchor is NW or SW, the float window
--- would be kept fixed even if the window would be truncated.
--- • hide: If true the floating window will be hidden.
--- @return integer
function vim.api.nvim_open_win(buffer, enter, config) end

View File

@ -113,6 +113,7 @@ error('Cannot require a meta file')
--- @field style? string
--- @field noautocmd? boolean
--- @field fixed? boolean
--- @field hide? boolean
--- @class vim.api.keyset.get_autocmds
--- @field event? any

View File

@ -113,6 +113,7 @@ typedef struct {
String style;
Boolean noautocmd;
Boolean fixed;
Boolean hide;
} Dict(float_config);
typedef struct {

View File

@ -165,6 +165,7 @@
/// calling this function.
/// - fixed: If true when anchor is NW or SW, the float window
/// would be kept fixed even if the window would be truncated.
/// - hide: If true the floating window will be hidden.
///
/// @param[out] err Error details, if any
///
@ -323,6 +324,7 @@ Dictionary nvim_win_get_config(Window window, Error *err)
PUT(rv, "focusable", BOOLEAN_OBJ(config->focusable));
PUT(rv, "external", BOOLEAN_OBJ(config->external));
PUT(rv, "hide", BOOLEAN_OBJ(config->hide));
if (wp->w_floating) {
PUT(rv, "width", INTEGER_OBJ(config->width));
@ -848,6 +850,10 @@ static bool parse_float_config(Dict(float_config) *config, FloatConfig *fconfig,
fconfig->fixed = config->fixed;
}
if (HAS_KEY_X(config, hide)) {
fconfig->hide = config->hide;
}
return true;
#undef HAS_KEY_X
}

View File

@ -967,6 +967,7 @@ typedef struct {
int footer_width;
bool noautocmd;
bool fixed;
bool hide;
} FloatConfig;
#define FLOAT_CONFIG_INIT ((FloatConfig){ .height = 0, .width = 0, \
@ -977,6 +978,7 @@ typedef struct {
.zindex = kZIndexFloatDefault, \
.style = kWinStyleUnused, \
.noautocmd = false, \
.hide = false, \
.fixed = false })
// Structure to store last cursor position and topline. Used by check_lnums()

View File

@ -990,9 +990,13 @@ void ui_ext_win_position(win_T *wp, bool validate)
wp->w_grid_alloc.zindex = wp->w_float_config.zindex;
if (ui_has(kUIMultigrid)) {
String anchor = cstr_as_string((char *)float_anchor_str[c.anchor]);
ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor,
grid->handle, row, col, c.focusable,
wp->w_grid_alloc.zindex);
if (!c.hide) {
ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor,
grid->handle, row, col, c.focusable,
wp->w_grid_alloc.zindex);
} else {
ui_call_win_hide(wp->w_grid_alloc.handle);
}
} else {
bool valid = (wp->w_redr_type == 0);
if (!valid && !validate) {
@ -1014,13 +1018,18 @@ void ui_ext_win_position(win_T *wp, bool validate)
}
wp->w_winrow = comp_row;
wp->w_wincol = comp_col;
ui_comp_put_grid(&wp->w_grid_alloc, comp_row, comp_col,
wp->w_height_outer, wp->w_width_outer, valid, false);
ui_check_cursor_grid(wp->w_grid_alloc.handle);
wp->w_grid_alloc.focusable = wp->w_float_config.focusable;
if (!valid) {
wp->w_grid_alloc.valid = false;
redraw_later(wp, UPD_NOT_VALID);
if (!c.hide) {
ui_comp_put_grid(&wp->w_grid_alloc, comp_row, comp_col,
wp->w_height_outer, wp->w_width_outer, valid, false);
ui_check_cursor_grid(wp->w_grid_alloc.handle);
wp->w_grid_alloc.focusable = wp->w_float_config.focusable;
if (!valid) {
wp->w_grid_alloc.valid = false;
redraw_later(wp, UPD_NOT_VALID);
}
} else {
ui_comp_remove_grid(&wp->w_grid_alloc);
}
}
} else {

View File

@ -1172,14 +1172,14 @@ describe('float window', function()
it('return their configuration', function()
local buf = meths.create_buf(false, false)
local win = meths.open_win(buf, false, {relative='editor', width=20, height=2, row=3, col=5, zindex=60})
local expected = {anchor='NW', col=5, external=false, focusable=true, height=2, relative='editor', row=3, width=20, zindex=60}
local expected = {anchor='NW', col=5, external=false, focusable=true, height=2, relative='editor', row=3, width=20, zindex=60, hide=false}
eq(expected, meths.win_get_config(win))
eq({relative='', external=false, focusable=true}, meths.win_get_config(0))
eq({relative='', external=false, focusable=true, hide=false}, meths.win_get_config(0))
if multigrid then
meths.win_set_config(win, {external=true, width=10, height=1})
eq({external=true,focusable=true,width=10,height=1,relative=''}, meths.win_get_config(win))
eq({external=true,focusable=true,width=10,height=1,relative='',hide=false}, meths.win_get_config(win))
end
end)
@ -4281,7 +4281,7 @@ describe('float window', function()
|
]]}
end
eq({relative='win', width=12, height=1, bufpos={1,32}, anchor='NW',
eq({relative='win', width=12, height=1, bufpos={1,32}, anchor='NW', hide=false,
external=false, col=0, row=1, win=firstwin, focusable=true, zindex=50}, meths.win_get_config(win))
feed('<c-e>')
@ -10809,6 +10809,124 @@ describe('float window', function()
]]}
end
end)
it('float window with hide option', function()
local buf = meths.create_buf(false,false)
local win = meths.open_win(buf, false, {relative='editor', width=10, height=2, row=2, col=5, hide = true})
local expected_pos = {
[4]={{id=1001}, 'NW', 1, 2, 5, true},
}
if multigrid then
screen:expect{grid=[[
## grid 1
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[3:----------------------------------------]|
## grid 2
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
## grid 3
|
## grid 4 (hidden)
{1: }|
{2:~ }|
]], float_pos = {}}
else
screen:expect([[
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
end
meths.win_set_config(win, {hide = false})
if multigrid then
screen:expect{grid=[[
## grid 1
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[3:----------------------------------------]|
## grid 2
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
## grid 3
|
## grid 4
{1: }|
{2:~ }|
]], float_pos = expected_pos}
else
screen:expect([[
^ |
{0:~ }|
{0:~ }{1: }{0: }|
{0:~ }{2:~ }{0: }|
{0:~ }|
{0:~ }|
|
]])
end
meths.win_set_config(win, {hide=true})
if multigrid then
screen:expect{grid=[[
## grid 1
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[2:----------------------------------------]|
[3:----------------------------------------]|
## grid 2
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
## grid 3
|
## grid 4 (hidden)
{1: }|
{2:~ }|
]], float_pos = {}}
else
screen:expect([[
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
end
end)
end
describe('with ext_multigrid', function()