codekingpro/portable-devtools
114k
1import urwid2 3 4class Tab(urwid.WidgetWrap):5 def __init__(self, offset, content, attr, onclick):6 """7 onclick is called on click with the tab offset as argument8 """9 p = urwid.Text(content, align="center")10 p = urwid.Padding(p, align="center", width=("relative", 100))11 p = urwid.AttrMap(p, attr)12 urwid.WidgetWrap.__init__(self, p)13 self.offset = offset14 self.onclick = onclick15 16 def mouse_event(self, size, event, button, col, row, focus):17 if event == "mouse press" and button == 1:18 self.onclick(self.offset)19 return True20 21 22class Tabs(urwid.WidgetWrap):23 def __init__(self, tabs, tab_offset=0):24 super().__init__(urwid.Pile([]))25 self.tab_offset = tab_offset26 self.tabs = tabs27 self.show()28 29 def change_tab(self, offset):30 self.tab_offset = offset31 self.show()32 33 def keypress(self, size, key):34 n = len(self.tabs)35 if key == "m_next":36 self.change_tab((self.tab_offset + 1) % n)37 elif key == "right":38 self.change_tab((self.tab_offset + 1) % n)39 elif key == "left":40 self.change_tab((self.tab_offset - 1) % n)41 return self._w.keypress(size, key)42 43 def show(self):44 if not self.tabs:45 return46 47 headers = []48 for i in range(len(self.tabs)):49 txt = self.tabs[i][0]()50 if i == self.tab_offset % len(self.tabs):51 headers.append(Tab(i, txt, "heading", self.change_tab))52 else:53 headers.append(Tab(i, txt, "heading_inactive", self.change_tab))54 headers = urwid.Columns(headers, dividechars=1)55 self._w = urwid.Frame(56 body=self.tabs[self.tab_offset % len(self.tabs)][1](), header=headers57 )58 self._w.focus_position = "body"59 