npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@el3um4s/electron-window

v1.1.0

Published

Electron - create a window with optional autoupdater and browserview

Downloads

15

Readme

Electron Window

Electron - create a window with optional autoupdater and browserview

NPM link: @el3um4s/electron-window

Install and use the package

To use the package in a project:

npm i @el3um4s/electron-window

Then in a file:

import ElectronWindow from "@el3um4s/electron-window";
import windowControls from "@el3um4s/renderer-for-electron-window-controls";

const createWindow = async (options: {
  url: string;
  preload: string;
  themeSource?: "system" | "light" | "dark";
  settings?: Electron.BrowserWindowConstructorOptions;
}): Promise<ElectronWindow> => {
  let window: ElectronWindow;

  const { url, themeSource = "system", preload } = options;

  const settings = {
    ...options?.settings,
    title: "GEST DASHBOARD",
  };
  window = new ElectronWindow(settings);

  window.createWindow({ url, themeSource, preload });

  await window.setIpcMain([windowControls]);

  await window.addBrowserViewHidden();
  await window.setIpcMainView([windowControls]);

  window.addAutoUpdater();
  return window;
};

let electronWindow: ElectronWindow;

electronWindow = await createWindow({
  url,
  preload,
  themeSource: "light",
  settings: {
    x: Math.floor(Math.random() * 64),
    y: Math.floor(Math.random() * 64),
  },
});

To use in the renderer install:

npm i @el3um4s/electron-window @el3um4s/renderer-electron-window-browser-view  @el3um4s/ipc-for-electron

Then in the preload file:

import { generateContextBridge } from "@el3um4s/ipc-for-electron";
import { browserView } from "@el3um4s/electron-window";

const listAPI = [browserView];

generateContextBridge(listAPI, "ipc");

Then in the renderer:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.openInBrowserView({ url: "https://www.google.com", apiKey: "ipc" });

browserView.showBrowserView({
  bounds: {
    paddingLeft: 64,
    paddingTop: 64,
    paddingRight: 128,
    paddingBottom: 128,
  },
  apiKey: "ipc",
});

browserView.on.browserViewCanBeShowed({
  callback: (data) => {
    console.log(data);
  },
});

API: main process

new ElectronWindow(settings?: Electron.BrowserWindowConstructorOptions):ElectronWindow: create a new instance of ElectronWindow

import ElectronWindow from "@el3um4s/electron-window";

let window: ElectronWindow;

const settings = {
  title: "GEST DASHBOARD",
  backgroundColor: "red",
};

window = new ElectronWindow(settings);

createWindow(options?: CreateWindow): BrowserWindow: create a new window

import ElectronWindow from "@el3um4s/electron-window";

let window: ElectronWindow;

const options = {
  url: "https://www.google.com",
  themeSource: "light",
  preload: "path/to/preload.js",
};

window = new ElectronWindow();
window.createWindow(options);

loadUrl(url: string): void: load a new url in the window

import ElectronWindow from "@el3um4s/electron-window";

let window: ElectronWindow;

const options = {
  url: "https://www.google.com",
  themeSource: "light",
  preload: "path/to/preload.js",
};

window = new ElectronWindow();
window.createWindow(options);

window.loadUrl("https://www.youtube.com");

async setIpcMain(api: Array<IPC>): Promise<void>: set the ipcMain for the window

Use el3um4s/ipc-for-electron (GitHub, NPM) to set the ipcMain for the window

import ElectronWindow from "@el3um4s/electron-window";
import windowControls from "@el3um4s/renderer-for-electron-window-controls";

let window: ElectronWindow;

window = new ElectronWindow();
window.createWindow();

const listAPI = [windowControls];
await window.setIpcMain(listAPI);

async addAutoUpdater(): Promise<void>: add the autoUpdater to the window

If you want to use the autoupdater, you need to install

  • el3um4s/ipc-for-electron (GitHub, NPM)
  • el3um4s/ipc-for-electron-auto-updater (GitHub, NPM)
  • el3um4s/renderer-for-electron-auto-updater (GitHub, NPM)
npm i @el3um4s/electron-window @el3um4s/ipc-for-electron @el3um4s/ipc-for-electron-auto-updater @el3um4s/renderer-for-electron-auto-updater

In the main process:

import ElectronWindow from "@el3um4s/electron-window";

let window: ElectronWindow;

window = new ElectronWindow();
window.createWindow();

window.addAutoUpdater();

In the preload file:

import { generateContextBridge } from "@el3um4s/ipc-for-electron";
import autoUpdater from "@el3um4s/ipc-for-electron-auto-updater";

const listAPI = [autoUpdater];

generateContextBridge(listAPI, "ipc");

async addBrowserView(options?: CreateBrowserView): Promise<void>: add a browserView to the window

import ElectronWindow from "@el3um4s/electron-window";

let window: ElectronWindow;

window = new ElectronWindow();
window.createWindow();

const options = {
  url: "https://www.google.com",
  preload: "path/to/preload.js",
  bounds: {
    paddingLeft: 64,
    paddingTop: 64,
    paddingRight: 64,
    paddingBottom: 64,
  },
};

await window.addBrowserView(options);

