NomadNet/nomadnet/ui/textui/Main.py

205 lines
7.4 KiB
Python
Raw Normal View History

2021-05-04 05:08:16 -04:00
import RNS
2021-05-12 08:02:44 -04:00
import time
2021-05-04 05:08:16 -04:00
from .Network import *
from .Conversations import *
from .Directory import *
2021-06-30 16:17:21 -04:00
from .Config import *
2021-05-04 05:08:16 -04:00
from .Map import *
2021-06-30 16:17:21 -04:00
from .Log import *
from .Guide import *
2021-05-12 08:02:44 -04:00
import urwid
2021-05-04 05:08:16 -04:00
class SubDisplays():
def __init__(self, app):
self.app = app
self.network_display = NetworkDisplay(self.app)
self.conversations_display = ConversationsDisplay(self.app)
self.directory_display = DirectoryDisplay(self.app)
2021-06-30 16:17:21 -04:00
self.config_display = ConfigDisplay(self.app)
2021-05-04 05:08:16 -04:00
self.map_display = MapDisplay(self.app)
2021-06-30 16:17:21 -04:00
self.log_display = LogDisplay(self.app)
self.guide_display = GuideDisplay(self.app)
2021-05-04 05:08:16 -04:00
2021-09-11 06:21:35 -04:00
if app.firstrun:
self.active_display = self.guide_display
else:
self.active_display = self.conversations_display
2021-05-04 05:08:16 -04:00
def active(self):
return self.active_display
2021-05-12 08:02:44 -04:00
class MenuButton(urwid.Button):
button_left = urwid.Text('[')
button_right = urwid.Text(']')
class MainFrame(urwid.Frame):
FOCUS_CHECK_TIMEOUT = 0.25
def __init__(self, body, header=None, footer=None, delegate=None):
self.delegate = delegate
self.current_focus = None
super().__init__(body, header, footer)
def keypress_focus_check(self, deferred=False):
current_focus = self.delegate.widget.get_focus_widgets()[-1]
if deferred:
if current_focus != self.current_focus:
self.focus_changed()
else:
def deferred_focus_check(loop, user_data):
self.keypress_focus_check(deferred=True)
self.delegate.app.ui.loop.set_alarm_in(MainFrame.FOCUS_CHECK_TIMEOUT, deferred_focus_check)
self.current_focus = current_focus
def focus_changed(self):
current_focus = self.delegate.widget.get_focus_widgets()[-1]
current_focus_path = self.delegate.widget.get_focus_path()
2021-05-12 08:02:44 -04:00
if len(current_focus_path) > 1:
if current_focus_path[0] == "body":
self.delegate.update_active_shortcuts()
if self.delegate.sub_displays.active() == self.delegate.sub_displays.conversations_display:
# Needed to refresh indicativelistbox styles on mouse focus change
self.delegate.sub_displays.conversations_display.focus_change_event()
2021-05-12 08:02:44 -04:00
def mouse_event(self, size, event, button, col, row, focus):
current_focus = self.delegate.widget.get_focus_widgets()[-1]
if current_focus != self.current_focus:
self.focus_changed()
self.current_focus = current_focus
return super(MainFrame, self).mouse_event(size, event, button, col, row, focus)
def keypress(self, size, key):
self.keypress_focus_check()
2021-06-30 17:29:37 -04:00
#if key == "ctrl q":
# raise urwid.ExitMainLoop
2021-05-12 08:02:44 -04:00
return super(MainFrame, self).keypress(size, key)
2021-05-04 05:08:16 -04:00
class MainDisplay():
def __init__(self, ui, app):
self.ui = ui
self.app = app
self.menu_display = MenuDisplay(self.app, self)
self.sub_displays = SubDisplays(self.app)
2021-05-12 08:02:44 -04:00
self.frame = MainFrame(self.sub_displays.active().widget, header=self.menu_display.widget, footer=self.sub_displays.active().shortcuts().widget, delegate=self)
2021-05-04 05:08:16 -04:00
self.widget = self.frame
def show_network(self, user_data):
self.sub_displays.active_display = self.sub_displays.network_display
self.update_active_sub_display()
self.sub_displays.network_display.start()
2021-05-04 05:08:16 -04:00
def show_conversations(self, user_data):
self.sub_displays.active_display = self.sub_displays.conversations_display
self.update_active_sub_display()
def show_directory(self, user_data):
self.sub_displays.active_display = self.sub_displays.directory_display
self.update_active_sub_display()
def show_map(self, user_data):
self.sub_displays.active_display = self.sub_displays.map_display
self.update_active_sub_display()
2021-06-30 16:17:21 -04:00
def show_config(self, user_data):
self.sub_displays.active_display = self.sub_displays.config_display
self.update_active_sub_display()
def show_log(self, user_data):
self.sub_displays.active_display = self.sub_displays.log_display
self.update_active_sub_display()
def show_guide(self, user_data):
self.sub_displays.active_display = self.sub_displays.guide_display
self.update_active_sub_display()
2021-05-04 05:08:16 -04:00
def update_active_sub_display(self):
self.frame.contents["body"] = (self.sub_displays.active().widget, None)
2021-05-12 08:02:44 -04:00
self.update_active_shortcuts()
self.app.ui.main_display.request_redraw(extra_delay=0.0)
2021-05-12 08:02:44 -04:00
def update_active_shortcuts(self):
2021-05-04 05:08:16 -04:00
self.frame.contents["footer"] = (self.sub_displays.active().shortcuts().widget, None)
2021-10-15 07:13:48 -04:00
def request_redraw(self, extra_delay=0.0):
self.app.ui.loop.set_alarm_in(0.25+extra_delay, self.redraw_now)
2021-06-30 17:29:37 -04:00
def redraw_now(self, sender=None, data=None):
2021-10-15 07:13:48 -04:00
self.app.ui.loop.screen.clear()
#self.app.ui.loop.draw_screen()
2021-06-30 17:29:37 -04:00
2021-09-23 11:20:13 -04:00
def start(self):
self.menu_display.start()
2021-05-14 11:36:35 -04:00
def quit(self, sender=None):
raise urwid.ExitMainLoop
2021-05-04 05:08:16 -04:00
2021-05-12 08:02:44 -04:00
2021-05-14 11:36:35 -04:00
class MenuColumns(urwid.Columns):
def keypress(self, size, key):
if key == "tab" or key == "down":
self.handler.frame.set_focus("body")
return super(MenuColumns, self).keypress(size, key)
2021-05-04 05:08:16 -04:00
class MenuDisplay():
2021-09-23 11:20:13 -04:00
UPDATE_INTERVAL = 2
2021-05-04 05:08:16 -04:00
def __init__(self, app, handler):
self.app = app
2021-09-23 11:20:13 -04:00
self.update_interval = MenuDisplay.UPDATE_INTERVAL
self.g = self.app.ui.glyphs
self.menu_indicator = urwid.Text("")
2021-05-04 05:08:16 -04:00
2021-09-23 11:20:13 -04:00
menu_text = ("pack", self.menu_indicator)
2021-05-04 05:08:16 -04:00
button_network = (11, MenuButton("Network", on_press=handler.show_network))
button_conversations = (17, MenuButton("Conversations", on_press=handler.show_conversations))
button_directory = (13, MenuButton("Directory", on_press=handler.show_directory))
2021-06-30 16:17:21 -04:00
button_map = (7, MenuButton("Map", on_press=handler.show_map))
button_log = (7, MenuButton("Log", on_press=handler.show_log))
button_config = (10, MenuButton("Config", on_press=handler.show_config))
button_guide = (9, MenuButton("Guide", on_press=handler.show_guide))
2021-06-30 16:17:21 -04:00
button_quit = (8, MenuButton("Quit", on_press=handler.quit))
2021-05-04 05:08:16 -04:00
2021-05-14 08:45:20 -04:00
# buttons = [menu_text, button_conversations, button_node, button_directory, button_map]
if self.app.config["textui"]["hide_guide"]:
buttons = [menu_text, button_conversations, button_network, button_log, button_config, button_quit]
else:
buttons = [menu_text, button_conversations, button_network, button_log, button_config, button_guide, button_quit]
2021-05-14 11:36:35 -04:00
columns = MenuColumns(buttons, dividechars=1)
columns.handler = handler
2021-05-04 05:08:16 -04:00
2021-09-23 11:20:13 -04:00
self.update_display()
2021-05-04 05:08:16 -04:00
self.widget = urwid.AttrMap(columns, "menubar")
2021-09-23 11:20:13 -04:00
def start(self):
self.update_display_job()
def update_display_job(self, event = None, sender = None):
self.update_display()
self.app.ui.loop.set_alarm_in(self.update_interval, self.update_display_job)
def update_display(self):
if self.app.has_unread_conversations():
self.indicate_unread()
else:
self.indicate_normal()
def indicate_normal(self):
self.menu_indicator.set_text(self.g["decoration_menu"])
def indicate_unread(self):
self.menu_indicator.set_text(self.g["unread_menu"])