Skip to content

Commit

Permalink
Refactor local _input to use async rather than callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbueno committed May 31, 2021
1 parent 8fad15e commit 64e9604
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions terminedia_paint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,31 +175,17 @@ def mouse_move(self, event):
async def _input(self, label, pos=(0,3), default="", width=30):

text = default
text_picked = False
def _pick_text(entry, event=None):
nonlocal text_picked, text
if not entry.value:
return
text = entry.value
self.file_name = entry.value

text_picked = True
widget.kill()
label_w.kill()
label_w = TM.widgets.Label(self.sc, pos=pos, text=label)

def _cancel(entry, event=None):
nonlocal text_picked, text
widget = TM.widgets.Entry(self.sc, value=text, pos=V2(pos) + (len(label) + 1, 0), width=width, cancellable=True)
try:
text = await widget
except TM.widgets.WidgetCancelled:
text = ""
text_picked = True
widget.kill()
finally:
label_w.kill()

label_w = TM.widgets.Label(self.sc, pos=pos, text=label)
widget = TM.widgets.Entry(self.sc, value=text, pos=V2(pos) + (len(label) + 1, 0), width=width, enter_callback=_pick_text, esc_callback=_cancel)

while not text_picked:
await asyncio.sleep(0.1)

return text

async def insert_image(self, event=None):
Expand Down Expand Up @@ -283,10 +269,10 @@ async def pick_character(self, options):
return
char = char[0]
elif char == "search":
try:
search = await self._input("Unicode char name :", width=40)
except WidgetCancelled:
search = await self._input("Unicode char name :", width=40)
if not search:
return

options = TM.unicode.lookup(search)
if not options:
self._message("Character not found")
Expand Down

0 comments on commit 64e9604

Please sign in to comment.