Skip to content

Commit 1694b8c

Browse files
authored
keyboard shortcut for tab switching (#1677)
* keyboard shortcut for tab switching * extension and menu bar * resolve swiftlint violations
1 parent ac3b4ca commit 1694b8c

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
58F2EB1E292FB954004A9BDE /* Sparkle in Frameworks */ = {isa = PBXBuildFile; productRef = 58F2EB1D292FB954004A9BDE /* Sparkle */; };
249249
58FD7608291EA1CB0051D6E4 /* QuickActionsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FD7605291EA1CB0051D6E4 /* QuickActionsViewModel.swift */; };
250250
58FD7609291EA1CB0051D6E4 /* QuickActionsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58FD7607291EA1CB0051D6E4 /* QuickActionsView.swift */; };
251+
5994B6DA2BD6B408006A4C5F /* EditorTabSwitchExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5994B6D92BD6B408006A4C5F /* EditorTabSwitchExtension.swift */; };
251252
5B241BF32B6DDBFF0016E616 /* IgnorePatternListItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B241BF22B6DDBFF0016E616 /* IgnorePatternListItemView.swift */; };
252253
5B698A0A2B262FA000DE9392 /* SearchSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B698A092B262FA000DE9392 /* SearchSettingsView.swift */; };
253254
5B698A0D2B26327800DE9392 /* SearchSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B698A0C2B26327800DE9392 /* SearchSettings.swift */; };
@@ -810,6 +811,7 @@
810811
58F2EAE1292FB2B0004A9BDE /* SoftwareUpdater.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SoftwareUpdater.swift; sourceTree = "<group>"; };
811812
58FD7605291EA1CB0051D6E4 /* QuickActionsViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QuickActionsViewModel.swift; sourceTree = "<group>"; };
812813
58FD7607291EA1CB0051D6E4 /* QuickActionsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QuickActionsView.swift; sourceTree = "<group>"; };
814+
5994B6D92BD6B408006A4C5F /* EditorTabSwitchExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorTabSwitchExtension.swift; sourceTree = "<group>"; };
813815
5B241BF22B6DDBFF0016E616 /* IgnorePatternListItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IgnorePatternListItemView.swift; sourceTree = "<group>"; };
814816
5B698A092B262FA000DE9392 /* SearchSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchSettingsView.swift; sourceTree = "<group>"; };
815817
5B698A0C2B26327800DE9392 /* SearchSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchSettings.swift; sourceTree = "<group>"; };
@@ -2752,6 +2754,7 @@
27522754
6CA1AE942B46950000378EAB /* EditorInstance.swift */,
27532755
6CC9E4B129B5669900C97388 /* Environment+ActiveEditor.swift */,
27542756
6C092ED92A53A58600489202 /* EditorLayout+StateRestoration.swift */,
2757+
5994B6D92BD6B408006A4C5F /* EditorTabSwitchExtension.swift */,
27552758
);
27562759
path = Models;
27572760
sourceTree = "<group>";
@@ -3475,6 +3478,7 @@
34753478
581550CF29FBD30400684881 /* StandardTableViewCell.swift in Sources */,
34763479
B62AEDB82A1FE2DC009A9F52 /* UtilityAreaOutputView.swift in Sources */,
34773480
B67DB0FC2AFDF71F002DC647 /* IconToggleStyle.swift in Sources */,
3481+
5994B6DA2BD6B408006A4C5F /* EditorTabSwitchExtension.swift in Sources */,
34783482
587B9E5C29301D8F00AC7927 /* Parameters.swift in Sources */,
34793483
61538B932B11201900A88846 /* String+Character.swift in Sources */,
34803484
613DF55E2B08DD5D00E9D902 /* FileHelper.swift in Sources */,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// EditorTabSwitchExtension.swift
3+
// CodeEdit
4+
//
5+
// Created by Roscoe Rubin-Rottenberg on 4/22/24.
6+
//
7+
8+
import Foundation
9+
10+
extension Editor {
11+
func selectNextTab() {
12+
guard let currentTab = selectedTab, let currentIndex = tabs.firstIndex(of: currentTab) else { return }
13+
let nextIndex = tabs.index(after: currentIndex)
14+
if nextIndex < tabs.endIndex {
15+
selectedTab = tabs[nextIndex]
16+
} else {
17+
// Wrap around to the first tab if it's the last one
18+
selectedTab = tabs.first
19+
}
20+
}
21+
22+
func selectPreviousTab() {
23+
guard let currentTab = selectedTab, let currentIndex = tabs.firstIndex(of: currentTab) else { return }
24+
let previousIndex = tabs.index(before: currentIndex)
25+
if previousIndex >= tabs.startIndex {
26+
selectedTab = tabs[previousIndex]
27+
} else {
28+
// Wrap around to the last tab if it's the first one
29+
selectedTab = tabs.last
30+
}
31+
}
32+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ struct EditorTabs: View {
114114
CGFloat(140)
115115
)
116116
}
117-
118117
// Disable the rule because this function is implementing the drag gesture and its animations.
119118
// It is fairly complicated, so ignore the function body length limitation for now.
120119
// swiftlint:disable function_body_length cyclomatic_complexity

CodeEdit/Features/WindowCommands/NavigateCommands.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ struct NavigateCommands: Commands {
3939
Divider()
4040

4141
}
42-
4342
Group {
4443
Button("Show Previous Tab") {
45-
44+
editor?.selectPreviousTab()
4645
}
47-
.disabled(true)
46+
.keyboardShortcut("{", modifiers: [.command])
47+
.disabled(editor?.tabs.count ?? 0 <= 1) // Disable if there's one or no tabs
4848

4949
Button("Show Next Tab") {
50-
50+
editor?.selectNextTab()
5151
}
52-
.disabled(true)
53-
52+
.keyboardShortcut("}", modifiers: [.command])
53+
.disabled(editor?.tabs.count ?? 0 <= 1) // Disable if there's one or no tabs
54+
}
55+
Group {
5456
Divider()
5557

5658
Button("Go Forward") {

0 commit comments

Comments
 (0)