This commit is contained in:
lrustand 2026-01-07 16:36:45 -08:00 committed by GitHub
commit e49eb7a9a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 27 deletions

View File

@ -413,10 +413,8 @@ def on_focus_changed(_old, new):
window = new.window()
if isinstance(window, mainwindow.MainWindow):
objreg.register('last-focused-main-window', window, update=True)
# A focused window must also be visible, and in this case we should
# consider it as the most recently looked-at window
objreg.register('last-visible-main-window', window, update=True)
objreg.window_visibility_history.append(window)
objreg.window_focus_history.append(window)
def open_desktopservices_url(url):

View File

@ -623,13 +623,22 @@ class MainWindow(QWidget):
self.tabbed_browser.widget.tab_bar().refresh()
def showEvent(self, e):
"""Extend showEvent to register us as the last-visible-main-window.
"""Extend showEvent to update window-visibility-history.
Args:
e: The QShowEvent
"""
super().showEvent(e)
objreg.register('last-visible-main-window', self, update=True)
objreg.window_visibility_history.append(self)
def hideEvent(self, e):
"""Extend hideEvent to update window-visibility-history.
Args:
e: The QHideEvent
"""
super().hideEvent(e)
objreg.window_visibility_history.remove(self)
def _confirm_quit(self):
"""Confirm that this window should be closed.
@ -689,13 +698,8 @@ class MainWindow(QWidget):
e.accept()
for key in ['last-visible-main-window', 'last-focused-main-window']:
try:
win = objreg.get(key)
if self is win:
objreg.delete(key)
except KeyError:
pass
objreg.window_focus_history.remove(self)
objreg.window_visibility_history.remove(self)
sessions.session_manager.save_last_window_session()
self._save_geometry()

View File

@ -7,6 +7,7 @@
import collections
import functools
import weakref
from typing import (TYPE_CHECKING, Any,
Optional, Union)
from collections.abc import MutableMapping, MutableSequence, Sequence, Callable
@ -38,6 +39,48 @@ class CommandOnlyError(Exception):
"""Raised when an object is requested which is used for commands only."""
_WindowRefType = weakref.ReferenceType['mainwindow.MainWindow']
class WindowAccessHistory():
"""Contains an ordered list of windows, sorted by last added.
Uses weak references to avoid memory leaks.
"""
def __init__(self) -> None:
self._windows: list[_WindowRefType] = []
def append(self, window: 'mainwindow.MainWindow') -> None:
"""Append a window to the access history."""
self.remove(window)
self._windows.append(weakref.ref(window))
def remove(self, window: 'mainwindow.MainWindow') -> None:
"""Remove a window from the access history."""
try:
self._windows.remove(weakref.ref(window))
except ValueError:
pass
def last(self) -> Optional['mainwindow.MainWindow']:
"""Return the last window from the access history.
Prune any None values and windows that are shutting down and
return the last still existing window from the list.
"""
for ref in reversed(self._windows):
win = ref()
if win is None or win.tabbed_browser.is_shutting_down:
self._windows.pop()
else:
return win
return None
window_visibility_history = WindowAccessHistory()
window_focus_history = WindowAccessHistory()
_IndexType = Union[str, int]
@ -308,24 +351,12 @@ def dump_objects() -> Sequence[str]:
def last_visible_window() -> 'mainwindow.MainWindow':
"""Get the last visible window, or the last focused window if none."""
try:
window = get('last-visible-main-window')
except KeyError:
return last_focused_window()
if window.tabbed_browser.is_shutting_down:
return last_focused_window()
return window
return window_visibility_history.last() or last_focused_window()
def last_focused_window() -> 'mainwindow.MainWindow':
"""Get the last focused window, or the last window if none."""
try:
window = get('last-focused-main-window')
except KeyError:
return last_opened_window()
if window.tabbed_browser.is_shutting_down:
return last_opened_window()
return window
return window_focus_history.last() or last_opened_window()
def _window_by_index(idx: int) -> 'mainwindow.MainWindow':