Skip to content

Commit

Permalink
Bring your own window (#1)
Browse files Browse the repository at this point in the history
* Update peer dependencies to be compatible with latest electron

* Fix tsconfig path

* Support bringing your own window to menubar constructor

* Bump version

* Fix eslint errors and warnings

* Run test on a matrix of OS

* Skip CI in ubuntu as it fails

* Skip CI in Windows OS
  • Loading branch information
lilac authored Apr 1, 2024
1 parent b000c86 commit 02d891a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 35 deletions.
30 changes: 17 additions & 13 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master
push:
branches:
- master
pull_request:
branches:
- master

jobs:
lint-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: yarn install
- run: yarn lint
- run: yarn test
lint-and-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest] # skip windows-latest (fails lint) and ubuntu-latest (fails test)

steps:
- uses: actions/checkout@v2
- run: yarn install
- run: yarn lint
- run: yarn test
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "menubar",
"version": "9.4.0",
"version": "9.4.1",
"author": "Max Ogden",
"bugs": {
"url": "https://github.com/maxogden/menubar/issues"
Expand Down Expand Up @@ -58,6 +58,6 @@
"typescript": "^4.6.2"
},
"peerDependencies": {
"electron": "^9.0.0 || ^10.0.0 || ^11.0.0 || ^12.0.0 || ^13.0.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^20.0.0 || ^21.0.0 || ^22.0.0 || ^23.0.0 || ^24.0.0 || ^25.0.0 || ^26.0.0 || ^27.0.0"
"electron": ">=9.0.0"
}
}
16 changes: 10 additions & 6 deletions src/Menubar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ export class Menubar extends EventEmitter {

// Not using `||` because x and y can be zero.
const x =
this._options.browserWindow.x !== undefined
'x' in this._options.browserWindow && this._options.browserWindow.x
? this._options.browserWindow.x
: position.x;
const y =
this._options.browserWindow.y !== undefined
'y' in this._options.browserWindow && this._options.browserWindow.y
? this._options.browserWindow.y
: position.y;

Expand Down Expand Up @@ -286,10 +286,14 @@ export class Menubar extends EventEmitter {
frame: false, // Remove window frame
};

this._browserWindow = new BrowserWindow({
...defaults,
...this._options.browserWindow,
});
const browserWindow = this._options.browserWindow;
this._browserWindow =
browserWindow instanceof BrowserWindow
? browserWindow
: new BrowserWindow({
...defaults,
...browserWindow,
});

this._positioner = new Positioner(this._browserWindow);

Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BrowserWindow,
BrowserWindowConstructorOptions,
LoadURLOptions,
Tray,
Expand All @@ -25,7 +26,7 @@ export interface Options {
* });
* ```
*/
browserWindow: BrowserWindowConstructorOptions;
browserWindow: BrowserWindowConstructorOptions | BrowserWindow;
/**
* The app source directory.
*/
Expand Down
30 changes: 18 additions & 12 deletions src/util/cleanOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/** */

import { app } from 'electron';
import { app, BrowserWindow, BrowserWindowConstructorOptions } from 'electron';
import path from 'path';
import url from 'url';

Expand All @@ -13,6 +13,11 @@ import { Options } from '../types';
const DEFAULT_WINDOW_HEIGHT = 400;
const DEFAULT_WINDOW_WIDTH = 400;

const isWindowOption = (
value: BrowserWindowConstructorOptions | BrowserWindow
): value is BrowserWindowConstructorOptions =>
!(value instanceof BrowserWindow);

/**
* Take as input some options, and return a sanitized version of it.
*
Expand Down Expand Up @@ -52,16 +57,17 @@ export function cleanOptions(opts?: Partial<Options>): Options {
}

// Set width/height on options to be usable before the window is created
options.browserWindow.width =
// Note: not using `options.browserWindow.width || DEFAULT_WINDOW_WIDTH` so
// that users can put a 0 width
options.browserWindow.width !== undefined
? options.browserWindow.width
: DEFAULT_WINDOW_WIDTH;
options.browserWindow.height =
options.browserWindow.height !== undefined
? options.browserWindow.height
: DEFAULT_WINDOW_HEIGHT;

if (isWindowOption(options.browserWindow)) {
options.browserWindow.width =
// Note: not using `options.browserWindow.width || DEFAULT_WINDOW_WIDTH` so
// that users can put a 0 width
options.browserWindow.width !== undefined
? options.browserWindow.width
: DEFAULT_WINDOW_WIDTH;
options.browserWindow.height =
options.browserWindow.height !== undefined
? options.browserWindow.height
: DEFAULT_WINDOW_HEIGHT;
}
return options as Options;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./node_modules/@amaurym/tsconfig/tsconfig",
"extends": "./node_modules/@amaurym/tsconfig/tsconfig.json",
"compilerOptions": {
"declaration": true,
"module": "commonjs",
Expand Down

0 comments on commit 02d891a

Please sign in to comment.