-
Notifications
You must be signed in to change notification settings - Fork 0
/
horizontal_menu.py
executable file
·131 lines (112 loc) · 4.38 KB
/
horizontal_menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
import urwid
top=None
class MenuButton(urwid.Button):
def __init__(self, caption, callback):
super(MenuButton, self).__init__("")
urwid.connect_signal(self, 'click', callback)
self._w = urwid.AttrMap(urwid.SelectableIcon(
[u' \N{BULLET} ', caption], 2), None, 'selected')
class EditField(urwid.Edit):
def __init__(self, caption, callback):
super(EditField, self).__init__("")
urwid.connect_signal(self, 'postchange', callback)
self._w = urwid.AttrMap(urwid.SelectableIcon(
[u' \N{BULLET} ', caption], 2), None, 'selected')
class SubMenu(urwid.WidgetWrap):
def __init__(self, caption, choices):
super(SubMenu, self).__init__(MenuButton(
[caption, u"\N{HORIZONTAL ELLIPSIS}"], self.open_menu))
line = urwid.Divider(u'\N{LOWER ONE QUARTER BLOCK}')
listbox = urwid.ListBox(urwid.SimpleFocusListWalker([
urwid.AttrMap(urwid.Text([u"\n ", caption]), 'heading'),
urwid.AttrMap(line, 'line'),
urwid.Divider()] + choices + [urwid.Divider()]))
self.menu = urwid.AttrMap(listbox, 'options')
def open_menu(self, button):
top.open_box(self.menu)
class EditMenu(urwid.WidgetWrap):
def __init__(self, caption, callback):
super(EditMenu, self).__init__(EditField(
[caption, u"\N{HORIZONTAL ELLIPSIS}"], self.text_changed))
self.callback = callback
def text_changed(self,edit_widget,text2):
pass
#quit()
#if edit_widget.text == "\n":
# self.callback(edit_widget,text2)
def keypress(self,size,key):
if key == 'enter':
choices = self.callback(self._w.text)
line = urwid.Divider(u'\N{LOWER ONE QUARTER BLOCK}')
listbox = urwid.ListBox(urwid.SimpleFocusListWalker([
urwid.AttrMap(urwid.Text([u"\n ", "Search Results"]), 'heading'),
urwid.AttrMap(line, 'line'),
urwid.Divider()] + choices + [urwid.Divider()]))
top.open_box(urwid.AttrMap(listbox, 'options'))
elif key in ('up','down'):
return key
else:
self._wrapped_widget.keypress(size,key)
class Choice(urwid.WidgetWrap):
def __init__(self, caption,handler = None,info = None):
super(Choice, self).__init__(
MenuButton(caption, self.item_chosen))
self.handler = handler
self.caption = caption
self.info = info
def item_chosen(self, button):
if self.handler == None:
response = urwid.Text([u' You chose ', self.caption, u'\n'])
done = MenuButton(u'Ok', exit_program)
response_box = urwid.Filler(urwid.Pile([response, done]))
top.open_box(urwid.AttrMap(response_box, 'options'))
else:
self.handler(self.caption,self.info)
def exit_program(key):
raise urwid.ExitMainLoop()
focus_map = {
'heading': 'focus heading',
'options': 'focus options',
'line': 'focus line'}
class HorizontalMenu(urwid.Columns):
palette = [
(None, 'light gray', 'black'),
('heading', 'black', 'light gray'),
('line', 'black', 'light gray'),
('options', 'dark gray', 'black'),
('focus heading', 'white', 'dark red'),
('focus line', 'black', 'dark red'),
('focus options', 'black', 'light gray'),
('selected', 'white', 'dark blue')]
def __init__(self):
super(HorizontalMenu, self).__init__([], dividechars=1)
def open_box(self, box):
if self.contents:
del self.contents[self.focus_position + 1:]
self.contents.append((urwid.AttrMap(box, 'options', focus_map),
self.options('given', 24)))
self.focus_position = len(self.contents) - 1
def horizontal_menu(menus):
global top
top = HorizontalMenu()
top.open_box(menus.menu)
return urwid.Filler(top, 'middle', 10)
def main(): #test the menu system with this
menu_top = SubMenu(u'Main Menu', [
SubMenu(u'Applications', [
SubMenu(u'Accessories', [
Choice(u'Text Editor'),
Choice(u'Terminal'),
]),
]),
SubMenu(u'System', [
SubMenu(u'Preferences', [
Choice(u'Appearance'),
]),
Choice(u'Lock Screen'),
]),
])
urwid.MainLoop(horizontal_menu(menu_top), HorizontalMenu.palette).run()
if __name__ == "__main__":
main()