async addBrowserViewHidden(options?: CreateBrowserView): Promise<void>: add a browserView to the window and hide it

import ElectronWindow from "@el3um4s/electron-window";

let window: ElectronWindow;

window = new ElectronWindow();
window.createWindow();

const options = {
  url: "https://www.google.com",
  preload: "path/to/preload.js",
};

await window.addBrowserViewHidden(options);

async setIpcMainView(api: Array<IPC>): Promise<void>: set the ipcMain for the browserView

Use el3um4s/ipc-for-electron (GitHub, NPM) to set the ipcMain for the window

import ElectronWindow from "@el3um4s/electron-window";
import systemInfo from "@el3um4s/ipc-for-electron-system-info";

let window: ElectronWindow;

window = new ElectronWindow();
window.createWindow();

const options = {
  url: "https://www.google.com",
  preload: "path/to/preload.js",
};

await window.addBrowserViewHidden(options);

const listAPI = [systemInfo];
await window.setIpcMainView(listAPI);

API: browserView - Electron Side

  • openInBrowserView: open the url in the browserView.
  • showBrowserView: show the browserView. The response is sent to the showBrowserView channel.
  • resizeBrowserViewToMaximized: resize the browserView to the maximized size.
  • resizeBrowserViewToUnMaximized: resize the window to the un-maximized size.
  • removeBrowserView: remove the browserView.
  • openBrowserViewDevTools: open the devTools of the browserView.
  • printBrowserView: print the browserView.
  • goBackBrowserView: go back in the browserView.
  • goForwardBrowserView: go forward in the browserView.
  • reloadCurrentPageBrowserView: reload the current page in the browserView.

API: browserView - Renderer Side

on.browserViewCanBeShowed = async (options: { callback?: (arg0: boolean) => void; apiKey?: string; }): Promise<boolean>

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.openInBrowserView({ url: "https://www.google.com", apiKey: "ipc" });

browserView.showBrowserView({
  bounds: {
    paddingLeft: 64,
    paddingTop: 64,
    paddingRight: 128,
    paddingBottom: 128,
  },
  apiKey: "ipc",
});

browserView.on.browserViewCanBeShowed({
  callback: (data) => {
    console.log(data);
  },
});

showBrowserView = async (options: { callback?: (arg0: boolean) => void; apiKey?: string; bounds?: BrowserViewBounds; }): Promise<boolean>: show the browserView

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.openInBrowserView({ url: "https://www.google.com", apiKey: "ipc" });

browserView.showBrowserView({
  bounds: {
    paddingLeft: 64,
    paddingTop: 64,
    paddingRight: 128,
    paddingBottom: 128,
  },
  apiKey: "ipc",
  callback: (data) => {
    console.log(
      data ? "BrowserView can be shown" : "BrowserView can't be shown"
    );
  },
});

openInBrowserView = (options: { apiKey?: string; url: string }): void: open the url in the browserView

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.openInBrowserView({ url: "https://www.google.com", apiKey: "ipc" });

resizeBrowserViewToMaximized = (options?: { apiKey?: string; bounds?: BrowserViewBounds; }): void: resize the browserView to the maximized size

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

const bounds: {
  paddingLeft: 64;
  paddingTop: 64;
  paddingRight: 64;
  paddingBottom: 64;
};

browserView.resizeBrowserViewToMaximized({ bounds, apiKey: "ipc" });

resizeBrowserViewToUnMaximized = (options?: { apiKey?: string; bounds?: BrowserViewBounds; }): void: resize the browserView to the un-maximized size

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

const bounds: {
  paddingLeft: 64;
  paddingTop: 64;
  paddingRight: 64;
  paddingBottom: 64;
};

browserView.resizeBrowserViewToUnMaximized({ bounds });

removeBrowserView = (options?: { apiKey?: string }): void: remove the browserView

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.removeBrowserView({ apiKey: "ipc" });

openBrowserViewDevTools = (options?: { apiKey?: string }): void: open the dev tools window of the browserView

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.openBrowserViewDevTools();

printBrowserView = (options?: { apiKey?: string }): void: print the browserView

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.printBrowserView();

goBackBrowserView = (options?: { apiKey?: string }): void: go back in the browserView

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.goBackBrowserView();

goForwardBrowserView = (options?: { apiKey?: string }): void: go forward in the browserView

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.goForwardBrowserView();

reloadCurrentPageBrowserView = (options?: { apiKey?: string }): void: reload the current page in the browserView

example:

import browserView from "@el3um4s/renderer-electron-window-browser-view";

browserView.reloadCurrentPageBrowserView();

Default settings

const defaultSettings = {
  title: appName,
  width: 854,
  height: 480,
  frame: false,
  backgroundColor: "#FFF",
};

Interfaces

CreateWindow

interface CreateWindow {
  url: string;
  iconPath?: string;
  preload?: string;
  themeSource?: "system" | "light" | "dark";
}

CreateBrowserView

interface CreateBrowserView {
  url?: string;
  preload?: string;
  bounds?: BrowserViewBounds;
}

BrowserViewBounds

interface BrowserViewBounds {
  paddingLeft?: number;
  paddingTop?: number;
  paddingRight?: number;
  paddingBottom?: number;
  show?: boolean;
}