Skip to content

Commit e1c69b2

Browse files
committed
feat: add consistent options close all option in working set and tab bar
1 parent 42fd15a commit e1c69b2

2 files changed

Lines changed: 40 additions & 91 deletions

File tree

src/extensions/default/CloseOthers/main.js

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ define(function (require, exports, module) {
3131
workingSetListCmenu = Menus.getContextMenu(Menus.ContextMenuIds.WORKING_SET_CONTEXT_MENU);
3232

3333
// Constants
34-
var closeOthers = "file.close_others",
35-
closeAbove = "file.close_above",
36-
closeBelow = "file.close_below";
34+
const closeAbove = "file.close_above",
35+
closeBelow = "file.close_below",
36+
closeAll = "file.close_all_in_pane";
3737

3838
// Global vars and preferences
3939
var prefs = PreferencesManager.getExtensionPrefs("closeOthers"),
@@ -42,16 +42,13 @@ define(function (require, exports, module) {
4242
prefs.definePreference("below", "boolean", true, {
4343
description: Strings.DESCRIPTION_CLOSE_OTHERS_BELOW
4444
});
45-
prefs.definePreference("others", "boolean", true, {
46-
description: Strings.DESCRIPTION_CLOSE_OTHERS
47-
});
4845
prefs.definePreference("above", "boolean", true, {
4946
description: Strings.DESCRIPTION_CLOSE_OTHERS_ABOVE
5047
});
5148

5249

5350
/**
54-
* Handle the different Close Other commands
51+
* Handle the different Close commands
5552
* @param {string} mode
5653
*/
5754
function handleClose(mode) {
@@ -63,23 +60,30 @@ define(function (require, exports, module) {
6360
i;
6461

6562
for (i = start; i < end; i++) {
66-
if ((mode === closeOthers && i !== targetIndex) || (mode !== closeOthers)) {
67-
files.push(workingSetList[i]);
68-
}
63+
files.push(workingSetList[i]);
6964
}
7065

7166
CommandManager.execute(Commands.FILE_CLOSE_LIST, {fileList: files});
7267
}
7368

69+
/**
70+
* Handle Close All - closes all files in the active pane
71+
*/
72+
function handleCloseAll() {
73+
let workingSetList = MainViewManager.getWorkingSet(MainViewManager.ACTIVE_PANE);
74+
CommandManager.execute(Commands.FILE_CLOSE_LIST, {fileList: workingSetList});
75+
}
76+
7477
/**
7578
* Enable/Disable the menu items depending on which document is selected in the working set
7679
*/
7780
function contextMenuOpenHandler() {
7881
var file = MainViewManager.getCurrentlyViewedFile(MainViewManager.ACTIVE_PANE);
7982

80-
// reset these labels for Working Set context (because tabBar may have changed them to "Left/Right")
83+
// reset these labels for Working Set context (because tabBar may have changed them)
8184
CommandManager.get(closeAbove).setName(Strings.CMD_FILE_CLOSE_ABOVE);
8285
CommandManager.get(closeBelow).setName(Strings.CMD_FILE_CLOSE_BELOW);
86+
CommandManager.get(closeAll).setName(Strings.CMD_FILE_CLOSE_ALL);
8387

8488
if (file) {
8589
var targetIndex = MainViewManager.findInWorkingSet(MainViewManager.ACTIVE_PANE, file.fullPath),
@@ -91,12 +95,6 @@ define(function (require, exports, module) {
9195
CommandManager.get(closeBelow).setEnabled(true);
9296
}
9397

94-
if (workingSetListSize === 1) { // hide "Close Others" if there is only one file in Working Files
95-
CommandManager.get(closeOthers).setEnabled(false);
96-
} else {
97-
CommandManager.get(closeOthers).setEnabled(true);
98-
}
99-
10098
if (targetIndex === 0) { // hide "Close Others Above" if the first file in Working Files is selected
10199
CommandManager.get(closeAbove).setEnabled(false);
102100
} else {
@@ -108,14 +106,13 @@ define(function (require, exports, module) {
108106

109107
/**
110108
* Returns the preferences used to add/remove the menu items
111-
* @return {{closeBelow: boolean, closeOthers: boolean, closeAbove: boolean}}
109+
* @return {{closeBelow: boolean, closeAbove: boolean}}
112110
*/
113111
function getPreferences() {
114112
// It's senseless to look prefs up for the current file, instead look them up for
115113
// the current project (or globally)
116114
return {
117115
closeBelow: prefs.get("below", PreferencesManager.CURRENT_PROJECT),
118-
closeOthers: prefs.get("others", PreferencesManager.CURRENT_PROJECT),
119116
closeAbove: prefs.get("above", PreferencesManager.CURRENT_PROJECT)
120117
};
121118
}
@@ -126,22 +123,6 @@ define(function (require, exports, module) {
126123
function prefChangeHandler() {
127124
var prefs = getPreferences();
128125

129-
if (prefs.closeBelow !== menuEntriesShown.closeBelow) {
130-
if (prefs.closeBelow) {
131-
workingSetListCmenu.addMenuItem(closeBelow, "", Menus.AFTER, Commands.FILE_CLOSE);
132-
} else {
133-
workingSetListCmenu.removeMenuItem(closeBelow);
134-
}
135-
}
136-
137-
if (prefs.closeOthers !== menuEntriesShown.closeOthers) {
138-
if (prefs.closeOthers) {
139-
workingSetListCmenu.addMenuItem(closeOthers, "", Menus.AFTER, Commands.FILE_CLOSE);
140-
} else {
141-
workingSetListCmenu.removeMenuItem(closeOthers);
142-
}
143-
}
144-
145126
if (prefs.closeAbove !== menuEntriesShown.closeAbove) {
146127
if (prefs.closeAbove) {
147128
workingSetListCmenu.addMenuItem(closeAbove, "", Menus.AFTER, Commands.FILE_CLOSE);
@@ -150,6 +131,14 @@ define(function (require, exports, module) {
150131
}
151132
}
152133

134+
if (prefs.closeBelow !== menuEntriesShown.closeBelow) {
135+
if (prefs.closeBelow) {
136+
workingSetListCmenu.addMenuItem(closeBelow, "", Menus.AFTER, closeAbove);
137+
} else {
138+
workingSetListCmenu.removeMenuItem(closeBelow);
139+
}
140+
}
141+
153142
menuEntriesShown = prefs;
154143
}
155144

@@ -162,22 +151,20 @@ define(function (require, exports, module) {
162151
CommandManager.register(Strings.CMD_FILE_CLOSE_BELOW, closeBelow, function () {
163152
handleClose(closeBelow);
164153
});
165-
CommandManager.register(Strings.CMD_FILE_CLOSE_OTHERS, closeOthers, function () {
166-
handleClose(closeOthers);
167-
});
168154
CommandManager.register(Strings.CMD_FILE_CLOSE_ABOVE, closeAbove, function () {
169155
handleClose(closeAbove);
170156
});
157+
CommandManager.register(Strings.CMD_FILE_CLOSE_ALL, closeAll, handleCloseAll);
171158

172-
if (prefs.closeBelow) {
173-
workingSetListCmenu.addMenuItem(closeBelow, "", Menus.AFTER, Commands.FILE_CLOSE);
174-
}
175-
if (prefs.closeOthers) {
176-
workingSetListCmenu.addMenuItem(closeOthers, "", Menus.AFTER, Commands.FILE_CLOSE);
177-
}
159+
// Menu order: Close, Close Others Above, Close Others Below, Close All
178160
if (prefs.closeAbove) {
179161
workingSetListCmenu.addMenuItem(closeAbove, "", Menus.AFTER, Commands.FILE_CLOSE);
180162
}
163+
if (prefs.closeBelow) {
164+
workingSetListCmenu.addMenuItem(closeBelow, "", Menus.AFTER, closeAbove);
165+
}
166+
workingSetListCmenu.addMenuItem(closeAll, "", Menus.AFTER, closeBelow);
167+
181168
menuEntriesShown = prefs;
182169
}
183170

src/extensionsIntegrated/TabBar/more-options.js

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,17 @@
2525
define(function (require, exports, module) {
2626
const CommandManager = require("command/CommandManager");
2727
const Commands = require("command/Commands");
28-
const FileSystem = require("filesystem/FileSystem");
2928
const MainViewManager = require("view/MainViewManager");
3029
const Menus = require("command/Menus");
3130
const Strings = require("strings");
3231

33-
const Global = require("./global");
34-
35-
// these are Tab bar specific commands for the context menu
36-
// not added in the Commands.js as Tab bar is not a core module but an extension
37-
// read init function
38-
const TABBAR_CLOSE_ALL = "tabbar.closeAllTabs";
39-
4032
// command IDs from working files - we reuse it here with different labels
4133
// Close Others Above = Close Tabs to the Left
4234
// Close Others Below = Close Tabs to the Right
35+
// Close All (in pane) = Close All Tabs
4336
const FILE_CLOSE_ABOVE = "file.close_above";
4437
const FILE_CLOSE_BELOW = "file.close_below";
38+
const FILE_CLOSE_ALL = "file.close_all_in_pane";
4539

4640
// stores the context of the right-clicked tab (which file, which pane)
4741
// this is set inside the showMoreOptionsContextMenu. read that func for more details
@@ -62,43 +56,13 @@ define(function (require, exports, module) {
6256
const pinCommand = CommandManager.get(Commands.FILE_PIN);
6357
pinCommand.setName(isPinned ? Strings.CMD_FILE_UNPIN : Strings.CMD_FILE_PIN);
6458

65-
// update Close Above/Below labels for TabBar context (Left/Right instead of Above/Below)
59+
// update Close Above/Below/All labels for TabBar context
6660
const closeAboveCmd = CommandManager.get(FILE_CLOSE_ABOVE);
6761
const closeBelowCmd = CommandManager.get(FILE_CLOSE_BELOW);
62+
const closeAllCmd = CommandManager.get(FILE_CLOSE_ALL);
6863
closeAboveCmd.setName(Strings.CLOSE_TABS_TO_THE_LEFT);
6964
closeBelowCmd.setName(Strings.CLOSE_TABS_TO_THE_RIGHT);
70-
}
71-
72-
// gets the working set (list of open files) for the given pane
73-
function _getWorkingSet(paneId) {
74-
return paneId === "first-pane" ? Global.firstPaneWorkingSet : Global.secondPaneWorkingSet;
75-
}
76-
77-
// closes files from right to left to avoid index shifts during iteration
78-
function _closeFiles(files, paneId) {
79-
for (let i = files.length - 1; i >= 0; i--) {
80-
const fileObj = FileSystem.getFileForPath(files[i].path);
81-
CommandManager.execute(Commands.FILE_CLOSE, { file: fileObj, paneId: paneId });
82-
}
83-
}
84-
85-
// executes a command with the right-clicked tab's file as the target
86-
function _executeWithFileContext(commandId, options = {}) {
87-
if (_currentTabContext.filePath) {
88-
// we need to get the file object from the file path, as the commandManager expects the file object
89-
const fileObj = FileSystem.getFileForPath(_currentTabContext.filePath);
90-
CommandManager.execute(commandId, { file: fileObj, ...options });
91-
}
92-
}
93-
94-
// **Close All Tabs**
95-
// closes all tabs in the pane where the tab was right-clicked
96-
function handleCloseAllTabs() {
97-
const workingSet = _getWorkingSet(_currentTabContext.paneId);
98-
if (workingSet && workingSet.length !== 0) {
99-
// close everything in the pane
100-
_closeFiles(workingSet, _currentTabContext.paneId);
101-
}
65+
closeAllCmd.setName(Strings.CLOSE_ALL_TABS);
10266
}
10367

10468
/**
@@ -124,14 +88,12 @@ define(function (require, exports, module) {
12488
* this registers the context menu and add the menu items inside it
12589
*/
12690
function init() {
127-
// these are the tab bar specific commands
128-
CommandManager.register(Strings.CLOSE_ALL_TABS, TABBAR_CLOSE_ALL, handleCloseAllTabs);
129-
91+
// Use commands from CloseOthers extension with TabBar-specific labels (via setName in _updateMenuItems)
13092
const menu = Menus.registerContextMenu("tabbar-context-menu");
13193
menu.addMenuItem(Commands.FILE_CLOSE);
132-
menu.addMenuItem(FILE_CLOSE_ABOVE); // updated label will be : "Close Tabs to the Left"
133-
menu.addMenuItem(FILE_CLOSE_BELOW); // updated label will be : "Close Tabs to the Right"
134-
menu.addMenuItem(TABBAR_CLOSE_ALL);
94+
menu.addMenuItem(FILE_CLOSE_ABOVE); // updated label: "Close Tabs to the Left"
95+
menu.addMenuItem(FILE_CLOSE_BELOW); // updated label: "Close Tabs to the Right"
96+
menu.addMenuItem(FILE_CLOSE_ALL); // updated label: "Close All Tabs"
13597
menu.addMenuDivider();
13698
menu.addMenuItem(Commands.FILE_PIN);
13799
menu.addMenuDivider();

0 commit comments

Comments
 (0)