From cc0220f030a94715a2d65042993611952e196bdf Mon Sep 17 00:00:00 2001 From: lethosor Date: Tue, 27 Jun 2017 21:10:14 -0400 Subject: [PATCH] Add a "key" option to EditField and FilteredList --- docs/Lua API.rst | 7 +++++++ library/lua/gui/widgets.lua | 28 ++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/Lua API.rst b/docs/Lua API.rst index 52470225b..dce48081a 100644 --- a/docs/Lua API.rst +++ b/docs/Lua API.rst @@ -3071,6 +3071,7 @@ Attributes: If it returns false, the character is ignored. :on_change: Change notification callback; used as ``on_change(new_text,old_text)``. :on_submit: Enter key callback; if set the field will handle the key and call ``on_submit(text)``. +:key: If specified, the field is disabled until this key is pressed. Must be given as a string. Label class ----------- @@ -3258,11 +3259,13 @@ supports: :edit_pen: If specified, used instead of ``cursor_pen`` for the edit field. :edit_below: If true, the edit field is placed below the list instead of above. +:edit_key: If specified, the edit field is disabled until this key is pressed. :not_found_label: Specifies the text of the label shown when no items match the filter. The list choices may include the following attributes: :search_key: If specified, used instead of **text** to match against the filter. + This is required for any entries where **text** is not a string. The widget implements: @@ -3274,6 +3277,10 @@ The widget implements: Returns the list of *all* choices. +* ``list:getVisibleChoices()`` + + Returns the *filtered* list of choices. + * ``list:getFilter()`` Returns the current filter string, and the *filtered* list of choices. diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index a1124323d..741d722dd 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -125,6 +125,7 @@ EditField.ATTRS{ on_char = DEFAULT_NIL, on_change = DEFAULT_NIL, on_submit = DEFAULT_NIL, + key = DEFAULT_NIL, } function EditField:onRenderBody(dc) @@ -135,8 +136,14 @@ function EditField:onRenderBody(dc) cursor = ' ' end local txt = self.text .. cursor - if #txt > dc.width then - txt = string.char(27)..string.sub(txt, #txt-dc.width+2) + local dx = dc.x + if self.key then + dc:key_string(self.key, '') + end + dx = dc.x - dx + local max_width = dc.width - dx + if #txt > max_width then + txt = string.char(27)..string.sub(txt, #txt-max_width+2) end dc:string(txt) end @@ -649,6 +656,7 @@ FilteredList = defclass(FilteredList, Widget) FilteredList.ATTRS { edit_below = false, + edit_key = DEFAULT_NIL, } function FilteredList:init(info) @@ -657,6 +665,8 @@ function FilteredList:init(info) frame = { l = info.icon_width, t = 0, h = 1 }, on_change = self:callback('onFilterChange'), on_char = self:callback('onFilterChar'), + key = self.edit_key, + active = (self.edit_key == nil), } self.list = List{ frame = { t = 2 }, @@ -701,10 +711,24 @@ function FilteredList:init(info) end end +function FilteredList:onInput(keys) + if self.edit_key and keys[self.edit_key] and not self.edit.active then + self.edit.active = true + elseif keys.LEAVESCREEN and self.edit.active then + self.edit.active = false + else + self:inputToSubviews(keys) + end +end + function FilteredList:getChoices() return self.choices end +function FilteredList:getVisibleChoices() + return self.list.choices +end + function FilteredList:setChoices(choices, pos) choices = choices or {} self.choices = choices