Skip to content

Commit 9fe3c45

Browse files
add event support
1 parent f3268bf commit 9fe3c45

File tree

1 file changed

+19
-0
lines changed
  • autoload/vital/__vital__/Experimental/UI

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function! s:create(opt) abort
4848
\ })
4949
endif
5050
let s:_popups[id] = data
51+
call s:_notify(id, {}, 'create')
5152
return id
5253
endfunction
5354

@@ -58,11 +59,19 @@ function! s:close(id) abort
5859
else
5960
silent! call popup_close(a:id)
6061
endif
62+
call s:_notify(a:id, {}, 'close')
6163
call remove(s:_popups, a:id)
6264
endif
6365
endfunction
6466

6567
function! s:_set(data, opt) abort
68+
" copy all callbacks to data
69+
for [key, Cb] in items(a:opt)
70+
if stridx(key, 'on_') == 0
71+
let a:data[key] = Cb
72+
endif
73+
endfor
74+
6675
" try to set values from a:opt, if not set from a:data, if not default
6776
let a:data['w'] = get(a:opt, 'w', get(a:data, 'w', 5))
6877
let a:data['h'] = get(a:opt, 'h', get(a:data, 'h', 5))
@@ -103,6 +112,16 @@ function! s:_set(data, opt) abort
103112
endif
104113
endfunction
105114

115+
function! s:_notify(id, data, event) abort
116+
if has_key(s:_popups[a:id], 'on_event')
117+
call s:_popups[a:id]['on_event'](a:id, a:data, a:event)
118+
endif
119+
let eventhandler = 'on_' . a:event
120+
if has_key(s:_popups[a:id], eventhandler)
121+
call s:_popups[a:id][eventhandler](a:id, a:data, a:event)
122+
endif
123+
endfunction
124+
106125
let &cpo = s:save_cpo
107126
unlet s:save_cpo
108127
"vim: sts=2 sw=2 smarttab et ai textwidth=0 fdm=marker

0 commit comments

Comments
 (0)