Commit bfd85982 authored by Fjen Undso's avatar Fjen Undso
Browse files

add mouse support

parent e07c9a28
......@@ -24,3 +24,13 @@ class Focusable(ABC):
key: The key to send.
"""
pass
@abstractmethod
def send_mouse(self, mx, my):
"""Send a mouse event to this object.
Args:
mx: Mouse x position.
my: Mouse x position.
"""
pass
......@@ -30,13 +30,16 @@ class CheckBox(Drawable):
self.active = False
Drawable.__init__(self, window=window, row=row, col=col)
def _get_text(self):
prefix = "[X] " if self.active else "[ ] "
return "{}{}".format(prefix, self.text)
def draw(self):
attr = curses.A_NORMAL
if self.focus:
attr = curses.A_STANDOUT
self.window.move(self.row, self.col)
prefix = "[X] " if self.active else "[ ] "
self.window.addstr(prefix + self.text, attr)
self.window.addstr(self._get_text(), attr)
self.window.refresh()
def send_key(self, key):
......@@ -49,6 +52,10 @@ class CheckBox(Drawable):
if key == self.toggle_key:
self.toggle()
def send_mouse(self, mx, my):
if my == self.row and self.col <= mx <= self.col + len(self._get_text()):
self.toggle()
def toggle(self):
self.active = not self.active
self.draw()
......@@ -52,6 +52,10 @@ class FocusSwitcher(Focusable):
for e in self.elements:
e.send_key(key)
def send_mouse(self, mx, my):
for e in self.elements:
e.send_mouse(mx, my)
def next(self):
"""Set focus to next element."""
for i in range(len(self.elements)):
......
......@@ -10,16 +10,29 @@ class NumberBarContainer(FocusSwitcher):
def __init__(self, *, elements=None):
super().__init__(elements=elements)
def _get_progressbar(self):
for e in self.elements:
if isinstance(e, ProgressBar):
return e
def _get_numberrange(self):
for e in self.elements:
if isinstance(e, NumberRange):
return e
def send_key(self, key):
"""Same as FocusSwitcher, but sets ProgressBar.value to NumberRange.value"""
super().send_key(key)
val = 0
for e in self.elements:
if isinstance(e, NumberRange):
val = e.value
for e in self.elements:
if isinstance(e, ProgressBar):
e.set(val)
val = self._get_numberrange().value
self._get_progressbar().set(val)
def send_mouse(self, mx, my):
"""Set the progress to the approx mouse position in ProgressBar."""
p = self._get_progressbar()
if p.row - 1 <= my <= p.row + p.height and p.col - 1 <= mx <= p.col + p.width:
val = 255 - (my - p.row) / p.height * 255
self._get_numberrange().set(val)
p.set(val)
def toggle_focus(self):
"""All elements focus state will be toggled"""
......
......@@ -65,6 +65,9 @@ class NumberRange(Drawable):
# add number on right side
self.set(int(str(self.value) + key))
def send_mouse(self, mx, my):
pass
def set(self, value):
"""Sets number value and respects range.
......
......@@ -71,3 +71,6 @@ class ProgressBar(Drawable):
def send_key(self, key):
pass
def send_mouse(self, mx, my):
pass
......@@ -158,6 +158,12 @@ class NCHL:
k = self.screen.getkey()
self.logger.info(k)
# mouse
if k == "KEY_MOUSE":
_, mx, my, _, _ = curses.getmouse()
self.logger.info(f"mouse: {mx} {my}")
self.fs_main.send_mouse(mx, my)
# send key to main window
self.fs_main.send_key(k)
......
Subproject commit 3ef088e61b55930428223f2c1440b062c5972a1a
Subproject commit 31a1e0b9bb5782e98cd7d9748efd7ed2fd7afb18
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment