Skip to content

Commit 267a61d

Browse files
autogen popup id
1 parent f91516d commit 267a61d

File tree

1 file changed

+16
-6
lines changed
  • autoload/vital/__vital__/Experimental/UI

1 file changed

+16
-6
lines changed

autoload/vital/__vital__/Experimental/UI/Popup.vim

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set cpo&vim
33

44
let s:_has_nvim = has('nvim')
55
let s:_popups = {}
6+
let s:_id = 0
67

78
function! s:is_supported() abort
89
return has('nvim') && exists('*nvim_open_win') || (!has('nvim') && exists('*popup_create') && has('patch-8.2.0286'))
@@ -17,12 +18,14 @@ endfunction
1718
" 'pos': 'topleft|topright|bottomleft|bottomright|topcenter|bottomcenter',
1819
" }
1920
function! s:create(opt) abort
21+
let id = s:_nextid()
2022
let data = {}
23+
2124
call s:_set(data, a:opt)
2225

2326
if s:_has_nvim
24-
let buf = nvim_create_buf(0, 1)
25-
call nvim_buf_set_lines(buf, 0, -1, 1, data['contents'])
27+
let data['bufnr'] = nvim_create_buf(0, 1)
28+
call nvim_buf_set_lines(data['bufnr'], 0, -1, 1, data['contents'])
2629
let opt = {
2730
\ 'relative': 'editor',
2831
\ 'style': 'minimal',
@@ -32,10 +35,10 @@ function! s:create(opt) abort
3235
\ 'row': data['sy'],
3336
\ 'focusable': 0,
3437
\ }
35-
let id = nvim_open_win(buf, 1, opt)
38+
let data['winid'] = nvim_open_win(data['bufnr'], 1, opt)
3639
else
3740
" neovim doesn't support scrollbar so don't enable it
38-
let id = popup_create(data['contents'], {
41+
let data['winid'] = popup_create(data['contents'], {
3942
\ 'width': data['w'],
4043
\ 'height': data['h'],
4144
\ 'minwidth': data['w'],
@@ -46,6 +49,7 @@ function! s:create(opt) abort
4649
\ 'line': data['sy'],
4750
\ 'scrollbar': 0,
4851
\ })
52+
let data['bufnr'] = winbufnr(data['winid'])
4953
endif
5054
let s:_popups[id] = data
5155
call s:_notify(id, {}, 'create')
@@ -54,10 +58,11 @@ endfunction
5458

5559
function! s:close(id) abort
5660
if has_key(s:_popups, a:id)
61+
let data = s:_popups[a:id]
5762
if s:_has_nvim
58-
silent! call nvim_win_close(a:id, 1)
63+
call nvim_win_close(data['winid'], 1)
5964
else
60-
silent! call popup_close(a:id)
65+
call popup_close(data['winid'])
6166
endif
6267
call s:_notify(a:id, {}, 'close')
6368
call remove(s:_popups, a:id)
@@ -112,6 +117,11 @@ function! s:_set(data, opt) abort
112117
endif
113118
endfunction
114119

120+
function! s:_nextid() abort
121+
let s:_id += 1
122+
return s:_id
123+
endfunction
124+
115125
function! s:_notify(id, data, event) abort
116126
if has_key(s:_popups[a:id], 'on_event')
117127
call s:_popups[a:id]['on_event'](a:id, a:data, a:event)

0 commit comments

Comments
 (0)