Skip to content

Commit 8994ddc

Browse files
author
skywind3000
committed
update intro-dialog.md
1 parent 916ff32 commit 8994ddc

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

docs/draft/intro-dialog.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ When the dialog closes, you need to know: did the user confirm or cancel? And if
125125

126126
The return value has two key fields:
127127

128-
- `button_index` — which button was pressed (1-based), or `0` for Enter, or `-1` for cancel
129-
- `button` — the name of the button control, or `''` if none
128+
- `button_index` — which button was pressed (0-based), or `-1` for cancel
129+
- `button` — the name of the button control, or `''` if Enter was pressed on a non-button control or dialog was cancelled
130130

131131
Here is the pattern you will use in every dialog:
132132

@@ -137,20 +137,21 @@ if r.button_index == -1
137137
" User pressed ESC, Ctrl-C, or clicked the close button.
138138
" Dialog was cancelled.
139139
echo 'Cancelled'
140-
elseif r.button_index == 0
140+
elseif r.button == ''
141141
" User pressed Enter while on an input, radio, or checkbox.
142-
" Treat this as a confirm.
142+
" No button was clicked. Treat this as a confirm.
143143
echo 'Confirmed (Enter): name=' . r.name
144144
else
145-
" User clicked a button. button_index is 1-based:
146-
" 1 = first button, 2 = second button, etc.
145+
" User clicked a button. button_index is 0-based:
146+
" 0 = first button, 1 = second button, etc.
147147
echo 'Button pressed: ' . r.button . ' #' . r.button_index
148148
endif
149149
```
150150

151151
A few things to note:
152152

153-
- **`button_index` is 1-based**, not 0-based. The first button returns `1`. This is consistent with Vim's built-in `confirm()`.
153+
- **`button_index` is 0-based**. The first button returns `0`, the second returns `1`, and so on.
154+
- **Distinguish Enter from button click using `button`.** When `button_index` is `0`, check `r.button`: if it is `''`, the user pressed Enter on a non-button control; if it is non-empty, the first button was clicked.
154155
- **Cancel still returns values.** Even after ESC, `r.name` and other fields contain whatever the user typed before cancelling. This is useful if you want to restore state when reopening the dialog.
155156
- **`button` tells you which button row was clicked.** If you have multiple button rows with different names, this field tells you which group the click came from.
156157

@@ -159,7 +160,7 @@ In most cases, you just need this:
159160
```vim
160161
let r = quickui#dialog#open(items, opts)
161162
162-
if r.button_index >= 1
163+
if r.button_index >= 0 && r.button != ''
163164
" User clicked a button — do something with the values
164165
echo 'Name: ' . r.name
165166
endif
@@ -168,8 +169,8 @@ endif
168169
Or if you have OK and Cancel buttons:
169170

170171
```vim
171-
" ' &OK ' is button 1, ' &Cancel ' is button 2
172-
if r.button_index == 1
172+
" ' &OK ' is button 0, ' &Cancel ' is button 1
173+
if r.button_index == 0 && r.button != ''
173174
echo 'Accepted: ' . r.name
174175
endif
175176
```
@@ -204,8 +205,8 @@ function! NewProject()
204205
let opts = {'title': 'New Project', 'w': 50, 'focus': 'project_name'}
205206
let result = quickui#dialog#open(items, opts)
206207
207-
" Check if the user clicked "Create" (button 1)
208-
if result.button_index == 1
208+
" Check if the user clicked "Create" (button 0)
209+
if result.button_index == 0 && result.button != ''
209210
" dropdown returns an index — convert it to text
210211
let languages = ['Python', 'JavaScript', 'Go', 'Rust', 'C++']
211212
let builds = ['Make', 'CMake', 'Cargo', 'npm', 'pip']

0 commit comments

Comments
 (0)