Skip to content

Commit

Permalink
feat: no customized window id
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanwww committed Aug 12, 2023
1 parent c4dd049 commit 2e0ec83
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
2 changes: 1 addition & 1 deletion packages/app-common/src/apis/app/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum AppAPIChannel {
GET_APP_DETAILS = 'App_GetAppDetails',
}

type CreateWindowAPI = IpcRendererInvokerAPI<(windowType: WindowType) => void>;
type CreateWindowAPI = IpcRendererInvokerAPI<(type: WindowType) => void>;
type GetAppDetails = IpcRendererInvokerAPI<() => AppDetails>;

export interface AppMainAPI {
Expand Down
4 changes: 2 additions & 2 deletions packages/app-main/src/main/apis/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { AppAPIChannel } from '@ter/app-common/apis/app';
import { ipcMain } from 'electron';

import { getAppDetails } from 'src/main/app';
import { windowManager } from 'src/main/window';
import { WindowManager } from 'src/main/window';

function registerCreateWindowHandler() {
const handler: AppMainAPI['handleCreateWindow'] = (_, windowType) => windowManager.createWindow({ windowType });
const handler: AppMainAPI['handleCreateWindow'] = (_, type) => WindowManager.INSTANCE.createWindow({ type });
ipcMain.handle(AppAPIChannel.CREATE_WINDOW, handler);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/app-main/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WindowType } from '@ter/app-common/apis/app';
import { app, BrowserWindow } from 'electron';

import { registerAppGlobalHandlers } from './apis/app';
import { windowManager } from './window';
import { WindowManager } from './window';

async function installExtensions(): Promise<void> {
const {
Expand All @@ -29,7 +29,7 @@ async function handleReady() {

registerAppGlobalHandlers();

windowManager.createWindow({ windowType: WindowType.MAIN });
WindowManager.INSTANCE.createWindow({ type: WindowType.MAIN });
}

// This method will be called when Electron has finished initialization and is ready to create browser windows.
Expand All @@ -48,6 +48,6 @@ app.on('activate', () => {
// On macOS, usually applications will re-create new windows if single click the dock icon when no other windows
// opened.
if (BrowserWindow.getAllWindows().length === 0) {
windowManager.createWindow({ windowType: WindowType.MAIN });
WindowManager.INSTANCE.createWindow({ type: WindowType.MAIN });
}
});
12 changes: 7 additions & 5 deletions packages/app-main/src/main/window/abstractWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import type { AbstractWindowOption, CloseWindowOption } from './types';
export abstract class AbstractWindow {
protected declare readonly _window: BrowserWindow;
protected declare readonly _windowType: WindowType;
protected declare readonly _windowId: string;

private declare readonly _onClose: (option: CloseWindowOption) => void | Promise<void>;

constructor(option: AbstractWindowOption) {
this._windowId = option.windowId;
this._windowType = option.windowType;
this._windowType = option.type;

const windowStateKeeper = new WindowStateKeeper(option.windowType);
const windowStateKeeper = new WindowStateKeeper(this._windowType);

this._window = new BrowserWindow({
x: windowStateKeeper.x,
Expand All @@ -42,6 +40,10 @@ export abstract class AbstractWindow {
this._addAPIHandlers();
}

get id() {
return this._window.id;
}

async show(): Promise<void> {
if (process.env.NODE_ENV === 'production') {
await this._window.loadFile(path.resolve(appPaths.src, 'index.html'));
Expand All @@ -66,7 +68,7 @@ export abstract class AbstractWindow {
private _close = () => {
this._removeAPIHandlers();

void this._onClose({ windowId: this._windowId });
void this._onClose({ id: this.id });
};

// ---------------------------------------------------------------------------------------------------- Ipc Handlers
Expand Down
2 changes: 1 addition & 1 deletion packages/app-main/src/main/window/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import type { WindowOption } from './types';

export class MainWindow extends AbstractWindow {
constructor(option: WindowOption) {
super({ windowType: WindowType.MAIN, ...option });
super({ type: WindowType.MAIN, ...option });
}
}
7 changes: 3 additions & 4 deletions packages/app-main/src/main/window/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import type { WindowType } from '@ter/app-common/apis/app';

export interface CreateWindowOption {
windowType: WindowType;
type: WindowType;
}

export interface CloseWindowOption {
windowId: string;
id: number;
}

export interface WindowOption {
windowId: string;
onClose: (option: CloseWindowOption) => void | Promise<void>;
}

export interface AbstractWindowOption extends WindowOption {
windowType: WindowType;
type: WindowType;
}
47 changes: 22 additions & 25 deletions packages/app-main/src/main/window/windowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,44 @@ import type { AbstractWindow } from './abstractWindow';
import { MainWindow } from './mainWindow';
import type { CloseWindowOption, CreateWindowOption } from './types';

interface WindowStore {
[id: string]: Nullable<AbstractWindow>;
}
type WindowStore = Record<number, Nullable<AbstractWindow>>;

export class WindowManager {
private declare _count: number;
private declare _store: WindowStore;
private declare static _instance?: WindowManager;

constructor() {
this._count = 0;
this._store = {};
static get INSTANCE() {
if (!WindowManager._instance) {
WindowManager._instance = new WindowManager();
}
return WindowManager._instance;
}

createWindow = (option: CreateWindowOption): void => {
this._count++;
private declare _store: WindowStore;

const { windowType } = option;
const windowId = `${windowType}-${this._count}`;
private constructor() {
this._store = {};
}

switch (windowType) {
private _newWindow(type: WindowType): AbstractWindow {
switch (type) {
case WindowType.MAIN:
this._store[windowId] = new MainWindow({
windowId,
return new MainWindow({
onClose: this._closeWindow,
});
break;

/* istanbul ignore next */
default:
assertIsNever(windowType);
assertIsNever(type);
}
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
void this._store[windowId]!.show();
};
createWindow(option: CreateWindowOption): void {
const window = this._newWindow(option.type);
this._store[window.id] = window;
void window.show();
}

private _closeWindow = (option: CloseWindowOption): void => {
const { windowId } = option;

this._store[windowId] = null;
this._store[option.id] = null;
};
}

export const windowManager = new WindowManager();

0 comments on commit 2e0ec83

Please sign in to comment.