美文网首页
electron开机自启

electron开机自启

作者: 吟风者 | 来源:发表于2023-04-17 14:27 被阅读0次
  1. windows/mac下开机自启,electron官方已经实现。文档地址

  2. linux下自启。

    下载插件

    npm install easy-auto-launch -S
    

    创建auto-launch.ts文件,写入内容

    import AutoLaunch from "easy-auto-launch";
    import {app} from "electron";
    
    /**
     * 获取开机启动状态
     */
    export const getAutoLaunchState = async () => {
        if (process.platform === "linux") {
            const autoLauncher = new AutoLaunch({
                name: app.getName(),
                isHidden: false,
                path: process.env.APPIMAGE
            });
            return await autoLauncher.isEnabled();
        }
    
        return app.getLoginItemSettings().openAtLogin;
    }
    /**
     * 更新开机启动
     * @param isAutoLaunchEnabled
     */
    export const updateAutoLaunch = async (isAutoLaunchEnabled: boolean = true) => {
        const electronIsDev = !app.isPackaged;
    
        // Don't run this in development
        if (electronIsDev) {
            return;
        }
    
        // `setLoginItemSettings` doesn't support linux
        if (process.platform === "linux") {
            const autoLauncher = new AutoLaunch({
                name: app.getName(),
                isHidden: false,
                path: process.env.APPIMAGE
            });
    
            if (isAutoLaunchEnabled) {
                await autoLauncher.enable();
            } else {
                await autoLauncher.disable();
            }
    
            return;
        }
    
        app.setLoginItemSettings({
            openAtLogin: isAutoLaunchEnabled,
            openAsHidden: true,
        });
    }
    

    在主进程main中调用就好了。

     app.whenReady().then(async () => {
         // enable auto launch
         await updateAutoLaunch();
         await createWindow()
     });
    

相关文章

网友评论

      本文标题:electron开机自启

      本文链接:https://www.haomeiwen.com/subject/vfrcjdtx.html