Skip to content

Commit a09eaf7

Browse files
committed
keyboard shortcut for tab switching
1 parent ac3b4ca commit a09eaf7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

CodeEdit/Features/Editor/TabBar/Tabs/Views/EditorTabs.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ struct EditorTabs: View {
114114
CGFloat(140)
115115
)
116116
}
117+
118+
117119

118120
// Disable the rule because this function is implementing the drag gesture and its animations.
119121
// It is fairly complicated, so ignore the function body length limitation for now.
@@ -271,6 +273,17 @@ struct EditorTabs: View {
271273
}
272274
}
273275
}
276+
private func selectNextTab() {
277+
guard let currentIndex = openedTabs.firstIndex(of: editor.selectedTab?.file.id ?? "") else { return }
278+
let nextIndex = (currentIndex + 1) % openedTabs.count // Wraps around to the first tab if it's the last one
279+
editor.selectedTab = editor.tabs.first { $0.file.id == openedTabs[nextIndex] }
280+
}
281+
282+
private func selectPreviousTab() {
283+
guard let currentIndex = openedTabs.firstIndex(of: editor.selectedTab?.file.id ?? ""), !openedTabs.isEmpty else { return }
284+
let previousIndex = (currentIndex - 1 + openedTabs.count) % openedTabs.count // Wraps around to the last tab if it's the first one
285+
editor.selectedTab = editor.tabs.first { $0.file.id == openedTabs[previousIndex] }
286+
}
274287

275288
// swiftlint:enable function_body_length cyclomatic_complexity
276289

@@ -385,6 +398,17 @@ struct EditorTabs: View {
385398
EditorTabBarNativeInactiveBackground()
386399
}
387400
}
401+
// keyboard shortcuts to go to next and previous tabs
402+
Button(action: selectNextTab) {
403+
EmptyView()
404+
}
405+
.hidden()
406+
.keyboardShortcut("]", modifiers: [.command, .shift])
407+
Button(action: selectPreviousTab) {
408+
EmptyView()
409+
}
410+
.hidden()
411+
.keyboardShortcut("[", modifiers: [.command, .shift])
388412
}
389413
.background {
390414
if tabBarStyle == .native {
@@ -412,6 +436,7 @@ struct EditorTabs: View {
412436
}
413437
}
414438
}
439+
415440
}
416441

417442
private struct EditorTabOnDropDelegate: DropDelegate {

0 commit comments

Comments
 (0)