Giter Site home page Giter Site logo

betahuhn / electron-win-state Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 2.0 952 KB

๐Ÿ–ผ๏ธ :electron: Store and restore your Electron Window's Size and Position.

Home Page: https://readme.fish/BetaHuhn/electron-win-state

License: MIT License

JavaScript 48.01% TypeScript 51.99%
electron typescript

electron-win-state's Introduction

๐Ÿ–ผ๏ธ๐Ÿ’พ Electron Window State

Node CI Release CI GitHub David

Store and restore your Electron Window's Size and Position.

Features

  • ๐Ÿ’พ Persistent - persistently stores your Electron Window's size and position
  • ๐Ÿ”Œ Easy integration - integrates easily with your existing BrowserWindow configuration
  • ๐Ÿ”จ Customization - customize the behaviour and the underlying electron-store instance
  • ๐Ÿ–ผ๏ธ Frame option - you can optionally store the frame option as well

๐Ÿš€ Get started

npm install electron-win-state

Requires Electron 11 or later

๐Ÿ“š Usage

The easiest way to use electron-win-state, is to add it as a wrapper around your normal BrowserWindow:

import WinState from 'electron-win-state'

const browserWindow = WinState.createBrowserWindow({
	width: 800,
	height: 600,
	// your normal BrowserWindow options...
})

This will create a new BrowserWindow with the provided options and store any changes once the window is closed.

Thats it! Your Electron app now has a persistent window! ๐ŸŽ‰

Manually

You can also use electron-win-state manually:

const winState = new WinState({ 
	defaultWidth: 800,
	defaultHeight: 600,
	// other winState options, see below
})

const browserWindow = new BrowserWindow({
	...winState.winOptions,
	// your normal BrowserWindow options...
})

// Attach the required event listeners
winState.manage(this.browserWindow)

With winState.winOptions you have access to the restored size and position of the window:

console.log(winState.winOptions) // => { width: 800, height: 600, x: 0, y: 0, frame: true }

โš™๏ธ Options

You can customize the behaviour of electron-win-state further, by passing an options object to new WinState() or by specifying a winState object when using .createBrowserWindow():

new WinState({ 
	defaultWidth: 800,
	defaultHeight: 600,
	dev: true,
	addMethods: true
})

Or:

WinState.createBrowserWindow({
    winState: {
		defaultWidth: 800,
		defaultHeight: 600,
		dev: true,
		addMethods: true
	}
})

Or:

WinState.createBrowserWindow({
	width: 800,
	height: 600,
	winState: {
		dev: true,
		addMethods: true
	}
})

All of these result in the same.

Here are all the options electron-win-state supports:

Name Type Description Default
defaultWidth number The default width which will be used when no stored value was found 800
defaultHeight number The default height which will be used when no stored value was found 600
defaultFrame boolean The default value for the frame option when no stored value was found true
storeFrameOption boolean Store and restore the frame option (will be enabled automatically if defaultFrame is provided) false
dev boolean Enable development mode. Changes will be stored immediately after resizing or moving and not just after closing a window false
addMethods boolean Add the .resetWindowToDefault(), .setFramed() and .getStoredWinOptions() methods to the provided BrowserWindow true
electronStoreOptions object Will be passed to the underlying electron-store instance { name: 'window-state' }
store instance An existing electron-store instance to use n/a

๐Ÿ“– Examples

Here are a few examples to help you get started!

Basic Example

This is the most basic example. A new BrowserWindow will be created and the required event listeners will be attached automatically.

import WinState from 'electron-win-state'

const browserWindow = WinState.createBrowserWindow({
	width: 800,
	height: 600
})

browserWindow.loadURL('https://github.com/BetaHuhn/electron-win-state')

Development Mode

During development it might be helpful to store the window size and position immediately after changing it and not just after closing the window. You can enable this functionality by setting dev to true:

import WinState from 'electron-win-state'

