Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOME and END in TextBoxPopup places cursor outside of popup box #180

Closed
pkazmier opened this issue Aug 30, 2022 · 1 comment
Closed

HOME and END in TextBoxPopup places cursor outside of popup box #180

pkazmier opened this issue Aug 30, 2022 · 1 comment
Labels
Bug Something isn't working

Comments

@pkazmier
Copy link

Describe the bug
show_text_box_popup does not properly position cursor when HOME and END keys are pressed. Instead of positioning at the start of the popup input box, HOME sends the cursor to the start of the line (left edge of terminal). END positions the cursor offset from the start of the line instead of the start of the popup box.

To Reproduce
Steps to reproduce the behavior:

  1. Open a popup text box.
  2. Enter text.
  3. Press HOME key and press END key.

Expected behavior
HOME and END should position the cursor within the bounding box of the popup.

Root Cause
Root cause is that _initial_cursor position of the TextBoxImplementation is always set to 0.

self._initial_cursor = 0

As a workaround, I have to use this hack:

# TODO Workaround until upstream py_cui fixes bug
root._popup._initial_cursor = root._popup.get_start_position()[0] + root._popup.get_padding()[0] + 2

I believe the fix for this is modifying update_height_width of TextBoxPopup:

    def update_height_width(self) -> None:
        """Need to update all cursor positions on resize
        """

        Popup.update_height_width(self)
        padx, pady              = self.get_padding()
        start_x, start_y        = self.get_start_position()
        height, width           = self.get_absolute_dimensions()
        self._cursor_text_pos   = 0
        self._cursor_x          = start_x + 2 + padx
        ###################################
        self._initial_cursor = self._cursor_x
        ###################################
        self._cursor_max_left   = self._cursor_x
        self._cursor_max_right  = start_x + width - 1 - pady
        self._cursor_y          = start_y + int(height / 2) + 1
        self._viewport_width    = self._cursor_max_right - self._cursor_max_left

Because the _jump_to_start and _jump_to_end methods in TextBoxImplemenation use the _initial_cursor position to place the cursor, which is currently always set to 0 thus causing the issue.

py_cui/py_cui/ui.py

Lines 625 to 638 in f1173ca

def _jump_to_start(self) -> None:
"""Jumps to the start of the textbox. Internal use only
"""
self._cursor_x = self._initial_cursor
self._cursor_text_pos = 0
def _jump_to_end(self) -> None:
"""Jumps to the end to the textbox. Internal use only
"""
self._cursor_text_pos = len(self._text)
self._cursor_x = self._initial_cursor + self._cursor_text_pos

Environment:

  • OS: macOS Monterey
  • Terminal: iTerm2
  • Version: 3.4.16
@pkazmier pkazmier added the Bug Something isn't working label Aug 30, 2022
@jwlodek
Copy link
Owner

jwlodek commented Sep 28, 2022

Good catch! Reproduced the issue with the popups example. Applied your fix and it is now working:

d5534cb

@jwlodek jwlodek mentioned this issue Sep 28, 2022
3 tasks
@jwlodek jwlodek closed this as completed Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants