Coding style.

- Use constants instead of strings for mouse actions.
This commit is contained in:
Andrew Hamilton 2021-04-13 20:54:55 +10:00
parent 7cfcc56250
commit ce8ac8f6e1
2 changed files with 21 additions and 12 deletions

View file

@ -651,10 +651,10 @@ class Help:
def on_mouse_input(self, term_code, appearance_changed_event):
event = terminal.decode_mouse_input(term_code)
if event[1] == 4: # Mouse wheel up
if event[1] == terminal.WHEEL_UP_MOUSE:
self.view.portal.scroll_up()
appearance_changed_event.set()
elif event[1] == 5: # Mouse wheel down
elif event[1] == terminal.WHEEL_DOWN_MOUSE:
self.view.portal.scroll_down()
appearance_changed_event.set()
@ -935,10 +935,10 @@ class Screen:
term_code, self._appearance_changed_event)
return
event = terminal.decode_mouse_input(term_code)
if event[0] not in ["mouse press", "mouse drag"]:
if event[0] not in [terminal.PRESS_MOUSE, terminal.DRAG_MOUSE]:
return
x, y = event[2:4]
if event[0] == "mouse drag":
if event[0] == terminal.DRAG_MOUSE:
last_x, last_y = self._last_mouse_position
dx, dy = x - last_x, y - last_y
if self._is_summary_focused:
@ -946,9 +946,9 @@ class Screen:
else:
self._move_listing((-dx, -dy))
else: # Mouse press
if event[1] == 4: # Mouse wheel up
if event[1] == terminal.WHEEL_UP_MOUSE:
self.listing_page_up()
elif event[1] == 5: # Mouse wheel down
elif event[1] == terminal.WHEEL_DOWN_MOUSE:
self.listing_page_down()
else:
view_width, view_height = \

View file

@ -77,20 +77,29 @@ def interactive():
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_termios_settings)
RELEASE_MOUSE = 0
DRAG_MOUSE = 1
CLICK_MOUSE = 2
PRESS_MOUSE = 3
WHEEL_UP_MOUSE = 4
WHEEL_DOWN_MOUSE = 5
def decode_mouse_input(term_code):
keys = [ord(byte) for byte in term_code]
b = keys[0] - 32
x, y = (keys[1] - 33) % 256, (keys[2] - 33) % 256
button = ((b & 64) / 64 * 3) + (b & 3) + 1
if b & 3 == 3:
action = "release"
action = RELEASE_MOUSE
button = 0
elif b & 2048:
action = "release"
action = RELEASE_MOUSE
elif b & 32:
action = "drag"
action = DRAG_MOUSE
elif b & 1536:
action = "click"
action = CLICK_MOUSE
else:
action = "press"
return ("mouse " + action, button, x, y)
action = PRESS_MOUSE
return (action, button, x, y)