const browserWindow = WinState.createBrowserWindow({
	width: 800,
	height: 600,
	winState: {
		dev: true
	}
})

browserWindow.loadURL('https://github.com/BetaHuhn/electron-win-state')

Resetting the Window

If addMethods is enabled (it is by default) a .resetWindowToDefault() method will be added to the provided BrowserWindow and can be used to both reset the stored state, as well as resizing the window to it's defaults:

import WinState from 'electron-win-state'

const browserWindow = WinState.createBrowserWindow({
	width: 800,
	height: 600,
	winState: {
		dev: true
	}
})

// Later
browserWindow.resetWindowToDefault()

This method could also be accessed anywhere else in your Electron app by getting the currently focused window:

const browserWindow = BrowserWindow.getFocusedWindow()
browserWindow.resetWindowToDefault()

Storing the frame option

You can optionally store the frame option as well. This might be useful if you want your users to enable or disable the window frame and store their choice.

If storeFrameOption is enabled (or defaultFrame is provided) the frame or defaultFrame option will be stored and can be later changed with the .setFramed() method on the provided BrowserWindow (will be added if addMethods is true).

You have to recreate the window for the frame option to take effect

import WinState from 'electron-win-state'

const browserWindow = WinState.createBrowserWindow({
	width: 800,
	height: 600,
	frame: false,
	winState: {
		storeFrameOption: true
	}
})

// Later
browserWindow.setFramed(true)

This method could also be accessed anywhere else in your Electron app by getting the currently focused window:

const browserWindow = BrowserWindow.getFocusedWindow()
browserWindow.setFramed(true)

Get stored values

You can access the restored window size and position with the winOptions object:

const winState = new WinState({ 
	defaultWidth: 800,
	defaultHeight: 600,
	defaultFrame: false
})

winState.winOptions // => { width: 1200, height: 700, x: 50, y: 105, frame: true }

You can get the stored values at any time using the added .getStoredWinOptions() method on the BrowserWindow (only added if addMethods is true):

import WinState from 'electron-win-state'

const browserWindow = WinState.createBrowserWindow({
	winState: {
		defaultWidth: 800,
		defaultHeight: 600,
		defaultFrame: false
	}
})

browserWindow.getStoredWinOptions() // => { width: 1200, height: 700, x: 50, y: 105, frame: true }

๐Ÿ’ป Development

Issues and PRs are very welcome!

  • run yarn lint or npm run lint to run eslint.
  • run yarn watch or npm run watch to watch for changes.
  • run yarn build or npm run build to produce a compiled version in the lib folder.

โ” About

This project was developed by me (@betahuhn) in my free time. If you want to support me:

Donate via PayPal

ko-fi

Credit

This library uses the great electron-store by @sindresorhus under the hood and was inspired by electron-window-state and electron-boilerplate.

๐Ÿ“„ License

Copyright 2021 Maximilian Schiller

This project is licensed under the MIT License - see the LICENSE file for details.

electron-win-state's People

Contributors

betahuhn avatar betahuhnbot avatar dependabot[bot] avatar pzeide avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

electron-win-state's Issues

TypeError: WinState is not a constructor

import WinState from 'electron-win-state'
const winState=new WinState({})

TypeError: WinState is not a constructor
at Object. (F:\electron\electron-app\out\main\index.js:7:18)

Electron binaries exported

๐Ÿž Describe the bug

Electron binaries are exported with the module when packaging.

๐Ÿ“š To Reproduce

Use any packager: (like electron-builder) to build an app with the electron-win-state module.

๐Ÿ’ก Expected behavior

The module should not export electron.

๐Ÿ–ผ๏ธ Screenshots

Not on my computer but I think that self-explanatory.

โš™๏ธ Environment

  • Library Version 1.1.19
  • Electron Version: 15.2.0
  • OS: Windows 11 (should affect every OS)

๐Ÿ“‹ Additional context

I think that the problem came from the fact that electron is set as a dependency and not a devdependency.

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.