forked from iliis/crossword
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_manager.py
63 lines (45 loc) · 1.58 KB
/
widget_manager.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
import curses
import logging
from helpers import *
from popup import Popup
log = logging.getLogger('puzzle')
class WidgetManager:
def __init__(self, app):
self.app = app
self.screen = app.screen
self.widgets = []
self.focus = None
def add(self, widget):
self.widgets.append(widget)
def handle_input(self, key):
if self.focus is not None:
return self.focus.handle_input(key)
else:
return False
def show_single_popup(self, *args, **kwargs):
if not isinstance(self.focus, Popup):
self.show_popup(*args, **kwargs)
def show_popup(self, title, text, callback=None, buttons=['OK']):
popup = Popup(self.app, self.screen, title, text, buttons)
self.add(popup)
old_focus = self.focus # save focus at time of popup call
self.focus = popup
def wrapped_callback(btn):
nonlocal popup # otherwise assignment to this variable will mark it as local
log.info('popup wrapped_callback, restoring focus to {}'.format(old_focus))
self.focus = old_focus # restore focus
popup.visible = False
#popup.screen.clear()
self.widgets.remove(popup)
self.screen.clear()
self.screen.refresh()
del popup
popup = None
if callback:
callback(btn)
self.screen.clear()
popup.callback = wrapped_callback
def render(self):
for widget in self.widgets:
widget.render()
curses.doupdate()