Improve UI

- text no longer jumps around when an error message is displayed
- you can only type valid room names
- esc exits the application
This commit is contained in:
Joscha 2019-05-15 16:59:09 +00:00
parent 43af84a395
commit c676d71a61

View file

@ -9,8 +9,9 @@ __all__ = ["SingleRoomApplication"]
class ChooseRoomWidget(urwid.WidgetWrap): class ChooseRoomWidget(urwid.WidgetWrap):
def __init__(self) -> None: def __init__(self) -> None:
self.text = urwid.Text("Choose a room:", align="center") self.error = None
self.edit = urwid.Edit("&", align="center") self.text = urwid.Text("Choose a room:", align=urwid.CENTER)
self.edit = urwid.Edit("&", align=urwid.CENTER)
self.pile = urwid.Pile([ self.pile = urwid.Pile([
self.text, self.text,
urwid.AttrMap(self.edit, Style.ROOM), urwid.AttrMap(self.edit, Style.ROOM),
@ -18,8 +19,16 @@ class ChooseRoomWidget(urwid.WidgetWrap):
self.filler = urwid.Filler(self.pile) self.filler = urwid.Filler(self.pile)
super().__init__(self.filler) super().__init__(self.filler)
def render(self, size: Any, focus: Any) -> Any:
if self.error:
width, _ = size
rows = self.error.rows((width,), focus)
self.filler.bottom = rows
return super().render(size, focus)
def set_error(self, text: Any) -> None: def set_error(self, text: Any) -> None:
self.error = urwid.Text(text, align="center") self.error = urwid.Text(text, align=urwid.CENTER)
self.pile = urwid.Pile([ self.pile = urwid.Pile([
self.error, self.error,
self.text, self.text,
@ -45,13 +54,17 @@ class ChooseRoomWidget(urwid.WidgetWrap):
] ]
self.set_error(text) self.set_error(text)
def invalid_room_name(self) -> None: def invalid_room_name(self, reason: str) -> None:
# TODO animate the invalid room name thingy? text = [f"Invalid room name: {reason}\n"]
text = ["Invalid room name.\n"]
self.set_error(text) self.set_error(text)
class SingleRoomApplication(urwid.WidgetWrap): class SingleRoomApplication(urwid.WidgetWrap):
ALPHABET = "abcdefghijklmnopqrstuvwxyz" # The characters in the ALPHABET make up the characters that are allowed in
# room names.
ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789"
# These are other characters or character combinations necessary for the
# editor to function well.
ALLOWED_EDITOR_KEYS = { ALLOWED_EDITOR_KEYS = {
"backspace", "delete", "backspace", "delete",
"left", "right", "left", "right",
@ -71,9 +84,9 @@ class SingleRoomApplication(urwid.WidgetWrap):
def keypress(self, size: Any, key: str) -> Optional[str]: def keypress(self, size: Any, key: str) -> Optional[str]:
if self._w == self.choose_room: if self._w == self.choose_room:
# This leads to the editor jumping around the screen. if key == "esc":
# raise urwid.ExitMainLoop()
# TODO Find a way for the editor to stay still.
self.choose_room.unset_error() self.choose_room.unset_error()
if key == "enter": if key == "enter":
@ -84,6 +97,8 @@ class SingleRoomApplication(urwid.WidgetWrap):
urwid.connect_signal(room, "close", self.switch_to_choose) urwid.connect_signal(room, "close", self.switch_to_choose)
room.connect() room.connect()
self._w = room self._w = room
else:
self.choose_room.invalid_room_name("too short")
# Make sure we only enter valid room names # Make sure we only enter valid room names
elif key.lower() in self.ALPHABET: elif key.lower() in self.ALPHABET: