mirror of
https://github.com/ihabunek/toot.git
synced 2025-06-30 22:18:36 -04:00
Slightly redesign the modal to be more like a window
+ Only do a full draw on init, and just refresh on updates / show + Better differenciate "No content" vs "quit" + Reset the cursor position when content is cleared
This commit is contained in:
parent
073d340908
commit
5d652754ec
@ -366,7 +366,10 @@ class EntryModal(Modal):
|
|||||||
window_height, window_width = self.window.getmaxyx()
|
window_height, window_width = self.window.getmaxyx()
|
||||||
draw_lines(self.window, [self.footer], window_height - self.pad_y + 1, 2, Color.WHITE)
|
draw_lines(self.window, [self.footer], window_height - self.pad_y + 1, 2, Color.WHITE)
|
||||||
|
|
||||||
text = ''.join(self.content)
|
self.refresh()
|
||||||
|
|
||||||
|
def refresh(self):
|
||||||
|
text = self.get_content()
|
||||||
lines = text.split('\n')
|
lines = text.split('\n')
|
||||||
draw_lines(self.text_window, lines, 0, 0, Color.WHITE)
|
draw_lines(self.text_window, lines, 0, 0, Color.WHITE)
|
||||||
|
|
||||||
@ -375,40 +378,61 @@ class EntryModal(Modal):
|
|||||||
y, x = size_as_drawn(text_on_screen, text_window_width)
|
y, x = size_as_drawn(text_on_screen, text_window_width)
|
||||||
self.text_window.move(y, x)
|
self.text_window.move(y, x)
|
||||||
|
|
||||||
|
def show(self):
|
||||||
|
super().show()
|
||||||
|
self.refresh()
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
self.content = []
|
||||||
|
self.cursor_pos = 0
|
||||||
|
|
||||||
def do_command(self, ch):
|
def do_command(self, ch):
|
||||||
if curses.ascii.isprint(ch) or ch == curses.ascii.LF:
|
if curses.ascii.isprint(ch) or ch == curses.ascii.LF:
|
||||||
text_window_height, text_window_width = self.text_window.getmaxyx()
|
text_window_height, text_window_width = self.text_window.getmaxyx()
|
||||||
y, x = size_as_drawn((''.join(self.content) + chr(ch)).split('\n'), text_window_width)
|
y, x = size_as_drawn((self.get_content() + chr(ch)).split('\n'), text_window_width)
|
||||||
if y < text_window_height - 1 and x < text_window_width:
|
if y < text_window_height - 1 and x < text_window_width:
|
||||||
self.content.insert(self.cursor_pos, chr(ch))
|
self.content.insert(self.cursor_pos, chr(ch))
|
||||||
self.cursor_pos += 1
|
self.cursor_pos += 1
|
||||||
|
else:
|
||||||
|
curses.beep()
|
||||||
|
|
||||||
elif ch == curses.KEY_BACKSPACE:
|
elif ch == curses.KEY_BACKSPACE:
|
||||||
if self.cursor_pos > 0:
|
if self.cursor_pos > 0:
|
||||||
del self.content[self.cursor_pos - 1]
|
del self.content[self.cursor_pos - 1]
|
||||||
self.cursor_pos -= 1
|
self.cursor_pos -= 1
|
||||||
|
else:
|
||||||
|
curses.beep()
|
||||||
|
|
||||||
elif ch == curses.KEY_DC:
|
elif ch == curses.KEY_DC:
|
||||||
if self.cursor_pos >= 0 and self.cursor_pos < len(self.content):
|
if self.cursor_pos >= 0 and self.cursor_pos < len(self.content):
|
||||||
del self.content[self.cursor_pos]
|
del self.content[self.cursor_pos]
|
||||||
|
else:
|
||||||
|
curses.beep()
|
||||||
|
|
||||||
elif ch == curses.KEY_LEFT:
|
elif ch == curses.KEY_LEFT:
|
||||||
if self.cursor_pos > 0:
|
if self.cursor_pos > 0:
|
||||||
self.cursor_pos -= 1
|
self.cursor_pos -= 1
|
||||||
|
else:
|
||||||
|
curses.beep()
|
||||||
|
|
||||||
elif ch == curses.KEY_RIGHT:
|
elif ch == curses.KEY_RIGHT:
|
||||||
if self.cursor_pos + 1 <= len(self.content):
|
if self.cursor_pos + 1 <= len(self.content):
|
||||||
self.cursor_pos += 1
|
self.cursor_pos += 1
|
||||||
|
else:
|
||||||
|
curses.beep()
|
||||||
|
|
||||||
elif ch in (curses.ascii.RS, curses.ascii.EOT):
|
elif ch in (curses.ascii.RS, curses.ascii.EOT):
|
||||||
return False
|
return False, False
|
||||||
|
|
||||||
elif ch == curses.ascii.ESC:
|
elif ch == curses.ascii.ESC:
|
||||||
self.content = []
|
self.clear()
|
||||||
return False
|
return False, True
|
||||||
|
|
||||||
self.draw()
|
self.refresh()
|
||||||
return True
|
return True, False
|
||||||
|
|
||||||
|
def get_content(self):
|
||||||
|
return ''.join(self.content)
|
||||||
|
|
||||||
def loop(self):
|
def loop(self):
|
||||||
self.show()
|
self.show()
|
||||||
@ -416,10 +440,14 @@ class EntryModal(Modal):
|
|||||||
ch = self.text_window.getch()
|
ch = self.text_window.getch()
|
||||||
if not ch:
|
if not ch:
|
||||||
continue
|
continue
|
||||||
if not self.do_command(ch):
|
should_continue, abort_flag = self.do_command(ch)
|
||||||
|
if not should_continue:
|
||||||
break
|
break
|
||||||
self.hide()
|
self.hide()
|
||||||
return ''.join(self.content)
|
if abort_flag:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return self.get_content()
|
||||||
|
|
||||||
|
|
||||||
class ComposeModal(EntryModal):
|
class ComposeModal(EntryModal):
|
||||||
@ -432,7 +460,7 @@ class ComposeModal(EntryModal):
|
|||||||
if ch == curses.ascii.ctrl(ord('s')):
|
if ch == curses.ascii.ctrl(ord('s')):
|
||||||
self.cw = self.cwmodal.loop() or None
|
self.cw = self.cwmodal.loop() or None
|
||||||
self.draw()
|
self.draw()
|
||||||
return True
|
return True, False
|
||||||
else:
|
else:
|
||||||
return super().do_command(ch)
|
return super().do_command(ch)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user