example.js 6.2 KB

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