• electron窗口相关操作(放大缩小退出,可拖动,可resize等)


     

    如下是对窗口最大化,最小化等相关操作:

    复制代码
    import { ipcMain, ipcRenderer, remote } from 'electron'
    import is from 'electron-is'
    
    // ipc 通信发送的窗口状态改变事件的channel名称
    export const windowStateChangeChannel = 'window-state-changed'
    
    // window的当前状态
    export const WINDOW_STATE = {
      FULLSCREEN: 'full-screen',
      MAXIMIZED: 'maximized',
      MINIMIZED: 'minimized',
      HIDDEN: 'hidden',
      NORMAL: 'normal',
    }
    
    // window可执行的操作,通过发送消息触发
    const windowAction = {
      maximize: 'window-maximize',
      unmaximize: 'window-unmaximize',
      minimize: 'window-minimize',
      close: 'window-close',
    }
    
    /**
    * 获取window的状态
    * @param {window对象} window
    */
    export function getWindowState(window) {
      if (window.isFullScreen()) {
        return WINDOW_STATE.FULLSCREEN
      } if (window.isMaximized()) {
        return WINDOW_STATE.MAXIMIZED
      } if (window.isMinimized()) {
        return WINDOW_STATE.MINIMIZED
      } if (!window.isVisible()) {
        return WINDOW_STATE.HIDDEN
      }
    
      return WINDOW_STATE.NORMAL
    }
    
    /**
    * 发送一个 window-state-changed 消息到 renderer 进程
    * @param {*} window
    * @param {*} state
    */
    function sendWindowStateEvent(window, state) {
      window.webContents.send(windowStateChangeChannel, state)
    }
    
    /**
     * 注册 window 状态变化后事件,它会发送一个消息到 renderer 进程
     * @param {window对象} window
     */
    export function registerWindowStateChangedEvents(window) {
      window.on('enter-full-screen', () => sendWindowStateEvent(window, 'full-screen'))
      window.on('leave-full-screen', () => sendWindowStateEvent(window, 'normal'))
      window.on('maximize', () => sendWindowStateEvent(window, 'maximized'))
      window.on('minimize', () => sendWindowStateEvent(window, 'minimized'))
      window.on('unmaximize', () => sendWindowStateEvent(window, 'normal'))
      window.on('restore', () => sendWindowStateEvent(window, 'normal'))
      window.on('hide', () => sendWindowStateEvent(window, 'hidden'))
      window.on('show', () => sendWindowStateEvent(window, 'normal'))
    }
    
    /**
     * 注册 window 状态变化动作,使用 ipc.send 对应的消息触发*
     * @param {window对象} window
     */
    export function registerWindowStateChangeActions(window) {
      // 窗口最小化
      ipcMain.on(windowAction.minimize, () => {
        window.minimize()
      })
    
      // 窗口最大化
      ipcMain.on(windowAction.maximize, () => {
        window.maximize()
      })
    
      // 窗口取消最大化
      ipcMain.on(windowAction.unmaximize, () => {
        window.unmaximize()
      })
    
      // 窗口关闭
      ipcMain.on(windowAction.close, () => {
        window.close()
      })
    }
    
    /**
     *  生成带有promise的操作窗口的函数,可以进一步处理事件结束后的逻辑
     * @param {窗口可执行的操作} windowAction
     */
    function generatePromisedWindowStateFunc(action) {
      return () => {
        return new Promise((resolve) => {
          ipcRenderer.send(action)
          ipcRenderer.once(windowStateChangeChannel, (event, args) => {
            resolve(args)
          })
        })
      }
    }
    
    /**
     * 生成不带有promise的操作窗口函数,只负责触发事件
     * @param {窗口可执行的操作} windowAction
     */
    function generateWindowStateFunc(action) {
      return () => {
        ipcRenderer.send(action)
      }
    }
    
    /**
     * 最大化窗口的方法,因为windows和macOS之间的差异,单独写成一个函数
     */
    function handleMaximizeWindow() {
      if (is.windows()) {
        remote.getCurrentWindow().maximize()
        return Promise.resolve(WINDOW_STATE.MAXIMIZED)
      }
    
      return new Promise((resolve) => {
        ipcRenderer.send(windowAction.maximize)
        ipcRenderer.once(windowStateChangeChannel, (event, args) => {
          resolve(args)
        })
      })
    }
    
    /**
     * 窗口操作方法,包括最大化,最小化,关闭
     * 每个方法返回一个promise,方便处理后续逻辑
     */
    export const windowStateActionResponse = {
      maximize: handleMaximizeWindow,
      unmaximize: generatePromisedWindowStateFunc(windowAction.unmaximize),
      minimize: generatePromisedWindowStateFunc(windowAction.minimize),
      close: generatePromisedWindowStateFunc(windowAction.close),
    }
    
    /**
     * 窗口操作方法,包括最大化,最小化,关闭
     * 只发送消息,不处理成功之后的回调
     */
    export const WindowStateAction = {
      maximize: generateWindowStateFunc(windowAction.maximize),
      unmaximize: generateWindowStateFunc(windowAction.unmaximize),
      minimize: generateWindowStateFunc(windowAction.minimize),
      close: generateWindowStateFunc(windowAction.close),
    }
    
    /**
     * 给窗口改变事件增加监听
     * @param {事件触发后的回调} handle
     */
    export function listenToWindowStateChange(handle) {
      ipcRenderer.on(windowStateChangeChannel, handle)
      return () => {
        ipcRenderer.removeListener(windowStateChangeChannel, handle)
      }
    }
    复制代码

    下面是electron配置:

    复制代码
    import { BrowserWindow, screen } from 'electron'
    
    function createWebRTCInternalWindow() {
      const displayWorkAreaSize = screen.getAllDisplays()[0].workArea
    
      const options = {
        x: displayWorkAreaSize.x,
        y: displayWorkAreaSize.y,
         displayWorkAreaSize.width,
        height: displayWorkAreaSize.height,
        resizable: true,
        movable: true,
        fullscreenable: false,
        enableLargerThanScreen: false,
        frame: true,
        transparent: false,
        alwaysOnTop: false,
        hasShadow: false,
        minWidth: 1000,
        minHeight: 648,
        webPreferences: {
          devTools: true,
          webSecurity: false,
          plugins: true,
          experimentalFeatures: true,
          experimentalCanvasFeatures: true,
          minimumFontSize: 10,
        },
      }
    
      const mainWindow = new BrowserWindow(options)
    
      return mainWindow
    }
    
    export default createWebRTCInternalWindow
    复制代码
    复制代码
    import { BrowserWindow, screen } from 'electron'
    
    function createBrowserWindow() {
      const displayWorkAreaSize = screen.getAllDisplays()[0].workArea
    
      const options = {
         parseInt(displayWorkAreaSize.width * 0.85, 10),
        height: parseInt(displayWorkAreaSize.height * 0.85, 10),
        center: true,
        resizable: true,
        movable: true,
        fullscreenable: false,
        enableLargerThanScreen: false,
        frame: false,
        transparent: true,
        alwaysOnTop: false,
        clickThrough: 'pointer-events',
        acceptFirstMouse: true,
        hasShadow: false,
        minWidth: 1000,
        minHeight: 648,
        webPreferences: {
          devTools: true,
          webSecurity: false,
          plugins: true,
          experimentalFeatures: true,
          experimentalCanvasFeatures: true,
          minimumFontSize: 10,
        },
      }
    
      const mainWindow = new BrowserWindow(options)
    
      return mainWindow
    }
    
    export default createBrowserWindow
    复制代码

    如上配置中可设置一系列初始值,其中包括 是否可resize,是否可拖动等。如果想在其它地方修改初始值,可按照如下实现:

    const currentWindow = require('electron').remote.getCurrentWindow()
    
    currentWindow.setResizable(false)
    currentWindow.setMovable(false)
    // 禁止Windows下双击最大化和调用maximize行为不一致导致的窗口尺寸变化
    currentWindow.setMaximizable(false)
  • 相关阅读:
    JS4
    JS3
    JS2
    JS1
    Dos命令
    面向对象的复习
    9.14Css
    9.13列表的用法
    9.12Css
    9.11Css
  • 原文地址:https://www.cnblogs.com/onesea/p/15330772.html
Copyright © 2020-2023  润新知