Skip to content

Commit 9eb9b48

Browse files
committed
feat: update pin/unpin architecture to use a single command and update the name dynamically
1 parent da72a5b commit 9eb9b48

7 files changed

Lines changed: 36 additions & 55 deletions

File tree

src/command/Commands.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,9 @@ define(function (require, exports, module) {
8585
/** Closes files from list */
8686
exports.FILE_CLOSE_LIST = "file.close_list"; // DocumentCommandHandlers.js handleFileCloseList()
8787

88-
/** Pins the selected file */
88+
/** Toggles the pinned state of the selected file */
8989
exports.FILE_PIN = "file.pin"; // DocumentCommandHandlers.js handleFilePin()
9090

91-
/** Unpins the selected file */
92-
exports.FILE_UNPIN = "file.unpin"; // DocumentCommandHandlers.js handleFileUnpin()
93-
9491
/** Reopens last closed file */
9592
exports.FILE_REOPEN_CLOSED = "file.reopen_closed"; // DocumentCommandHandlers.js handleReopenClosed()
9693

src/command/DefaultMenus.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ define(function (require, exports, module) {
7070
});
7171
}
7272

73-
// Pin/Unpin logic: to hide the option when its not applicable
73+
// Pin/Unpin logic: update label based on current state
7474
const contextFile = WorkingSetView.getContext();
7575
if (contextFile) {
7676
const isPinned = MainViewManager.isPathPinned(MainViewManager.ACTIVE_PANE, contextFile.fullPath);
77-
CommandManager.get(Commands.FILE_PIN).setEnabled(!isPinned);
78-
CommandManager.get(Commands.FILE_UNPIN).setEnabled(isPinned);
77+
const pinCommand = CommandManager.get(Commands.FILE_PIN);
78+
pinCommand.setName(isPinned ? Strings.CMD_FILE_UNPIN : Strings.CMD_FILE_PIN);
7979
}
8080
}
8181

