GithubHelp home page GithubHelp logo

madosuki / electron-window-state Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mawie81/electron-window-state

1.0 2.0 1.0 727 KB

A library to store and restore window sizes and positions for your Electron app

License: MIT License

JavaScript 100.00%

electron-window-state's Introduction

electron-window-state Build Status

A library to store and restore window sizes and positions for your Electron app

Heavily influenced by the implementation in electron-boilerplate.

Install

$ npm install --save electron-window-state

Usage

const windowStateKeeper = require('electron-window-state');
let win;

app.on('ready', function () {
  // Load the previous state with fallback to defaults
  let mainWindowState = windowStateKeeper({
    defaultWidth: 1000,
    defaultHeight: 800
  });

  // Create the window using the state information
  win = new BrowserWindow({
    'x': mainWindowState.x,
    'y': mainWindowState.y,
    'width': mainWindowState.width,
    'height': mainWindowState.height
  });

  // Let us register listeners on the window, so we can update the state
  // automatically (the listeners will be removed when the window is closed)
  // and restore the maximized or full screen state
  mainWindowState.manage(win);
});

Please do not set useContentSize to true at creating BrowserWindow instance because it changes how to calculate window size.

API

windowStateKeeper(opts)

Note: Don't call this function before the ready event is fired.

opts

defaultWidth - Number

The width that should be returned if no file exists yet. Defaults to 800.

defaultHeight - Number

The height that should be returned if no file exists yet. Defaults to 600.

path - String

The path where the state file should be written to. Defaults to app.getPath('userData')

file - String

The name of file. Defaults to window-state.json. This is usefull if you want to support multiple windows. Simply create multiple windowStateKeeper instances with different filenames.

maximize - Boolean

Should we automatically maximize the window, if it was last closed maximized. Defaults to true

fullScreen - Boolean

Should we automatically restore the window to full screen, if it was last closed full screen. Defaults to true

state object

const windowState = windowStateKeeper({
  defaultWidth: 1000,
  defaultHeight: 800
});

x - Number

The saved x coordinate of the loaded state. undefined if the state has not been saved yet.

y - Number

The saved y coordinate of the loaded state. undefined if the state has not been saved yet.

width - Number

The saved width of loaded state. defaultWidth if the state has not been saved yet.

height - Number

The saved heigth of loaded state. defaultHeight if the state has not been saved yet.

isMaximized - Boolean

true if the window state was saved while the window was maximized. undefined if the state has not been saved yet.

isFullScreen - Boolean

true if the window state was saved while the window was in full screen mode. undefined if the state has not been saved yet.

manage(window) - Function

Register listeners on the given BrowserWindow for events that are related to size or position changes (resize, move). It will also restore the window's maximized or full screen state. When the window is closed we automatically remove the listeners and save the state.

unmanage - Function

Removes all listeners of the managed BrowserWindow in case it does not need to be managed anymore.

saveState(window) - Function

Saves the current state of the given BrowserWindow. This exists mostly for legacy purposes, and in most cases it's better to just use manage.

License

MIT © Marcel Wiehle

electron-window-state's People

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

rekordcloud

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.