Skip to content

Conversation

sendevent
Copy link

This PR introduces a tab's RMB-click menu with dynamically available actions for copying file location info and advanced closing options:

  • Right-click: activates the selected tab and opens a persistent context menu.
  • Copy File Name: Copies the file name of the clicked tab, if available.
  • Copy File Path: Copies the absolute file path of the clicked tab, if available.
  • Close: Closes the clicked tab.
  • Close All Tabs: Closes all tabs; enabled if 2+ tabs are open.
  • Close Other Tabs: Closes all tabs except the clicked one; enabled if 2+ tabs are open.
  • Close Unmodified Tabs: Closes tabs with no unsaved changes; enabled if 2+ tabs are open.
  • Close Tabs to the Left/Right: Closes all tabs in the specified direction; enabled if 2+ tabs are open and the clicked one is not at the edge.

image

Unknown = ''

CopyFileName = QObject.tr('Copy File Name')
CopyFilePath = QObject.tr('Copy file path')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 9 you capitalized all words, and here only one.

I think other interface capitalizes only the first word, can we make everything like that?

if action_type is TabActionTypes.Close:
indices_to_close = [curr_tab]
elif action_type is TabActionTypes.CloseAll:
indices_to_close = list(range(last_tab + 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of last_tab + 1, maybe use total_tabs (here and below)?

CloseOther = QObject.tr('Close Other Tabs')
CloseUnmodified = QObject.tr('Close Unmodified Tabs')
CloseToLeft = QObject.tr('Close Tabs to the Left')
CloseToRight = QObject.tr('Close Tabs to the Right')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need so many options? Are you going to use all them yourself?

self._tabs_ctx_menu_actions = {}

for action_type in TabActionTypes:
if action_type is not TabActionTypes.Unknown:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to compare enum values with is / is not instead of == / !=?


clicked_tab_index = self.tabWidget.tabBar().tabAt(p)
if -1 == clicked_tab_index:
raise RuntimeWarning(f"No tab found at [{p.x()};{p.y()}].")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably wanted to use warnings.warn() function instead of raise?

self._handle_tab_context_action(chosen_action)

def _handle_tab_context_action(self, action):
if not action or action.text() == TabActionTypes.Unknown.value:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the action.text() == TabActionTypes.Unknown.value part ever happen? Do we need the Unknown enum member at all?

@@ -519,6 +520,107 @@ def dropEvent(e):
self.tabWidget.dropEvent = dropEvent
self.tabWidget.setTabBarAutoHide(globalSettings.tabBarAutoHide)

self._init_tabs_context_menu()

def _init_tabs_context_menu(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to the other PR, please use camelCase and no leading underscore.


indices_to_close.sort(reverse=True)

for tab_num in indices_to_close:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use reversed(indices_to_close) here, then you won’t need to convert ranges to lists.

@@ -0,0 +1,18 @@
from enum import StrEnum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StrEnum was introduced in Python 3.11, so if you want to use that, we would need to bump required Python version to 3.11. Please do that in a separate PR.

Or use simple Enum.

indices_to_close = [i for i in range(last_tab + 1) if i != curr_tab]
elif action_type is TabActionTypes.CloseUnmodified:
indices_to_close = [i for i in range(last_tab + 1)
if i != curr_tab and not self.tabWidget.widget(i).editBox.document().isModified()]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion: you can also use iterateTabs() method, maybe that will be a bit shorter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants