Skip to content

Commit

Permalink
Use an absolute filename for the file save dialog on Linux
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pelya committed Dec 5, 2024
1 parent 94dba11 commit 601ac41
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { join, normalize, extname, dirname, basename } from 'path';
import { pathToFileURL } from 'url';
import * as os from 'os';
import { chmod, realpath, writeFile } from 'fs-extra';
import { exec } from 'child_process';
import { promisify } from 'util';
import { randomBytes } from 'crypto';
import { createParser } from 'dashdash';

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

if (
process.platform === 'linux' &&
defaultPath &&
defaultPath.charAt(0) !== '/'
) {
// On Linux the defaultPath should be an absolute path, otherwise the save dialog will have an empty filename on KDE/Plasma
let downloadsPath = '';
try {
downloadsPath = (
await promisify(exec)('xdg-user-dir DOWNLOAD')
).stdout.trim();
getLogger().info(
'show-save-dialog: saving to user downloads directory: ' + downloadsPath
);
} catch (e) {
// If we cannot get Downloads path, fall back to the user's home directory
downloadsPath = process.env['HOME'] || '';
getLogger().info(
'show-save-dialog: saving to user home directory: ' + downloadsPath
);
}
if (downloadsPath) {
defaultPath = downloadsPath + '/' + defaultPath;
}
}

const { canceled, filePath: selectedFilePath } = await dialog.showSaveDialog(
mainWindow,
{
Expand Down

0 comments on commit 601ac41

Please sign in to comment.