2015-07-23 02:30:57 +00:00
|
|
|
# Copyright 2015 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
""" View policy
|
|
|
|
|
|
|
|
Contains some default key navigations
|
|
|
|
"""
|
|
|
|
|
2018-03-12 01:25:27 +00:00
|
|
|
from urwid import Columns, Overlay, Pile, Text, WidgetWrap
|
2015-07-23 02:30:57 +00:00
|
|
|
|
2015-11-02 22:48:37 +00:00
|
|
|
|
2016-07-26 02:21:24 +00:00
|
|
|
class BaseView(WidgetWrap):
|
2017-04-05 03:19:01 +00:00
|
|
|
|
|
|
|
def __init__(self, w):
|
|
|
|
self.orig_w = None
|
|
|
|
super().__init__(w)
|
|
|
|
|
2017-01-06 07:44:35 +00:00
|
|
|
def show_overlay(self, overlay_widget, **kw):
|
|
|
|
self.orig_w = self._w
|
|
|
|
args = dict(
|
|
|
|
align='center',
|
|
|
|
width=('relative', 60),
|
|
|
|
min_width=80,
|
|
|
|
valign='middle',
|
|
|
|
height='pack'
|
|
|
|
)
|
2018-03-13 07:56:13 +00:00
|
|
|
PADDING = 3
|
|
|
|
# Don't expect callers to account for the padding if they pass a fixed width.
|
|
|
|
if 'width' in kw:
|
|
|
|
if isinstance(kw['width'], int):
|
|
|
|
kw['width'] += 2*PADDING
|
2017-01-06 07:44:35 +00:00
|
|
|
args.update(kw)
|
2018-03-12 01:25:27 +00:00
|
|
|
top = Pile([
|
|
|
|
('pack', Text("")),
|
|
|
|
Columns([
|
2018-03-13 07:56:13 +00:00
|
|
|
(PADDING, Text("")),
|
2018-03-12 01:25:27 +00:00
|
|
|
overlay_widget,
|
2018-03-13 07:56:13 +00:00
|
|
|
(PADDING, Text(""))
|
2018-03-12 01:25:27 +00:00
|
|
|
]),
|
|
|
|
('pack', Text("")),
|
|
|
|
])
|
|
|
|
self._w = Overlay(top_w=top, bottom_w=self._w, **args)
|
2017-01-06 07:44:35 +00:00
|
|
|
|
|
|
|
def remove_overlay(self):
|
|
|
|
self._w = self.orig_w
|
|
|
|
self.orig_w = None
|
|
|
|
|
2017-04-04 04:20:56 +00:00
|
|
|
def cancel(self):
|
|
|
|
pass
|
|
|
|
|
2015-07-23 02:30:57 +00:00
|
|
|
def keypress(self, size, key):
|
2017-02-12 23:18:48 +00:00
|
|
|
if key in ['ctrl x']:
|
|
|
|
self.controller.signal.emit_signal('control-x-quit')
|
|
|
|
return None
|
|
|
|
key = super().keypress(size, key)
|
2015-07-23 02:30:57 +00:00
|
|
|
if key == 'esc':
|
2017-04-05 03:19:01 +00:00
|
|
|
if self.orig_w is not None:
|
|
|
|
self.remove_overlay()
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
self.cancel()
|
|
|
|
return None
|
2017-02-12 23:18:48 +00:00
|
|
|
return key
|