Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src-node/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,40 @@ function openNativeTerminal({cwd, usePowerShell = false}) {
});
}

/**
* Opens a file in the default application for its type on Windows, macOS, and Linux.
*
* @param {string} fullPath - The path to the file/folder to open.
* @returns {Promise<void>} - Resolves if the file/folder is opened successfully, rejects otherwise.
*/
function openInDefaultApp(fullPath) {
return new Promise((resolve, reject) => {
const platform = os.platform();
let command;

if (platform === 'win32') {
// Windows: Use 'start' command
command = `start "" "${fullPath}"`;
} else if (platform === 'darwin') {
// macOS: Use 'open' command
command = `open "${fullPath}"`;
} else {
// Linux: Use 'xdg-open' command
command = `xdg-open "${fullPath}"`;
}

// Execute the command
exec(command, (error) => {
if (error) {
reject(new Error(`Failed to open file: ${error.message}`));
} else {
resolve();
}
});
});
}



async function ESLintFile({text, fullFilePath, projectFullPath}) {
return lintFile(text, fullFilePath, projectFullPath);
Expand All @@ -243,5 +277,6 @@ exports.openUrlInBrowser = openUrlInBrowser;
exports.getEnvironmentVariable = getEnvironmentVariable;
exports.ESLintFile = ESLintFile;
exports.openNativeTerminal = openNativeTerminal;
exports.openInDefaultApp = openInDefaultApp;
exports._loadNodeExtensionModule = _loadNodeExtensionModule;
exports._npmInstallInFolder = _npmInstallInFolder;
3 changes: 3 additions & 0 deletions src/command/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ define(function (require, exports, module) {
/** Shows current file in open powershell in Windows os */
exports.NAVIGATE_OPEN_IN_POWERSHELL = "navigate.openInPowerShell";

/** Open current file in the default associated app in the os */
exports.NAVIGATE_OPEN_IN_DEFAULT_APP = "navigate.openInDefaultApp";

/** Opens quick open dialog */
exports.NAVIGATE_QUICK_OPEN = "navigate.quickOpen"; // QuickOpen.js doFileSearch()

Expand Down
13 changes: 11 additions & 2 deletions src/command/DefaultMenus.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ define(function (require, exports, module) {
*/
function _setContextMenuItemsVisible(enabled, items) {
items.forEach(function (item) {
CommandManager.get(item).setEnabled(enabled);
const command = CommandManager.get(item);
if(command) {
// some commands may be only selectively present in browser or in some oses.
// Eg. NAVIGATE_OPEN_IN_POWERSHELL is only present in Windows desktop apps
command.setEnabled(enabled);
}
});
}

Expand All @@ -58,7 +63,9 @@ define(function (require, exports, module) {
return err;
}
_setContextMenuItemsVisible(isPresent, [Commands.FILE_RENAME,
Commands.NAVIGATE_SHOW_IN_FILE_TREE, Commands.NAVIGATE_SHOW_IN_OS, Commands.NAVIGATE_OPEN_IN_TERMINAL]);
Commands.NAVIGATE_SHOW_IN_FILE_TREE, Commands.NAVIGATE_SHOW_IN_OS,
Commands.NAVIGATE_OPEN_IN_TERMINAL, Commands.NAVIGATE_OPEN_IN_POWERSHELL,
Commands.NAVIGATE_OPEN_IN_DEFAULT_APP]);
});
}
}
Expand Down Expand Up @@ -303,6 +310,7 @@ define(function (require, exports, module) {
if (brackets.platform === "win") {
subMenu.addMenuItem(Commands.NAVIGATE_OPEN_IN_POWERSHELL);
}
subMenu.addMenuItem(Commands.NAVIGATE_OPEN_IN_DEFAULT_APP);
}
workingset_cmenu.addMenuDivider();
workingset_cmenu.addMenuItem(Commands.FILE_COPY);
Expand Down Expand Up @@ -342,6 +350,7 @@ define(function (require, exports, module) {
if (brackets.platform === "win") {
subMenu.addMenuItem(Commands.NAVIGATE_OPEN_IN_POWERSHELL);
}
subMenu.addMenuItem(Commands.NAVIGATE_OPEN_IN_DEFAULT_APP);
}
project_cmenu.addMenuDivider();
project_cmenu.addMenuItem(Commands.FILE_CUT);
Expand Down
10 changes: 9 additions & 1 deletion src/document/DocumentCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1974,6 +1974,13 @@ define(function (require, exports, module) {
}
}

function openDefaultApp() {
const entry = ProjectManager.getSelectedItem();
if (entry && entry.fullPath) {
NodeUtils.openInDefaultApp(entry.fullPath, true);
}
}

function raceAgainstTime(promise, timeout = 2000) {
const timeoutPromise = new Promise((_resolve, reject) => {
setTimeout(() => {
Expand Down Expand Up @@ -2268,7 +2275,7 @@ define(function (require, exports, module) {
// Set some command strings
let quitString = Strings.CMD_QUIT,
showInOS = Strings.CMD_SHOW_IN_FILE_MANAGER,
defaultTerminal = Strings.CMD_OPEN_IN_TERMINAL;
defaultTerminal = Strings.CMD_OPEN_IN_TERMINAL_DO_NOT_TRANSLATE;
if (brackets.platform === "win") {
quitString = Strings.CMD_EXIT;
showInOS = Strings.CMD_SHOW_IN_EXPLORER;
Expand Down Expand Up @@ -2325,6 +2332,7 @@ define(function (require, exports, module) {
if (brackets.platform === "win") {
CommandManager.register(Strings.CMD_OPEN_IN_POWER_SHELL, Commands.NAVIGATE_OPEN_IN_POWERSHELL, openPowerShell);
}
CommandManager.register(Strings.CMD_OPEN_IN_DEFAULT_APP, Commands.NAVIGATE_OPEN_IN_DEFAULT_APP, openDefaultApp);
CommandManager.register(Strings.CMD_NEW_BRACKETS_WINDOW, Commands.FILE_NEW_WINDOW, handleFileNewWindow);
CommandManager.register(quitString, Commands.FILE_QUIT, handleFileCloseWindow);
CommandManager.register(Strings.CMD_SHOW_IN_TREE, Commands.NAVIGATE_SHOW_IN_FILE_TREE, handleShowInTree);
Expand Down
5 changes: 3 additions & 2 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,10 @@ define({
"CMD_SHOW_IN_EXPLORER": "Windows File Explorer",
"CMD_SHOW_IN_FINDER": "macOS Finder",
"CMD_SHOW_IN_FILE_MANAGER": "File Manager",
"CMD_OPEN_IN_TERMINAL": "Terminal",
"CMD_OPEN_IN_TERMINAL_DO_NOT_TRANSLATE": "Terminal",
"CMD_OPEN_IN_CMD": "Command Prompt",
"CMD_OPEN_IN_POWER_SHELL": "Power Shell",
"CMD_OPEN_IN_POWER_SHELL": "PowerShell",
"CMD_OPEN_IN_DEFAULT_APP": "System Default App",
"CMD_SWITCH_PANE_FOCUS": "Switch Pane Focus",

// Debug menu commands
Expand Down
14 changes: 14 additions & 0 deletions src/utils/NodeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ define(function (require, exports, module) {
});
}

/**
* Opens a file in the default application for its type on Windows, macOS, and Linux.
*
* @param {string} fullPath - The path to the file/folder to open.
* @returns {Promise<void>} - Resolves if the file/folder is opened successfully, rejects otherwise.
*/
async function openInDefaultApp(fullPath) {
if(!Phoenix.isNativeApp) {
throw new Error("openInDefaultApp not available in browser");
}
return utilsConnector.execPeer("openInDefaultApp", window.fs.getTauriPlatformPath(fullPath));
}

if(NodeConnector.isNodeAvailable()) {
// todo we need to update the strings if a user extension adds its translations. Since we dont support
// node extensions for now, should consider when we support node extensions.
Expand Down Expand Up @@ -213,6 +226,7 @@ define(function (require, exports, module) {
exports.ESLintFile = ESLintFile;
exports.getEnvironmentVariable = getEnvironmentVariable;
exports.openNativeTerminal = openNativeTerminal;
exports.openInDefaultApp = openInDefaultApp;

/**
* checks if Node connector is ready
Expand Down
Loading