electron避免重复创建子窗口

收藏

代码如下:

import { app, shell, BrowserWindow , ipcMain , screen } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'

function createWindow(): void {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 700,
    height: 500,
    show: false,
    titleBarStyle:'hidden',
    autoHideMenuBar: true,
    ...(process.platform === 'linux' ? { icon } : {}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false
    }
  })
  
  
 
  const context = {
    // 是否退出应用
    allowQuitting: false,
    isShow:false,
    listWindow:null
  }
  // 隐藏主窗口
  const hideWindow = () => {
    if (context.listWindow && !context.listWindow.isDestroyed()) {
      context.isShow = false;
      context.listWindow.hide();
    }
  }

  // 展示主窗口
  const showWindow = () => {
    
    if (context.listWindow && !context.listWindow.isDestroyed()) {
      context.isShow = true;
      context.listWindow.show()
    }
  }
  //创建窗口
  const createList = () => {
    context.listWindow = new BrowserWindow({
      width: 400,
      height: 120,
      frame: false,
      show: false,
      autoHideMenuBar: true,
      ...(process.platform === 'linux' ? { icon } : {}),
      webPreferences: {
        preload: join(__dirname, '../preload/index.js'),
        sandbox: false
      }
    })
    
    context.listWindow.on('ready-to-show', () => {
      let bounds = context.listWindow.getBounds();
      bounds.y = screen.getPrimaryDisplay().workAreaSize.height-120;
      //300为当前窗口本身的高度
      context.listWindow.setBounds(bounds);
      context.listWindow.show()
    })  
    context.isShow = true;

    context.listWindow.on('close', (event) => {
      // 如果此时,应用并没有被退出,则终止默认行为,并且隐藏主窗口
      if (context.allowQuitting == false ) {
        event.preventDefault();
        hideWindow()
      } else {
        context.listWindow = null;
      }
    })

    if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
      context.listWindow.loadURL(process.env['ELECTRON_RENDERER_URL']+'/#/list');
    } else {
      context.listWindow.loadFile(join(__dirname, '../renderer/index.html'));
    }

  }

  ipcMain.handle('created-ci',(event,data)=>{

    if (context.listWindow === null) {
      createList()
    } else {
      if(  context.isShow  ){
        hideWindow()
      }else{
        showWindow()
      }
    }
  })

  if( context ){
    ipcMain.handle('custom-list',(event,res) => {
        let x = res.appX;
        let y = res.appY;
        context.listWindow.setPosition(res.appX,res.appY)
    })
  }

  if( context ){
    ipcMain.handle('close-list',(event,res) => {
      context.listWindow.close();
    })
  }

  //拖拽窗口
  ipcMain.handle('custom-adsorption',(event,res)=>{
    let x = res.appX;
    let y = res.appY;
    mainWindow.setPosition(res.appX,res.appY);
  })

  
  mainWindow.on('ready-to-show', () => {
    mainWindow.show()
  })

  mainWindow.webContents.setWindowOpenHandler((details) => {
    shell.openExternal(details.url)
    return { action: 'deny' }
  })

  // HMR for renderer base on electron-vite cli.
  // Load the remote URL for development or the local html file for production.
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  }
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  // Set app user model id for windows
  electronApp.setAppUserModelId('com.electron')

  // Default open or close DevTools by F12 in development
  // and ignore CommandOrControl + R in production.
  // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  })

  createWindow()

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// 当app退出之前,将allowQuitting设置为truw
// app.on('before-quit', () => (context.allowQuitting = true))

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})


评论(

您还未登录,请先去登录
表情
查看更多

相关作者

  • 获取点赞0
  • 文章阅读量133

相关文章

暂无相关文章

联系小鹿线

咨询老师

咨询老师

扫码下载APP