diff --git a/qutebrowser/app.py b/qutebrowser/app.py index bcf5bed9a..8b1e7a53f 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -33,7 +33,7 @@ import traceback from PyQt5.QtWidgets import QApplication, QDialog from PyQt5.QtCore import (pyqtSlot, qInstallMessageHandler, QTimer, QUrl, - QStandardPaths, QObject) + QStandardPaths, QObject, Qt) import qutebrowser from qutebrowser.commands import cmdutils, runners @@ -230,6 +230,28 @@ class Application(QApplication): self._open_startpage() self._open_quickstart() + def _get_window(self, ipc): + """Helper function for process_args to get a window id.""" + if not ipc: + return 0 + open_target = config.get('general', 'ipc-open-target') + if open_target in ('tab', 'tab-silent'): + try: + window = objreg.get('last-main-window') + except KeyError: + message.error("No main window found!") + return None + else: + if open_target != 'tab-silent': + window.setWindowState(window.windowState() & + ~Qt.WindowMinimized | + Qt.WindowActive) + window.raise_() + window.activateWindow() + return window.win_id + else: + return mainwindow.MainWindow.spawn() + def process_args(self, args, ipc=False): """Process commandline args. @@ -239,10 +261,9 @@ class Application(QApplication): args: A list of arguments to process. ipc: Whether the arguments were transmitted over IPC. """ - if ipc: - win_id = mainwindow.MainWindow.spawn() - else: - win_id = 0 + win_id = self._get_window(ipc) + if win_id is None: + return for cmd in args: if cmd.startswith(':'): log.init.debug("Startup cmd {}".format(cmd)) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 193277975..6e94f253c 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -181,6 +181,11 @@ DATA = collections.OrderedDict([ "The encoding must be a string describing an encoding such as " '_utf-8_, _iso-8859-1_, etc. If left empty a default value will be ' "used."), + + ('ipc-open-target', + SettingValue(typ.IPCOpenTarget(), 'window'), + "How to open links in an existing instance if a new one is " + "launched."), )), ('ui', sect.KeyValue( diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index f1b4fd1b7..6e6b75b31 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -1254,3 +1254,15 @@ class IgnoreCase(Bool): ('false', 'Search case-sensitively'), ('smart', 'Search case-sensitively if there are capital ' 'chars')] + + +class IPCOpenTarget(BaseType): + + """How to open links in an existing instance if a new one is launched.""" + + valid_values = ValidValues(('tab', "Open a new tab in the existing " + "window and activate it."), + ('tab-silent', "Open a new tab in the existing " + "window without activating " + "it."), + ('window', "Open in a new window.")) diff --git a/qutebrowser/widgets/mainwindow.py b/qutebrowser/widgets/mainwindow.py index da22c4e7b..1ed38ef18 100644 --- a/qutebrowser/widgets/mainwindow.py +++ b/qutebrowser/widgets/mainwindow.py @@ -62,6 +62,7 @@ class MainWindow(QWidget): self.registry = objreg.ObjectRegistry() objreg.window_registry[win_id] = self objreg.register('main-window', self, scope='window', window=win_id) + objreg.register('last-main-window', self, update=True) tab_registry = objreg.ObjectRegistry() objreg.register('tab-registry', tab_registry, scope='window', window=win_id)