capacitor-community / electron

Deploy your Capacitor apps to Linux, Mac, and Windows desktops, with the Electron platform! 🖥️
https://capacitor-community.github.io/electron/
MIT License
318 stars 58 forks source link

Detecting electron #199

Open albertreed opened 1 year ago

albertreed commented 1 year ago

Apologies if this is in the documentation, I've looked through them and couldn't find the answer. What is the appropriate way of detecting that capacitor is running through electron? The best way I've found is that Capacitor.getPlatform() !== "web" AND Device.getInfo() (from the @capacitor/device plugin) returns an object with platform set to "web" which is pretty weird!

jdgjsag67251 commented 1 year ago

You could try window.CapacitorCustomPlatform?.name === 'electron'. Or you could add the following to the preload.ts file:

import { addPlatform, setPlatform } from "@capacitor/core";

addPlatform('electron', {
    name: 'electron',
    isNativePlatform: () => true,
    getPlatform: () => 'electron',
});

setPlatform('electron');

On the most recent version Capacitor.getPlatform() === 'electron' should just work, but I have experienced Capacitor being a bit flaky with this.