make key_sep more configurable

and add more configuration to HotkeyLabel and EditField to take
advantage of it
develop
myk002 2022-05-18 16:35:06 -07:00 committed by Myk
parent 53609db1f9
commit 18628ff5b8
3 changed files with 32 additions and 14 deletions

@ -3832,6 +3832,7 @@ Subclass of Widget; implements a simple edit field.
Attributes: Attributes:
:label_text: The optional text label displayed before the editable text.
:text: The current contents of the field. :text: The current contents of the field.
:text_pen: The pen to draw the text with. :text_pen: The pen to draw the text with.
:on_char: Input validation callback; used as ``on_char(new_char,text)``. :on_char: Input validation callback; used as ``on_char(new_char,text)``.
@ -3839,6 +3840,8 @@ Attributes:
:on_change: Change notification callback; used as ``on_change(new_text,old_text)``. :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)``. :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. :key: If specified, the field is disabled until this key is pressed. Must be given as a string.
:key_sep: If specified, will be used to customize how the activation key is
displayed. See ``token.key_sep`` in the ``Label`` documentation below.
Label class Label class
----------- -----------
@ -3907,8 +3910,8 @@ containing newlines, or a table with the following possible fields:
* ``token.key_sep = '...'`` * ``token.key_sep = '...'``
Specifies the separator to place between the keybinding label produced Specifies the separator to place between the keybinding label produced
by ``token.key``, and the main text of the token. If the separator is by ``token.key``, and the main text of the token. If the separator starts with
'()', the token is formatted as ``text..' ('..binding..')'``. Otherwise '()', the token is formatted as ``text..' ('..binding..sep:sub(2)``. Otherwise
it is simply ``binding..sep..text``. it is simply ``binding..sep..text``.
* ``token.enabled``, ``token.disabled`` * ``token.enabled``, ``token.disabled``
@ -3999,6 +4002,8 @@ a hotkey.
It has the following attributes: It has the following attributes:
:key: The hotkey keycode to display, e.g. ``'CUSTOM_A'``. :key: The hotkey keycode to display, e.g. ``'CUSTOM_A'``.
:key_sep: If specified, will be used to customize how the activation key is
displayed. See ``token.key_sep`` in the ``Label`` documentation.
:label: The string (or a function that returns a string) to display after the :label: The string (or a function that returns a string) to display after the
hotkey. hotkey.
:on_activate: If specified, it is the callback that will be called whenever :on_activate: If specified, it is the callback that will be called whenever

@ -48,6 +48,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## API ## API
## Lua ## Lua
- ``widgets.HotkeyLabel``: the ``key_sep`` string is now configurable
- ``widgets.EditField``: the ``key_sep`` string is now configurable
- ``widgets.EditField``: can now display an optional string label in addition to the activation key
# 0.47.05-r5 # 0.47.05-r5

@ -178,14 +178,27 @@ end
EditField = defclass(EditField, Widget) EditField = defclass(EditField, Widget)
EditField.ATTRS{ EditField.ATTRS{
label_text = DEFAULT_NIL,
text = '', text = '',
text_pen = DEFAULT_NIL, text_pen = DEFAULT_NIL,
on_char = DEFAULT_NIL, on_char = DEFAULT_NIL,
on_change = DEFAULT_NIL, on_change = DEFAULT_NIL,
on_submit = DEFAULT_NIL, on_submit = DEFAULT_NIL,
key = DEFAULT_NIL, key = DEFAULT_NIL,
key_sep = DEFAULT_NIL,
} }
function EditField:init()
self:addviews{HotkeyLabel{frame={t=0,l=0},
key=self.key,
key_sep=self.key_sep,
label=self.label_text}}
end
function EditField:postUpdateLayout()
self.text_offset = self.subviews[1]:getTextWidth()
end
function EditField:onRenderBody(dc) function EditField:onRenderBody(dc)
dc:pen(self.text_pen or COLOR_LIGHTCYAN):fill(0,0,dc.width-1,0) dc:pen(self.text_pen or COLOR_LIGHTCYAN):fill(0,0,dc.width-1,0)
@ -194,16 +207,11 @@ function EditField:onRenderBody(dc)
cursor = ' ' cursor = ' '
end end
local txt = self.text .. cursor local txt = self.text .. cursor
local dx = dc.x local max_width = dc.width - self.text_offset
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 if #txt > max_width then
txt = string.char(27)..string.sub(txt, #txt-max_width+2) txt = string.char(27)..string.sub(txt, #txt-max_width+2)
end end
dc:string(txt) dc:advance(self.text_offset):string(txt)
end end
function EditField:onInput(keys) function EditField:onInput(keys)
@ -359,12 +367,13 @@ function render_text(obj,dc,x0,y0,pen,dpen,disabled)
x = x + #keystr x = x + #keystr
if sep == '()' then if sep:startswith('()') then
if dc then if dc then
dc:string(text) dc:string(text)
dc:string(' ('):string(keystr,keypen):string(')') dc:string(' ('):string(keystr,keypen)
dc:string(sep:sub(2))
end end
x = x + 3 x = x + 1 + #sep
else else
if dc then if dc then
dc:string(keystr,keypen):string(sep):string(text) dc:string(keystr,keypen):string(sep):string(text)
@ -605,12 +614,13 @@ HotkeyLabel = defclass(HotkeyLabel, Label)
HotkeyLabel.ATTRS{ HotkeyLabel.ATTRS{
key=DEFAULT_NIL, key=DEFAULT_NIL,
key_sep=': ',
label=DEFAULT_NIL, label=DEFAULT_NIL,
on_activate=DEFAULT_NIL, on_activate=DEFAULT_NIL,
} }
function HotkeyLabel:init() function HotkeyLabel:init()
self:setText{{key=self.key, key_sep=': ', text=self.label, self:setText{{key=self.key, key_sep=self.key_sep, text=self.label,
on_activate=self.on_activate}} on_activate=self.on_activate}}
end end