Skip to content

Electron transparent window use maximize() can not emit 'maximize' event #12854

Closed

Description

@wunairuoshi
  • Electron Version: 2.0.0
  • Operating System (Platform and Version): windows 8.1
  • Last known working Electron version: none

Expected Behavior
I create a browerwindow with frame: false and transparent: true, when i use maximize(), i need the event 'maximize' and isMaximized() return true.

Actual behavior
no event emited, and isMaximize() return false.

Activity

  1. welcome commented on May 8, 2018

    @welcome

    👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

    To help make it easier for us to investigate your issue, please follow the contributing guidelines.

  2. ckerr commented on May 8, 2018

    @ckerr
    Member

    @wunairuoshi, thanks for reporting this issue!

    Would it be possible for you to create a small test app -- e.g., forking https://github.com/electron/electron-quick-start and adding a few lines -- for the Electron team to test with?

  3. sofianguy commented on May 21, 2018

    @sofianguy
    Contributor

    Thank you for your issue!

    We haven't gotten a response to our questions in our comment above. With only the information that is currently in the issue, we don't have enough information to take action. I'm going to close this but don't hesitate to reach out if you have or find the answers we need, we'll be happy to reopen the issue.

  4. ZaDarkSide commented on Jul 15, 2019

    @ZaDarkSide

    This is still happening for Electron 5.0.6 on Windows 10 (1903) very old bug that wasn't been fixed.
    To reproduce it you need to do exactly what the original poster said, set BrowserWindow with frame: false and transparent: true and do win.on('maximize', function() {});
    No event 'maximize is triggered and the function will not be called when you maximize the window.

  5. a1292999652 commented on Aug 28, 2019

    @a1292999652

    I also encountered this problem in Electron 6.02, transparent: true in Windows10 will lead to isMaximized() being false all the time

  6. added a commit that references this issue on Jan 14, 2020
  7. chenjietao commented on Mar 21, 2020

    @chenjietao

    I wrote a plug-in to fix this problem. See electron-frameless-window-plugin. In fact, I rewrote several methods of BrowserWindow instance, including getNormalBounds(), maximize(), unmaximize() and isMaximzed(). And I've added some features to make frameless windows work better. Such as disable the menu when right clicking the drag region.

    Code:

      if (win.webContents.browserWindowOptions.transparent) {
        // rewrite getNormalBounds, maximize, unmaximize and isMaximized API for the transparent window
        let resizable = win.isResizable()
        let normalBounds = win.getNormalBounds ? win.getNormalBounds() : win.getBounds()
    
        win.getNormalBounds = function () {
          if (!this.isMaximized()) {
            if (BrowserWindow.prototype.getNormalBounds) {
              normalBounds = BrowserWindow.prototype.getNormalBounds.call(this)
            } else {
              normalBounds = BrowserWindow.prototype.getBounds.call(this)
            }
          }
          return normalBounds
        }.bind(win)
    
        win.maximize = function () {
          normalBounds = this.getNormalBounds() // store the bounds of normal window
          resizable = this.isResizable() // store resizable value
          BrowserWindow.prototype.maximize.call(this)
          if (!BrowserWindow.prototype.isMaximized.call(this)) {
            // while isMaximized() was returning false, it will not emit 'maximize' event
            this.emit('maximize', { sender: this, preventDefault: () => {} })
          }
          this.setResizable(false) // disable resize when the window is maximized
        }.bind(win)
    
        win.unmaximize = function () {
          const fromMaximized = BrowserWindow.prototype.isMaximized.call(this)
          BrowserWindow.prototype.unmaximize.call(this)
          if (!fromMaximized) {
            // isMaximized() returned false before unmaximize was called, it will not emit 'unmaximize' event
            this.emit('unmaximize', { sender: this, preventDefault: () => {} })
          }
          this.setResizable(resizable) // restore resizable
        }.bind(win)
    
        win.isMaximized = function () {
          const nativeIsMaximized = BrowserWindow.prototype.isMaximized.call(this)
          if (!nativeIsMaximized) {
            // determine whether the window is full of the screen work area
            const bounds = this.getBounds()
            const workArea = electron.screen.getDisplayMatching(bounds).workArea
            if (bounds.x <= workArea.x && bounds.y <= workArea.y && bounds.width >= workArea.width && bounds.height >= workArea.height) {
              return true
            }
          }
          return nativeIsMaximized
        }.bind(win)
      }
  8. DuannHang commented on May 3, 2020

    @DuannHang

    楼上老哥,真佩服你!

  9. codebytere commented on Jun 19, 2020

    @codebytere
    Member

    Duplicate of #23275

  10. amrbashir commented on Jun 22, 2020

    @amrbashir

    @chenjietao thanks, man. you're a life saver.
    if you don't mind me asking, do you have any solution to get the window to dock/snap to the edges of screen. transparent:true also mess up the snapping functionality.

  11. Nihilop commented on Jan 9, 2021

    @Nihilop

    I wrote a plug-in to fix this problem. See electron-frameless-window-plugin. In fact, I rewrote several methods of BrowserWindow instance, including getNormalBounds(), maximize(), unmaximize() and isMaximzed(). And I've added some features to make frameless windows work better. Such as disable the menu when right clicking the drag region.

    Genius !

  12. IzKuipers commented on Apr 28, 2021

    @IzKuipers

    I wrote a plug-in to fix this problem. See electron-frameless-window-plugin. In fact, I rewrote several methods of BrowserWindow instance, including getNormalBounds(), maximize(), unmaximize() and isMaximzed(). And I've added some features to make frameless windows work better. Such as disable the menu when right clicking the drag region.

    Code:

      if (win.webContents.browserWindowOptions.transparent) {
        // rewrite getNormalBounds, maximize, unmaximize and isMaximized API for the transparent window
        let resizable = win.isResizable()
        let normalBounds = win.getNormalBounds ? win.getNormalBounds() : win.getBounds()
    
        win.getNormalBounds = function () {
          if (!this.isMaximized()) {
            if (BrowserWindow.prototype.getNormalBounds) {
              normalBounds = BrowserWindow.prototype.getNormalBounds.call(this)
            } else {
              normalBounds = BrowserWindow.prototype.getBounds.call(this)
            }
          }
          return normalBounds
        }.bind(win)
    
        win.maximize = function () {
          normalBounds = this.getNormalBounds() // store the bounds of normal window
          resizable = this.isResizable() // store resizable value
          BrowserWindow.prototype.maximize.call(this)
          if (!BrowserWindow.prototype.isMaximized.call(this)) {
            // while isMaximized() was returning false, it will not emit 'maximize' event
            this.emit('maximize', { sender: this, preventDefault: () => {} })
          }
          this.setResizable(false) // disable resize when the window is maximized
        }.bind(win)
    
        win.unmaximize = function () {
          const fromMaximized = BrowserWindow.prototype.isMaximized.call(this)
          BrowserWindow.prototype.unmaximize.call(this)
          if (!fromMaximized) {
            // isMaximized() returned false before unmaximize was called, it will not emit 'unmaximize' event
            this.emit('unmaximize', { sender: this, preventDefault: () => {} })
          }
          this.setResizable(resizable) // restore resizable
        }.bind(win)
    
        win.isMaximized = function () {
          const nativeIsMaximized = BrowserWindow.prototype.isMaximized.call(this)
          if (!nativeIsMaximized) {
            // determine whether the window is full of the screen work area
            const bounds = this.getBounds()
            const workArea = electron.screen.getDisplayMatching(bounds).workArea
            if (bounds.x <= workArea.x && bounds.y <= workArea.y && bounds.width >= workArea.width && bounds.height >= workArea.height) {
              return true
            }
          }
          return nativeIsMaximized
        }.bind(win)
      }

    Wonderful job, this is the key to near perfect transparent frameless windows, mainly for use with say for example a window that needs border-radius. I love this plugin and will be using it very often. Thanks, @chenjietao

  13. paddotk commented on Jun 26, 2022

    @paddotk

    @chenjietao Thanks, it doesn't seem to work (anymore) though. For one, browserWindowOptions has been removed from win.webContents, and when I remove this check (and adding const electron = require('electron'); at the top of my main.js file) it makes the window act weirdly when maximizing/unmaximizing; but the maximize, unmaximize and minimize events are not fired.

  14. distant-xingsu commented on Jul 8, 2023

    @distant-xingsu
    // if (win.webContents.browserWindowOptions&&win.webContents.browserWindowOptions.transparent) {}
    // edit to
    // if(true)
    
    win.unmaximize = function () {
        const fromMaximized = BrowserWindow.prototype.isMaximized.call(this)
        BrowserWindow.prototype.unmaximize.call(this)
        if (!fromMaximized) {
          // isMaximized() returned false before unmaximize was called, it will not emit 'unmaximize' event
          this.emit('unmaximize', { sender: this, preventDefault: () => {} })
    
          // add+
          win.setBounds(normalBounds)
    
        }
        this.setResizable(resizable) // restore resizable
      }.bind(win)
  15. winchestercanal commented on Jan 29, 2025

    @winchestercanal

    Escrevi um plug-in para corrigir esse problema. Ver plug-in de janela sem estrutura eletrônica. . Na verdade, reescrevi vários métodos de instância do BrowserWindow, inclusive getNormalBounds(), maximize(), unmaximize() e isMaximzed(). E adicionei alguns recursos para fazer com que as janelas sem moldura funcionem melhor. Como desabilitar o menu ao clicar com o botão direito na região de arrastar.

    Código:

    se (ganhar.conteúdos Web.browserWindowOpções.transparente) {
    // reescrever getNormalBounds, maximizar, desmaximizar e isMaximized API para a janela transparente
    deixar redimensionável = ganhar.é redimensionável()
    deixar limites normais = ganhar.obterLimites normais ? ganhar.obterLimites normais() : ganhar.obter limites()

    ganhar.obterLimites normais = função () {
      se (!isto.isMaximizado()) {
        se (Janela do navegador.protótipo.obterLimites normais) {
          limites normais = Janela do navegador.protótipo.obterLimites normais.ligar(isto)
        } outra outra {
          limites normais = Janela do navegador.protótipo.obter limites.ligar(isto)
        }
      }
      retorno limites normais
    }.ligar(ganhar)
    
    ganhar.maximizar = função () {
      limites normais = isto.obterLimites normais() // armazene os limites da janela normal
      redimensionável = isto.é redimensionável() // armazenar valor redimensionável
      Janela do navegador.protótipo.maximizar.ligar(isto)
      se (!Janela do navegador.protótipo.isMaximizado.ligar(isto)) {
        //enquanto isMaximized () estava retornando falso, ele não emitirá evento 'maximizar'
        isto.emitir('maximizar', { remetente: isto, prevenir padrão: () => {} })
      }
      isto.definirResizável(falso) //desativar redimensionar quando a janela estiver maximizada
    }.ligar(ganhar)
    
    ganhar.não maximizar = função () {
      const doMaximizado = Janela do navegador.protótipo.isMaximizado.ligar(isto)
      Janela do navegador.protótipo.não maximizar.ligar(isto)
      se (!doMaximizado) {
        // isMaximized () retornou falso antes de unmaximize foi chamado, ele não vai emitir 'unmaximize' evento
        isto.emitir('desmaximizar', { remetente: isto, prevenir padrão: () => {} })
      }
      isto.definirResizável(redimensionável) // restaurar redimensionável
    }.ligar(ganhar)
    
    ganhar.isMaximizado = função () {
      const nativoIsMaximizado = Janela do navegador.protótipo.isMaximizado.ligar(isto)
      se (!nativoIsMaximizado) {
        //determinar se a janela está cheia da área de trabalho do ecrã
        const limites = isto.obter limites()
        const área de trabalho = elétron.tela.getDisplayMatching(limites).área de trabalho
        se (limites.x <= área de trabalho.x && limites.y <= área de trabalho.y && limites.largura >= área de trabalho.largura && limites.altura >= área de trabalho.altura) {
          retorno verdadeiro
        }
      }
      retorno nativoIsMaximizado
    }.ligar(ganhar)
    

    }

    como faço para usar sua soluçao?

    I wrote a plug-in to fix this problem. See electron-frameless-window-plugin. In fact, I rewrote several methods of BrowserWindow instance, including getNormalBounds(), maximize(), unmaximize() and isMaximzed(). And I've added some features to make frameless windows work better. Such as disable the menu when right clicking the drag region.

    Code:

    if (win.webContents.browserWindowOptions.transparent) {
    // rewrite getNormalBounds, maximize, unmaximize and isMaximized API for the transparent window
    let resizable = win.isResizable()
    let normalBounds = win.getNormalBounds ? win.getNormalBounds() : win.getBounds()

    win.getNormalBounds = function () {
      if (!this.isMaximized()) {
        if (BrowserWindow.prototype.getNormalBounds) {
          normalBounds = BrowserWindow.prototype.getNormalBounds.call(this)
        } else {
          normalBounds = BrowserWindow.prototype.getBounds.call(this)
        }
      }
      return normalBounds
    }.bind(win)
    
    win.maximize = function () {
      normalBounds = this.getNormalBounds() // store the bounds of normal window
      resizable = this.isResizable() // store resizable value
      BrowserWindow.prototype.maximize.call(this)
      if (!BrowserWindow.prototype.isMaximized.call(this)) {
        // while isMaximized() was returning false, it will not emit 'maximize' event
        this.emit('maximize', { sender: this, preventDefault: () => {} })
      }
      this.setResizable(false) // disable resize when the window is maximized
    }.bind(win)
    
    win.unmaximize = function () {
      const fromMaximized = BrowserWindow.prototype.isMaximized.call(this)
      BrowserWindow.prototype.unmaximize.call(this)
      if (!fromMaximized) {
        // isMaximized() returned false before unmaximize was called, it will not emit 'unmaximize' event
        this.emit('unmaximize', { sender: this, preventDefault: () => {} })
      }
      this.setResizable(resizable) // restore resizable
    }.bind(win)
    
    win.isMaximized = function () {
      const nativeIsMaximized = BrowserWindow.prototype.isMaximized.call(this)
      if (!nativeIsMaximized) {
        // determine whether the window is full of the screen work area
        const bounds = this.getBounds()
        const workArea = electron.screen.getDisplayMatching(bounds).workArea
        if (bounds.x <= workArea.x && bounds.y <= workArea.y && bounds.width >= workArea.width && bounds.height >= workArea.height) {
          return true
        }
      }
      return nativeIsMaximized
    }.bind(win)
    

    }

    How do I use your solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions