Skip to content

Commit

Permalink
Merge pull request #48 from penguinolog/urwid_constants
Browse files Browse the repository at this point in the history
Use urwid constants for widgets instead of string equivalents
  • Loading branch information
markqvist authored Jan 18, 2024
2 parents bf0b08c + a876c09 commit 92a49c1
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 248 deletions.
73 changes: 53 additions & 20 deletions nomadnet/ui/textui/Browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ def keypress(self, size, key):
self.delegate.save_node_dialog()
elif key == "ctrl g":
nomadnet.NomadNetworkApp.get_shared_instance().ui.main_display.sub_displays.network_display.toggle_fullscreen()
elif self.get_focus() == "body":
elif self.focus_position == "body":
if key == "down" or key == "up":
try:
if hasattr(self.delegate, "page_pile") and self.delegate.page_pile:
def df(loop, user_data):
st = None
nf = self.delegate.page_pile.get_focus()
nf = self.delegate.page_pile.focus
if hasattr(nf, "key_timeout"):
st = nf
elif hasattr(nf, "original_widget"):
Expand Down Expand Up @@ -279,7 +279,10 @@ def build_display(self):
self.browser_footer = urwid.Text("")

self.page_pile = None
self.browser_body = urwid.Filler(urwid.Text("Disconnected\n"+self.g["arrow_l"]+" "+self.g["arrow_r"], align="center"), "middle")
self.browser_body = urwid.Filler(
urwid.Text("Disconnected\n"+self.g["arrow_l"]+" "+self.g["arrow_r"], align=urwid.CENTER),
urwid.MIDDLE,
)

self.frame = BrowserFrame(self.browser_body, header=self.browser_header, footer=self.browser_footer)
self.frame.delegate = self
Expand All @@ -306,28 +309,29 @@ def back_action(sender):
self.update_display()

columns = urwid.Columns([
("weight", 0.5, urwid.Text(" ")),
(urwid.WEIGHT, 0.5, urwid.Text(" ")),
(8, urwid.Button("Back", on_press=back_action)),
("weight", 0.5, urwid.Text(" "))
(urwid.WEIGHT, 0.5, urwid.Text(" ")),
])

if len(self.attr_maps) > 0:
pile = urwid.Pile([
urwid.Text("!\n\n"+self.status_text()+"\n", align="center"),
columns
urwid.Text("!\n\n"+self.status_text()+"\n", align=urwid.CENTER),
columns
])
else:
pile = urwid.Pile([
urwid.Text("!\n\n"+self.status_text(), align="center")
])
pile = urwid.Pile([urwid.Text("!\n\n"+self.status_text(), align=urwid.CENTER)])

return urwid.Filler(pile, "middle")
return urwid.Filler(pile, urwid.MIDDLE)

def update_display(self):
if self.status == Browser.DISCONECTED:
self.display_widget.set_attr_map({None: "inactive_text"})
self.page_pile = None
self.browser_body = urwid.Filler(urwid.Text("Disconnected\n"+self.g["arrow_l"]+" "+self.g["arrow_r"], align="center"), "middle")
self.browser_body = urwid.Filler(
urwid.Text("Disconnected\n"+self.g["arrow_l"]+" "+self.g["arrow_r"], align=urwid.CENTER),
urwid.MIDDLE,
)
self.browser_footer = urwid.Text("")
self.browser_header = urwid.Text("")
self.linebox.set_title("Remote Node")
Expand All @@ -354,7 +358,10 @@ def update_display(self):

elif self.status <= Browser.REQUEST_SENT:
if len(self.attr_maps) == 0:
self.browser_body = urwid.Filler(urwid.Text("Retrieving\n["+self.current_url()+"]", align="center"), "middle")
self.browser_body = urwid.Filler(
urwid.Text("Retrieving\n["+self.current_url()+"]", align=urwid.CENTER),
urwid.MIDDLE,
)

self.browser_footer = self.make_status_widget()

Expand Down Expand Up @@ -608,7 +615,7 @@ def reload(self):
self.load_page()

def close_dialogs(self):
options = self.delegate.columns.options("weight", self.delegate.right_area_width)
options = self.delegate.columns.options(urwid.WEIGHT, self.delegate.right_area_width)
self.delegate.columns.contents[1] = (self.display_widget, options)

def url_dialog(self):
Expand All @@ -629,17 +636,30 @@ def confirmed(sender):
dialog = UrlDialogLineBox(
urwid.Pile([
e_url,
urwid.Columns([("weight", 0.45, urwid.Button("Cancel", on_press=dismiss_dialog)), ("weight", 0.1, urwid.Text("")), ("weight", 0.45, urwid.Button("Go", on_press=confirmed))])
urwid.Columns([
(urwid.WEIGHT, 0.45, urwid.Button("Cancel", on_press=dismiss_dialog)),
(urwid.WEIGHT, 0.1, urwid.Text("")),
(urwid.WEIGHT, 0.45, urwid.Button("Go", on_press=confirmed)),
])
]), title="Enter URL"
)
e_url.confirmed = confirmed
dialog.confirmed = confirmed
dialog.delegate = self
bottom = self.display_widget

