electron.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { BrowserWindow, BrowserWindowConstructorOptions, app, session, screen, desktopCapturer, globalShortcut } from "electron"
  2. import { Window } from "./win"
  3. import { join } from "path"
  4. import { platform, env } from "process"
  5. export default class ScreenShotsWindow implements Window {
  6. win: BrowserWindow
  7. option: BrowserWindowConstructorOptions
  8. constructor(opts?: BrowserWindowConstructorOptions) {
  9. this.option = {
  10. ...opts,
  11. frame: false,
  12. transparent: true,
  13. show: false,
  14. resizable: false,
  15. webPreferences: {
  16. webSecurity: false,
  17. contextIsolation: true,
  18. nodeIntegration: true,
  19. preload: join(app.getAppPath(), 'preload.js'),
  20. }
  21. }
  22. if (platform !== 'linux') {
  23. this.option.titleBarStyle = 'hidden'
  24. this.option.skipTaskbar = true
  25. }
  26. this.setup()
  27. }
  28. async setup() {
  29. this.win = new BrowserWindow(this.option)
  30. this.win?.loadFile(join(app.getAppPath(), 'screen-shots/index.html'))
  31. // window
  32. if (platform === 'win32') {
  33. // 禁用右键菜单
  34. this.win.hookWindowMessage(278, () => {
  35. this.win.setEnabled(false); //窗口禁用
  36. setTimeout(() => {
  37. this.win.setEnabled(true);
  38. }, 100); //延时太快会立刻启动,太慢会妨碍窗口其他操作,可自行测试最佳时间
  39. return true;
  40. })
  41. }
  42. // 注册截图快捷键
  43. globalShortcut.register('ESC', () => {
  44. if (!this.win?.isDestroyed() && this.win?.isFocused()) {
  45. this.win.hide()
  46. this.win.reload()
  47. }
  48. })
  49. }
  50. async forward(evt: string, data?: Any) {
  51. if (evt === 'screen') {
  52. // 截图
  53. if (this.win && !this.win.isDestroyed()) {
  54. const allPlay = screen.getAllDisplays()
  55. const { size } = screen.getPrimaryDisplay()
  56. const source = await this.getScreenData(size.width, size.height, allPlay)
  57. this.win.webContents.send('source-screen', source)
  58. this.win.show()
  59. }
  60. } else if (evt === 'screenSave' || evt === 'screenClose') {
  61. this.win.hide()
  62. this.win.reload()
  63. }
  64. }
  65. debug() {
  66. if (this.win?.isFocused()) {
  67. this.win?.webContents.openDevTools()
  68. }
  69. }
  70. async getScreenData(width: number, height: number, allPlay: Electron.Display[]) {
  71. const sors = await desktopCapturer.getSources({
  72. types: ['screen'],
  73. thumbnailSize: {
  74. width, height
  75. }
  76. })
  77. const allSource = []
  78. if (sors.length > 1) {
  79. const { x } = screen.getCursorScreenPoint()
  80. const sc = []
  81. for (let k = 0; k < allPlay.length; k++) {
  82. const el = allPlay[k];
  83. const X = el.bounds.x + el.bounds.width
  84. const obj = {
  85. num: X - x,
  86. _index: k,
  87. ...el
  88. }
  89. if (X - x > 0) sc.push(obj)
  90. }
  91. const array = sc.sort((a, b) => a.num - b.num)
  92. if (!array.length) return []
  93. const { id } = array[0]
  94. const { thumbnail } = sors.find(el => +el.display_id === id)
  95. allSource.push({
  96. width: thumbnail.getSize().width,
  97. height: thumbnail.getSize().height,
  98. base64: thumbnail.toDataURL()
  99. })
  100. this.win.setBounds(array[0].bounds)
  101. } else if (sors.length === 1) {
  102. // 一块屏
  103. const { thumbnail } = sors[0]
  104. allSource.push({
  105. width: thumbnail.getSize().width,
  106. height: thumbnail.getSize().height,
  107. base64: thumbnail.toDataURL()
  108. })
  109. this.win.setBounds(screen.getPrimaryDisplay().bounds)
  110. }
  111. this.win.setFullScreen(true)
  112. this.win.setAlwaysOnTop(true)
  113. return allSource
  114. }
  115. }