前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >electron开机自启

electron开机自启

作者头像
吟风者
发布2023-04-27 15:05:17
1.2K0
发布2023-04-27 15:05:17
举报
文章被收录于专栏:吟风者

windows/mac下开机自启,electron官方已经实现。文档地址

linux下自启。

下载插件

代码语言:javascript
复制
npm install easy-auto-launch -S

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

代码语言:javascript
复制
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中调用就好了。

代码语言:javascript
复制
 app.whenReady().then(async () => {
     // enable auto launch
     await updateAutoLaunch();
     await createWindow()
 });
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-04-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档