diff --git a/qutebrowser/app.py b/qutebrowser/app.py index ad6936e38..f1ec6607b 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -231,8 +231,8 @@ class Application(QApplication): window_to_raise = window else: try: - window = objreg.get('last-main-window') - except KeyError: + window = objreg.last_window() + except objreg.NoWindow: # We can't display an error here because... duh, there is no # window. log.ipc.error("No main window found!") diff --git a/qutebrowser/utils/objreg.py b/qutebrowser/utils/objreg.py index 8018a1867..b9715a960 100644 --- a/qutebrowser/utils/objreg.py +++ b/qutebrowser/utils/objreg.py @@ -45,6 +45,11 @@ class RegistryUnavailableError(Exception): pass +class NoWindow(Exception): + + """Exception raised by last_window if no window is available.""" + + _UNSET = UnsetObject() @@ -141,8 +146,8 @@ def _get_window_registry(window): win = get('last-focused-main-window') except KeyError: try: - win = get('last-main-window') - except KeyError: + win = last_window() + except NoWindow: raise RegistryUnavailableError('window') assert hasattr(win, 'registry') else: @@ -238,3 +243,12 @@ def dump_objects(): for line in data: lines.append(" {}".format(line)) return lines + + +def last_window(): + """Get the last opened window object.""" + if not window_registry: + raise NoWindow() + else: + key = sorted(window_registry)[-1] + return window_registry[key] diff --git a/qutebrowser/widgets/mainwindow.py b/qutebrowser/widgets/mainwindow.py index cdbf4dd7d..bbf4c95b8 100644 --- a/qutebrowser/widgets/mainwindow.py +++ b/qutebrowser/widgets/mainwindow.py @@ -62,7 +62,6 @@ 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)