Skip to content

Commit 99caa68

Browse files
committed
Close tab with x key
Because: - This used to work in the old Safari extension This commit: - Closes the current Safari tab when the user presses x. The previous implementation let the user pick whether to close the tab left or right. For now, this implementation relies on Safari's default behaviour to pick the left or right tab. This feels more 'Mac-like'.
1 parent 7f3d9f5 commit 99caa68

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Changelog
55

66
* Fix newTabHintToggle to use shift+f instead of F
77
* Implement forward tab and backward tab commands.
8+
* Close tab with x is now implemented. Note that this relies on Safari's default behaviour to choose whether to switch to the left or right tab after closing the current tab.
89

910
### 2.0.2 (2019-09-23)
1011

Vimari Extension/SafariExtensionHandler.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ enum ActionType: String {
55
case openNewTab
66
case tabForward
77
case tabBackward
8+
case closeTab
89
}
910

1011
enum TabDirection: String {
@@ -37,6 +38,8 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
3738
changeTab(withDirection: .forward, from: page)
3839
case .tabBackward:
3940
changeTab(withDirection: .backward, from: page)
41+
case .closeTab:
42+
closeTab(from: page)
4043
}
4144
}
4245

@@ -58,7 +61,7 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
5861
}
5962
}
6063

61-
func changeTab(withDirection direction: TabDirection, from page: SFSafariPage) {
64+
func changeTab(withDirection direction: TabDirection, from page: SFSafariPage, completionHandler: (() -> Void)? = nil ) {
6265
page.getContainingTab(completionHandler: { currentTab in
6366
currentTab.getContainingWindow(completionHandler: { window in
6467
window?.getAllTabs(completionHandler: { tabs in
@@ -69,13 +72,21 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
6972
// % calculates the remainder, not the modulus, so we need a
7073
// custom function.
7174
let newIndex = mod(currentIndex + indexStep, tabs.count)
72-
73-
tabs[newIndex].activate {}
75+
76+
tabs[newIndex].activate(completionHandler: completionHandler ?? {})
77+
7478
}
7579
})
7680
})
7781
})
7882
}
83+
84+
func closeTab(from page: SFSafariPage) {
85+
page.getContainingTab {
86+
tab in
87+
tab.close()
88+
}
89+
}
7990

8091
override func toolbarItemClicked(in _: SFSafariWindow) {
8192
// This method will be called when your toolbar item is clicked.

Vimari Extension/js/injected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var actionMap = {
6767
function() { openNewTab(); },
6868

6969
'closeTab':
70-
function() { safari.self.tab.dispatchMessage('closeTab', 0); },
70+
function() { safari.extension.dispatchMessage("closeTab"); },
7171

7272
'closeTabReverse':
7373
function() { safari.self.tab.dispatchMessage('closeTab', 1); },

0 commit comments

Comments
 (0)