如何在电子应用程序中恢复默认窗口大小? [英] How to restore default window size in an electron app?

查看:289
本文介绍了如何在电子应用程序中恢复默认窗口大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将电子应用程序中的主窗口恢复到特定大小?为了解释我要完成的工作,让我举一个例子:

Is there a way to restore main window in an electron app to a certain size? To explain what I am trying to accomplish, let me give you an example:

当我打开Windows资源管理器时,它将以特定的大小和位置打开。然后,我最大化该窗口并关闭资源管理器。下次重新启动资源管理器时,它会以与关闭时相同的状态打开,即处于最大化状态。但是,当我单击还原按钮时,资源管理器会还原到最大化之前的宽度/高度和位置。

When I open Windows Explorer, it opens with a certain size and at a certain position. Then I maximize that window and close the explorer. Next time when I restart the explorer, it opens in the same state as when I closed it i.e. in maximized state. However when I click on the restore button, explorer restores to the width/height and position before it was maximized.

我也想在电子应用中实现相同的目的。

I would like to accomplish the same in my electron application as well. However that is not happening.

我正在做的是,当我的应用程序关闭时,我捕获了尺寸(宽度,高度)和位置(x,y坐标)。在应用程序窗口中使用( win.getBounds()),然后使用 electron-config 将其保存在配置文件中。这是我的代码:

What I am doing is when my application closes, I capture the dimensions (width, height) and position (x, y coordinates) of the application window using (win.getBounds())and save those in a config file using electron-config. This is what my code looks like:

  const Config = require('electron-config')
  const config = new Config();

  mainWindow.on('close', () => {
    config.set('winBounds', mainWindow.getBounds())
  });

现在,当我启动应用程序时,我会从配置文件中读取设置(实际上电子配置会对我来说),并使用它来设置窗口的初始尺寸/位置。我正在使用如下代码:

Now when I start the application, I read the settings from the config file (actually electron-config does that for me) and use that to set the initial dimensions/position of the window. I am using code like below:

  let opts = {
    show: false,
    icon: path.join(__dirname, 'app-window-icon.png')
  };
  Object.assign(opts, config.get('winBounds'));
  mainWindow = new BrowserWindow(opts);

这也很好用。但是,我现在唯一的选择是最大化窗口。我找不到任何选项可以将窗口还原到最大化之前的大小。

This is also working great. However the only option I get now is maximize the window. I'm not able to find any option to restore the window to a size before it was maximized.

例如,假设用户以1024x768 px的尺寸启动应用程序。然后,用户将应用程序窗口最大化,然后将其关闭。当用户重新启动应用程序时,它会以关闭时的大小打开,我看到的唯一选择是最大化窗口。我正在寻找的是将窗口大小恢复为1024x768px的选项。

For example, let's say the user launches the application with 1024x768 px dimensions. User then maximizes the application window and then closes it. When the user relaunches the application, it opens with the size it was closed and the only option I see is to maximize the window. What I am looking for is an option to restore the window size to 1024x768px.

我在这里查找了文档: https://github.com/electron/electron/blob/master/docs/api/browser-window.md ,但不幸的是在那里找不到任何东西。

I looked up the documentation here: https://github.com/electron/electron/blob/master/docs/api/browser-window.md but unfortunately couldn't find anything there.

推荐答案

我使用了此处的解决方案,其中涉及在电子设置中保持窗口大小和位置不变,并且效果很好。

I used the solution here which involves persisting your windows size and position in electron-settings and it works perfectly.

在代码中包含 windowStateKeeper 函数,然后修改您的 createMainWindow 函数如下:

Include the windowStateKeeper function in your code and then adapt your createMainWindow function as follows:

function createMainWindow() {
    const mainWindowStateKeeper = windowStateKeeper('main');
    const win = new electron.BrowserWindow({
        title: 'main',
        x: mainWindowStateKeeper.x,
        y: mainWindowStateKeeper.y,
        width: mainWindowStateKeeper.width,
        height: mainWindowStateKeeper.height
    });
    mainWindowStateKeeper.track(win);

    win.loadURL(`file://${__dirname}/index.html`);
    win.on('closed', onClosed);

    return win;
}

这篇关于如何在电子应用程序中恢复默认窗口大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