From 55cdaea63698c3d2af99f8d43e1470fdc30cb325 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Tue, 3 Jan 2023 12:57:02 -0800 Subject: [PATCH 1/2] refactor EditField text insertion --- library/lua/gui/widgets.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index ba61bab0e..c3d89c49b 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -657,6 +657,12 @@ function EditField:onRenderBody(dc) dc:string((' '):rep(dc.clip_x2 - dc.x)) end +function EditField:insert(text) + local old = self.text + self:setText(old:sub(1,self.cursor-1)..text..old:sub(self.cursor), + self.cursor + #text) +end + function EditField:onInput(keys) if not self.focus then -- only react to our hotkey @@ -713,8 +719,7 @@ function EditField:onInput(keys) else local cv = string.char(keys._STRING) if not self.on_char or self.on_char(cv, old) then - self:setText(old:sub(1,self.cursor-1)..cv..old:sub(self.cursor), - self.cursor + 1) + self:insert(cv) elseif self.on_char then return self.modal end From b766b727845beb9a62df1b190c68882fc02b4401 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Tue, 3 Jan 2023 13:06:57 -0800 Subject: [PATCH 2/2] update docs for widgets.EditField --- docs/dev/Lua API.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index e6cd2f9e8..83a99e5e6 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -4350,6 +4350,23 @@ following keyboard hotkeys: - Ctrl-B/Ctrl-F: move the cursor one word back or forward. - Ctrl-A/Ctrl-E: move the cursor to the beginning/end of the text. +The ``EditField`` class also provides the following functions: + +* ``editfield:setCursor([cursor_pos])`` + + Sets the text insert cursor to the specified position. If ``cursor_pos`` is + not specified or is past the end of the current text string, the cursor will + be set to the end of the current input (that is, ``#editfield.text + 1``). + +* ``editfield:setText(text[, cursor_pos])`` + + Sets the input text string and, optionally, the cursor position. If the + cursor position is not specified, it sets it to the end of the string. + +* ``editfield:insert(text)`` + + Inserts the given text at the current cursor position. + Scrollbar class ---------------