Merge pull request #25386 from glepnir/toggle_float

feat(float): support toggle show float window
This commit is contained in:
bfredl 2023-09-30 13:40:35 +02:00 committed by GitHub
commit 578d634176
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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. fire from calling this function.
• fixed: If true when anchor is NW or SW, the float window • fixed: If true when anchor is NW or SW, the float window
would be kept fixed even if the window would be truncated. would be kept fixed even if the window would be truncated.
• hide: If true the floating window will be hidden.
Return: ~ Return: ~
Window handle, or 0 on error 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 now requires an explicit range argument to be passed. If injections are
required, provide an explicit range via `parser:parse({ start_row, end_row })`. 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* NEW FEATURES *news-features*

View File

@ -1617,6 +1617,7 @@ function vim.api.nvim_open_term(buffer, opts) end
--- fire from calling this function. --- fire from calling this function.
--- • fixed: If true when anchor is NW or SW, the float window --- • fixed: If true when anchor is NW or SW, the float window
--- would be kept fixed even if the window would be truncated. --- would be kept fixed even if the window would be truncated.
--- • hide: If true the floating window will be hidden.
--- @return integer --- @return integer
function vim.api.nvim_open_win(buffer, enter, config) end 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 style? string
--- @field noautocmd? boolean --- @field noautocmd? boolean
--- @field fixed? boolean --- @field fixed? boolean
--- @field hide? boolean
--- @class vim.api.keyset.get_autocmds --- @class vim.api.keyset.get_autocmds
--- @field event? any --- @field event? any

View File

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

View File

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

View File

@ -967,6 +967,7 @@ typedef struct {
int footer_width; int footer_width;
bool noautocmd; bool noautocmd;
bool fixed; bool fixed;
bool hide;
} FloatConfig; } FloatConfig;
#define FLOAT_CONFIG_INIT ((FloatConfig){ .height = 0, .width = 0, \ #define FLOAT_CONFIG_INIT ((FloatConfig){ .height = 0, .width = 0, \
@ -977,6 +978,7 @@ typedef struct {
.zindex = kZIndexFloatDefault, \ .zindex = kZIndexFloatDefault, \
.style = kWinStyleUnused, \ .style = kWinStyleUnused, \
.noautocmd = false, \ .noautocmd = false, \
.hide = false, \
.fixed = false }) .fixed = false })
// Structure to store last cursor position and topline. Used by check_lnums() // 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; wp->w_grid_alloc.zindex = wp->w_float_config.zindex;
if (ui_has(kUIMultigrid)) { if (ui_has(kUIMultigrid)) {
String anchor = cstr_as_string((char *)float_anchor_str[c.anchor]); String anchor = cstr_as_string((char *)float_anchor_str[c.anchor]);
ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor, if (!c.hide) {
grid->handle, row, col, c.focusable, ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor,
wp->w_grid_alloc.zindex); grid->handle, row, col, c.focusable,
wp->w_grid_alloc.zindex);
} else {
ui_call_win_hide(wp->w_grid_alloc.handle);
}
} else { } else {
bool valid = (wp->w_redr_type == 0); bool valid = (wp->w_redr_type == 0);
if (!valid && !validate) { if (!valid && !validate) {
@ -1014,13 +1018,18 @@ void ui_ext_win_position(win_T *wp, bool validate)
} }
wp->w_winrow = comp_row; wp->w_winrow = comp_row;
wp->w_wincol = comp_col; 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); if (!c.hide) {
ui_check_cursor_grid(wp->w_grid_alloc.handle); ui_comp_put_grid(&wp->w_grid_alloc, comp_row, comp_col,
wp->w_grid_alloc.focusable = wp->w_float_config.focusable; wp->w_height_outer, wp->w_width_outer, valid, false);
if (!valid) { ui_check_cursor_grid(wp->w_grid_alloc.handle);
wp->w_grid_alloc.valid = false; wp->w_grid_alloc.focusable = wp->w_float_config.focusable;
redraw_later(wp, UPD_NOT_VALID); if (!valid) {
wp->w_grid_alloc.valid = false;
redraw_later(wp, UPD_NOT_VALID);
}
} else {
ui_comp_remove_grid(&wp->w_grid_alloc);
} }
} }
} else { } else {

View File

@ -1172,14 +1172,14 @@ describe('float window', function()
it('return their configuration', function() it('return their configuration', function()
local buf = meths.create_buf(false, false) 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 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(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 if multigrid then
meths.win_set_config(win, {external=true, width=10, height=1}) 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
end) end)
@ -4281,7 +4281,7 @@ describe('float window', function()
| |
]]} ]]}
end 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)) external=false, col=0, row=1, win=firstwin, focusable=true, zindex=50}, meths.win_get_config(win))
feed('<c-e>') feed('<c-e>')
@ -10809,6 +10809,124 @@ describe('float window', function()
]]} ]]}
end end
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 end
describe('with ext_multigrid', function() describe('with ext_multigrid', function()