Skip to content

iamtidu/Electron-notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 

Repository files navigation

Electron Js

1. Introduction to Electron.js

  • What is Electron.js?
  • History and Evolution
  • Use Cases and Popular Applications (e.g., VS Code, Slack)

2. Setting Up the Environment

  • Prerequisites (Node.js, npm)
  • Installing Electron
  • Setting Up Your First Electron Project
  • Project Structure Overview
  • Running Your First Electron App

3. Core Concepts

  • Main Process vs Renderer Process
  • Understanding the package.json File
  • Using main.js and index.html
  • Communicating Between Processes (IPC - Inter-Process Communication)
  • Creating Windows (BrowserWindow API)

4. Building the User Interface

  • Using HTML/CSS/JavaScript in Electron
  • Loading External Resources
  • Integrating Frontend Frameworks (React, Angular, Vue)
  • Working with Preload Scripts
  • Handling Native Menus and Context Menus

5. Working with System Features

  • File System Access (fs module)
  • Using Native Dialogs (e.g., Open, Save, Message Boxes)
  • Clipboard API Integration
  • Notification API
  • Interacting with the Operating System

6. Advanced Window Management

  • Multiple Windows and Window Management
  • Handling Child Windows and Modals
  • Offscreen Rendering
  • Fullscreen and Kiosk Mode
  • Custom Window Shapes and Transparent Windows

7. Security Best Practices

  • Understanding Security Concerns in Electron
  • Using Context Isolation
  • Disabling Node Integration in the Renderer
  • Content Security Policy (CSP)
  • Preventing Remote Code Execution

8. Packaging and Distribution

  • Building and Packaging Your App (Electron Forge, Electron Builder)
  • Code Signing and Certificates
  • Auto-Updates (Electron-Updater)
  • Creating Installers (Windows, macOS, Linux)
  • Publishing Your Electron App

9. Performance Optimization

  • Reducing App Size
  • Optimizing Memory Usage
  • Efficient Resource Management
  • Debugging Performance Issues (DevTools, Profiler)

10. Integrating with Native Modules

  • Using Node.js Native Addons
  • Integrating C/C++ Libraries
  • Managing Cross-Platform Compatibility
  • Working with Native Node Modules

11. Advanced IPC and Data Management

  • Handling Complex IPC Scenarios
  • Sharing Data Between Windows and Processes
  • Storing Data Locally (SQLite, LevelDB, etc.)
  • Implementing Data Persistence

12. Testing and Debugging

  • Debugging Main and Renderer Processes
  • Unit Testing with Mocha/Chai
  • End-to-End Testing with Spectron
  • Continuous Integration (CI) Setup for Electron

13. Real-World Projects

  • Building a Simple Text Editor
  • Creating a Note-Taking App with Offline Sync
  • Developing a Media Player with Custom Controls
  • Implementing a Task Management App with Notifications

14. Electron.js in Production

  • Best Practices for Electron App Deployment
  • Handling User Data and Privacy
  • Logging and Monitoring in Production
  • Managing App Updates and Rollbacks

15. Future of Electron.js

  • Latest Updates and Roadmap
  • Comparison with Other Desktop Frameworks
  • Community and Resources for Further Learning

16. Conclusion

  • Summary of Key Takeaways
  • Resources for Continued Learning (Books, Tutorials, Communities)


1. Introduction to Electron.js

What is Electron.js?

Electron.js is an open-source framework developed by GitHub that enables developers to create cross-platform desktop applications using web technologies like HTML, CSS, and JavaScript. By leveraging Chromium (the open-source project behind Google Chrome) and Node.js, Electron allows you to build applications that run seamlessly on Windows, macOS, and Linux from a single codebase.

Key Features:

  • Cross-Platform Compatibility: Write code once and deploy it across multiple operating systems.
  • Web Technologies: Utilize the familiarity and power of web development tools and languages.
  • Access to Native APIs: Through Node.js integration, Electron apps can interact with the file system and other OS-level APIs.
  • Rich Ecosystem: Benefit from a vast collection of npm packages and Electron-specific modules.
  • Automatic Updates: Built-in mechanisms to update applications smoothly.

History and Evolution

  • 2013: Electron began as "Atom Shell," developed by Cheng Zhao for GitHub's Atom text editor.
  • 2014: GitHub open-sourced Atom Shell.
  • 2015: Atom Shell was rebranded to Electron.
  • 2016-Present: Electron gained widespread adoption, with major companies using it for their desktop applications. Continuous development has led to performance improvements, enhanced security, and new features.

Use Cases and Popular Applications

Electron is favored for its ability to deliver a native desktop experience using web development skills.

Popular Applications Built with Electron:

  • Visual Studio Code: A powerful code editor from Microsoft.
  • Slack: A collaboration and communication platform.
  • Discord: A voice, video, and text chat app for gamers and communities.
  • GitHub Desktop: A GUI for managing GitHub repositories.
  • Atom: A customizable text editor.
  • Figma: A collaborative interface design tool.

