Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

Electron アプリのウィンドウサイズ&ポジションを復元する

Chrome はウィンドウを閉じた時のサイズとポジションを覚えてくれているので Electron でもそういう API があると期待したけれど,無かった ので自前実装したメモ.


var info_path = path.join(app.getPath("userData"), "bounds-info.json");


var bounds_info;
try {
    bounds_info = JSON.parse(fs.readFileSync(info_path, 'utf8'));
catch(e) {
    bounds_info = {width: 800, height: 1000};  // デフォルトバリュー

mainWindow = new BrowserWindow(bounds_info);


mainWindow.on('close', function() {
    fs.writeFileSync(info_path, JSON.stringify(mainWindow.getBounds()));

書き込むのはウィンドウを閉じる直前のイベントである close であり,closed ではないことに注意.

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Help us understand the problem. What are the problem?