UNPKG

@minimaltech/electron-infra

Version:

Minimal Technology ElectronJS Infrastructure

153 lines 7.01 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WindowManager = void 0; const node_infra_1 = require("@minimaltech/node-infra"); const models_1 = require("../models"); const electron_1 = require("electron"); const rxjs_1 = require("rxjs"); class WindowManager extends node_infra_1.BaseService { constructor() { super({ scope: WindowManager.name }); this.container = new Map(); } // ----------------------------------------------------------------------------------- static getInstance() { if (!WindowManager.instance) { WindowManager.instance = new WindowManager(); } return WindowManager.instance; } // ----------------------------------------------------------------------------------- getContainer() { return this.container; } // ----------------------------------------------------------------------------------- open(opts) { return __awaiter(this, void 0, void 0, function* () { const { name, useDevTool = false, onClose, onClosed, onReadyToShow, onShow, onMove, onResize, onBlur, onFocus, } = opts; if (opts.identifier && this.container.has(opts.identifier)) { throw (0, node_infra_1.getError)({ statusCode: node_infra_1.ResultCodes.RS_4.BadRequest, message: `[open] Identifier: ${opts.identifier} | Invalid window identifier | Existed in container`, }); } const window = new models_1.BrowserWindow(opts); const identifier = window.getIdentifier(); this.container.set(identifier, { window, options: opts }); if (opts.menuFactory) { const menu = yield opts.menuFactory.getMenu(window); electron_1.Menu.setApplicationMenu(menu); } this.logger.info('[open] Identifier: %s | Name: %s | Window CREATED', identifier, name); // -------------------------------------------------- const subscriptionResize = (0, rxjs_1.fromEvent)(window, 'resize') .pipe((0, rxjs_1.debounceTime)(1000)) .subscribe(() => { onResize === null || onResize === void 0 ? void 0 : onResize(window); }); const subscriptionMove = (0, rxjs_1.fromEvent)(window, 'move') .pipe((0, rxjs_1.debounceTime)(1000)) .subscribe(() => { onMove === null || onMove === void 0 ? void 0 : onMove(window); }); // -------------------------------------------------- window.on('ready-to-show', () => { onReadyToShow === null || onReadyToShow === void 0 ? void 0 : onReadyToShow(window); window.show(); }); window.on('show', () => { onShow === null || onShow === void 0 ? void 0 : onShow(window); }); // -------------------------------------------------- window.on('closed', () => { onClosed === null || onClosed === void 0 ? void 0 : onClosed(window); if (!this.container.has(identifier)) { return; } subscriptionResize.unsubscribe(); subscriptionMove.unsubscribe(); this.container.delete(identifier); }); window.on('close', event => { onClose === null || onClose === void 0 ? void 0 : onClose({ window, event }); }); // ----------------------------------------------------------------------------------- window.on('focus', () => { onFocus === null || onFocus === void 0 ? void 0 : onFocus(window); }); window.on('blur', () => { onBlur === null || onBlur === void 0 ? void 0 : onBlur(window); }); if (useDevTool) { window.webContents.toggleDevTools(); } return window; }); } // ----------------------------------------------------------------------------------- getWindowByIdentifier(identifier) { var _a, _b; return (_b = (_a = this.container.get(identifier)) === null || _a === void 0 ? void 0 : _a.window) !== null && _b !== void 0 ? _b : null; } // ----------------------------------------------------------------------------------- getWindows(opts) { const { identifier, name } = opts; const rs = []; if (!identifier && !name) { for (const el of this.container.values()) { rs.push(el.window); } return rs; } if (identifier && this.container.has(identifier)) { const el = this.container.get(identifier); if (el === null || el === void 0 ? void 0 : el.window) { rs.push(el.window); } } if (name) { for (const [_, v] of this.container) { const windowName = v.window.getName(); if (name !== windowName) { continue; } rs.push(v.window); } } return rs; } // ----------------------------------------------------------------------------------- getAll() { return Array.from(this.container.values()).map(v => v.window); } // ----------------------------------------------------------------------------------- closeByIdentifier(identifier) { var _a; const window = (_a = this.container.get(identifier)) === null || _a === void 0 ? void 0 : _a.window; if (window) { window.close(); } } // ----------------------------------------------------------------------------------- closeWindows(opts) { const { identifier, name } = opts; this.getWindows({ identifier, name }).forEach(w => w.close()); } // ----------------------------------------------------------------------------------- closeAll() { for (const el of this.container.values()) { el.window.close(); } } } exports.WindowManager = WindowManager; //# sourceMappingURL=window-manager.service.js.map