diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 4053826cf..ff34008e1 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -307,10 +307,10 @@ function EditField:onInput(keys) elseif keys.CURSOR_LEFT then self:setCursor(self.cursor - 1) return true - elseif keys.A_MOVE_W_DOWN then -- Ctrl-Left (prev word start) - local _, prev_word_start = self.text:sub(1, self.cursor-1): - find('.*[^%w_%-]+[%w_%-]') - self:setCursor(prev_word_start or 1) + elseif keys.A_MOVE_W_DOWN then -- Ctrl-Left (end of prev 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.A_CARE_MOVE_W then -- Alt-Left (home) self:setCursor(1) @@ -318,9 +318,9 @@ function EditField:onInput(keys) elseif keys.CURSOR_RIGHT then self:setCursor(self.cursor + 1) return true - elseif keys.A_MOVE_E_DOWN then -- Ctrl-Right (next word end) - local _, next_word_end = self.text:find('[%w_%-]+[^%w_%-]', self.cursor) - self:setCursor(next_word_end) + elseif keys.A_MOVE_E_DOWN then -- Ctrl-Right (beginning of next word) + local _,next_word_start = self.text:find('[^%w_%-][%w_%-]', self.cursor) + self:setCursor(next_word_start) return true elseif keys.A_CARE_MOVE_E then -- Alt-Right (end) self:setCursor() diff --git a/test/library/gui/widgets.EditField.lua b/test/library/gui/widgets.EditField.lua new file mode 100644 index 000000000..15acfddb0 --- /dev/null +++ b/test/library/gui/widgets.EditField.lua @@ -0,0 +1,56 @@ +local widgets = require('gui.widgets') + +function test.editfield_cursor() + local e = widgets.EditField{} + e:setFocus(true) + expect.eq(1, e.cursor, 'cursor should be after the empty string') + + e:onInput{_STRING=string.byte('a')} + expect.eq('a', e.text) + expect.eq(2, e.cursor) + + e:setText('one two three') + expect.eq(14, e.cursor, 'cursor should be after the last char') + e:onInput{_STRING=string.byte('s')} + expect.eq('one two threes', e.text) + expect.eq(15, e.cursor) + + e:setCursor(4) + e:onInput{_STRING=string.byte('s')} + expect.eq('ones two threes', e.text) + expect.eq(5, e.cursor) + + e:onInput{CURSOR_LEFT=true} + expect.eq(4, e.cursor) + e:onInput{CURSOR_RIGHT=true} + expect.eq(5, e.cursor) + e:onInput{A_CARE_MOVE_W=true} + expect.eq(1, e.cursor, 'interpret alt-left as home') + e:onInput{A_MOVE_E_DOWN=true} + expect.eq(6, e.cursor, 'interpret ctrl-right as goto beginning of next word') + e:onInput{A_CARE_MOVE_E=true} + expect.eq(16, e.cursor, 'interpret alt-right as end') + e:onInput{A_MOVE_W_DOWN=true} + expect.eq(9, e.cursor, 'interpret ctrl-left as goto end of previous word') +end + +function test.editfield_click() + local e = widgets.EditField{text='word'} + e:setFocus(true) + expect.eq(5, e.cursor) + + mock.patch(e, 'getMousePos', mock.func(0), function() + e:onInput{_MOUSE_L=true} + expect.eq(1, e.cursor) + end) + + mock.patch(e, 'getMousePos', mock.func(20), function() + e:onInput{_MOUSE_L=true} + expect.eq(5, e.cursor, 'should only seek to end of text') + end) + + mock.patch(e, 'getMousePos', mock.func(2), function() + e:onInput{_MOUSE_L=true} + expect.eq(3, e.cursor) + end) +end diff --git a/test/library/gui/widgets.lua b/test/library/gui/widgets.lua index 1eed30e4f..95dbd34f1 100644 --- a/test/library/gui/widgets.lua +++ b/test/library/gui/widgets.lua @@ -1,18 +1,37 @@ local widgets = require('gui.widgets') +function test.hotkeylabel_click() + local func = mock.func() + local l = widgets.HotkeyLabel{key='SELECT', on_activate=func} + + mock.patch(l, 'getMousePos', mock.func(0), function() + l:onInput{_MOUSE_L=true} + expect.eq(1, func.call_count) + end) +end + function test.togglehotkeylabel() - local toggle = widgets.ToggleHotkeyLabel{} - expect.true_(toggle:getOptionValue()) - toggle:cycle() - expect.false_(toggle:getOptionValue()) - toggle:cycle() - expect.true_(toggle:getOptionValue()) + local toggle = widgets.ToggleHotkeyLabel{} + expect.true_(toggle:getOptionValue()) + toggle:cycle() + expect.false_(toggle:getOptionValue()) + toggle:cycle() + expect.true_(toggle:getOptionValue()) end function test.togglehotkeylabel_default_value() - local toggle = widgets.ToggleHotkeyLabel{initial_option=2} - expect.false_(toggle:getOptionValue()) + local toggle = widgets.ToggleHotkeyLabel{initial_option=2} + expect.false_(toggle:getOptionValue()) + + toggle = widgets.ToggleHotkeyLabel{initial_option=false} + expect.false_(toggle:getOptionValue()) +end - toggle = widgets.ToggleHotkeyLabel{initial_option=false} - expect.false_(toggle:getOptionValue()) +function test.togglehotkeylabel_click() + local l = widgets.ToggleHotkeyLabel{} + expect.true_(l:getOptionValue()) + mock.patch(l, 'getMousePos', mock.func(0), function() + l:onInput{_MOUSE_L=true} + expect.false_(l:getOptionValue()) + end) end