view.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # This file is part of DarkFi (https://dark.fi)
  2. #
  3. # Copyright (C) 2020-2023 Dyne.org foundation
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import urwid
  18. import asyncio
  19. import time
  20. from scroll import ScrollBar, Scrollable
  21. event_loop = asyncio.get_event_loop()
  22. class MyListBox(urwid.ListBox):
  23. def focus_next(self):
  24. try:
  25. self.body.set_focus(self.body.get_next(self.body.get_focus()[1])[1])
  26. except:
  27. pass
  28. def focus_previous(self):
  29. try:
  30. self.body.set_focus(self.body.get_prev(self.body.get_focus()[1])[1])
  31. except:
  32. pass
  33. def load_info(self):
  34. return InfoWidget(self)
  35. class ServiceWidget(urwid.WidgetWrap):
  36. def __init__(self):
  37. test = urwid.Text("1")
  38. super().__init__(test)
  39. self._w = urwid.AttrWrap(self._w, None)
  40. self.update_w()
  41. def selectable(self):
  42. return True
  43. def keypress(self, size, key):
  44. if key in ('q'):
  45. raise urwid.ExitMainLoop()
  46. return key
  47. def update_w(self):
  48. self._w.focus_attr = 'line'
  49. def name(self):
  50. return "ServiceWidget"
  51. class SessionWidget(urwid.WidgetWrap):
  52. def __init__(self):
  53. test = urwid.Text("2")
  54. super().__init__(test)
  55. self._w = urwid.AttrWrap(self._w, None)
  56. self.update_w()
  57. def selectable(self):
  58. return True
  59. def keypress(self, size, key):
  60. if key in ('q'):
  61. raise urwid.ExitMainLoop()
  62. return key
  63. def update_w(self):
  64. self._w.focus_attr = 'line'
  65. def name(self):
  66. return "SessionWidget"
  67. class ConnectWidget(urwid.WidgetWrap):
  68. def __init__(self):
  69. test = urwid.Text("3")
  70. super().__init__(test)
  71. self._w = urwid.AttrWrap(self._w, None)
  72. self.update_w()
  73. def selectable(self):
  74. return True
  75. def keypress(self, size, key):
  76. if key in ('q'):
  77. raise urwid.ExitMainLoop()
  78. return key
  79. def update_w(self):
  80. self._w.focus_attr = 'line'
  81. def name(self):
  82. return "ConnectWidget"
  83. def main():
  84. palette = [
  85. ('body','light gray','black', 'standout'),
  86. ("line","dark cyan","black","standout"),
  87. ]
  88. info_text = urwid.Text("")
  89. pile = urwid.Pile([info_text])
  90. scroll = ScrollBar(Scrollable(pile))
  91. rightbox = urwid.LineBox(scroll)
  92. widget = ServiceWidget()
  93. widget2 = SessionWidget()
  94. widget3 = ConnectWidget()
  95. listbox_content = [widget, widget2, widget3]
  96. listbox = MyListBox(urwid.SimpleListWalker(listbox_content))
  97. leftbox = urwid.LineBox(listbox)
  98. columns = urwid.Columns([leftbox, rightbox], focus_column=0)
  99. view = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
  100. event_loop.create_task(render_info(pile, listbox))
  101. loop = urwid.MainLoop(view, palette,
  102. event_loop=urwid.AsyncioEventLoop(loop=event_loop),
  103. )
  104. loop.run()
  105. async def render_info(pile, listbox):
  106. while True:
  107. await asyncio.sleep(0.1)
  108. pile.contents.clear()
  109. focus_w = listbox.get_focus()
  110. match focus_w[0].name():
  111. case "ServiceWidget":
  112. pile.contents.append((urwid.Text("1"), pile.options()))
  113. case "SessionWidget":
  114. pile.contents.append((urwid.Text("2"), pile.options()))
  115. case "ConnectWidget":
  116. pile.contents.append((urwid.Text("3"), pile.options()))
  117. if __name__ == "__main__":
  118. main()