example.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. 'use strict';
  2. /**
  3. * 前端(html)调用electron功能时,建议使用该模块
  4. *
  5. * 定义的function 接收三个参数
  6. * @param event ipcMain事件对象
  7. * @param channel 频道
  8. * @param arg 接收到的消息
  9. */
  10. const {app, dialog, BrowserWindow, BrowserView, Notification, powerMonitor, screen, nativeTheme} = require('electron');
  11. const path = require('path');
  12. const _ = require('lodash');
  13. const is = require('electron-is');
  14. const config = require('../config');
  15. let myTimer = null;
  16. let browserViewObj = null;
  17. let notificationObj = null;
  18. exports.hello = function (event, channel, arg) {
  19. let newMsg = arg + " +1"
  20. let reply = ''
  21. reply = '收到:' + arg + ',返回:' + newMsg
  22. return reply
  23. }
  24. exports.messageShow = function (event, channel, arg) {
  25. dialog.showMessageBoxSync({
  26. type: 'info', // "none", "info", "error", "question" 或者 "warning"
  27. title: '自定义标题-message',
  28. message: '自定义消息内容',
  29. detail: '其它的额外信息'
  30. })
  31. return '打开了消息框';
  32. }
  33. exports.messageShowConfirm = function (event, channel, arg) {
  34. const res = dialog.showMessageBoxSync({
  35. type: 'info',
  36. title: '自定义标题-message',
  37. message: '自定义消息内容',
  38. detail: '其它的额外信息',
  39. cancelId: 1, // 用于取消对话框的按钮的索引
  40. defaultId: 0, // 设置默认选中的按钮
  41. buttons: ['确认', '取消'], // 按钮及索引
  42. })
  43. let data = (res === 0) ? '点击确认按钮' : '点击取消按钮';
  44. console.log('[electron] [ipc] [example] [messageShowConfirm] 结果:', res);
  45. return data;
  46. }
  47. /**
  48. * 长消息 - 开始
  49. */
  50. exports.socketMessageStart = function (event, channel, arg) {
  51. // 每隔1秒,向前端页面发送消息
  52. // 用定时器模拟
  53. myTimer = setInterval(function(e, c, msg) {
  54. let timeNow = Date.now();
  55. let data = msg + ':' + timeNow;
  56. e.reply(`${c}`, data)
  57. }, 1000, event, channel, arg)
  58. return '开始了'
  59. }
  60. /**
  61. * 长消息 - 停止
  62. */
  63. exports.socketMessageStop = function () {
  64. clearInterval(myTimer);
  65. return '停止了'
  66. }
  67. /**
  68. * 加载视图内容
  69. */
  70. exports.loadViewContent = function (event, channel, arg) {
  71. let content = null;
  72. if (arg.type == 'html') {
  73. content = path.join('file://', app.getAppPath(), arg.content)
  74. } else {
  75. content = arg.content;
  76. }
  77. browserViewObj = new BrowserView();
  78. MAIN_WINDOW.setBrowserView(browserViewObj)
  79. browserViewObj.setBounds({
  80. x: 300,
  81. y: 170,
  82. width: 650,
  83. height: 400
  84. });
  85. browserViewObj.webContents.loadURL(content);
  86. return true
  87. }
  88. /**
  89. * 移除视图内容
  90. */
  91. exports.removeViewContent = function () {
  92. MAIN_WINDOW.removeBrowserView(browserViewObj);
  93. return true
  94. }
  95. /**
  96. * 打开新窗口
  97. */
  98. exports.createWindow = function (event, channel, arg) {
  99. let content = null;
  100. if (arg.type == 'html') {
  101. content = path.join('file://', app.getAppPath(), arg.content)
  102. } else {
  103. content = arg.content;
  104. }
  105. let winObj = new BrowserWindow({
  106. x: 10,
  107. y: 10,
  108. width: 980,
  109. height: 650
  110. })
  111. winObj.loadURL(content);
  112. return winObj.id
  113. }
  114. /**
  115. * 创建系统通知
  116. */
  117. exports.sendNotification = function (event, channel, arg) {
  118. if (!Notification.isSupported()) {
  119. return '当前系统不支持通知';
  120. }
  121. let options = {};
  122. if (!_.isEmpty(arg.title)) {
  123. options.title = arg.title;
  124. }
  125. if (!_.isEmpty(arg.subtitle)) {
  126. options.subtitle = arg.subtitle;
  127. }
  128. if (!_.isEmpty(arg.body)) {
  129. options.body = arg.body;
  130. }
  131. if (!_.isEmpty(arg.silent)) {
  132. options.silent = arg.silent;
  133. }
  134. notificationObj = new Notification(options);
  135. if (arg.clickEvent) {
  136. notificationObj.on('click', (e) => {
  137. let data = {
  138. type: 'click',
  139. msg: '您点击了通知消息'
  140. }
  141. event.reply(`${channel}`, data)
  142. });
  143. }
  144. if (arg.closeEvent) {
  145. notificationObj.on('close', (e) => {
  146. let data = {
  147. type: 'close',
  148. msg: '您关闭了通知消息'
  149. }
  150. event.reply(`${channel}`, data)
  151. });
  152. }
  153. notificationObj.show();
  154. return true
  155. }
  156. /**
  157. * 电源监控
  158. */
  159. exports.initPowerMonitor = function (event, channel, arg) {
  160. powerMonitor.on('on-ac', (e) => {
  161. let data = {
  162. type: 'on-ac',
  163. msg: '接入了电源'
  164. }
  165. event.reply(`${channel}`, data)
  166. });
  167. powerMonitor.on('on-battery', (e) => {
  168. let data = {
  169. type: 'on-battery',
  170. msg: '使用电池中'
  171. }
  172. event.reply(`${channel}`, data)
  173. });
  174. powerMonitor.on('lock-screen', (e) => {
  175. let data = {
  176. type: 'lock-screen',
  177. msg: '锁屏了'
  178. }
  179. event.reply(`${channel}`, data)
  180. });
  181. powerMonitor.on('unlock-screen', (e) => {
  182. let data = {
  183. type: 'unlock-screen',
  184. msg: '解锁了'
  185. }
  186. event.reply(`${channel}`, data)
  187. });
  188. return true
  189. }
  190. /**
  191. * 获取屏幕信息
  192. */
  193. exports.getScreen = function (event, channel, arg) {
  194. let data = [];
  195. let res = {};
  196. if (arg == 0) {
  197. let res = screen.getCursorScreenPoint();
  198. data = [
  199. {
  200. title: '横坐标',
  201. desc: res.x
  202. },
  203. {
  204. title: '纵坐标',
  205. desc: res.y
  206. },
  207. ]
  208. return data;
  209. }
  210. if (arg == 1) {
  211. res = screen.getPrimaryDisplay();
  212. }
  213. if (arg == 2) {
  214. let resArr = screen.getAllDisplays();
  215. // 数组,只取一个吧
  216. res = resArr[0];
  217. }
  218. // console.log('[electron] [ipc] [example] [getScreen] res:', res);
  219. data = [
  220. {
  221. title: '分辨率',
  222. desc: res.bounds.width + ' x ' + res.bounds.height
  223. },
  224. {
  225. title: '单色显示器',
  226. desc: res.monochrome ? '是' : '否'
  227. },
  228. {
  229. title: '色深',
  230. desc: res. colorDepth
  231. },
  232. {
  233. title: '色域',
  234. desc: res.colorSpace
  235. },
  236. {
  237. title: 'scaleFactor',
  238. desc: res.scaleFactor
  239. },
  240. {
  241. title: '加速器',
  242. desc: res.accelerometerSupport
  243. },
  244. {
  245. title: '触控',
  246. desc: res.touchSupport == 'unknown' ? '不支持' : '支持'
  247. },
  248. ]
  249. return data;
  250. }
  251. /**
  252. * 获取系统主题
  253. */
  254. exports.getTheme = function (event, channel, arg) {
  255. let theme = 'system';
  256. if (nativeTheme.shouldUseHighContrastColors) {
  257. theme = 'light';
  258. } else if (nativeTheme.shouldUseInvertedColorScheme) {
  259. theme = 'dark';
  260. }
  261. return theme;
  262. }
  263. /**
  264. * 设置系统主题
  265. */
  266. exports.setTheme = function (event, channel, arg) {
  267. // TODO 好像没有什么明显效果
  268. nativeTheme.themeSource = arg;
  269. return arg;
  270. }
  271. /**
  272. * 检查是否有新版本
  273. */
  274. exports.checkForUpdater = function (event, channel, arg) {
  275. const updateConfig = config.get('autoUpdate');
  276. if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
  277. || (is.linux() && updateConfig.linux)) {
  278. const autoUpdater = require('../lib/autoUpdater');
  279. autoUpdater.checkUpdate();
  280. }
  281. return;
  282. }
  283. /**
  284. * 下载新版本
  285. */
  286. exports.downloadApp = function (event, channel, arg) {
  287. const updateConfig = config.get('autoUpdate');
  288. if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
  289. || (is.linux() && updateConfig.linux)) {
  290. const autoUpdater = require('../lib/autoUpdater');
  291. autoUpdater.download();
  292. }
  293. return;
  294. }