From 59a89d31fe6273c704c2dced4ae1a72a72f8571a Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 9 May 2014 16:03:46 +0200 Subject: [PATCH] Add reasons why modes are entered/left --- qutebrowser/app.py | 2 +- qutebrowser/browser/hints.py | 6 +++--- qutebrowser/keyinput/modeman.py | 32 +++++++++++++++++-------------- qutebrowser/widgets/_statusbar.py | 4 ++-- qutebrowser/widgets/webview.py | 11 ++++++----- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 3c27b9768..5411b41bf 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -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() diff --git a/qutebrowser/browser/hints.py b/qutebrowser/browser/hints.py index dbae9f45e..e86cf5ceb 100644 --- a/qutebrowser/browser/hints.py +++ b/qutebrowser/browser/hints.py @@ -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.""" diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index db7cd259c..a8b74ce4a 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -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): diff --git a/qutebrowser/widgets/_statusbar.py b/qutebrowser/widgets/_statusbar.py index 1dbe9da5d..21269050d 100644 --- a/qutebrowser/widgets/_statusbar.py +++ b/qutebrowser/widgets/_statusbar.py @@ -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) diff --git a/qutebrowser/widgets/webview.py b/qutebrowser/widgets/webview.py index 83c449651..3aed9500d 100644 --- a/qutebrowser/widgets/webview.py +++ b/qutebrowser/widgets/webview.py @@ -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