Skip to content

Commit

Permalink
Revert "[widgets.EditField] use new text navigation keys"
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 authored Sep 14, 2024
1 parent 232ebeb commit 9cb260d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
3 changes: 0 additions & 3 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ Template for new versions:
- `buildingplan`: only consider building materials that can be accessed by at least one citizen/resident
- Dreamfort: integrate with `preserve-rooms` to assign relevant rooms to nobles/adimistrators
- Dreamfort: smooth tiles under statues and other large furniture that you can't easily smooth later
- DFHack text edit fields now delete the character at the cursor when you hit the Delete key
- DFHack text edit fields now move the cursor by one word left or right with Ctrl-Left and Ctrl-Right
- DFHack text edit fields now move the cursor to the beginning or end of the line with Home and End

## Documentation
- add documentation for ``dfhack.items.findType(string)`` and ``dfhack.items.findSubtype(string)``
Expand Down
6 changes: 3 additions & 3 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5478,9 +5478,9 @@ The ``EditField`` cursor can be moved to where you want to insert/remove text.
You can click where you want the cursor to move or you can use any of the
following keyboard hotkeys:

- Left/Right arrow: move the cursor one character to the left or right
- Ctrl-Left/Ctrl-Right: move the cursor one word back or forward
- Home/End: move the cursor to the beginning/end of the text
- Left/Right arrow: move the cursor one character to the left or right.
- 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 widget also supports integration with the system clipboard:

Expand Down
23 changes: 9 additions & 14 deletions library/lua/gui/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -988,20 +988,14 @@ function EditField:onInput(keys)
end
end
return not not self.key
elseif keys.CUSTOM_DELETE then
local old = self.text
local del_pos = self.cursor
if del_pos <= #old then
self:setText(old:sub(1, del_pos-1) .. old:sub(del_pos+1), del_pos)
end
return true
elseif keys._STRING then
local old = self.text
if keys._STRING == 0 then
-- handle backspace
local del_pos = self.cursor - 1
if del_pos > 0 then
self:setText(old:sub(1, del_pos-1) .. old:sub(del_pos+1), del_pos)
self:setText(old:sub(1, del_pos-1) .. old:sub(del_pos+1),
del_pos)
end
else
local cv = string.char(keys._STRING)
Expand All @@ -1015,22 +1009,23 @@ function EditField:onInput(keys)
elseif keys.KEYBOARD_CURSOR_LEFT then
self:setCursor(self.cursor - 1)
return true
elseif keys.CUSTOM_CTRL_LEFT then -- back one word
elseif keys.CUSTOM_CTRL_B then -- back one word
local _, prev_word_end = self.text:sub(1, self.cursor-1):
find('.*[%w_%-][^%w_%-]')
self:setCursor(prev_word_end or 1)
return true
elseif keys.CUSTOM_HOME then
self:setCursor(1)
return true
-- commented out until we get HOME key support from DF
-- elseif keys.CUSTOM_CTRL_A then -- home
-- self:setCursor(1)
-- return true
elseif keys.KEYBOARD_CURSOR_RIGHT then
self:setCursor(self.cursor + 1)
return true
elseif keys.CUSTOM_CTRL_RIGHT then -- forward one word
elseif keys.CUSTOM_CTRL_F then -- forward one word
local _,next_word_start = self.text:find('[^%w_%-][%w_%-]', self.cursor)
self:setCursor(next_word_start)
return true
elseif keys.CUSTOM_END then
elseif keys.CUSTOM_CTRL_E then -- end
self:setCursor()
return true
elseif keys.CUSTOM_CTRL_C then
Expand Down
16 changes: 8 additions & 8 deletions test/library/gui/widgets.EditField.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ function test.editfield_cursor()
expect.eq(4, e.cursor)
e:onInput{KEYBOARD_CURSOR_RIGHT=true}
expect.eq(5, e.cursor)
e:onInput{CUSTOM_HOME=true}
expect.eq(1, e.cursor, 'cursor should be at beginning of string')
e:onInput{CUSTOM_CTRL_RIGHT=true}
expect.eq(6, e.cursor, 'goto beginning of next word')
e:onInput{CUSTOM_END=true}
expect.eq(16, e.cursor, 'cursor should be at end of string')
e:onInput{CUSTOM_CTRL_LEFT=true}
expect.eq(9, e.cursor, 'goto end of previous word')
-- e:onInput{A_CARE_MOVE_W=true}
-- expect.eq(1, e.cursor, 'interpret alt-left as home') -- uncomment when we have a home key
e:onInput{CUSTOM_CTRL_F=true}
expect.eq(6, e.cursor, 'interpret Ctrl-f as goto beginning of next word')
e:onInput{CUSTOM_CTRL_E=true}
expect.eq(16, e.cursor, 'interpret Ctrl-e as end')
e:onInput{CUSTOM_CTRL_B=true}
expect.eq(9, e.cursor, 'interpret Ctrl-b as goto end of previous word')
end

function test.editfield_click()
Expand Down

0 comments on commit 9cb260d

Please sign in to comment.