Skip to content

Commit

Permalink
Move focused tab to forward / backward by shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
JeongJun-Lee committed Aug 23, 2024
1 parent 3cd59c1 commit 76b638c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mu/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ def load_theme(theme):
editor_window.connect_find_again(find_again_handlers, "F3")
editor_window.connect_toggle_comments(editor.toggle_comments, "Ctrl+K")
editor_window.connect_close_tab(editor.close_tab, "Ctrl+W")
editor_window.connect_move_forward_tab(editor.move_forward_tab, "Ctrl+.")
editor_window.connect_move_backward_tab(editor.move_backward_tab, "Ctrl+,")
editor.connect_to_status_bar(editor_window.status_bar)

# Restore the previous session along with files passed by the os
Expand Down
34 changes: 33 additions & 1 deletion mu/interface/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,22 @@ def focus_tab(self, tab):
self.tabs.setCurrentIndex(index)
tab.setFocus()

def move_tab(self, tab, is_forward):
"""
Move focused tab to forward / backward.
"""
tab_cnt = len(self.tabs)
if tab_cnt:
index = self.tabs.indexOf(tab)
if is_forward:
index += 1
if index == tab_cnt: index = 0
else:
index -= 1
if index < 0: index = tab_cnt - 1
self.tabs.setCurrentIndex(index)
tab.setFocus()

@property
def tab_count(self):
"""
Expand Down Expand Up @@ -1366,11 +1382,27 @@ def connect_close_tab(self, handler, shortcut):

def close_tab(self):
"""
Delete complete line on the cursor in the currently active tab.
Close the currently active tab.
"""
if self.current_tab:
self.tabs.removeTab(self.widgets.index(self.current_tab))

def connect_move_forward_tab(self, handler, shortcut):
"""
Create a keyboard shortcut and associate it with a handler for moving
a current active tab forward.
"""
self.move_forward_tab_shortcut = QShortcut(QKeySequence(shortcut), self)
self.move_forward_tab_shortcut.activated.connect(handler)

def connect_move_backward_tab(self, handler, shortcut):
"""
Create a keyboard shortcut and associate it with a handler for moving
a current active tab backward.
"""
self.move_backward_tab_shortcut = QShortcut(QKeySequence(shortcut), self)
self.move_backward_tab_shortcut.activated.connect(handler)

def show_device_selector(self):
"""
Reveals the device selector in the status bar
Expand Down
12 changes: 12 additions & 0 deletions mu/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,18 @@ def close_tab(self):
"""
self._view.close_tab()

def move_forward_tab(self):
"""
Handle a shortcut for moving the current tab forward.
"""
self._view.move_tab(self._view.current_tab, True)

def move_backward_tab(self):
"""
Handle a shortcut for moving the current tab backward.
"""
self._view.move_tab(self._view.current_tab, False)

def tidy_code(self):
"""
Prettify code with Black.
Expand Down

0 comments on commit 76b638c

Please sign in to comment.