Skip to content

Commit cc0220f

Browse files
committed
Add a "key" option to EditField and FilteredList
1 parent 6af5f3b commit cc0220f

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

docs/Lua API.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,6 +3071,7 @@ Attributes:
30713071
If it returns false, the character is ignored.
30723072
:on_change: Change notification callback; used as ``on_change(new_text,old_text)``.
30733073
:on_submit: Enter key callback; if set the field will handle the key and call ``on_submit(text)``.
3074+
:key: If specified, the field is disabled until this key is pressed. Must be given as a string.
30743075

30753076
Label class
30763077
-----------
@@ -3258,11 +3259,13 @@ supports:
32583259

32593260
:edit_pen: If specified, used instead of ``cursor_pen`` for the edit field.
32603261
:edit_below: If true, the edit field is placed below the list instead of above.
3262+
:edit_key: If specified, the edit field is disabled until this key is pressed.
32613263
:not_found_label: Specifies the text of the label shown when no items match the filter.
32623264

32633265
The list choices may include the following attributes:
32643266

32653267
:search_key: If specified, used instead of **text** to match against the filter.
3268+
This is required for any entries where **text** is not a string.
32663269

32673270
The widget implements:
32683271

@@ -3274,6 +3277,10 @@ The widget implements:
32743277

32753278
Returns the list of *all* choices.
32763279

3280+
* ``list:getVisibleChoices()``
3281+
3282+
Returns the *filtered* list of choices.
3283+
32773284
* ``list:getFilter()``
32783285

32793286
Returns the current filter string, and the *filtered* list of choices.

library/lua/gui/widgets.lua

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ EditField.ATTRS{
125125
on_char = DEFAULT_NIL,
126126
on_change = DEFAULT_NIL,
127127
on_submit = DEFAULT_NIL,
128+
key = DEFAULT_NIL,
128129
}
129130

130131
function EditField:onRenderBody(dc)
@@ -135,8 +136,14 @@ function EditField:onRenderBody(dc)
135136
cursor = ' '
136137
end
137138
local txt = self.text .. cursor
138-
if #txt > dc.width then
139-
txt = string.char(27)..string.sub(txt, #txt-dc.width+2)
139+
local dx = dc.x
140+
if self.key then
141+
dc:key_string(self.key, '')
142+
end
143+
dx = dc.x - dx
144+
local max_width = dc.width - dx
145+
if #txt > max_width then
146+
txt = string.char(27)..string.sub(txt, #txt-max_width+2)
140147
end
141148
dc:string(txt)
142149
end
@@ -649,6 +656,7 @@ FilteredList = defclass(FilteredList, Widget)
649656

650657
FilteredList.ATTRS {
651658
edit_below = false,
659+
edit_key = DEFAULT_NIL,
652660
}
653661

654662
function FilteredList:init(info)
@@ -657,6 +665,8 @@ function FilteredList:init(info)
657665
frame = { l = info.icon_width, t = 0, h = 1 },
658666
on_change = self:callback('onFilterChange'),
659667
on_char = self:callback('onFilterChar'),
668+
key = self.edit_key,
669+
active = (self.edit_key == nil),
660670
}
661671
self.list = List{
662672
frame = { t = 2 },
@@ -701,10 +711,24 @@ function FilteredList:init(info)
701711
end
702712
end
703713

714+
function FilteredList:onInput(keys)
715+
if self.edit_key and keys[self.edit_key] and not self.edit.active then
716+
self.edit.active = true
717+
elseif keys.LEAVESCREEN and self.edit.active then
718+
self.edit.active = false
719+
else
720+
self:inputToSubviews(keys)
721+
end
722+
end
723+
704724
function FilteredList:getChoices()
705725
return self.choices
706726
end
707727

728+
function FilteredList:getVisibleChoices()
729+
return self.list.choices
730+
end
731+
708732
function FilteredList:setChoices(choices, pos)
709733
choices = choices or {}
710734
self.choices = choices

0 commit comments

Comments
 (0)