Skip to content

Commit

Permalink
Fix opening windows in foreground on windows everywhere in app
Browse files Browse the repository at this point in the history
* Ensure electron/remote is used
  • Loading branch information
ClementPasteau committed Jan 2, 2025
1 parent 49e176a commit e412cbf
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import {
ExportFlow,
} from '../GenericExporters/CordovaExport';
import { downloadUrlsToLocalFiles } from '../../Utils/LocalFileDownloader';
const electron = optionalRequire('electron');
const shell = electron ? electron.shell : null;
// It's important to use remote and not electron for folder actions,
// otherwise they will be opened in the background.
// See https://github.com/electron/electron/issues/4349#issuecomment-777475765
const remote = optionalRequire('@electron/remote');
const shell = remote ? remote.shell : null;

const gd: libGDevelop = global.gd;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import {
ExportFlow,
} from '../GenericExporters/ElectronExport';
import { downloadUrlsToLocalFiles } from '../../Utils/LocalFileDownloader';
const electron = optionalRequire('electron');
const shell = electron ? electron.shell : null;
// It's important to use remote and not electron for folder actions,
// otherwise they will be opened in the background.
// See https://github.com/electron/electron/issues/4349#issuecomment-777475765
const remote = optionalRequire('@electron/remote');
const shell = remote ? remote.shell : null;

const gd: libGDevelop = global.gd;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import {
import { downloadUrlsToLocalFiles } from '../../Utils/LocalFileDownloader';

const path = optionalRequire('path');
const electron = optionalRequire('electron');
// It's important to use remote and not electron for folder actions,
// otherwise they will be opened in the background.
// See https://github.com/electron/electron/issues/4349#issuecomment-777475765
const remote = optionalRequire('@electron/remote');
const app = remote ? remote.app : null;
const shell = electron ? electron.shell : null;
const shell = remote ? remote.shell : null;

const gd: libGDevelop = global.gd;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import {
import { downloadUrlsToLocalFiles } from '../../Utils/LocalFileDownloader';
import DismissableTutorialMessage from '../../Hints/DismissableTutorialMessage';

const electron = optionalRequire('electron');
const shell = electron ? electron.shell : null;
// It's important to use remote and not electron for folder actions,
// otherwise they will be opened in the background.
// See https://github.com/electron/electron/issues/4349#issuecomment-777475765
const remote = optionalRequire('@electron/remote');
const shell = remote ? remote.shell : null;

const gd: libGDevelop = global.gd;

Expand Down
14 changes: 8 additions & 6 deletions newIDE/app/src/GameDashboard/GameDashboardCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ import { textEllipsisStyle } from '../UI/TextEllipsis';
import FileWithLines from '../UI/CustomSvgIcons/FileWithLines';
import TextButton from '../UI/TextButton';
import { getRelativeOrAbsoluteDisplayDate } from '../Utils/DateDisplay';
const electron = optionalRequire('electron');
// It's important to use remote and not electron for folder actions,
// otherwise they will be opened in the background.
// See https://github.com/electron/electron/issues/4349#issuecomment-777475765
const remote = optionalRequire('@electron/remote');
const shell = remote ? remote.shell : null;
const path = optionalRequire('path');

export const getThumbnailWidth = ({ isMobile }: {| isMobile: boolean |}) =>
Expand All @@ -74,7 +78,7 @@ export const getDetailedProjectDisplayDate = (i18n: I18nType, date: number) =>
});