Use Cases:

  • Developer Tools: IDEs, code editors, and Git clients.
  • Communication Platforms: Messaging and video conferencing apps.
  • Productivity Software: Note-taking apps, project management tools.
  • Media Players: Applications for playing audio and video files.
  • Design Tools: Software for UI/UX design and prototyping.

2. Setting Up the Environment

Prerequisites (Node.js, npm)

Before diving into Electron development, ensure you have the following installed:

  • Node.js: Provides the runtime environment for executing JavaScript code server-side.
  • npm (Node Package Manager): Comes bundled with Node.js, used for installing packages. Installation:

Verify Installation:

node -v   # Should display the Node.js version
npm -v    # Should display the npm version

Installing Electron

Electron can be installed globally or locally within your project.

Local Installation (Recommended):

Create a Project Directory:

mkdir my-electron-app
cd my-electron-app

Initialize npm:

npm init -y

Install Electron as a Development Dependency:

npm install electron --save-dev

Global Installation:

npm install -g electron

Note: Global installation is less common and not recommended for application development.

Setting Up Your First Electron Project

Project Structure Overview

Your project directory should look like this:

my-electron-app/
├── package.json
├── main.js
├── index.html
├── preload.js (optional)
└── node_modules/
  • package.json: Contains project metadata and dependencies.
  • main.js: The main process script.
  • index.html: The UI of your application.
  • preload.js: A script that runs before the renderer process is loaded (used for context bridging).

Creating main.js

// main.js
const { app, BrowserWindow } = require('electron');

function createWindow() {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      // Enable the use of preload script
      preload: __dirname + '/preload.js',
    },
  });

  // Load the index.html file.
  win.loadFile('index.html');
}

// Called when Electron has finished initialization.
app.whenReady().then(createWindow);

// Quit the app when all windows are closed (except on macOS).
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit();
});

// Re-create a window when the app icon is clicked (macOS).
app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) createWindow();
});

Creating index.html

<!-- index.html -->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>My First Electron App</title>
  </head>
  <body>
    <h1>Hello, Electron!</h1>
    <script src="renderer.js"></script>
  </body>
</html>

(Optional) Creating preload.js

// preload.js
const { contextBridge } = require('electron');

contextBridge.exposeInMainWorld('myAPI', {
  // Expose APIs here
});

Running Your First Electron App

Update package.json:

Add a start script:

"scripts": {
  "start": "electron ."
}

Start the Application:

npm start

An Electron window should appear displaying "Hello, Electron!".

3. Core Concepts

Main Process vs Renderer Process

Understanding Electron's multi-process architecture is crucial.

Main Process

  • Role: Controls the lifecycle of the application.

  • Characteristics:

    • Runs main.js.
    • Manages native GUI elements (windows, menus).
    • Has full access to Node.js APIs.
    • Single instance per application.

Renderer Process

  • Role: Manages each window's web content.

  • Characteristics:

    • Runs web pages (like index.html).
    • Limited or no access to Node.js APIs (for security).
    • Multiple instances (one per window).
    • Similar to a browser tab in Chrome.

Communication Between Processes:

Use Electron's Inter-Process Communication (IPC) modules: ipcMain and ipcRenderer.

Understanding the package.json File

The package.json file is essential for defining your application's metadata and dependencies.

Key Fields:

  • name: The application name.
  • version: The current version.
  • main: Entry point for the main process (main.js).
  • scripts: Commands that can be run using npm run .
  • dependencies: Packages required at runtime.
  • devDependencies: Packages required during development.

Example:

{
  "name": "my-electron-app",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "devDependencies": {
    "electron": "^25.0.0"
  }
}

Using main.js and index.html

  • main.js: Manages the application's lifecycle, creates windows, and handles system events.

  • index.html: The front-end interface displayed in the application window. How They Interact:

  • main.js creates a BrowserWindow and loads index.html.

  • index.html can include scripts and styles to build the UI.

Communicating Between Processes (IPC)

IPC Modules

  • ipcMain: Used in the main process.
  • ipcRenderer: Used in the renderer process.

Sending Messages

From Renderer to Main:

  • Renderer Process (renderer.js):
const { ipcRenderer } = require('electron');
ipcRenderer.send('channel-name', 'message');
  • Main Process (main.js):
const { ipcMain } = require('electron');
ipcMain.on('channel-name', (event, arg) => {
  console.log(arg); // 'message'
});

From Main to Renderer:

  • Main Process (main.js):
event.reply('reply-channel', 'response');
  • Renderer Process (renderer.js):
ipcRenderer.on('reply-channel', (event, arg) => {
  console.log(arg); // 'response'
});

Using contextBridge and preload.js for Security:

  • Expose specific APIs to the renderer process without exposing the entire Node.js environment.

Example:

  • preload.js:
