Skip to content

Commit 0013ffe

Browse files
author
skywind3000
committed
dialog: replace opts.focus with control-level focus field
Move initial focus specification from opts.focus (string name) to a per-control 'focus' field (integer). This allows specifying both which control receives focus and the sub-item index within group controls (button/radio). Deprecated opts.focus now shows a warning message. 🤖 Generated with [Qoder][https://qoder.com]
1 parent 8994ddc commit 0013ffe

4 files changed

Lines changed: 50 additions & 16 deletions

File tree

DIALOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,18 @@ Hotkeys are globally active when focus is **not** on an input. When an input is
350350
| `Up` | Move focus backward (vertical intuition); in vertical radio, moves cursor within items first |
351351
| `Down` | Move focus forward (vertical intuition); in vertical radio, moves cursor within items first |
352352

353-
Initial focus defaults to the first focusable control. Use `opts.focus` to specify initial focus:
353+
Initial focus defaults to the first focusable control. Use the control-level `focus` field to specify initial focus:
354354

355355
```vim
356-
let result = quickui#dialog#open(items, {'focus': 'email'})
356+
let items = [
357+
\ {'type': 'input', 'name': 'email', 'prompt': 'Email:'},
358+
\ {'type': 'button', 'items': [' &OK ', ' &Cancel '], 'focus': 1},
359+
\ ]
360+
let result = quickui#dialog#open(items)
357361
```
358362

363+
The `focus` field is an integer. For button/radio, it specifies the sub-item index (which button or radio item gets cursor). For other controls, the value is ignored — just having the field means "start focus here".
364+
359365
## Layout Rules
360366

361367
### Vertical Stacking

autoload/quickui/dialog.vim

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,19 +1771,31 @@ function! quickui#dialog#open(items, ...) abort
17711771
" -- build focus list --
17721772
call s:build_focus_list(hwnd)
17731773

1774-
" -- initial focus --
1774+
" -- deprecated opts.focus warning --
17751775
if has_key(opts, 'focus')
1776-
let focus_name = opts.focus
1777-
let fi = 0
1778-
for entry in hwnd.focus_list
1779-
if has_key(entry.control, 'name') && entry.control.name ==# focus_name
1780-
let hwnd.focus_index = fi
1781-
break
1782-
endif
1783-
let fi += 1
1784-
endfor
1776+
echohl WarningMsg
1777+
echomsg 'quickui#dialog: opts.focus is deprecated, use control-level "focus" field instead'
1778+
echohl None
17851779
endif
17861780

1781+
" -- initial focus from control-level 'focus' field --
1782+
let fi = 0
1783+
for entry in hwnd.focus_list
1784+
let item = a:items[entry.control.index]
1785+
if has_key(item, 'focus')
1786+
let hwnd.focus_index = fi
1787+
let ctrl = entry.control
1788+
let fv = item.focus
1789+
if ctrl.type ==# 'button' && fv >= 0 && fv < len(ctrl.parsed)
1790+
let ctrl.value = fv
1791+
elseif ctrl.type ==# 'radio' && fv >= 0 && fv < len(ctrl.items)
1792+
let ctrl.cursor = fv
1793+
endif
1794+
break
1795+
endif
1796+
let fi += 1
1797+
endfor
1798+
17871799
" -- select all for initially focused input --
17881800
if len(hwnd.focus_list) > 0
17891801
let fc = hwnd.focus_list[hwnd.focus_index].control

docs/dialog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ quickui#dialog#open(items, opts)
216216
+-- s:calc_width() -> hwnd.w
217217
+-- s:calc_layout() -> prompt alignment, alignment inflation width check, line_start, content_h
218218
+-- s:build_focus_list() -> focus_list
219-
+-- opts.focus -> set initial focus
219+
+-- control-level 'focus' field -> set initial focus + sub-index
220+
+-- deprecated opts.focus -> warning message
220221
+-- initial focus is input -> s:input_select_all() to select all content
221222
+-- s:build_keymap() -> keymap (with conflict detection)
222223
+-- compute hwnd.sep_char (obtain horizontal line character from border style)

test/test_dialog.vim

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ function! Test_dialog_basic()
2828
endfunc
2929

3030

31+
"----------------------------------------------------------------------
32+
" focus test: control-level focus field
33+
"----------------------------------------------------------------------
34+
function! Test_dialog_focus()
35+
let items = [
36+
\ {'type': 'input', 'name': 'user', 'prompt': 'User:',
37+
\ 'value': 'admin'},
38+
\ {'type': 'input', 'name': 'pass', 'prompt': 'Pass:'},
39+
\ {'type': 'button', 'name': 'confirm',
40+
\ 'items': [' &Login ', ' &Cancel '], 'focus': 0},
41+
\ ]
42+
let result = quickui#dialog#open(items, {'title': 'Focus Test'})
43+
echo result
44+
endfunc
45+
46+
3147
"----------------------------------------------------------------------
3248
" multi button rows test
3349
"----------------------------------------------------------------------
@@ -141,7 +157,7 @@ function! Test_dialog_project_form()
141157
let items = [
142158
\ {'type': 'label', 'text': 'Create New Project:'},
143159
\ {'type': 'input', 'name': 'project_name', 'prompt': 'Project:',
144-
\ 'value': 'my-app'},
160+
\ 'value': 'my-app', 'focus': 0},
145161
\ {'type': 'input', 'name': 'email', 'prompt': 'Email:',
146162
\ 'value': 'dev@example.com'},
147163
\ {'type': 'dropdown', 'name': 'language', 'prompt': 'Language:',
@@ -158,8 +174,7 @@ function! Test_dialog_project_form()
158174
\ 'items': [' &Create ', ' Cancel ']},
159175
\ ]
160176
let opts = {'title': 'New Project', 'w': 50,
161-
\ 'validator': function('s:validate_project'),
162-
\ 'focus': 'project_name'}
177+
\ 'validator': function('s:validate_project')}
163178
let result = quickui#dialog#open(items, opts)
164179
if result.button_index >= 0
165180
echo 'Project: ' . result.project_name

0 commit comments

Comments
 (0)