summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoofar <toofar@spalge.com>2024-03-10 21:20:03 +1300
committertoofar <toofar@spalge.com>2024-03-10 21:37:43 +1300
commitc2e8be53bc365bf346c3d38e39e87e4028fb2e3e (patch)
treee714e31dd2e71c8d0883b56ccb6c6860776f8574
parentcdf275993a258f6a8d7b05099c573524f0640ebe (diff)
downloadqutebrowser-c2e8be53bc365bf346c3d38e39e87e4028fb2e3e.tar.gz
qutebrowser-c2e8be53bc365bf346c3d38e39e87e4028fb2e3e.zip
Remove tree specific code from parent's undo() method.
Following from the previous commit that added an `UndoEntry.from_tab()` so that the TabbedBrowser could have a generic way of creating an undo entry, this commit adds an `UndoEntry.restore_into_tab()` that lets the parent TabbedBrowser handle restoring a tab without the TreeTabbedBrowser class overriding the `undo()` method. Mostly this just moves code around so that TreeTabbedBrowser doesn't have to store the undo entries before calling the super method, and we don't have to change the parent method to return something just for a child class. The core to that is being able to call a method per undo entry instead of going through all of them at once. We could have added a new helper function in TabbedBrowser to handle that and overridden it in TreeTabbedBrowser. But I think it is nice to tuck the undo related code into the undo related object anyhow. The code doesn't look like it has moved because I changed the variable names: newtab -> tab and entry -> self.
-rw-r--r--qutebrowser/mainwindow/tabbedbrowser.py14
-rw-r--r--qutebrowser/mainwindow/treetabbedbrowser.py63
2 files changed, 37 insertions, 40 deletions
diff --git a/qutebrowser/mainwindow/tabbedbrowser.py b/qutebrowser/mainwindow/tabbedbrowser.py
index bd81cc371..12b29e4e2 100644
--- a/qutebrowser/mainwindow/tabbedbrowser.py
+++ b/qutebrowser/mainwindow/tabbedbrowser.py
@@ -36,6 +36,12 @@ class _UndoEntry:
created_at: datetime.datetime = dataclasses.field(
default_factory=datetime.datetime.now)
+ def restore_into_tab(self, tab: browsertab.AbstractTab):
+ """Set the url, history and state of `tab` from this undo entry."""
+ tab.history.private_api.deserialize(self.history)
+ tab.set_pinned(self.pinned)
+ tab.setFocus()
+
@classmethod
def from_tab(cls, tab: browsertab.AbstractTab, idx: int):
"""Generate an undo entry from `tab`."""
@@ -597,8 +603,6 @@ class TabbedBrowser(QWidget):
entries = self.undo_stack[-depth]
del self.undo_stack[-depth]
- # we return the tab list because tree_tabs needs it in post_processing
- new_tabs = []
for entry in reversed(entries):
if use_current_tab:
newtab = self._tab_by_idx(0)
@@ -612,11 +616,7 @@ class TabbedBrowser(QWidget):
idx=entry.index
)
- newtab.history.private_api.deserialize(entry.history)
- newtab.set_pinned(entry.pinned)
- new_tabs.append(newtab)
- newtab.setFocus()
- return new_tabs
+ entry.restore_into_tab(newtab)
@pyqtSlot('QUrl', bool)
def load_url(self, url, newtab):
diff --git a/qutebrowser/mainwindow/treetabbedbrowser.py b/qutebrowser/mainwindow/treetabbedbrowser.py
index 4e729942a..6c7e89d8b 100644
--- a/qutebrowser/mainwindow/treetabbedbrowser.py
+++ b/qutebrowser/mainwindow/treetabbedbrowser.py
@@ -32,6 +32,35 @@ class _TreeUndoEntry():
created_at: datetime.datetime = dataclasses.field(
default_factory=datetime.datetime.now)
+ def restore_into_tab(self, tab: browsertab.AbstractTab):
+ tab.history.private_api.deserialize(self.history)
+ tab.set_pinned(self.pinned)
+ tab.setFocus()
+
+ root = tab.node.path[0]
+ uid = self.uid
+ parent_uid = self.parent_node_uid
+ parent_node = root.get_descendent_by_uid(parent_uid)
+ if not parent_node:
+ parent_node = root
+
+ children = []
+ for child_uid in self.children_node_uids:
+ child_node = root.get_descendent_by_uid(child_uid)
+ if child_node:
+ children.append(child_node)
+ tab.node.parent = None # Remove the node from the tree
+ tab.node = notree.Node(tab, parent_node,
+ children, uid)
+
+ # correctly reposition the tab
+ local_idx = self.local_index
+ if tab.node.parent: # should always be true
+ new_siblings = list(tab.node.parent.children)
+ new_siblings.remove(tab.node)
+ new_siblings.insert(local_idx, tab.node)
+ tab.node.parent.children = new_siblings
+
@classmethod
def from_tab(
cls,
@@ -153,39 +182,7 @@ class TreeTabbedBrowser(TabbedBrowser):
def undo(self, depth=1):
"""Undo removing of a tab or tabs."""
- # save entries before super().undo() pops them
- entries = self.undo_stack[-depth]
- new_tabs = super().undo(depth)
-
- for entry, tab in zip(reversed(entries), new_tabs):
- if not isinstance(entry, _TreeUndoEntry):
- # Likely we are undoing the close of a non-tree tab window
- # while a tree tab window is focused.
- continue
- root = self.widget.tree_root
- uid = entry.uid
- parent_uid = entry.parent_node_uid
- parent_node = root.get_descendent_by_uid(parent_uid)
- if not parent_node:
- parent_node = root
-
- children = []
- for child_uid in entry.children_node_uids:
- child_node = root.get_descendent_by_uid(child_uid)
- if child_node:
- children.append(child_node)
- tab.node.parent = None # Remove the node from the tree
- tab.node = notree.Node(tab, parent_node,
- children, uid)
-
- # correctly reposition the tab
- local_idx = entry.local_index
- if tab.node.parent: # should always be true
- new_siblings = list(tab.node.parent.children)
- new_siblings.remove(tab.node)
- new_siblings.insert(local_idx, tab.node)
- tab.node.parent.children = new_siblings
-
+ super().undo(depth)
self.widget.tree_tab_update()
def tabs(