diff --git a/grill/views/description.py b/grill/views/description.py index 4a11f746..b64a41f1 100644 --- a/grill/views/description.py +++ b/grill/views/description.py @@ -596,33 +596,24 @@ def tabRemoved(self, index: int) -> None: del self._browsers_by_layer[self._tab_layer_by_idx.pop(index)] def mousePressEvent(self, event): - if event.button() == QtCore.Qt.RightButton: - if (tab_index:=self.tabBar().tabAt(event.pos())) != -1: - def _copy(content): - QtWidgets.QApplication.instance().clipboard().setText(content) - - widget = self.widget(tab_index) - copy_identifier = QtGui.QAction("Copy Identifier", self) - copy_identifier.triggered.connect(partial(_copy, widget._identifier)) - copy_resolved_path = QtGui.QAction("Copy Resolved Path", self) - copy_resolved_path.triggered.connect(partial(_copy, widget._resolved_path)) - - menu = QtWidgets.QMenu(self) - menu.addAction(copy_identifier) - menu.addAction(copy_resolved_path) - menu.addSeparator() - if tab_index < (max_tab_idx:=len(self._tab_layer_by_idx))-1: - close_right_tabs = QtGui.QAction("Close Tabs to the Right", self) - close_right_tabs.triggered.connect(partial(self._close_many, range(tab_index+1, max_tab_idx+1))) - menu.addAction(close_right_tabs) - if tab_index > 0: - close_left_tabs = QtGui.QAction("Close Tabs to the Left", self) - close_left_tabs.triggered.connect(partial(self._close_many, range(tab_index))) - menu.addAction(close_left_tabs) - menu.exec(event.globalPos()) + if event.button() == QtCore.Qt.RightButton and (tab_index := self.tabBar().tabAt(event.pos())) != -1: + self._menu_for_tab(tab_index).exec(event.globalPos()) super().mousePressEvent(event) + def _menu_for_tab(self, tab_index): + widget = self.widget(tab_index) + clipboard = QtWidgets.QApplication.instance().clipboard() + menu = QtWidgets.QMenu(self) + menu.addAction("Copy Identifier", partial(clipboard.setText, widget._identifier)) + menu.addAction("Copy Resolved Path", partial(clipboard.setText, widget._resolved_path)) + menu.addSeparator() + if tab_index < (max_tab_idx := len(self._tab_layer_by_idx)) - 1: + menu.addAction("Close Tabs to the Right", partial(self._close_many, range(tab_index + 1, max_tab_idx + 1))) + if tab_index > 0: + menu.addAction("Close Tabs to the Left", partial(self._close_many, range(tab_index))) + return menu + def _close_many(self, indices: range): for index in reversed(indices): self.tabCloseRequested.emit(index) diff --git a/tests/test_views.py b/tests/test_views.py index c8be9950..a57e5954 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -633,7 +633,7 @@ def _fake_run(run_args: list): # sdffilter still not coming via pypi, so patch for now with mock.patch("grill.views.description._core._run", new=_fake_run if not description._which("sdffilter") else _core_run): dialog = description._start_content_browser(*args) - browser = dialog.findChild(description._PseudoUSDBrowser) + browser: description._PseudoUSDBrowser = dialog.findChild(description._PseudoUSDBrowser) assert browser._browsers_by_layer.values() first_browser_widget, = browser._browsers_by_layer.values() first_browser_widget._format_options.setCurrentIndex(0) # pseudoLayer (through sdffilter) @@ -643,6 +643,10 @@ def _fake_run(run_args: list): browser._on_identifier_requested(anchor, layers[1].identifier) with mock.patch(f"{QtWidgets.__name__}.QMessageBox.warning", new=_log): browser._on_identifier_requested(anchor, "/missing/file.usd") + + menu = browser._menu_for_tab(0) + self.assertTrue(bool(menu.actions())) + browser.tabCloseRequested.emit(0) # request closing our first tab for child in dialog.findChildren(description._PseudoUSDBrowser): child._resolved_layers.clear() @@ -651,10 +655,9 @@ def _fake_run(run_args: list): _, sourcepath = tempfile.mkstemp() prim_index.DumpToDotGraph(sourcepath) targetpath = f"{sourcepath}.png" - error, __ = _core_run([_core._which("dot"), sourcepath, "-Tpng", "-o", targetpath]) - if error: - raise RuntimeError(error) - browser._addImageTab(targetpath, identifier=targetpath) + # create a temporary file loadable by our image tab + _core_run([_core._which("dot"), sourcepath, "-Tpng", "-o", targetpath]) + browser._on_identifier_requested(anchor, targetpath) invalid_crate_layer = Sdf.Layer.CreateAnonymous() invalid_crate_layer.ImportFromString(