0.1.2 • Published 7 years ago

electron-window-helper v0.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

Electron Window Helper

This is an helper tool for electron window management and communication inter renderer process or between renderer process and main process

Electron's main process and renderer process execute script in differert environment, we can use ipcMain and ipcRenderer send and receive message between main process and render process,there is also a BrowserWindow.webContents.send method.

How we send message to other renderder process from renderer process directly?

First, we call remote.BrowserWindow.getAllWindows method get all windows, then find the window object, then call webContents.send of the window object.

How we find the target window? Does target window have a name attribute?

There is only an id attribute.

How about add a name to the window? I did this for you.

Features

have a container that contains all window we create, so we can find a target window by calling helper.get(name) method.

  1. Add name attribute to BrowserWindow
  2. There is a Window Container, it is easy to create and get a window object
  3. A Wrapper for IPC, renderer process can communication win each other "directly"

More detail see the docs

Before i wrote this tool, I find electron-window-manager, I tryed to commit to this module, but it is very hard to make big change.

I am still working on it, issue me when you get problems

Install

npm install --save electron-window-helper

install with --save, this package should be packed into you app client

Usage

// in main process
const electron = require('electron')
const app = electron.app
const helper = require('electron-window-helper')

let url = {
  home: `file://${__dirname}/app/home.html`,
  login: `file://${__dirname}/app/login.html`
}

app.on('ready', () => {
  // create and open home window
  let home = helper.open('home', url['home'], {
  	width: 800,
    height: 600,
    title: 'home window'
  })

  // create login window but hide
  let login = helper.create('home', url['login'], {
    width: 400,
    height: 300,
    title: 'login window',
    frame: false,
    show: false,
    center: true,
    movable: false
  })
})
-----------------------------------------------------------
// in home window js file
const electron = require('electron')
const helper = require('electron-window-helper/renderer')

// get current window
let win = helper.currentWindow
console.log('currentWindow is ', win.name)

// register linstener on `hello` channel
helper.on('hello', (data, from) => {
  console.log(`received ${data} from ${from} window`)
  // print `receive i am login from login window`
})

// show login window
helper.show('login')
-----------------------------------------------------------
// in login window js file
const electron = require('electron')
const helper = require('electron-window-helper/renderer')

// send msg to home window
helper.currentWindow.on('show', () => {
  helper.send('hello', 'i am login', 'home')
})

Detailed usage

0.1.2

7 years ago

0.1.0

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.4

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago