浏览代码

auto launch

gaoshuaixing 5 年之前
父节点
当前提交
157db438b3
共有 4 个文件被更改,包括 83 次插入0 次删除
  1. 28 0
      app/controller/v1/setting.js
  2. 10 0
      app/service/setting.js
  3. 35 0
      electron/lib/AutoLaunch.js
  4. 10 0
      electron/lib/Constant.js

+ 28 - 0
app/controller/v1/setting.js

@@ -0,0 +1,28 @@
+'use strict';
+
+const BaseController = require('../base');
+
+class SettingController extends BaseController {
+
+  async autoLaunchEnable() {
+    const { ctx } = this;
+
+    const data = {
+      title: 'hello electron-egg'
+    };
+
+    await ctx.render('index.ejs', data);
+  }
+  
+  async autoLaunchDisable() {
+    const { ctx } = this;
+
+    const data = {
+      title: 'hello'
+    };
+
+    await ctx.render('hello.ejs', data);
+  }
+}
+
+module.exports = SettingController;

+ 10 - 0
app/service/setting.js

@@ -0,0 +1,10 @@
+'use strict';
+
+const BaseService = require('./base');
+
+class SettingService extends BaseService {
+
+    
+}
+
+module.exports = SettingService;

+ 35 - 0
electron/lib/AutoLaunch.js

@@ -0,0 +1,35 @@
+const { app } = require('electron');
+const { LOGIN_SETTING_OPTIONS } = require('./Constant').AutoLaunch;
+
+class AutoLaunch {
+  enable () {
+    return new Promise((resolve, reject) => {
+      const enabled = app.getLoginItemSettings(LOGIN_SETTING_OPTIONS).openAtLogin
+      if (enabled) {
+        resolve()
+      }
+
+      app.setLoginItemSettings({
+        ...LOGIN_SETTING_OPTIONS,
+        openAtLogin: true
+      })
+      resolve()
+    })
+  }
+  
+  disable () {
+    return new Promise((resolve, reject) => {
+      app.setLoginItemSettings({ openAtLogin: false })
+      resolve()
+    })
+  }
+
+  isEnabled () {
+    return new Promise((resolve, reject) => {
+      const enabled = app.getLoginItemSettings(LOGIN_SETTING_OPTIONS).openAtLogin
+      resolve(enabled)
+    })
+  }
+}
+
+module.exports = AutoLaunch;

+ 10 - 0
electron/lib/Constant.js

@@ -0,0 +1,10 @@
+module.exports = {
+  AutoLaunch: {
+    LOGIN_SETTING_OPTIONS = {
+      // For Windows
+      args: [
+        '--opened-at-login=1'
+      ]
+    }
+  },
+};