os.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. const { Service } = require('ee-core');
  3. const { BrowserView, Notification } = require('electron');
  4. const Electron = require('ee-core/electron');
  5. /**
  6. * os(service层为单例)
  7. * @class
  8. */
  9. class OsService extends Service {
  10. constructor(ctx) {
  11. super(ctx);
  12. this.myBrowserView = null;
  13. this.myNotification = null;
  14. }
  15. /**
  16. * createBrowserView
  17. */
  18. createBrowserView(contentUrl) {
  19. // electron 实验性功能,慎用
  20. this.myBrowserView = new BrowserView();
  21. Electron.mainWindow.setBrowserView(this.myBrowserView);
  22. this.myBrowserView.setBounds({
  23. x: 300,
  24. y: 170,
  25. width: 650,
  26. height: 400
  27. });
  28. this.myBrowserView.webContents.loadURL(contentUrl);
  29. }
  30. /**
  31. * removeBrowserView
  32. */
  33. removeBrowserView() {
  34. // removeBrowserView移除视图后,进程依然存在,估计是electron bug
  35. Electron.mainWindow.removeBrowserView(this.myBrowserView);
  36. }
  37. /**
  38. * createNotification
  39. */
  40. createNotification(options, event) {
  41. const channel = 'controller.os.sendNotification';
  42. this.myNotification = new Notification(options);
  43. if (options.clickEvent) {
  44. this.myNotification.on('click', (e) => {
  45. let data = {
  46. type: 'click',
  47. msg: '您点击了通知消息'
  48. }
  49. event.reply(`${channel}`, data)
  50. });
  51. }
  52. if (options.closeEvent) {
  53. this.myNotification.on('close', (e) => {
  54. let data = {
  55. type: 'close',
  56. msg: '您关闭了通知消息'
  57. }
  58. event.reply(`${channel}`, data)
  59. });
  60. }
  61. this.myNotification.show();
  62. }
  63. }
  64. OsService.toString = () => '[class OsService]';
  65. module.exports = OsService;