From 0a1bdd79dac564e6fb78818c0daaebf3cb46ad92 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 23 Nov 2014 21:26:59 +0100 Subject: [PATCH] Split some functions into smaller ones. --- qutebrowser/app.py | 32 ++++++++++++++++++++-------- qutebrowser/widgets/tabbedbrowser.py | 30 ++++++++++++++++++-------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 85db46ed1..92810346f 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -512,15 +512,17 @@ class Application(QApplication): """Quit qutebrowser.""" QApplication.closeAllWindows() - @cmdutils.register(instance='app', ignore_args=True) - def restart(self, shutdown=True, pages=None): - """Restart qutebrowser while keeping existing tabs open.""" - if pages is None: - pages = self._recover_pages() - log.destroy.debug("sys.executable: {}".format(sys.executable)) - log.destroy.debug("sys.path: {}".format(sys.path)) - log.destroy.debug("sys.argv: {}".format(sys.argv)) - log.destroy.debug("frozen: {}".format(hasattr(sys, 'frozen'))) + def _get_restart_args(self, pages): + """Get the current working directory and args to relaunch qutebrowser. + + Args: + pages: The pages to re-open. + + Return: + An (args, cwd) tuple. + args: The commandline as a list of strings. + cwd: The current working directory as a string. + """ if os.path.basename(sys.argv[0]) == 'qutebrowser': # Launched via launcher script args = [sys.argv[0]] @@ -545,6 +547,18 @@ class Application(QApplication): args.extend(page_args[:-1]) log.destroy.debug("args: {}".format(args)) log.destroy.debug("cwd: {}".format(cwd)) + return args, cwd + + @cmdutils.register(instance='app', ignore_args=True) + def restart(self, shutdown=True, pages=None): + """Restart qutebrowser while keeping existing tabs open.""" + if pages is None: + pages = self._recover_pages() + log.destroy.debug("sys.executable: {}".format(sys.executable)) + log.destroy.debug("sys.path: {}".format(sys.path)) + log.destroy.debug("sys.argv: {}".format(sys.argv)) + log.destroy.debug("frozen: {}".format(hasattr(sys, 'frozen'))) + args, cwd = self._get_restart_args(pages) # Open a new process and immediately shutdown the existing one try: if cwd is None: diff --git a/qutebrowser/widgets/tabbedbrowser.py b/qutebrowser/widgets/tabbedbrowser.py index ebe293967..b6f11d63f 100644 --- a/qutebrowser/widgets/tabbedbrowser.py +++ b/qutebrowser/widgets/tabbedbrowser.py @@ -328,6 +328,26 @@ class TabbedBrowser(tabwidget.TabWidget): return tabbed_browser.tabopen(url, background, explicit) tab = webview.WebView(self._win_id, self) self._connect_tab_signals(tab) + idx = self._get_new_tab_idx(explicit) + self.insertTab(idx, tab, "") + if url is not None: + tab.openurl(url) + if background is None: + background = config.get('tabs', 'background-tabs') + if not background: + self.setCurrentWidget(tab) + tab.show() + return tab + + def _get_new_tab_idx(self, explicit): + """Get the index of a tab to insert. + + Args: + explicit: Whether the tab was opened explicitely. + + Return: + The index of the new tab. + """ if explicit: pos = config.get('tabs', 'new-tab-position-explicit') else: @@ -352,15 +372,7 @@ class TabbedBrowser(tabwidget.TabWidget): "next left: {} / right: {}".format( pos, idx, self._tab_insert_idx_left, self._tab_insert_idx_right)) - self.insertTab(idx, tab, "") - if url is not None: - tab.openurl(url) - if background is None: - background = config.get('tabs', 'background-tabs') - if not background: - self.setCurrentWidget(tab) - tab.show() - return tab + return idx @pyqtSlot(str, int) def search(self, text, flags):