Full Code of sfatihk/electron-tray-window for AI

master 03e561b52f96 cached
9 files
16.4 KB
4.4k tokens
14 symbols
1 requests
Download .txt
Repository: sfatihk/electron-tray-window
Branch: master
Commit: 03e561b52f96
Files: 9
Total size: 16.4 KB

Directory structure:
gitextract_s_3v73pl/

├── .gitignore
├── LICENSE
├── README.MD
├── examples/
│   ├── README.MD
│   ├── index.js
│   ├── package.json
│   └── resources/
│       └── index.html
├── package.json
└── src/
    └── index.js

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
node_modules


================================================
FILE: LICENSE
================================================
MIT License

Copyright (c) 2018 Seyyid Fatih KOÇ

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


================================================
FILE: README.MD
================================================
[![npm version](https://badge.fury.io/js/electron-tray-window.svg)](https://www.npmjs.com/package/electron-tray-window)
[![npm version](https://img.shields.io/github/languages/code-size/sfatihk/electron-tray-window)](https://github.com/sfatihk/electron-tray-window)
[![npm version](https://img.shields.io/npm/dt/electron-tray-window)](https://github.com/sfatihk/electron-tray-window)
[![npm version](https://img.shields.io/github/issues/sfatihk/electron-tray-window)](https://github.com/sfatihk/electron-tray-window)
[![npm version](https://img.shields.io/github/license/sfatihk/electron-tray-window)](https://github.com/sfatihk/electron-tray-window)





<p align="center">
<img src="resources/icon.png" style="max-width:100%; width:300px"/>
</p>
<h2 align="center">Quickly create customizable  menu/pop-up for your application in system tray.</h2>

---

Electron Tray Window, basically places the window near the tray icon. While these happening, you can customize window / tray or tracking the events.

## Preview [demo project](https://github.com/sfatihk/electron-tray-window/tree/master/examples)

<img src="https://raw.githubusercontent.com/sfatihk/electron-tray-window/master/resources/showcase-short.gif" style="max-width:100%; width:300px"  /> <img src="https://raw.githubusercontent.com/sfatihk/electron-tray-window/master/resources/showcase.gif" style="max-width:100%; width:550px"/>

## Install

```
npm install electron-tray-window
```

or

```
yarn add electron-tray-window
```

## Usage

```js
const trayWindow = require("electron-tray-window");
```

You can use different ways. "setOptions()" function accepts object value.

If you have already created tray or window outside **TrayWindow**, you can pass as arguments to **.setOptions()** function,

```js
//...

tray = new Tray(...);
window = new BrowserWindow(...);
window.loadUrl(...);

trayWindow.setOptions({tray: tray,window: window});

//...
```

or if you pass just tray icon path or window url, it prepare automatically.

```js
//...

trayWindow.setOptions({
  trayIconPath: "...",
  windowUrl: "..."
});

//...
```

By the way you can make different combines. But object must contains;

- **tray** or **trayIconPath**
- **window** or **windowUrl**

```js

//...

tray = new Tray(...);
trayWindow.setOptions({
  tray: tray,
  windowUrl: "..."
});

//...

window = new BrowserWindow(...);

trayWindow.setOptions({
  trayIconPath: "...",
  window: window
});

//...
```

## Other Functions

You can always change **TrayWindow** with setOptions() and you can use different functions after setOptions().

## .setTray( tray )

```js
//...

trayWindow.setOptions({...});

//...

differentTray = new Tray(...);

trayWindow.setTray(differentTray); //now, follows different tray

//..
```

## .setWindow( window )

```js
//...

trayWindow.setOptions({...});

//...

differentWindow = new BrowserWindow(...);

trayWindow.setWindow(differentWindow); //now, shows different window

//..
```

## .setWindowSize( object )

```js
//...

trayWindow.setOptions({...});

//...

differentWindow = new BrowserWindow(...);

trayWindow.setWindowSize({
    width    : 200,    //optional
    height   : 300,   //optional
    margin_x : 10,  //optional
    margin_y : 10   //optional
});

//..
```

## Events

You can listen events. All event contains window and tray objects

```js
//...
const { ipcMain } = electron;

ipcMain.on("tray-window-ready", (e, a) => {
  console.log("tray window is ready");
  //console.log(e.window)
  //console.log(e.tray)
});

ipcMain.on("tray-window-clicked", (e, a) => {
  console.log("clicked the tray icon");
  //console.log(e.window)
  //console.log(e.tray)
});

ipcMain.on("tray-window-visible", (e, a) => {
  console.log("tray window is visible now");
  //console.log(e.window)
  //console.log(e.tray)
});

ipcMain.on("tray-window-hidden", (e, a) => {
  console.log("tray window is hidden now");
  //console.log(e.window)
  //console.log(e.tray)
});

//..
```

## Overview

### All parameters of setOptions()

| parameter    | description                                      | default |
| ------------ | ------------------------------------------------ | ------- |
| tray         | Electron's tray object type                      |         |
| trayIconPath | Image file path                                  |         |
| window       | Electron's BrowserWindow object type             |         |
| windowUrl    | Html etc. file path or website url               |         |
| width        | Window width                                     | 200px   |
| height       | Window height                                    | 200px   |
| margin_x     | Vertical distance between window and tray icon   | 0px     |
| margin_y     | Horizontal distance between window and tray icon | 0px     |
| framed       | Is it window framed?                             | false   |

### Other functions

| function      | description                                                       |
| ------------- | ----------------------------------------------------------------- |
| setTray       | Electron's tray object type.                                      |
| setWindow     | Electron's BrowserWindow object type                              |
| setWindowSize | Object type. Optional arguments width, height, margin_x, margin_y |

### IPC (Inter-Process Communication) Events

| event               | description              |
| ------------------- | ------------------------ |
| tray-window-ready   | when created TrayWindow. |
| tray-window-clicked | when clicked tray icon   |
| tray-window-visible | when window show up      |
| tray-window-hidden  | when window close down   |

### Default Window Properties

| properties                           | default in TrayWindow |
| ------------------------------------ | --------------------- |
| width                                | 200px                 |
| height                               | 300px                 |
| maxWidth                             | 200px                 |
| maxHeight                            | 300px                 |
| show                                 | false                 |
| frame                                | false                 |
| fullscreenable                       | false                 |
| resizable                            | false                 |
| useContentSize                       | true                  |
| transparent                          | true                  |
| alwaysOnTop                          | true                  |
| webPreferences{backgroundThrottling} | false                 |

P.S. : if you use this way `setOptions({windowUrl:"..."})`, default window values uses.


================================================
FILE: examples/README.MD
================================================
## install dependencies
```npm install```

## then run the project
```npm start```


================================================
FILE: examples/index.js
================================================
const TrayWindow = require("electron-tray-window");

const { ipcMain, Tray, app, BrowserWindow } = require("electron");
const path = require("path");

app.on("ready", () => {
  var timeout = 10;
  if (process.platform === "linux") {
    timeout = 200;
  }
  setTimeout(function () {
    TrayWindow.setOptions({
      trayIconPath: path.join("resources/icon.png"),
      windowUrl: `file://${path.join(__dirname, "resources/index.html")}`,
      width: 340,
      height: 380,
    });
  }, timeout);
});


================================================
FILE: examples/package.json
================================================
{
  "name": "examples",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron --enable-transparent-visuals ."
  },
  "author": "sfatihk <sakkarmen@gmail.com>",
  "license": "MIT",
  "dependencies": {
    "electron-tray-window": "^1.2.2"
  }
}


================================================
FILE: examples/resources/index.html
================================================
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Electron-Tray-Window Example</title>
  </head>
  <style>
    body {
      overflow: hidden;
      width: 341px;
    }
    #container {
      border-radius: 30px;
      border: 20px;
      border-color: #434344;
      border-style: solid;
      width: 290px;
      height: 330px;
      background-color: #cd970c;
    }
    #left-wrapper {
      width: 140px;
      margin-left: 10px;
      margin-top: 20px;
      float: left;
    }
    .small-box {
      border-radius: 7px;
      width: 40px;
      height: 40px;
      background-color: #434344;
      float: left;
      margin-left: 20px;
      margin-top: 20px;
    }
    #big-box {
      border-radius: 7px;
      background-color: #434344;
      float: left;
      width: 100px;
      height: 100px;
      margin-right: 20px;
      margin-top: 40px;
      color: #cd970c;
      font-weight: bolder;
      font-size: 18px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-family: "Segoe UI";
    }
    #wide-box {
      border-radius: 7px;
      background-color: #434344;
      float: left;
      width: 100px;
      height: 40px;
      margin-left: 30px;
      margin-top: 20px;
      color: #cd970c;
      font-weight: bolder;
      font-size: 18px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-family: "Segoe UI";
    }
    #center-wrapper {
      float: left;
      width: 120px;
    }
    #huge-box {
      border-radius: 7px;
      background-color: #434344;
      float: left;
      width: 220px;
      height: 80px;
      margin-left: 30px;
      margin-top: 20px;
      color: #cd970c;
      font-weight: bolder;
      font-size: 18px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-family: "Segoe UI";
    }
    .interactive {
      cursor: pointer !important;
    }
    .interactive:hover {
      background-color: #3c3c40 !important;
      color: #e9af1d !important;
    }
  </style>

  <body>
    <div id="container">
      <div id="left-wrapper">
        <div class="small-box"></div>
        <div class="small-box"></div>
        <div class="small-box"></div>
        <div class="small-box"></div>
      </div>
      <div id="big-box" class="interactive">ELECTRON</div>
      <div id="wide-box" class="interactive">TRAY</div>
      <div id="center-wrapper">
        <div class="small-box"></div>
        <div class="small-box"></div>
      </div>
      <div id="huge-box" class="interactive">WINDOWS</div>
    </div>
  </body>
</html>


================================================
FILE: package.json
================================================
{
  "name": "electron-tray-window",
  "version": "1.2.7",
  "description": "A package for generate custom tray windows with Electron.js",
  "main": "src/index.js",
  "files": [
    "src"
  ],
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/sfatihk/electron-tray-window.git"
  },
  "keywords": [
    "electron",
    "tray",
    "window",
    "popup",
    "menu"
  ],
  "author": "Seyyid Fatih KOÇ",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/sfatihk/electron-tray-window/issues"
  },
  "homepage": "https://github.com/sfatihk/electron-tray-window#readme",
  "dependencies": {
    "electron": "^3.0.10"
  },
  "peerDependencies": {
    "electron": "^3.0.10"
  }
}


================================================
FILE: src/index.js
================================================
const electron = require("electron");

const { BrowserWindow, ipcMain, Tray } = electron;

let tray = undefined;
let window = undefined;

//defaults
let width = 200;
let height = 300;

let margin_x = 0;
let margin_y = 0;
let framed = false;

function setOptions(options) {
  if (!validation(options)) return;

  init(options);
}

function validation(options) {
  if (typeof options !== "object") {
    console.log("tray-window can not create without any [options]!!!");
    return false;
  }
  if (!options.tray && !options.trayIconPath) {
    console.log(
      "tray-window can not create without [tray] or [trayIconPath] parameters!!!"
    );
    return false;
  }
  if (!options.window && !options.windowUrl) {
    console.log(
      "tray-window can not create without [window] or [windowUrl] parameters!!!"
    );
    return false;
  }

  return true;
}

function init(options) {
  setWindowSize(options);

  options.tray ? setTray(options.tray) : createTray(options.trayIconPath);
  options.window ? setWindow(options.window) : createWindow(options.windowUrl);

  tray.on("click", function(event) {
    ipcMain.emit("tray-window-clicked", { window: window, tray: tray });
    toggleWindow();
  });

  setWindowAutoHide();
  alignWindow();

  ipcMain.emit("tray-window-ready", { window: window, tray: tray });
}

function setWindowSize(options) {
  if (options.width) width = options.width;
  if (options.height) height = options.height;
  if (options.margin_x) margin_x = options.margin_x;
  if (options.margin_y) margin_y = options.margin_y;
  if (options.framed) framed = options.framed;
}

function createTray(trayIconPath) {
  tray = new Tray(trayIconPath);
}

function setTray(newTray) {
  tray = newTray;
}

function setWindow(newWindow) {
  window = newWindow;
  setWindowSize(window.getBounds());
}

function createWindow(windowUrl) {
  window = undefined;

  window = new BrowserWindow({
    width: width,
    height: height,
    maxWidth: width,
    maxHeight: height,
    show: false,
    frame: framed,
    fullscreenable: false,
    resizable: false,
    useContentSize: true,
    transparent: true,
    alwaysOnTop: true,
    webPreferences: {
      backgroundThrottling: false
    }
  });
  window.setMenu(null);

  setWindowUrl(windowUrl);

  return window;
}

function setWindowUrl(windowUrl) {
  window.loadURL(windowUrl);
}

function setWindowAutoHide() {
  window.hide();
  window.on("blur", () => {
    if (!window.webContents.isDevToolsOpened()) {
      window.hide();
      ipcMain.emit("tray-window-hidden", { window: window, tray: tray });
    }
  });
  if (framed) {
    window.on("close", function(event) {
      event.preventDefault();
      window.hide();
    });
  }
}

function toggleWindow() {
  if (window.isVisible()) {
    window.hide();
    ipcMain.emit("tray-window-hidden", { window: window, tray: tray });
    return;
  }

  showWindow();
  ipcMain.emit("tray-window-visible", { window: window, tray: tray });
}

function alignWindow() {
  const position = calculateWindowPosition();
  window.setBounds({
    width: width,
    height: height,
    x: position.x,
    y: position.y
  });
}

function showWindow() {
  alignWindow();
  window.show();
}

function calculateWindowPosition() {
  const screenBounds = electron.screen.getPrimaryDisplay().size;
  const trayBounds = tray.getBounds();

  //where is the icon on the screen?
  let trayPos = 4; // 1:top-left 2:top-right 3:bottom-left 4.bottom-right
  trayPos = trayBounds.y > screenBounds.height / 2 ? trayPos : trayPos / 2;
  trayPos = trayBounds.x > screenBounds.width / 2 ? trayPos : trayPos - 1;

  let DEFAULT_MARGIN = { x: margin_x, y: margin_y };

  //calculate the new window position
  switch (trayPos) {
    case 1: // for TOP - LEFT
      x = Math.floor(trayBounds.x + DEFAULT_MARGIN.x + trayBounds.width / 2);
      y = Math.floor(trayBounds.y + DEFAULT_MARGIN.y + trayBounds.height / 2);
      break;

    case 2: // for TOP - RIGHT
      x = Math.floor(
        trayBounds.x - width - DEFAULT_MARGIN.x + trayBounds.width / 2
      );
      y = Math.floor(trayBounds.y + DEFAULT_MARGIN.y + trayBounds.height / 2);
      break;

    case 3: // for BOTTOM - LEFT
      x = Math.floor(trayBounds.x + DEFAULT_MARGIN.x + trayBounds.width / 2);
      y = Math.floor(
        trayBounds.y - height - DEFAULT_MARGIN.y + trayBounds.height / 2
      );
      break;

    case 4: // for BOTTOM - RIGHT
      x = Math.floor(
        trayBounds.x - width - DEFAULT_MARGIN.x + trayBounds.width / 2
      );
      y = Math.floor(
        trayBounds.y - height - DEFAULT_MARGIN.y + trayBounds.height / 2
      );
      break;
  }

  return { x: x, y: y };
}

module.exports = { setOptions, setTray, setWindow, setWindowSize };
Download .txt
gitextract_s_3v73pl/

├── .gitignore
├── LICENSE
├── README.MD
├── examples/
│   ├── README.MD
│   ├── index.js
│   ├── package.json
│   └── resources/
│       └── index.html
├── package.json
└── src/
    └── index.js
Download .txt
SYMBOL INDEX (14 symbols across 1 files)

FILE: src/index.js
  function setOptions (line 16) | function setOptions(options) {
  function validation (line 22) | function validation(options) {
  function init (line 43) | function init(options) {
  function setWindowSize (line 60) | function setWindowSize(options) {
  function createTray (line 68) | function createTray(trayIconPath) {
  function setTray (line 72) | function setTray(newTray) {
  function setWindow (line 76) | function setWindow(newWindow) {
  function createWindow (line 81) | function createWindow(windowUrl) {
  function setWindowUrl (line 107) | function setWindowUrl(windowUrl) {
  function setWindowAutoHide (line 111) | function setWindowAutoHide() {
  function toggleWindow (line 127) | function toggleWindow() {
  function alignWindow (line 138) | function alignWindow() {
  function showWindow (line 148) | function showWindow() {
  function calculateWindowPosition (line 153) | function calculateWindowPosition() {
Condensed preview — 9 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (18K chars).
[
  {
    "path": ".gitignore",
    "chars": 13,
    "preview": "node_modules\n"
  },
  {
    "path": "LICENSE",
    "chars": 1073,
    "preview": "MIT License\n\nCopyright (c) 2018 Seyyid Fatih KOÇ\n\nPermission is hereby granted, free of charge, to any person obtaining "
  },
  {
    "path": "README.MD",
    "chars": 6692,
    "preview": "[![npm version](https://badge.fury.io/js/electron-tray-window.svg)](https://www.npmjs.com/package/electron-tray-window)\n"
  },
  {
    "path": "examples/README.MD",
    "chars": 83,
    "preview": "## install dependencies\n```npm install```\n\n## then run the project\n```npm start```\n"
  },
  {
    "path": "examples/index.js",
    "chars": 503,
    "preview": "const TrayWindow = require(\"electron-tray-window\");\n\nconst { ipcMain, Tray, app, BrowserWindow } = require(\"electron\");\n"
  },
  {
    "path": "examples/package.json",
    "chars": 351,
    "preview": "{\n  \"name\": \"examples\",\n  \"version\": \"1.0.0\",\n  \"description\": \"\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"test\": \"ech"
  },
  {
    "path": "examples/resources/index.html",
    "chars": 2616,
    "preview": "<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"utf-8\" />\n    <title>Electron-Tray-Window Example</title>\n "
  },
  {
    "path": "package.json",
    "chars": 784,
    "preview": "{\n  \"name\": \"electron-tray-window\",\n  \"version\": \"1.2.7\",\n  \"description\": \"A package for generate custom tray windows w"
  },
  {
    "path": "src/index.js",
    "chars": 4724,
    "preview": "const electron = require(\"electron\");\n\nconst { BrowserWindow, ipcMain, Tray } = electron;\n\nlet tray = undefined;\nlet win"
  }
]

About this extraction

This page contains the full source code of the sfatihk/electron-tray-window GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 9 files (16.4 KB), approximately 4.4k tokens, and a symbol index with 14 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!