Add reasons why modes are entered/left

This commit is contained in:
Florian Bruhin 2014-05-09 16:03:46 +02:00
parent 91d1be1020
commit 59a89d31fe
5 changed files with 30 additions and 25 deletions

View File

@ -146,7 +146,7 @@ class QuteBrowser(QApplication):
self.setQuitOnLastWindowClosed(False)
self._connect_signals()
self.modeman.enter('normal')
self.modeman.enter('normal', 'init')
self.mainwindow.show()
self._python_hacks()

View File

@ -400,7 +400,7 @@ class HintManager(QObject):
self._elems[string] = ElemTuple(e, label)
frame.contentsSizeChanged.connect(self.on_contents_size_changed)
self.hint_strings_updated.emit(strings)
modeman.enter('hint')
modeman.enter('hint', 'HintManager.start')
def handle_partial_key(self, keystr):
"""Handle a new partial keypress."""
@ -442,7 +442,7 @@ class HintManager(QObject):
visible[k] = e
if not visible:
# Whoops, filtered all hints
modeman.leave('hint')
modeman.leave('hint', 'all filtered')
elif len(visible) == 1 and config.get('hints', 'auto-follow'):
# unpacking gets us the first (and only) key in the dict.
self.fire(*visible)
@ -485,7 +485,7 @@ class HintManager(QObject):
else:
raise ValueError("No suitable handler found!")
if self._target != Target.rapid:
modeman.leave('hint')
modeman.leave('hint', 'followed')
def follow_hint(self):
"""Follow the currently selected hint."""

View File

@ -37,20 +37,20 @@ def instance():
return QApplication.instance().modeman
def enter(mode):
def enter(mode, reason=None):
"""Enter the mode 'mode'."""
instance().enter(mode)
instance().enter(mode, reason)
def leave(mode):
def leave(mode, reason=None):
"""Leave the mode 'mode'."""
instance().leave(mode)
instance().leave(mode, reason)
def maybe_leave(mode):
def maybe_leave(mode, reason=None):
"""Convenience method to leave 'mode' without exceptions."""
try:
instance().leave(mode)
instance().leave(mode, reason)
except ValueError:
pass
@ -177,16 +177,18 @@ class ModeManager(QObject):
self.passthrough.append(mode)
@cmdutils.register(instance='modeman', name='enter_mode', hide=True)
def enter(self, mode):
def enter(self, mode, reason=None):
"""Enter a new mode.
Args:
mode; The name of the mode to enter.
mode: The name of the mode to enter.
reason: Why the mode was entered.
Emit:
entered: With the new mode name.
"""
logging.debug("Switching mode to {}".format(mode))
logging.debug("Entering mode {}{}".format(
mode, '' if reason is None else ' (reason: {})'.format(reason)))
if mode not in self._handlers:
raise ValueError("No handler for mode {}".format(mode))
if self._mode_stack and self._mode_stack[-1] == mode:
@ -196,11 +198,12 @@ class ModeManager(QObject):
logging.debug("New mode stack: {}".format(self._mode_stack))
self.entered.emit(mode)
def leave(self, mode):
def leave(self, mode, reason=None):
"""Leave a mode.
Args:
mode; The name of the mode to leave.
mode: The name of the mode to leave.
reason: Why the mode was left.
Emit:
left: With the old mode name.
@ -209,8 +212,9 @@ class ModeManager(QObject):
self._mode_stack.remove(mode)
except ValueError:
raise ValueError("Mode {} not on mode stack!".format(mode))
logging.debug("Leaving mode {}".format(mode))
logging.debug("New mode stack: {}".format(self._mode_stack))
logging.debug("Leaving mode {}{}, new mode stack {}".format(
mode, '' if reason is None else ' (reason: {})'.format(reason),
self._mode_stack))
self.left.emit(mode)
@cmdutils.register(instance='modeman', name='leave_mode',
@ -219,7 +223,7 @@ class ModeManager(QObject):
"""Leave the mode we're currently in."""
if self.mode == 'normal':
raise ValueError("Can't leave normal mode!")
self.leave(self.mode)
self.leave(self.mode, 'leave current')
@pyqtSlot(str, str)
def on_config_changed(self, section, option):

View File

@ -344,7 +344,7 @@ class _Command(QLineEdit):
}
text = self.text()
self.history.append(text)
modeman.leave("command")
modeman.leave('command', 'cmd accept')
if text[0] in signals:
signals[text[0]].emit(text.lstrip(text[0]))
@ -371,7 +371,7 @@ class _Command(QLineEdit):
def focusInEvent(self, e):
"""Extend focusInEvent to enter command mode."""
modeman.enter("command")
modeman.enter('command', 'cmd focus')
super().focusInEvent(e)

View File

@ -89,7 +89,8 @@ class WebView(QWebView):
self.page_.setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
self.page_.linkHovered.connect(self.linkHovered)
self.linkClicked.connect(self.on_link_clicked)
self.loadStarted.connect(lambda: modeman.maybe_leave('insert'))
self.loadStarted.connect(
lambda: modeman.maybe_leave('insert', 'load started'))
self.loadFinished.connect(self.on_load_finished)
# FIXME find some way to hide scrollbars without setScrollBarPolicy
@ -260,9 +261,9 @@ class WebView(QWebView):
webelem.SELECTORS[webelem.Group.editable_focused])
logging.debug("focus element: {}".format(not elem.isNull()))
if elem.isNull():
modeman.maybe_leave("insert")
modeman.maybe_leave('insert', 'load finished')
else:
modeman.enter("insert")
modeman.enter('insert', 'load finished')
@pyqtSlot(str)
def set_force_open_target(self, target):
@ -361,11 +362,11 @@ class WebView(QWebView):
hitresult = frame.hitTestContent(pos)
if self._is_editable(hitresult):
logging.debug("Clicked editable element!")
modeman.enter("insert")
modeman.enter('insert', 'click')
else:
logging.debug("Clicked non-editable element!")
try:
modeman.leave("insert")
modeman.leave('insert', 'click')
except ValueError:
pass