@@ -329,8 +329,7 @@ define(function (require, exports, module) {
329329
subMenu.addMenuItem(Commands.NAVIGATE_OPEN_IN_DEFAULT_APP);
330330
}
331331
workingset_cmenu.addMenuDivider();
332-
workingset_cmenu.addMenuItem(Commands.FILE_PIN, null, null, null, { hideWhenCommandDisabled: true });
333-
workingset_cmenu.addMenuItem(Commands.FILE_UNPIN, null, null, null, { hideWhenCommandDisabled: true });
332+
workingset_cmenu.addMenuItem(Commands.FILE_PIN);
334333
workingset_cmenu.addMenuDivider();
335334
workingset_cmenu.addMenuItem(Commands.FILE_COPY);
336335
workingset_cmenu.addMenuItem(Commands.FILE_COPY_PATH);

src/document/DocumentCommandHandlers.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,31 +2003,33 @@ define(function (require, exports, module) {
20032003
}
20042004

20052005
/**
2006-
* we call this function when the user selects the 'Pin' option from the context menu
2007-
* so we pin the file. the pinned file is displayed before the normal files in the working set and tab bar
2008-
* we also prevent the pinned files from being closed during bulk close operations
2009-
* @param {{file: File, paneId: string}} commandData - the file to pin and paneId,
2010-
* if unavailable we use the current file and active pane
2006+
* Toggles the pin state of a file. Pinned files are displayed before normal files
2007+
* in the working set and tab bar, and are protected from bulk close operations.
2008+
* @param {{file: File, paneId: string, forcePin: boolean, forceUnpin: boolean}} commandData
2009+
* - file: the file to pin/unpin (defaults to currently viewed file)
2010+
* - paneId: the pane ID (defaults to active pane)
2011+
* - forcePin: if true, always pins
2012+
* - forceUnpin: if true, always unpins
2013+
* - if neither forcePin nor forceUnpin, toggles based on current state
20112014
*/
20122015
function handleFilePin(commandData = {}) {
20132016
const file = commandData.file || MainViewManager.getCurrentlyViewedFile();
20142017
const paneId = commandData.paneId || MainViewManager.ACTIVE_PANE;
20152018

20162019
if (file) {
2017-
MainViewManager.pinFile(paneId, file);
2018-
}
2019-
}
2020-
2021-
/**
2022-
* this unpins the file so it goes back to normal behavior in the working set and tab bar
2023-
* @param {{file: File, paneId: string}} commandData - read the JSDoc for handleFilePin
2024-
*/
2025-
function handleFileUnpin(commandData = {}) {
2026-
const file = commandData.file || MainViewManager.getCurrentlyViewedFile();
2027-
const paneId = commandData.paneId || MainViewManager.ACTIVE_PANE;
2028-
2029-
if (file) {
2030-
MainViewManager.unpinFile(paneId, file);
2020+
if (commandData.forcePin) {
2021+
MainViewManager.pinFile(paneId, file);
2022+
} else if (commandData.forceUnpin) {
2023+
MainViewManager.unpinFile(paneId, file);
2024+
} else {
2025+
// Toggle based on current state
2026+
const isPinned = MainViewManager.isPathPinned(paneId, file.fullPath);
2027+
if (isPinned) {
2028+
MainViewManager.unpinFile(paneId, file);
2029+
} else {
2030+
MainViewManager.pinFile(paneId, file);
2031+
}
2032+
}
20312033
}
20322034
}
20332035

@@ -2410,7 +2412,6 @@ define(function (require, exports, module) {
24102412
CommandManager.register(Strings.CMD_FILE_RENAME, Commands.FILE_RENAME, handleFileRename);
24112413
CommandManager.register(Strings.CMD_FILE_DELETE, Commands.FILE_DELETE, handleFileDelete);
24122414
CommandManager.register(Strings.CMD_FILE_PIN, Commands.FILE_PIN, handleFilePin);
2413-
CommandManager.register(Strings.CMD_FILE_UNPIN, Commands.FILE_UNPIN, handleFileUnpin);
24142415

24152416
// Close Commands
24162417
CommandManager.register(Strings.CMD_FILE_CLOSE, Commands.FILE_CLOSE, handleFileClose);

src/extensionsIntegrated/TabBar/drag-drop.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,7 @@ define(function (require, exports, module) {
320320
// if the prev file is not pinned, we unpin this file too!
321321
if (!MainViewManager.isPathPinned(paneId, prevFilePath)) {
322322
const fileObj = FileSystem.getFileForPath(draggedPath);
323-
324-
// we consciously enable the unpin command here, because if the tab is pinned,
325-
// the unpin command will be disabled by default as the last context menu item
326-
// and the FILE_UNPIN command will not execute
327-
CommandManager.get(Commands.FILE_UNPIN).setEnabled(true);
328-
CommandManager.execute(Commands.FILE_UNPIN, { file: fileObj, paneId: paneId });
323+
CommandManager.execute(Commands.FILE_PIN, { file: fileObj, paneId: paneId, forceUnpin: true });
329324
}
330325
}
331326

@@ -341,10 +336,7 @@ define(function (require, exports, module) {
341336
// if the next file is pinned, we pin this file too!
342337
if (MainViewManager.isPathPinned(paneId, nextFilePath)) {
343338
const fileObj = FileSystem.getFileForPath(draggedPath);
344-
345-
// we consciously enable the pin command here, same reason as above
346-
CommandManager.get(Commands.FILE_PIN).setEnabled(true);
347-
CommandManager.execute(Commands.FILE_PIN, { file: fileObj, paneId: paneId });
339+
CommandManager.execute(Commands.FILE_PIN, { file: fileObj, paneId: paneId, forcePin: true });
348340
}
349341
}
350342
}

src/extensionsIntegrated/TabBar/main.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -519,16 +519,11 @@ define(function (require, exports, module) {
519519
CommandManager.execute(Commands.FILE_CLOSE, { file: fileObj, paneId: paneId }); // close the file
520520
}
521521

522-
// check if the clicked element is the pin icon, if yes then we need to unpin it
522+
// check if the clicked element is the pin icon, if yes then we toggle pin state
523523
if ($(event.target).hasClass("fa-thumbtack") || $(event.target).closest(".tab-pin").length) {
524524
event.preventDefault();
525525
event.stopPropagation();
526-
527-
// we consciously enable the unpin command here, because if the tab is pinned,
528-
// the unpin command will be disabled by default as the last context menu item
529-
// and the user will not be able to unpin the tab
530-
CommandManager.get(Commands.FILE_UNPIN).setEnabled(true);
531-
CommandManager.execute(Commands.FILE_UNPIN, { file: fileObj, paneId: paneId });
526+
CommandManager.execute(Commands.FILE_PIN, { file: fileObj, paneId: paneId });
532527
}
533528
});
534529

src/extensionsIntegrated/TabBar/more-options.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ define(function (require, exports, module) {
5050
* so we only show the relevant options
5151
*/
5252
function _updateMenuItems() {
53-
// PIN/UNPIN logic
53+
// PIN/UNPIN logic: update label based on current state
5454
const isPinned = MainViewManager.isPathPinned(
5555
_currentTabContext.paneId,
5656
_currentTabContext.filePath
5757
);
5858

59-
CommandManager.get(Commands.FILE_PIN).setEnabled(!isPinned);
60-
CommandManager.get(Commands.FILE_UNPIN).setEnabled(isPinned);
59+
const pinCommand = CommandManager.get(Commands.FILE_PIN);
60+
pinCommand.setName(isPinned ? Strings.CMD_FILE_UNPIN : Strings.CMD_FILE_PIN);
6161
}
6262

6363
// gets the working set (list of open files) for the given pane
@@ -168,8 +168,7 @@ define(function (require, exports, module) {
168168
menu.addMenuItem(TABBAR_CLOSE_SAVED_TABS);
169169
menu.addMenuItem(TABBAR_CLOSE_ALL);
170170
menu.addMenuDivider();
171-
menu.addMenuItem(Commands.FILE_PIN, null, null, null, { hideWhenCommandDisabled: true });
172-
menu.addMenuItem(Commands.FILE_UNPIN, null, null, null, { hideWhenCommandDisabled: true });
171+
menu.addMenuItem(Commands.FILE_PIN);
173172
menu.addMenuDivider();
174173
menu.addMenuItem(Commands.FILE_RENAME);
175174
menu.addMenuItem(Commands.FILE_DELETE);

src/project/WorkingSetView.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,7 @@ define(function (require, exports, module) {
804804
const prevFilePath = workingSet[newIndex - 1].fullPath;
805805

806806
if (!MainViewManager.isPathPinned(sourceView.paneId, prevFilePath)) {
807-
CommandManager.get(Commands.FILE_UNPIN).setEnabled(true);
808-
CommandManager.execute(Commands.FILE_UNPIN, { file: sourceFile, paneId: sourceView.paneId });
807+
CommandManager.execute(Commands.FILE_PIN, { file: sourceFile, paneId: sourceView.paneId, forceUnpin: true });
809808
}
810809
}
811810

@@ -819,8 +818,7 @@ define(function (require, exports, module) {
819818

820819
// if the next file is pinned, pin this file too
821820
if (MainViewManager.isPathPinned(sourceView.paneId, nextFilePath)) {
822-
CommandManager.get(Commands.FILE_PIN).setEnabled(true);
823-
CommandManager.execute(Commands.FILE_PIN, { file: sourceFile, paneId: sourceView.paneId });
821+
CommandManager.execute(Commands.FILE_PIN, { file: sourceFile, paneId: sourceView.paneId, forcePin: true });
824822
}
825823
}
826824
}

0 commit comments

Comments
 (0)