const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
  sendMessage: (message) => ipcRenderer.send('channel-name', message),
  onMessage: (callback) => ipcRenderer.on('reply-channel', callback),
});
  • renderer.js:
window.electronAPI.sendMessage('Hello from Renderer');
window.electronAPI.onMessage((event, message) => {
  console.log(message);
});

Creating Windows (BrowserWindow API)

The BrowserWindow class is used to create and manage application windows.

Creating a Window

const { BrowserWindow } = require('electron');

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    // Additional options
    webPreferences: {
      preload: __dirname + '/preload.js', // Preload script
      nodeIntegration: false,             // Security best practice
      contextIsolation: true,             // Security best practice
    },
  });

  win.loadFile('index.html');
}

BrowserWindow Options

  • width and height: Set window dimensions.
  • resizable: Boolean indicating if the window can be resized.
  • fullscreen: Open the window in fullscreen mode.
  • icon: Path to the window's icon.
  • webPreferences: Web content settings.

Managing Multiple Windows

  • Create multiple instances of BrowserWindow for multi-window applications.
  • Keep track of windows to prevent garbage collection.

4. Building the User Interface

Using HTML/CSS/JavaScript in Electron

Electron's renderer process behaves like a web browser, allowing you to use:

  • HTML: For structuring content.
  • CSS: For styling.
  • JavaScript: For interactivity.

Including External Resources:

  • CSS Stylesheets:
<link rel="stylesheet" href="styles.css" />
  • JavaScript Files:
<script src="renderer.js"></script>

Loading External Resources

Local Resources

  • Store images, fonts, and other assets in your project's directory.

  • Reference them using relative paths.

<img src="images/logo.png" alt="App Logo" />

Web Resources

  • Load resources from the internet cautiously.
  • Be aware of security implications.
<script src="https://cdn.example.com/library.js"></script>

Security Tip: Enable a Content Security Policy (CSP) to mitigate risks.

Integrating Frontend Frameworks (React, Angular, Vue)

Electron can be combined with popular frontend frameworks for building complex UIs.

React Integration

1. Initialize a React App:

npx create-react-app my-electron-app
cd my-electron-app

2. Install Electron:

npm install electron --save-dev

3. Modify package.json:

Add Electron start scripts and set the main entry point.

4. Build and Run:

  • Development Mode:

Use tools like electron-forge or electron-builder to streamline development.

  • Production Build:
npm run build
npm run electron

Angular and Vue Integration

  • Angular:

Use the Angular CLI to set up the project, then configure Electron to load the compiled Angular application.

  • Vue:

Use Vue CLI for project setup, then integrate Electron using plugins like electron-builder.

Working with Preload Scripts

Preload scripts bridge the gap between the main and renderer processes while maintaining security.

Purpose of Preload Scripts

  • Run in the renderer process before web content is loaded.
  • Have access to both DOM APIs and limited Node.js APIs.
  • Used to expose safe APIs via contextBridge.

Setting Up a Preload Script

In main.js:

const win = new BrowserWindow({
  webPreferences: {
    preload: __dirname + '/preload.js',
    nodeIntegration: false,
    contextIsolation: true,
  },
});

In preload.js:

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('api', {
  send: (channel, data) => ipcRenderer.send(channel, data),
  receive: (channel, func) =>
    ipcRenderer.on(channel, (event, ...args) => func(...args)),
});

In renderer.js:

// Sending a message to the main process
window.api.send('channel-name', 'Hello from Renderer');

// Receiving a message from the main process
window.api.receive('reply-channel', (message) => {
  console.log(message);
});

Handling Native Menus and Context Menus

Electron allows you to create custom menus that integrate with the native OS.

Application Menu

Defining a Menu Template:

const { Menu } = require('electron');

const menuTemplate = [
  {
    label: 'File',
    submenu: [
      {
        label: 'Open',
        click: () => {
          // Handle file open
        },
      },
      { type: 'separator' },
      { role: 'quit' },
    ],
  },
  {
    label: 'Edit',
    submenu: [{ role: 'undo' }, { role: 'redo' }],
  },
];

const menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);

Roles:

  • Predefined actions like undo, redo, cut, copy, paste. Context Menu

Creating a Context Menu:

  • In main.js:
const { ipcMain, Menu } = require('electron');

ipcMain.on('show-context-menu', (event) => {
  const template = [
    {
      label: 'Inspect Element',
      click: () => {
        event.sender.inspectElement(rightClickPosition.x, rightClickPosition.y);
      },
    },
  ];
  const menu = Menu.buildFromTemplate(template);
  menu.popup(BrowserWindow.fromWebContents(event.sender));
});

In renderer.js:

window.addEventListener(
  'contextmenu',
  (e) => {
    e.preventDefault();
    window.api.send('show-context-menu');
  },
  false
);

Security Note: Avoid using remote module. Use IPC and preload scripts to interact with the main process.

Context Menu Libraries:

  • Use packages like electron-context-menu for easier implementation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors