Add ipc-open-target config option.

This commit is contained in:
Florian Bruhin 2014-10-14 07:37:12 +02:00
parent ca85dde71f
commit 817259f4f5
4 changed files with 44 additions and 5 deletions

View File

@ -33,7 +33,7 @@ import traceback
from PyQt5.QtWidgets import QApplication, QDialog from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.QtCore import (pyqtSlot, qInstallMessageHandler, QTimer, QUrl, from PyQt5.QtCore import (pyqtSlot, qInstallMessageHandler, QTimer, QUrl,
QStandardPaths, QObject) QStandardPaths, QObject, Qt)
import qutebrowser import qutebrowser
from qutebrowser.commands import cmdutils, runners from qutebrowser.commands import cmdutils, runners
@ -230,6 +230,28 @@ class Application(QApplication):
self._open_startpage() self._open_startpage()
self._open_quickstart() 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): def process_args(self, args, ipc=False):
"""Process commandline args. """Process commandline args.
@ -239,10 +261,9 @@ class Application(QApplication):
args: A list of arguments to process. args: A list of arguments to process.
ipc: Whether the arguments were transmitted over IPC. ipc: Whether the arguments were transmitted over IPC.
""" """
if ipc: win_id = self._get_window(ipc)
win_id = mainwindow.MainWindow.spawn() if win_id is None:
else: return
win_id = 0
for cmd in args: for cmd in args:
if cmd.startswith(':'): if cmd.startswith(':'):
log.init.debug("Startup cmd {}".format(cmd)) log.init.debug("Startup cmd {}".format(cmd))

View File

@ -181,6 +181,11 @@ DATA = collections.OrderedDict([
"The encoding must be a string describing an encoding such as " "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 ' '_utf-8_, _iso-8859-1_, etc. If left empty a default value will be '
"used."), "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( ('ui', sect.KeyValue(

View File

@ -1254,3 +1254,15 @@ class IgnoreCase(Bool):
('false', 'Search case-sensitively'), ('false', 'Search case-sensitively'),
('smart', 'Search case-sensitively if there are capital ' ('smart', 'Search case-sensitively if there are capital '
'chars')] '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."))

View File

@ -62,6 +62,7 @@ class MainWindow(QWidget):
self.registry = objreg.ObjectRegistry() self.registry = objreg.ObjectRegistry()
objreg.window_registry[win_id] = self objreg.window_registry[win_id] = self
objreg.register('main-window', self, scope='window', window=win_id) objreg.register('main-window', self, scope='window', window=win_id)
objreg.register('last-main-window', self, update=True)
tab_registry = objreg.ObjectRegistry() tab_registry = objreg.ObjectRegistry()
objreg.register('tab-registry', tab_registry, scope='window', objreg.register('tab-registry', tab_registry, scope='window',
window=win_id) window=win_id)