javascript - Making a Child window move with Parent in a frameless Electron window - Stack Overflow

I'm currently working on a frameless application with ElectronJS where I'm trying to have a p

I'm currently working on a frameless application with ElectronJS where I'm trying to have a parent window handle the dragging, minimize, maximize, and exit, as well as a controller for the child window which will handle all of the content. However, when I set the parent to the main window in my main.js, the content window stays in place when I move the parent.

Below is my main.js:

const electron = require('electron');
const url = require('url');
const path = require('path');

const {app, BrowserWindow, Menu} = electron;

let mainWindow;
let contentWindow;

app.on('ready', function createWindow(){
    //Create window
    mainWindow = new BrowserWindow({
        width: 1280,
        height: 720,
        frame: false
    })
    contentWindow = new BrowserWindow({
        width: 1280,
        height: 720,
        frame: false,
        parent: mainWindow
    })

    //load html for window
    mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'assets/html/mainWindow.html'),
        protocol:'file:',
        slashes: true
    }));

    contentWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'assets/html/contentWindow.html'),
        protocol:'file:',
        slashes: true
    }));
});

One of the div's in my mainWindow.html has -webkit-app-region: drag; to allow it to act as the top bar like a normal window. Basically I would like this to move both windows.

I'm currently working on a frameless application with ElectronJS where I'm trying to have a parent window handle the dragging, minimize, maximize, and exit, as well as a controller for the child window which will handle all of the content. However, when I set the parent to the main window in my main.js, the content window stays in place when I move the parent.

Below is my main.js:

const electron = require('electron');
const url = require('url');
const path = require('path');

const {app, BrowserWindow, Menu} = electron;

let mainWindow;
let contentWindow;

app.on('ready', function createWindow(){
    //Create window
    mainWindow = new BrowserWindow({
        width: 1280,
        height: 720,
        frame: false
    })
    contentWindow = new BrowserWindow({
        width: 1280,
        height: 720,
        frame: false,
        parent: mainWindow
    })

    //load html for window
    mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'assets/html/mainWindow.html'),
        protocol:'file:',
        slashes: true
    }));

    contentWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'assets/html/contentWindow.html'),
        protocol:'file:',
        slashes: true
    }));
});

One of the div's in my mainWindow.html has -webkit-app-region: drag; to allow it to act as the top bar like a normal window. Basically I would like this to move both windows.

Share Improve this question edited Jul 9, 2018 at 10:27 Elucid asked Jul 9, 2018 at 8:15 ElucidElucid 792 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

It's like what @Dennis L said, use the move event provided in the docs. I though it only support mac, but i'm using windows. And once i tried it working.

In my case, i'm using getPosition method to keep track of the mainWindow position during move or drag. And then setPosition of the secondWindow with value that came from mainWindow itself.

  mainWindow.on('move', function() {
    let position = mainWindow.getPosition();
    secondWindow.setPosition(position[0], position[1]);
  });

As far as i know that your goal is basically that, what only is supported under macOS with parent and child window Doc, the way i reach that my child moves with the parent, is that i listen on the move event from the parent and recalculate the position for the child. Not the best way but it works.

mainWindow.on("move", function () {"calculate and set new position here"}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248796a4618550.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信