Giter VIP home page Giter VIP logo

electron-window-manager's Introduction

electron-window-manager

A window manager and message dispatcher for Electron.

Demo & Example

npm install
npm start

Usage

Server side (main process)

var WindowManagerServer = require('./path/to/window_manager_server.js');

var windowManagerServer = new WindowManagerServer({
  // a window named `mainWindow`
  mainWindow: {
    create: function() {
      var win = new BrowserWindow({width: 1024, height: 768});
      win.openDevTools();
      win.loadURL('file://' + __dirname + '/app/mainWindow.html');
      
      // do not need to listen ipc.on('closed') event, 
      // WindowManager will help you to do that
      //
      // ipc.on('closed', function() {...}); // no need this
      
      return win;
    }
  },
  // a window named `window2`
  window2: {
    create: function() {
      ...
    }
  }
});
windowManagerServer.registerIPC(); // register ipc to dispatch events

// This method will be called when Electron has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {
  windowManagerServer.open('mainWindow');
});

Client side (Renderer)

With React

var WindowManagerClient = require('./path/to/window_manager_client.js');

var MainWindow = React.createClass({
  componentDidMount: function() {
    this.windowManagerClient = new WindowManagerClient('mainWindow', function(event, arg){
      this.setState({message: arg});
    }.bind(this));

    // windowManagerClient.init() should be called in `componentDidMount` life cycle
    // which is all DOM are rendered 
    this.windowManagerClient.init();
  },
  handleWindowClose: function() {
    this.windowManagerClient.close();
  },
  render: function() {
    return (
     ...
    );
  }
});

ReactDOM.render(
  <MainWindow/>,
  document.getElementById('content')
);

API Document

Class: WindowManagerServer

new WindowManagerServer(windowDefinition)

  • windowDefinition (Object), properties:
    • create (Function):

server.open(window_name)

  • window_name (String): the window name in the definition object

create and open the window process

server.close(window_name)

  • window_name (String): the window name in the definition object

close the window

server.isOpened(window_name)

  • window_name (String): the window name in the definition object

check the window whether is opened.

server.registerIPC()

let server listen to the channel 'WindowManager' and register the message handler.

Class: WindowManagerClient

new WindowManagerClient(window_name, dispatcher)

  • window_name (String): the window name in the definition object
  • dispatcher (Functaion): the dispatcher of the receiving message.
    • dispatcher(event, arg)

client.init()

Initial the client, should be called after window rendered.

This method will do following steps:

  • listen to the ipc channel 'WindowManager'
  • send 'ready' signal to server

client.open(window_name[, args])

  • window_name (String): the window name in the definition object
  • args (Object, optional): open the window with initial data

notify server to open window with initial data.

client.close([window_name])

  • window_name (String, optional): if not provide, it will close current window

notify server to close window.

client.send(window_name, payload)

  • window_name (String): the window name in the definition object
  • payload (any): the data / message you want to send

send data/message to the window with window_name through server.

Contribution

PR & feedback are welcome.

License

MIT

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.