overlay = urwid.Overlay(dialog, bottom, align="center", width=("relative", 65), valign="middle", height="pack", left=2, right=2)
overlay = urwid.Overlay(
dialog,
bottom,
align=urwid.CENTER,
width=(urwid.RELATIVE, 65),
valign=urwid.MIDDLE,
height=urwid.PACK,
left=2,
right=2,
)

options = self.delegate.columns.options("weight", self.delegate.right_area_width)
options = self.delegate.columns.options(urwid.WEIGHT, self.delegate.right_area_width)
self.delegate.columns.contents[1] = (overlay, options)
self.delegate.columns.focus_position = 1

Expand All @@ -663,17 +683,30 @@ def confirmed(sender):
dialog = UrlDialogLineBox(
urwid.Pile([
urwid.Text("Save connected node"+disp_str+" "+RNS.prettyhexrep(self.destination_hash)+" to Known Nodes?\n"),
urwid.Columns([("weight", 0.45, urwid.Button("Cancel", on_press=dismiss_dialog)), ("weight", 0.1, urwid.Text("")), ("weight", 0.45, urwid.Button("Save", on_press=confirmed))])
urwid.Columns([
(urwid.WEIGHT, 0.45, urwid.Button("Cancel", on_press=dismiss_dialog)),
(urwid.WEIGHT, 0.1, urwid.Text("")),
(urwid.WEIGHT, 0.45, urwid.Button("Save", on_press=confirmed)),
])
]), title="Save Node"
)

dialog.confirmed = confirmed
dialog.delegate = self
bottom = self.display_widget

overlay = urwid.Overlay(dialog, bottom, align="center", width=("relative", 50), valign="middle", height="pack", left=2, right=2)
overlay = urwid.Overlay(
dialog,
bottom,
align=urwid.CENTER,
width=(urwid.RELATIVE, 50),
valign=urwid.MIDDLE,
height=urwid.PACK,
left=2,
right=2,
)

options = self.delegate.columns.options("weight", self.delegate.right_area_width)
options = self.delegate.columns.options(urwid.WEIGHT, self.delegate.right_area_width)
self.delegate.columns.contents[1] = (overlay, options)
self.delegate.columns.focus_position = 1

Expand Down
20 changes: 14 additions & 6 deletions nomadnet/ui/textui/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def __init__(self, app):
class ConfigFiller(urwid.WidgetWrap):
def __init__(self, widget, app):
self.app = app
self.filler = urwid.Filler(widget, "top")
self.filler = urwid.Filler(widget, urwid.TOP)
super().__init__(self.filler)


def keypress(self, size, key):
if key == "up":
self.app.ui.main_display.frame.set_focus("header")
self.app.ui.main_display.frame.focus_position = "header"

return super(ConfigFiller, self).keypress(size, key)

Expand All @@ -31,12 +31,20 @@ def open_editor(sender):
self.editor_term = EditorTerminal(self.app, self)
self.widget = urwid.LineBox(self.editor_term)
self.app.ui.main_display.update_active_sub_display()
self.app.ui.main_display.frame.set_focus("body")
self.app.ui.main_display.frame.focus_position = "body"
self.editor_term.term.change_focus(True)

pile = urwid.Pile([
urwid.Text(("body_text", "\nTo change the configuration, edit the config file located at:\n\n"+self.app.configpath+"\n\nRestart Nomad Network for changes to take effect\n"), align="center"),
urwid.Padding(urwid.Button("Open Editor", on_press=open_editor), width=15, align="center"),
urwid.Text(
(
"body_text",
"\nTo change the configuration, edit the config file located at:\n\n"
+self.app.configpath
+"\n\nRestart Nomad Network for changes to take effect\n",
),
align=urwid.CENTER,
),
urwid.Padding(urwid.Button("Open Editor", on_press=open_editor), width=15, align=urwid.CENTER),
])

self.config_explainer = ConfigFiller(pile, self.app)
Expand Down Expand Up @@ -77,5 +85,5 @@ def quit_term(*args, **kwargs):
def keypress(self, size, key):
# TODO: Decide whether there should be a way to get out while editing
#if key == "up":
# nomadnet.NomadNetworkApp.get_shared_instance().ui.main_display.frame.set_focus("header")
# nomadnet.NomadNetworkApp.get_shared_instance().ui.main_display.frame.focus_position = "header"
return super(EditorTerminal, self).keypress(size, key)
Loading

0 comments on commit 92a49c1

Please sign in to comment.