UNPKG

8.59 kBMarkdownView Raw
1![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/max-mapper/menubar/test.yml)
2[![npm (scoped)](https://img.shields.io/npm/v/menubar.svg)](https://www.npmjs.com/package/menubar)
3![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/max-mapper/menubar)
4![npm bundle size](https://img.shields.io/bundlephobia/minzip/menubar.svg)
5![npm bundle size](https://img.shields.io/bundlephobia/min/menubar.svg)
6
7<br /><br /><br />
8
9<h1 align="center">➖ Menubar</h1>
10<h4 align="center">High level way to create menubar desktop applications with Electron.</h4>
11
12<br /><br /><br />
13
14This module provides boilerplate for setting up a menubar application using Electron. All you have to do is point it at your `index.html` and `menubar` will handle the rest.
15
16✅ Only one dependency, and one peer-dependency.
17
18✅ Works on macOS, Windows and most Linuxes. See [details](./WORKING_PLATFORMS.md).
19
20✅ 💥 [**3.6kB minified + gzipped**](https://bundlephobia.com/result?p=menubar) 💥
21
22| <img src="assets/screenshot-macos-dark.png" height="250px" /> | <img src="assets/screenshot-windows.png" height="250px" /> | <img src="assets/screenshot-linux.png" height="250px" /> |
23| :-----------------------------------------------------------: | :--------------------------------------------------------: | :------------------------------------------------------: |
24| macOS Mojave 10.14 | Windows 10 | Ubuntu 18.04 |
25
26## Installation
27
28```bash
29yarn add menubar
30```
31
32## Usage
33
34Starting with your own new project, run these commands:
35
36```bash
37$ yarn add menubar
38$ touch myApp.js
39$ touch index.html
40```
41
42Fill `index.html` with some HTML, and `myApp.js` like this:
43
44```javascript
45const { menubar } = require('menubar');
46
47const mb = menubar();
48
49mb.on('ready', () => {
50 console.log('app is ready');
51 // your app code here
52});
53```
54
55Then use `electron` to run the app:
56
57```bash
58$ electron myApp.js
59```
60
61Alternatively, see [`examples/hello-world`](/examples/hello-world) folder for a simple working example.
62
63## `Menubar` Class
64
65The return value of `menubar()` is a `Menubar` class instance, which has these properties:
66
67- `app`: the [Electron App](https://electronjs.org/docs/api/app) instance,
68- `window`: the [Electron Browser Window](https://electronjs.org/docs/api/browser-window) instance,
69- `tray`: the [Electron Tray](https://electronjs.org/docs/api/tray) instance,
70- `positioner`: the [Electron Positioner](https://github.com/jenslind/electron-positioner) instance,
71- `setOption(option, value)`: change an option after menubar is created,
72- `getOption(option)`: get an menubar option,
73- `showWindow()`: show the menubar window,
74- `hideWindow()`: hide the menubar window
75
76See the reference [API docs](./docs/classes/_menubar_.menubar.md).
77
78## `menubar()` Options
79
80You can pass an optional options object into the `menubar({ ... })` function:
81
82- `dir` (default `process.cwd()`) - the app source directory
83- `index` (default `file:// + opts.dir + index.html`) - The URL to load the menubar's browserWindow with. The url can be a remote address (e.g. `http://`) or a path to a local HTML file using the `file://` protocol.
84- `browserWindow` - BrowserWindow options to be passed to the BrowserWindow constructor, see [Electron docs](https://electronjs.org/docs/api/browser-window#new-browserwindowoptions). Some interesting fields to passed down are:
85 - `x` (default `undefined`) - the x position of the window
86 - `y` (default `undefined`) - the y position of the window
87 - `width` (default 400) - window width
88 - `height` (default 400) - window height
89 - `alwaysOnTop` (default false) - if true, the window will not hide on blur
90- `icon` (default `opts.dir + IconTemplate.png`) - the png icon to use for the menubar. A good size to start with is 20x20. To support retina, supply a 2x sized image (e.g. 40x40) with `@2x` added to the end of the name, so `icon.png` and `icon@2x.png` and Electron will automatically use your `@2x` version on retina screens.
91- `tooltip` (default empty) - menubar tray icon tooltip text
92- `tray` (default created on-the-fly) - an electron `Tray` instance. if provided `opts.icon` will be ignored
93- `preloadWindow` (default false) - Create [BrowserWindow](https://electronjs.org/docs/api/browser-window#new-browserwindowoptions) instance before it is used -- increasing resource usage, but making the click on the menubar load faster.
94- `loadUrlOptions` - (default undefined) The options passed when loading the index URL in the menubar's browserWindow. Everything browserWindow.loadURL supports is supported; this object is simply passed onto [browserWindow.loadURL](https://electronjs.org/docs/api/browser-window#winloadurlurl-options)
95- `showOnAllWorkspaces` (default true) - Makes the window available on all OS X workspaces.
96- `windowPosition` (default trayCenter and trayBottomCenter on Windows) - Sets the window position (x and y will still override this), check [positioner docs](https://github.com/jenslind/electron-positioner#docs) for valid values.
97- `showDockIcon` (default false) - Configure the visibility of the application dock icon.
98- `showOnRightClick` (default false) - Show the window on 'right-click' event instead of regular 'click'
99
100See the reference [API docs](./docs/interfaces/_types_.options.md).
101
102## Events
103
104The `Menubar` class is an event emitter:
105
106- `ready` - when `menubar`'s tray icon has been created and initialized, i.e. when `menubar` is ready to be used. Note: this is different than Electron app's `ready` event, which happens much earlier in the process
107- `create-window` - the line before `new BrowserWindow()` is called
108- `before-load` - after create window, before loadUrl (can be used for `require("@electron/remote/main").enable(webContents)`)
109- `after-create-window` - the line after all window init code is done and url was loaded
110- `show` - the line before `window.show()` is called
111- `after-show` - the line after `window.show()` is called
112- `hide` - the line before `window.hide()` is called (on window blur)
113- `after-hide` - the line after `window.hide()` is called
114- `after-close` - after the `.window` (BrowserWindow) property has been deleted
115- `focus-lost` - emitted if always-on-top option is set and the user clicks away
116
117## Compatibility with Electron
118
119| menubar | Electron | Notes |
120| -------- | -------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
121| 9.x.x | >=9.x.x <33.x.x | |
122| 8.x.x | 8.x.xx | |
123| 7.x.x | 7.x.xx | |
124| 6.x.x | >=4.x.x <7.x.x | Not recommended for [security reasons](https://electronjs.org/docs/tutorial/security#17-use-a-current-version-of-electron) |
125| <= 5.x.x | <= 3.x.x | Please, _please_ don't use these old versions |
126
127## API Docs
128
129See the reference [API docs](./docs/globals.md).
130
131## Tips
132
133- Use `mb.on('after-create-window', callback)` to run things after your app has loaded. For example you could run `mb.window.openDevTools()` to open the developer tools for debugging, or load a different URL with `mb.window.loadURL()`
134- Use `mb.on('focus-lost')` if you would like to perform some operation when using the option `browserWindow.alwaysOnTop: true`
135- To restore focus of previous window after menubar hide, use `mb.on('after-hide', () => { mb.app.hide() } )` or similar
136- To create a native menu, you can use `tray.setContextMenu(contextMenu)`, and pass this custom tray to menubar: `const mb = menubar({ tray });`. See [this example](https://github.com/maxogden/menubar/tree/master/examples/native-menu) for more information.
137- To avoid a flash when opening your menubar app, you can disable backgrounding the app using the following: `mb.app.commandLine.appendSwitch('disable-backgrounding-occluded-windows', 'true');`