os.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. 'use strict';
  2. const _ = require('lodash');
  3. const path = require('path');
  4. const { Controller } = require('ee-core');
  5. const {
  6. app: electronApp,
  7. dialog, shell, BrowserView, Notification,
  8. powerMonitor, screen, nativeTheme
  9. } = require('electron');
  10. let browserViewObj = null;
  11. let notificationObj = null;
  12. /**
  13. * 操作系统 - 功能demo
  14. * @class
  15. */
  16. class OsController extends Controller {
  17. constructor(ctx) {
  18. super(ctx);
  19. }
  20. /**
  21. * 所有方法接收两个参数
  22. * @param args 前端传的参数
  23. * @param event - ipc通信时才有值。详情见:控制器文档
  24. */
  25. /**
  26. * 消息提示对话框
  27. */
  28. messageShow () {
  29. dialog.showMessageBoxSync({
  30. type: 'info', // "none", "info", "error", "question" 或者 "warning"
  31. title: '自定义标题-message',
  32. message: '自定义消息内容',
  33. detail: '其它的额外信息'
  34. })
  35. return '打开了消息框';
  36. }
  37. /**
  38. * 消息提示与确认对话框
  39. */
  40. messageShowConfirm () {
  41. const res = dialog.showMessageBoxSync({
  42. type: 'info',
  43. title: '自定义标题-message',
  44. message: '自定义消息内容',
  45. detail: '其它的额外信息',
  46. cancelId: 1, // 用于取消对话框的按钮的索引
  47. defaultId: 0, // 设置默认选中的按钮
  48. buttons: ['确认', '取消'], // 按钮及索引
  49. })
  50. let data = (res === 0) ? '点击确认按钮' : '点击取消按钮';
  51. return data;
  52. }
  53. /**
  54. * 选择目录
  55. */
  56. selectFolder () {
  57. const filePaths = dialog.showOpenDialogSync({
  58. properties: ['openDirectory', 'createDirectory']
  59. });
  60. if (_.isEmpty(filePaths)) {
  61. return null
  62. }
  63. return filePaths[0];
  64. }
  65. /**
  66. * 打开目录
  67. */
  68. openDirectory (args) {
  69. if (!args.id) {
  70. return false;
  71. }
  72. let dir = '';
  73. if (path.isAbsolute(args.id)) {
  74. dir = args.id;
  75. } else {
  76. dir = electronApp.getPath(args.id);
  77. }
  78. shell.openPath(dir);
  79. return true;
  80. }
  81. /**
  82. * 加载视图内容
  83. */
  84. loadViewContent (args) {
  85. let content = null;
  86. if (args.type == 'html') {
  87. content = path.join('file://', electronApp.getAppPath(), args.content)
  88. } else {
  89. content = args.content;
  90. }
  91. // electron实验性功能,慎用
  92. browserViewObj = new BrowserView();
  93. this.app.electron.mainWindow.setBrowserView(browserViewObj)
  94. browserViewObj.setBounds({
  95. x: 300,
  96. y: 170,
  97. width: 650,
  98. height: 400
  99. });
  100. browserViewObj.webContents.loadURL(content);
  101. return true
  102. }
  103. /**
  104. * 移除视图内容
  105. */
  106. removeViewContent () {
  107. // removeBrowserView移除视图后,进程依然存在,估计是electron bug
  108. this.app.electron.mainWindow.removeBrowserView(browserViewObj);
  109. return true
  110. }
  111. /**
  112. * 打开新窗口
  113. */
  114. createWindow (args) {
  115. let content = null;
  116. if (args.type == 'html') {
  117. content = path.join('file://', electronApp.getAppPath(), args.content)
  118. } else if (args.type == 'web') {
  119. content = args.content;
  120. } else if (args.type == 'vue') {
  121. let addr = 'http://localhost:8080'
  122. if (this.config.env == 'prod') {
  123. const mainServer = this.app.config.mainServer;
  124. addr = mainServer.protocol + mainServer.host + ':' + mainServer.port;
  125. }
  126. content = addr + args.content;
  127. } else {
  128. // some
  129. }
  130. const addonWindow = this.app.addon.window;
  131. let opt = {
  132. title: args.windowName || 'new window'
  133. }
  134. const name = args.windowName || 'window-1';
  135. const win = addonWindow.create(name, opt);
  136. const winContentsId = win.webContents.id;
  137. // load page
  138. win.loadURL(content);
  139. return winContentsId
  140. }
  141. /**
  142. * 获取窗口contents id
  143. */
  144. getWCid (args) {
  145. const addonWindow = this.app.addon.window;
  146. // 主窗口的name默认是main,其它窗口name开发者自己定义
  147. const name = args;
  148. const id = addonWindow.getWCid(name);
  149. return id;
  150. }
  151. /**
  152. * 加载扩展程序
  153. */
  154. // async loadExtension (args) {
  155. // const crxFile = args[0];
  156. // if (_.isEmpty(crxFile)) {
  157. // return false;
  158. // }
  159. // const extensionId = path.basename(crxFile, '.crx');
  160. // const chromeExtensionDir = chromeExtension.getDirectory();
  161. // const extensionDir = path.join(chromeExtensionDir, extensionId);
  162. // Log.info("[api] [example] [loadExtension] extension id:", extensionId);
  163. // unzip(crxFile, extensionDir).then(() => {
  164. // Log.info("[api] [example] [loadExtension] unzip success!");
  165. // chromeExtension.load(extensionId);
  166. // });
  167. // return true;
  168. // }
  169. /**
  170. * 创建系统通知
  171. */
  172. sendNotification (arg, event) {
  173. const channel = 'controller.example.sendNotification';
  174. if (!Notification.isSupported()) {
  175. return '当前系统不支持通知';
  176. }
  177. let options = {};
  178. if (!_.isEmpty(arg.title)) {
  179. options.title = arg.title;
  180. }
  181. if (!_.isEmpty(arg.subtitle)) {
  182. options.subtitle = arg.subtitle;
  183. }
  184. if (!_.isEmpty(arg.body)) {
  185. options.body = arg.body;
  186. }
  187. if (!_.isEmpty(arg.silent)) {
  188. options.silent = arg.silent;
  189. }
  190. notificationObj = new Notification(options);
  191. if (arg.clickEvent) {
  192. notificationObj.on('click', (e) => {
  193. let data = {
  194. type: 'click',
  195. msg: '您点击了通知消息'
  196. }
  197. event.reply(`${channel}`, data)
  198. });
  199. }
  200. if (arg.closeEvent) {
  201. notificationObj.on('close', (e) => {
  202. let data = {
  203. type: 'close',
  204. msg: '您关闭了通知消息'
  205. }
  206. event.reply(`${channel}`, data)
  207. });
  208. }
  209. notificationObj.show();
  210. return true
  211. }
  212. /**
  213. * 电源监控
  214. */
  215. initPowerMonitor (arg, event) {
  216. const channel = 'controller.example.initPowerMonitor';
  217. powerMonitor.on('on-ac', (e) => {
  218. let data = {
  219. type: 'on-ac',
  220. msg: '接入了电源'
  221. }
  222. event.reply(`${channel}`, data)
  223. });
  224. powerMonitor.on('on-battery', (e) => {
  225. let data = {
  226. type: 'on-battery',
  227. msg: '使用电池中'
  228. }
  229. event.reply(`${channel}`, data)
  230. });
  231. powerMonitor.on('lock-screen', (e) => {
  232. let data = {
  233. type: 'lock-screen',
  234. msg: '锁屏了'
  235. }
  236. event.reply(`${channel}`, data)
  237. });
  238. powerMonitor.on('unlock-screen', (e) => {
  239. let data = {
  240. type: 'unlock-screen',
  241. msg: '解锁了'
  242. }
  243. event.reply(`${channel}`, data)
  244. });
  245. return true
  246. }
  247. /**
  248. * 获取屏幕信息
  249. */
  250. getScreen (arg) {
  251. let data = [];
  252. let res = {};
  253. if (arg == 0) {
  254. let res = screen.getCursorScreenPoint();
  255. data = [
  256. {
  257. title: '横坐标',
  258. desc: res.x
  259. },
  260. {
  261. title: '纵坐标',
  262. desc: res.y
  263. },
  264. ]
  265. return data;
  266. }
  267. if (arg == 1) {
  268. res = screen.getPrimaryDisplay();
  269. }
  270. if (arg == 2) {
  271. let resArr = screen.getAllDisplays();
  272. // 数组,只取一个吧
  273. res = resArr[0];
  274. }
  275. // Log.info('[electron] [ipc] [example] [getScreen] res:', res);
  276. data = [
  277. {
  278. title: '分辨率',
  279. desc: res.bounds.width + ' x ' + res.bounds.height
  280. },
  281. {
  282. title: '单色显示器',
  283. desc: res.monochrome ? '是' : '否'
  284. },
  285. {
  286. title: '色深',
  287. desc: res. colorDepth
  288. },
  289. {
  290. title: '色域',
  291. desc: res.colorSpace
  292. },
  293. {
  294. title: 'scaleFactor',
  295. desc: res.scaleFactor
  296. },
  297. {
  298. title: '加速器',
  299. desc: res.accelerometerSupport
  300. },
  301. {
  302. title: '触控',
  303. desc: res.touchSupport == 'unknown' ? '不支持' : '支持'
  304. },
  305. ]
  306. return data;
  307. }
  308. /**
  309. * 获取系统主题
  310. */
  311. getTheme () {
  312. let theme = 'system';
  313. if (nativeTheme.shouldUseHighContrastColors) {
  314. theme = 'light';
  315. } else if (nativeTheme.shouldUseInvertedColorScheme) {
  316. theme = 'dark';
  317. }
  318. return theme;
  319. }
  320. /**
  321. * 设置系统主题
  322. */
  323. setTheme (args) {
  324. // TODO 好像没有什么明显效果
  325. nativeTheme.themeSource = args;
  326. return args;
  327. }
  328. }
  329. OsController.toString = () => '[class OsController]';
  330. module.exports = OsController;