Skip to content

Commit 601ac41

Browse files
committed
Use an absolute filename for the file save dialog on Linux
This fixes #6912. On Gnome the save dialog works with any filename, on KDE Plasma the save dialog opens with an empty filename input field, unless the application uses an absolute file path instead of only the filename. This commit will use the ~/Downloads directory as the default path, which seems like a better choice then dumping files into the user's home dir.
1 parent 94dba11 commit 601ac41

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

app/main.ts

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { join, normalize, extname, dirname, basename } from 'path';
55
import { pathToFileURL } from 'url';
66
import * as os from 'os';
77
import { chmod, realpath, writeFile } from 'fs-extra';
8+
import { exec } from 'child_process';
9+
import { promisify } from 'util';
810
import { randomBytes } from 'crypto';
911
import { createParser } from 'dashdash';
1012

@@ -3021,6 +3023,32 @@ ipc.handle('show-save-dialog', async (_event, { defaultPath }) => {
30213023
return { canceled: true };
30223024
}
30233025

3026+
if (
3027+
process.platform === 'linux' &&
3028+
defaultPath &&
3029+
defaultPath.charAt(0) !== '/'
3030+
) {
3031+
// On Linux the defaultPath should be an absolute path, otherwise the save dialog will have an empty filename on KDE/Plasma
3032+
let downloadsPath = '';
3033+
try {
3034+
downloadsPath = (
3035+
await promisify(exec)('xdg-user-dir DOWNLOAD')
3036+
).stdout.trim();
3037+
getLogger().info(
3038+
'show-save-dialog: saving to user downloads directory: ' + downloadsPath
3039+
);
3040+
} catch (e) {
3041+
// If we cannot get Downloads path, fall back to the user's home directory
3042+
downloadsPath = process.env['HOME'] || '';
3043+
getLogger().info(
3044+
'show-save-dialog: saving to user home directory: ' + downloadsPath
3045+
);
3046+
}
3047+
if (downloadsPath) {
3048+
defaultPath = downloadsPath + '/' + defaultPath;
3049+
}
3050+
}
3051+
30243052
const { canceled, filePath: selectedFilePath } = await dialog.showSaveDialog(
30253053
mainWindow,
30263054
{

0 commit comments

Comments
 (0)