From 02d891a043a0d04849a287b99319058721dbb5bd Mon Sep 17 00:00:00 2001 From: lilac Date: Mon, 1 Apr 2024 23:05:10 +0800 Subject: [PATCH] Bring your own window (#1) * 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 --- .github/workflows/pr.yml | 30 +++++++++++++++++------------- package.json | 4 ++-- src/Menubar.ts | 16 ++++++++++------ src/types.ts | 3 ++- src/util/cleanOptions.ts | 30 ++++++++++++++++++------------ tsconfig.json | 2 +- 6 files changed, 50 insertions(+), 35 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index a111f98..ed95f73 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -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 diff --git a/package.json b/package.json index 292af19..ff6ea37 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" } } diff --git a/src/Menubar.ts b/src/Menubar.ts index b7c555a..6fbef86 100644 --- a/src/Menubar.ts +++ b/src/Menubar.ts @@ -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; @@ -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); diff --git a/src/types.ts b/src/types.ts index 5028fe5..f6a4399 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ import { + BrowserWindow, BrowserWindowConstructorOptions, LoadURLOptions, Tray, @@ -25,7 +26,7 @@ export interface Options { * }); * ``` */ - browserWindow: BrowserWindowConstructorOptions; + browserWindow: BrowserWindowConstructorOptions | BrowserWindow; /** * The app source directory. */ diff --git a/src/util/cleanOptions.ts b/src/util/cleanOptions.ts index 6d2c43b..da9599c 100644 --- a/src/util/cleanOptions.ts +++ b/src/util/cleanOptions.ts @@ -4,7 +4,7 @@ /** */ -import { app } from 'electron'; +import { app, BrowserWindow, BrowserWindowConstructorOptions } from 'electron'; import path from 'path'; import url from 'url'; @@ -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. * @@ -52,16 +57,17 @@ export function cleanOptions(opts?: Partial): 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; } diff --git a/tsconfig.json b/tsconfig.json index 19e6790..4594f94 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "./node_modules/@amaurym/tsconfig/tsconfig", + "extends": "./node_modules/@amaurym/tsconfig/tsconfig.json", "compilerOptions": { "declaration": true, "module": "commonjs",