From 6ca96147807dac34ada74ef0398f37ea7466bf63 Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Wed, 13 Mar 2019 00:21:29 -0700 Subject: [PATCH 1/3] Add target for private-window --- qutebrowser/app.py | 10 +++++++--- qutebrowser/config/configdata.yml | 1 + qutebrowser/mainwindow/mainwindow.py | 12 +++++++----- qutebrowser/qutebrowser.py | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 5c347fa86..474f81536 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -217,7 +217,8 @@ def _process_args(args): session_manager = objreg.get('session-manager') if not session_manager.did_load: log.init.debug("Initializing main window...") - window = mainwindow.MainWindow(private=None) + private = args.target == 'private-window' or None + window = mainwindow.MainWindow(private=private) if not args.nowindow: window.show() q_app.setActiveWindow(window) @@ -275,8 +276,10 @@ def process_pos_args(args, via_ipc=False, cwd=None, target_arg=None): ipc. If the --target argument was not specified, target_arg will be an empty string. """ + is_private = 'private-window' if target_arg == 'private-window' else None if via_ipc and not args: - win_id = mainwindow.get_window(via_ipc, force_window=True) + win_id = mainwindow.get_window(via_ipc, force_window=True, + force_target=is_private) _open_startpage(win_id) return win_id = None @@ -289,7 +292,8 @@ def process_pos_args(args, via_ipc=False, cwd=None, target_arg=None): commandrunner.run_safely(cmd[1:]) elif not cmd: log.init.debug("Empty argument") - win_id = mainwindow.get_window(via_ipc, force_window=True) + win_id = mainwindow.get_window(via_ipc, force_window=True, + force_target=is_private) else: if via_ipc and target_arg and target_arg != 'auto': open_target = target_arg diff --git a/qutebrowser/config/configdata.yml b/qutebrowser/config/configdata.yml index e2d443280..c8f647092 100644 --- a/qutebrowser/config/configdata.yml +++ b/qutebrowser/config/configdata.yml @@ -60,6 +60,7 @@ new_instance_open_target: - tab-bg-silent: Open a new background tab in the existing window without activating the window. - window: Open in a new window. + - private-window: Open in a new private window. default: tab desc: >- How to open links in an existing instance if a new one is launched. diff --git a/qutebrowser/mainwindow/mainwindow.py b/qutebrowser/mainwindow/mainwindow.py index a80fa0150..f08bb6674 100644 --- a/qutebrowser/mainwindow/mainwindow.py +++ b/qutebrowser/mainwindow/mainwindow.py @@ -69,9 +69,9 @@ def get_window(via_ipc, force_window=False, force_tab=False, # Apply any target overrides, ordered by precedence if force_target is not None: open_target = force_target - if force_window: + if force_window and open_target != 'private-window': open_target = 'window' - if force_tab and open_target == 'window': + if force_tab and open_target in {'window', 'private-window'}: # Command sent via IPC open_target = 'tab-silent' @@ -79,13 +79,15 @@ def get_window(via_ipc, force_window=False, force_tab=False, should_raise = False # Try to find the existing tab target if opening in a tab - if open_target != 'window': + if open_target not in {'window', 'private-window'}: window = get_target_window() - should_raise = open_target not in ['tab-silent', 'tab-bg-silent'] + should_raise = open_target not in {'tab-silent', 'tab-bg-silent'} + + is_private = open_target == 'private-window' or None # Otherwise, or if no window was found, create a new one if window is None: - window = MainWindow(private=None) + window = MainWindow(private=is_private) window.show() should_raise = True diff --git a/qutebrowser/qutebrowser.py b/qutebrowser/qutebrowser.py index 65b150b3f..1fb3238df 100644 --- a/qutebrowser/qutebrowser.py +++ b/qutebrowser/qutebrowser.py @@ -76,7 +76,7 @@ def get_argparser(): action='store_true') parser.add_argument('--target', choices=['auto', 'tab', 'tab-bg', 'tab-silent', 'tab-bg-silent', - 'window'], + 'window', 'private-window'], help="How URLs should be opened if there is already a " "qutebrowser instance running.") parser.add_argument('--backend', choices=['webkit', 'webengine'], From 138881e3eb606c956b17ae857b617e1f588a85e0 Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Wed, 13 Mar 2019 19:38:47 -0700 Subject: [PATCH 2/3] Add test for new_instance_open_target private-window --- tests/end2end/features/invoke.feature | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/end2end/features/invoke.feature b/tests/end2end/features/invoke.feature index 9be38659e..ee45dcb29 100644 --- a/tests/end2end/features/invoke.feature +++ b/tests/end2end/features/invoke.feature @@ -36,6 +36,21 @@ Feature: Invoking a new process - history: - url: http://localhost:*/data/search.html + Scenario: Using new_instance_open_target = private-window + When I set new_instance_open_target to private-window + And I open data/title.html + And I open data/search.html as a URL + Then the session should look like: + windows: + - tabs: + - history: + - url: about:blank + - url: http://localhost:*/data/title.html + - private: True + tabs: + - history: + - url: http://localhost:*/data/search.html + Scenario: Using new_instance_open_target_window = last-opened When I set new_instance_open_target to tab And I set new_instance_open_target_window to last-opened From 33f2657ce0ef048f7f2011015f0db05417d138d2 Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Wed, 13 Mar 2019 19:55:35 -0700 Subject: [PATCH 3/3] Add type hints for MainWindow --- qutebrowser/app.py | 2 +- qutebrowser/mainwindow/mainwindow.py | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 474f81536..b7d896608 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -217,7 +217,7 @@ def _process_args(args): session_manager = objreg.get('session-manager') if not session_manager.did_load: log.init.debug("Initializing main window...") - private = args.target == 'private-window' or None + private = args.target == 'private-window' window = mainwindow.MainWindow(private=private) if not args.nowindow: window.show() diff --git a/qutebrowser/mainwindow/mainwindow.py b/qutebrowser/mainwindow/mainwindow.py index f08bb6674..ee6a063ae 100644 --- a/qutebrowser/mainwindow/mainwindow.py +++ b/qutebrowser/mainwindow/mainwindow.py @@ -23,9 +23,10 @@ import binascii import base64 import itertools import functools +import typing from PyQt5.QtCore import (pyqtSlot, QRect, QPoint, QTimer, Qt, - QCoreApplication, QEventLoop) + QCoreApplication, QEventLoop, QByteArray, QObject) from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QSizePolicy from qutebrowser.commands import runners @@ -43,8 +44,11 @@ from qutebrowser.misc import crashsignal, keyhintwidget win_id_gen = itertools.count(0) -def get_window(via_ipc, force_window=False, force_tab=False, - force_target=None, no_raise=False): +def get_window(via_ipc: bool, + force_window: bool = False, + force_tab: bool = False, + force_target: typing.Optional[bool] = None, + no_raise: bool = False): """Helper function for app.py to get a window id. Args: @@ -83,7 +87,7 @@ def get_window(via_ipc, force_window=False, force_tab=False, window = get_target_window() should_raise = open_target not in {'tab-silent', 'tab-bg-silent'} - is_private = open_target == 'private-window' or None + is_private = open_target == 'private-window' # Otherwise, or if no window was found, create a new one if window is None: @@ -147,7 +151,10 @@ class MainWindow(QWidget): _private: Whether the window is in private browsing mode. """ - def __init__(self, *, private, geometry=None, parent=None): + def __init__(self, *, + private: bool, + geometry: typing.Optional[QByteArray] = None, + parent: typing.Optional[QObject] = None) -> None: """Create a new main window. Args: @@ -163,7 +170,7 @@ class MainWindow(QWidget): self.setAttribute(Qt.WA_DeleteOnClose) self._commandrunner = None - self._overlays = [] + self._overlays = [] # type: typing.List[typing.Tuple] self.win_id = next(win_id_gen) self.registry = objreg.ObjectRegistry() objreg.window_registry[self.win_id] = self