Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close Electron frameless window not working

I'm building an app using electron 1.0 and unfortunately everywhere I look for guides and tutorials with it, no one uses electron 1 because it's so new.

I am trying to close a frameless window through the click of a button I made. I know the button works because I have check to make sure it can do simple things (i.e. change some text or whatever) but that's only when I use internal javascript but I am trying to use external javascript. When I use external the function never gets called...

    const {remote} = require('electron');
    const {BrowserWindow} = require('electron').remote;

    document.getElementById("close-button").addEventListener("click", function (e) {
         var window = remote.getCurrentWindow();
         window.close();
    });

That is my javascript file. I also know I am linking the file properly because I can use document.write() and it works.

What am I doing wrong here?

any help is greatly appreciated thanks!

Edit: Added remote line.

Although there is another reason why my event handler isn't working for my button. This question is closed and the answer has been accepted.

like image 368
Clayton Perroni Avatar asked May 29 '16 03:05

Clayton Perroni


People also ask

How do you close the electron window?

addEventListener("click", function (e) { var window = remote. getCurrentWindow(); window. close(); });

How do you make the electron app draggable?

Applications need to specify the -webkit-app-region: drag CSS property to tell Electron which regions are draggable. To make the whole window draggable, this CSS property is set to the style of the body tag in HTML. Applications can also use -webkit-app-region: no-drag CSS property to indicate the non-draggable area.

How do I get rid of the top bar on my electron app?

The only workaround is to use Menu. setApplicationMenu(null) , however, this will disable all the menu shortcuts like F11 for toggling fullscreen etc. In new versions of Electron, you can set autoHideMenuBar: true while creating browserWindow, pressing Alt will show the menu bar again. Save this answer.

How do you create an electron window?

Windows can be created from the renderer in two ways: clicking on links or submitting forms adorned with target=_blank. JavaScript calling window. open()


1 Answers

I don't know if you omitted the relevant import from the snippet you posted, but assuming you haven't remote will be undefined when you call remote.getCurrentWindow(). If you add const { remote } = require('electron'); to the top of your snippet I think your click handler will work as expected.

like image 56
Vadim Macagon Avatar answered Oct 23 '22 03:10

Vadim Macagon