electron-seamless-titlebar-tutorial icon indicating copy to clipboard operation
electron-seamless-titlebar-tutorial copied to clipboard

Not working on electron version 14.0.0

Open CAESARSNOOT opened this issue 4 years ago • 10 comments

The minimize, maximize, and close functions don't working on version 14.0.0.

CAESARSNOOT avatar Sep 06 '21 11:09 CAESARSNOOT

Same hear, does not work.

plasmak avatar Sep 10 '21 10:09 plasmak

It's not working on version 14 cause remote was remove. Check this post : https://github.com/electron/electron/issues/21408

KasaiJo avatar Oct 22 '21 18:10 KasaiJo

Happy to accept a PR to get it working on more recent versions of Electron, but don't have much time to work on this myself

binaryfunt avatar Oct 23 '21 15:10 binaryfunt

I found a solution but I don't know if it's a proper way... I share in case someone wanna try it.

Install @remote package : https://www.npmjs.com/package/@electron/remote Add the following line in main.js : require('@electron/remote/main').initialize();

and this one after "mainWindow" implementation : require("@electron/remote/main").enable(mainWindow.webContents)

Change require in renderer.js by : const remote = require('@electron/remote');

KasaiJo avatar Oct 28 '21 16:10 KasaiJo

It's not working on version 14 cause remote was remove. Check this post : https://github.com/electron/electron/issues/21408

Yes, I was aware of this and it truly has been a fucking pain to me, mainly because I'm stupid and don't understand ipcRenderer that well and I'm to lazy to learn how it works.

CAESARSNOOT avatar Nov 27 '21 05:11 CAESARSNOOT

Ok so I have made fork on my other GitHub where I have fixed this problem https://github.com/NovaAppsInc/electron-seamless-titlebat-tutorial

CAESARSNOOT avatar Dec 05 '21 21:12 CAESARSNOOT

Thank you for your work,

I repost your link (is broken "titlebat => titlebar") : https://github.com/NovaAppsInc/electron-seamless-titlebar-tutorial

KasaiJo avatar Dec 06 '21 07:12 KasaiJo

This is how it would work with ipscRenderer.

In main.js add the following below mainWindow.loadFile("index.html");:

  //Minimise the app
  ipc.on("minimiseApp", () => {
    console.log("Clicked Minimise button!");
    mainWindow.minimize();
  });

  // Maximize Restore App
  ipc.on("maximizeRestoreApp", () => {
    if (mainWindow.isMaximized()) {
      console.log("Clicked on Restore");
      mainWindow.restore();
    } else {
      console.log("Clicked on Maximize");
      mainWindow.maximize();
    }
  });

  //Check if maximized
  mainWindow.on("maximize", () => {
    mainWindow.webContents.send("isMaximized");
  });

  // Check if is restored
  mainWindow.on("unmaximize", () => {
    mainWindow.webContents.send("isRestored");
  });

  //Close the app
  ipc.on("closeApp", () => {
    console.log("Clicked Close button!");
    mainWindow.close();
  });

renderer.js should look like this:

const { ipcRenderer } = require("electron");
const ipc = ipcRenderer;

// Minimise app
minBtn.addEventListener("click", () => {
  ipc.send("minimiseApp");
});

//Maximize Restore App
maxBtn.addEventListener("click", () => {
  ipc.send("maximizeRestoreApp");
});

//Close app
closeBtn.addEventListener("click", () => {
  ipc.send("closeApp");
});

Credit to Wanderson on YouTube, I watched this video of his to get it to work.

Kvanrooyen avatar May 20 '22 15:05 Kvanrooyen

I put up a pull request @binaryfunt

NovaAppsInc avatar Jun 16 '22 03:06 NovaAppsInc

The solution is very simple:

1.) Install @electron/remote

2.) add after mainWindow.loadFile(..) in main,js

require("@electron/remote/main").initialize();
require("@electron/remote/main").enable(mainWindow.webContents);

3.) replace the content in renderer.js to:

const {BrowserWindow} = require('@electron/remote')

const win = BrowserWindow.getFocusedWindow();

document.onreadystatechange = (event) => {
    if (document.readyState == "complete") {
        handleWindowControls();

        document.getElementById('electron-ver').innerHTML = `${process.versions.electron}`
    }
};

window.onbeforeunload = (event) => {
    win.removeAllListeners();
}

function handleWindowControls() {
    document.getElementById('min-button').addEventListener("click", event => {
        win.minimize();
    });

    document.getElementById('max-button').addEventListener("click", event => {
        win.maximize();
    });

    document.getElementById('restore-button').addEventListener("click", event => {
        win.unmaximize();
    });

    document.getElementById('close-button').addEventListener("click", event => {
        win.close();
    });

    toggleMaxRestoreButtons();
    win.on('maximize', toggleMaxRestoreButtons);
    win.on('unmaximize', toggleMaxRestoreButtons);

    function toggleMaxRestoreButtons() {
        if (win.isMaximized()) {
            document.body.classList.add('maximized');
        } else {
            document.body.classList.remove('maximized');
        }
    }
}

baxterbln avatar Oct 31 '22 19:10 baxterbln