const getNoProjectAlertMessage = () => {
if (!electron) {
if (!remote) {
// Trying to open a local project from the web app of the mobile app.
return t`Looks like your project isn't there!${'\n\n'}Your project must be stored on your computer.`;
} else {
Expand Down Expand Up @@ -107,10 +111,8 @@ const styles = {
};

const locateProjectFile = (file: FileMetadataAndStorageProviderName) => {
if (!electron) return;
electron.shell.showItemInFolder(
path.resolve(file.fileMetadata.fileIdentifier)
);
if (!shell) return;
shell.showItemInFolder(path.resolve(file.fileMetadata.fileIdentifier));
};

const getFileNameWithoutExtensionFromPath = (path: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import ContextMenu, {
import type { ClientCoordinates } from '../../../../Utils/UseLongTouch';
import PreferencesContext from '../../../Preferences/PreferencesContext';
import useAlertDialog from '../../../../UI/Alert/useAlertDialog';
const electron = optionalRequire('electron');
// It's important to use remote and not electron for folder actions,
// otherwise they will be opened in the background.
// See https://github.com/electron/electron/issues/4349#issuecomment-777475765
const remote = optionalRequire('@electron/remote');
const shell = remote ? remote.shell : null;
const path = optionalRequire('path');

const styles = {
Expand Down Expand Up @@ -72,10 +76,8 @@ type Props = {|
|};

const locateProjectFile = (file: FileMetadataAndStorageProviderName) => {
if (!electron) return;
electron.shell.showItemInFolder(
path.resolve(file.fileMetadata.fileIdentifier)
);
if (!shell) return;
shell.showItemInFolder(path.resolve(file.fileMetadata.fileIdentifier));
};

const ProjectFileList = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import Window from '../../Utils/Window';
import ResourcesLoader from '../../ResourcesLoader';

const path = optionalRequire('path');
const electron = optionalRequire('electron');
// It's important to use remote and not electron for folder actions,
// otherwise they will be opened in the background.
// See https://github.com/electron/electron/issues/4349#issuecomment-777475765
const remote = optionalRequire('@electron/remote');
const shell = remote ? remote.shell : null;
const app = remote ? remote.app : null;

export const generateGetResourceActions = ({
Expand Down Expand Up @@ -40,7 +43,7 @@ export const generateGetResourceActions = ({
resource.getName(),
{}
);
if (app && path && electron) {
if (app && path && shell) {
const defaultPath = path.join(
app.getPath('downloads'),
resource.getName()
Expand Down Expand Up @@ -68,8 +71,7 @@ export const generateGetResourceActions = ({
informUser({
actionLabel: <Trans>Open folder</Trans>,
message: <Trans>The resource has been downloaded</Trans>,
onActionClick: () =>
electron.shell.showItemInFolder(path.resolve(targetPath)),
onActionClick: () => shell.showItemInFolder(path.resolve(targetPath)),
});
} else {
Window.openExternalURL(resourceUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const path = optionalRequire('path');
// otherwise they will be opened in the background.
// See https://github.com/electron/electron/issues/4349#issuecomment-777475765
const remote = optionalRequire('@electron/remote');
const shell = remote ? remote.shell : null;

export const locateResourceFile = ({
project,
Expand All @@ -32,7 +33,7 @@ export const locateResourceFile = ({
resource.getName()
);

remote.shell.showItemInFolder(path.resolve(resourceFilePath));
if (shell) shell.showItemInFolder(path.resolve(resourceFilePath));
};

export const openResourceFile = ({
Expand All @@ -46,7 +47,7 @@ export const openResourceFile = ({
project,
resource.getName()
);
remote.shell.openPath(path.resolve(resourceFilePath));
if (shell) shell.openPath(path.resolve(resourceFilePath));
};

export const copyResourceFilePath = ({
Expand Down
5 changes: 3 additions & 2 deletions newIDE/app/src/ResourcesEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const gd: libGDevelop = global.gd;
// otherwise they will be opened in the background.
// See https://github.com/electron/electron/issues/4349#issuecomment-777475765
const remote = optionalRequire('@electron/remote');
const shell = remote ? remote.shell : null;
const path = optionalRequire('path');
const styles = {
container: {
Expand Down Expand Up @@ -208,8 +209,8 @@ export default class ResourcesEditor extends React.Component<Props, State> {
};

openProjectFolder = () => {
if (remote)
remote.shell.openPath(path.dirname(this.props.project.getProjectFile()));
if (shell)
shell.openPath(path.dirname(this.props.project.getProjectFile()));
};

toggleProperties = () => {
Expand Down

0 comments on commit e412cbf

Please sign in to comment.