From 10835267d8620a8dd493dc0250ea20ea089bd1c7 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Wed, 5 Oct 2011 20:58:32 +0100 Subject: make input wrapping explicit command creation is done in the topmost widget, wich is ui.inputwrap --- alot/command.py | 16 ++++++++-------- alot/ui.py | 46 +++++++++++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/alot/command.py b/alot/command.py index fdf771d8..14fe34c6 100644 --- a/alot/command.py +++ b/alot/command.py @@ -1057,14 +1057,13 @@ class TaglistSelectCommand(Command): ui.apply_command(cmd) -class MoveCommand(Command): - def __init__(self, direction, **kwargs): +class SendKeypressCommand(Command): + def __init__(self, key, **kwargs): Command.__init__(self, **kwargs) - self.direction = direction + self.key = key def apply(self, ui): - if self.direction in ['up', 'down', 'left', 'right', 'page down']: - ui.keypress(self.direction) + ui.keypress(self.key) COMMANDS = { @@ -1103,7 +1102,8 @@ COMMANDS = { 'toggleheaders': (ToggleHeaderCommand, {}), }, 'global': { - 'move': (MoveCommand, {}), + 'move': (SendKeypressCommand, {}), + 'sendkey': (SendKeypressCommand, {}), 'bnext': (BufferFocusCommand, {'offset': 1}), 'bprevious': (BufferFocusCommand, {'offset': -1}), 'bufferlist': (OpenBufferlistCommand, {}), @@ -1172,8 +1172,8 @@ def interpret_commandline(cmdline, mode): if cmd == 'search': return commandfactory(cmd, mode=mode, query=params) - if cmd == 'move': - return commandfactory(cmd, mode=mode, direction=params) + if cmd in ['move', 'sendkey']: + return commandfactory(cmd, mode=mode, key=params) elif cmd == 'compose': h = {} if params: diff --git a/alot/ui.py b/alot/ui.py index 2cf03b1d..38a6d14c 100644 --- a/alot/ui.py +++ b/alot/ui.py @@ -22,16 +22,31 @@ from twisted.internet import reactor, defer from settings import config from buffer import BufferlistBuffer +import command from command import commandfactory from command import interpret_commandline import widgets from completion import CommandLineCompleter -class MainWidget(urwid.Frame): - def __init__(self, ui, *args, **kwargs): - urwid.Frame.__init__(self, urwid.SolidFill(), *args, **kwargs) +class InputWrap(urwid.WidgetWrap): + def __init__(self, ui, rootwidget): + urwid.WidgetWrap.__init__(self, rootwidget) self.ui = ui + self.rootwidget = rootwidget + self.select_cancel_only = False + + def set_root(self, w): + self._w = w + + def allowed_command(self, cmd): + if not self.select_cancel_only: + return True + elif isinstance(cmd, command.SendKeypressCommand): + if cmd.key in ['select', 'cancel']: + return True + else: + return False def keypress(self, size, key, interpret=True): self.ui.logger.debug('got key: \'%s\'' % key) @@ -39,11 +54,11 @@ class MainWidget(urwid.Frame): cmdline = config.get_mapping(self.ui.mode, key) if cmdline: cmd = interpret_commandline(cmdline, self.ui.mode) - if cmd: + if self.allowed_command(cmd): self.ui.apply_command(cmd) return None self.ui.logger.debug('relaying key: %s' % key) - return urwid.Frame.keypress(self, size, key) + return self._w.keypress(size, key) @@ -60,8 +75,9 @@ class UI(object): if not colourmode: colourmode = config.getint('general', 'colourmode') self.logger.info('setup gui in %d colours' % colourmode) - self.mainframe = MainWidget(self) - self.mainloop = urwid.MainLoop(self.mainframe, + self.mainframe = urwid.Frame(urwid.SolidFill()) + self.inputwrap = InputWrap(self, self.mainframe) + self.mainloop = urwid.MainLoop(self.inputwrap, config.get_palette(), handle_mouse=False, event_loop=urwid.TwistedEventLoop(), @@ -106,7 +122,9 @@ class UI(object): d = defer.Deferred() # create return deferred def select_or_cancel(text): - self.mainloop.widget = self.mainframe # restore main screen + # restore main screen + self.inputwrap.set_root(self.mainframe) + self.inputwrap.select_cancel_only = False d.callback(text) #set up widgets @@ -131,7 +149,8 @@ class UI(object): ('fixed right', 0), ('fixed bottom', 1), None) - self.mainloop.widget = overlay + self.inputwrap.set_root(overlay) + self.inputwrap.select_cancel_only = True return d # return deferred def exit(self): @@ -268,10 +287,10 @@ class UI(object): assert msg_position in ['left', 'above'] d = defer.Deferred() # create return deferred - main = self.mainloop.widget # save main widget def select_or_cancel(text): - self.mainloop.widget = main # restore main screen + self.inputwrap.set_root(self.mainframe) + self.inputwrap.select_cancel_only = False d.callback(text) #set up widgets @@ -291,12 +310,13 @@ class UI(object): urwid.AttrMap(both, 'prompt', 'prompt') # put promptwidget as overlay on main widget - overlay = urwid.Overlay(both, main, + overlay = urwid.Overlay(both, self.mainframe, ('fixed left', 0), ('fixed right', 0), ('fixed bottom', 1), None) - self.mainloop.widget = overlay + self.inputwrap.set_root(overlay) + self.inputwrap.select_cancel_only = True return d # return deferred def notify(self, message, priority='normal', timeout=0, block=False): -- cgit v1.2.3