diff --git a/qutebrowser/app.py b/qutebrowser/app.py index f94d37ef0..1a5d56bd0 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -314,7 +314,7 @@ class Application(QApplication): quickstart_done = False if not quickstart_done: tabbed_browser = objreg.get('tabbed-browser', scope='window', - window='current') + window='last-focused') tabbed_browser.tabopen( QUrl('http://www.qutebrowser.org/quickstart.html')) try: @@ -342,6 +342,7 @@ class Application(QApplication): config_obj = objreg.get('config') self.lastWindowClosed.connect(self.shutdown) config_obj.style_changed.connect(style.get_stylesheet.cache_clear) + self.focusChanged.connect(self.on_focus_changed) def _get_widgets(self): """Get a string list of all widgets.""" @@ -542,7 +543,7 @@ class Application(QApplication): out = traceback.format_exc() qutescheme.pyeval_output = out tabbed_browser = objreg.get('tabbed-browser', scope='window', - window='current') + window='last-focused') tabbed_browser.openurl(QUrl('qute:pyeval'), newtab=True) @cmdutils.register(instance='app') @@ -706,6 +707,20 @@ class Application(QApplication): # segfaults. QTimer.singleShot(0, functools.partial(self.exit, status)) + def on_focus_changed(self, _old, new): + """Register currently focused main window in the object registry.""" + if new is None: + window = None + else: + window = new.window() + if window is None or not isinstance(window, mainwindow.MainWindow): + try: + objreg.delete('last-focused-main-window') + except KeyError: + pass + else: + objreg.register('last-focused-main-window', window, update=True) + def exit(self, status): """Extend QApplication::exit to log the event.""" log.destroy.debug("Now calling QApplication::exit.") diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index d47ea020a..e59c3cd83 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -352,7 +352,7 @@ class DownloadManager(QAbstractListModel): page: The QWebPage to get the download from. """ if not url.isValid(): - urlutils.invalid_url_error('current', url, "start download") + urlutils.invalid_url_error('last-focused', url, "start download") return req = QNetworkRequest(url) reply = page.networkAccessManager().get(req) @@ -407,7 +407,7 @@ class DownloadManager(QAbstractListModel): self.questions.append(q) download.cancelled.connect(q.abort) message_bridge = objreg.get('message-bridge', scope='window', - window='current') + window='last-focused') message_bridge.ask(q, blocking=False) @pyqtSlot(DownloadItem) @@ -431,7 +431,7 @@ class DownloadManager(QAbstractListModel): @pyqtSlot(str) def on_error(self, msg): """Display error message on download errors.""" - message.error('current', "Download error: {}".format(msg)) + message.error('last-focused', "Download error: {}".format(msg)) def last_index(self): """Get the last index in the model. diff --git a/qutebrowser/commands/argparser.py b/qutebrowser/commands/argparser.py index 684361698..1d21c2ffa 100644 --- a/qutebrowser/commands/argparser.py +++ b/qutebrowser/commands/argparser.py @@ -59,7 +59,7 @@ class HelpAction(argparse.Action): def __call__(self, parser, _namespace, _values, _option_string=None): tabbed_browser = objreg.get('tabbed-browser', scope='window', - window='current') + window='last-focused') tabbed_browser.tabopen( QUrl('qute://help/commands.html#{}'.format(parser.name))) parser.exit() diff --git a/qutebrowser/utils/objreg.py b/qutebrowser/utils/objreg.py index 7b9fd5332..8018a1867 100644 --- a/qutebrowser/utils/objreg.py +++ b/qutebrowser/utils/objreg.py @@ -136,6 +136,15 @@ def _get_window_registry(window): win = app.activeWindow() if win is None or not hasattr(win, 'win_id'): raise RegistryUnavailableError('window') + elif window == 'last-focused': + try: + win = get('last-focused-main-window') + except KeyError: + try: + win = get('last-main-window') + except KeyError: + raise RegistryUnavailableError('window') + assert hasattr(win, 'registry') else: try: win = window_registry[window]