Migrate to private class properties/methods

This commit is contained in:
Jamie Kyle 2025-01-14 11:11:52 -08:00 committed by GitHub
commit aa9f53df57
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
100 changed files with 3795 additions and 3944 deletions

View file

@ -24,29 +24,20 @@ export type SystemTrayServiceOptionsType = Readonly<{
* [0]: https://www.electronjs.org/docs/api/tray
*/
export class SystemTrayService {
private browserWindow?: BrowserWindow;
private readonly i18n: LocalizerType;
private tray?: Tray;
private isEnabled = false;
private isQuitting = false;
private unreadCount = 0;
private boundRender: typeof SystemTrayService.prototype.render;
private createTrayInstance: (icon: NativeImage) => Tray;
#browserWindow?: BrowserWindow;
readonly #i18n: LocalizerType;
#tray?: Tray;
#isEnabled = false;
#isQuitting = false;
#unreadCount = 0;
#createTrayInstance: (icon: NativeImage) => Tray;
constructor({ i18n, createTrayInstance }: SystemTrayServiceOptionsType) {
log.info('System tray service: created');
this.i18n = i18n;
this.boundRender = this.render.bind(this);
this.createTrayInstance = createTrayInstance || (icon => new Tray(icon));
this.#i18n = i18n;
this.#createTrayInstance = createTrayInstance || (icon => new Tray(icon));
nativeTheme.on('updated', this.boundRender);
nativeTheme.on('updated', this.#render);
}
/**
@ -55,7 +46,7 @@ export class SystemTrayService {
* toggle in the tray's context menu.
*/
setMainWindow(newBrowserWindow: undefined | BrowserWindow): void {
const oldBrowserWindow = this.browserWindow;
const oldBrowserWindow = this.#browserWindow;
if (oldBrowserWindow === newBrowserWindow) {
return;
}
@ -67,18 +58,18 @@ export class SystemTrayService {
);
if (oldBrowserWindow) {
oldBrowserWindow.off('show', this.boundRender);
oldBrowserWindow.off('hide', this.boundRender);
oldBrowserWindow.off('show', this.#render);
oldBrowserWindow.off('hide', this.#render);
}
if (newBrowserWindow) {
newBrowserWindow.on('show', this.boundRender);
newBrowserWindow.on('hide', this.boundRender);
newBrowserWindow.on('show', this.#render);
newBrowserWindow.on('hide', this.#render);
}
this.browserWindow = newBrowserWindow;
this.#browserWindow = newBrowserWindow;
this.render();
this.#render();
}
/**
@ -86,27 +77,27 @@ export class SystemTrayService {
* `setMainWindow`), the tray icon will not be shown, even if enabled.
*/
setEnabled(isEnabled: boolean): void {
if (this.isEnabled === isEnabled) {
if (this.#isEnabled === isEnabled) {
return;
}
log.info(`System tray service: ${isEnabled ? 'enabling' : 'disabling'}`);
this.isEnabled = isEnabled;
this.#isEnabled = isEnabled;
this.render();
this.#render();
}
/**
* Update the unread count, which updates the tray icon if it's visible.
*/
setUnreadCount(unreadCount: number): void {
if (this.unreadCount === unreadCount) {
if (this.#unreadCount === unreadCount) {
return;
}
log.info(`System tray service: setting unread count to ${unreadCount}`);
this.unreadCount = unreadCount;
this.render();
this.#unreadCount = unreadCount;
this.#render();
}
/**
@ -118,35 +109,36 @@ export class SystemTrayService {
markShouldQuit(): void {
log.info('System tray service: markShouldQuit');
this.tray = undefined;
this.isQuitting = true;
this.#tray = undefined;
this.#isQuitting = true;
}
isVisible(): boolean {
return this.tray !== undefined;
return this.#tray !== undefined;
}
private render(): void {
if (this.isEnabled && this.browserWindow) {
this.renderEnabled();
#render = (): void => {
if (this.#isEnabled && this.#browserWindow) {
this.#renderEnabled();
return;
}
this.renderDisabled();
}
this.#renderDisabled();
};
private renderEnabled() {
if (this.isQuitting) {
#renderEnabled() {
if (this.#isQuitting) {
log.info('System tray service: not rendering the tray, quitting');
return;
}
log.info('System tray service: rendering the tray');
this.tray ??= this.createTray();
const { browserWindow, tray } = this;
this.#tray ??= this.#createTray();
const tray = this.#tray;
const browserWindow = this.#browserWindow;
try {
tray.setImage(getIcon(this.unreadCount));
tray.setImage(getIcon(this.#unreadCount));
} catch (err: unknown) {
log.warn(
'System tray service: failed to set preferred image. Falling back...'
@ -164,7 +156,7 @@ export class SystemTrayService {
id: 'toggleWindowVisibility',
...(browserWindow?.isVisible()
? {
label: this.i18n('icu:hide'),
label: this.#i18n('icu:hide'),
click: () => {
log.info(
'System tray service: hiding the window from the context menu'
@ -172,25 +164,25 @@ export class SystemTrayService {
// We re-fetch `this.browserWindow` here just in case the browser window
// has changed while the context menu was open. Same applies in the
// "show" case below.
this.browserWindow?.hide();
this.#browserWindow?.hide();
},
}
: {
label: this.i18n('icu:show'),
label: this.#i18n('icu:show'),
click: () => {
log.info(
'System tray service: showing the window from the context menu'
);
if (this.browserWindow) {
this.browserWindow.show();
focusAndForceToTop(this.browserWindow);
if (this.#browserWindow) {
this.#browserWindow.show();
focusAndForceToTop(this.#browserWindow);
}
},
}),
},
{
id: 'quit',
label: this.i18n('icu:quit'),
label: this.#i18n('icu:quit'),
click: () => {
log.info(
'System tray service: quitting the app from the context menu'
@ -202,21 +194,21 @@ export class SystemTrayService {
);
}
private renderDisabled() {
#renderDisabled() {
log.info('System tray service: rendering no tray');
if (!this.tray) {
if (!this.#tray) {
return;
}
this.tray.destroy();
this.tray = undefined;
this.#tray.destroy();
this.#tray = undefined;
}
private createTray(): Tray {
#createTray(): Tray {
log.info('System tray service: creating the tray');
// This icon may be swiftly overwritten.
const result = this.createTrayInstance(getDefaultIcon());
const result = this.#createTrayInstance(getDefaultIcon());
// Note: "When app indicator is used on Linux, the click event is ignored." This
// doesn't mean that the click event is always ignored on Linux; it depends on how
@ -224,7 +216,7 @@ export class SystemTrayService {
//
// See <https://github.com/electron/electron/blob/v13.1.3/docs/api/tray.md#class-tray>.
result.on('click', () => {
const { browserWindow } = this;
const browserWindow = this.#browserWindow;
if (!browserWindow) {
return;
}
@ -236,7 +228,7 @@ export class SystemTrayService {
}
});
result.setToolTip(this.i18n('icu:signalDesktop'));
result.setToolTip(this.#i18n('icu:signalDesktop'));
return result;
}
@ -246,7 +238,7 @@ export class SystemTrayService {
* into the existing tray instances. It should not be used by "real" code.
*/
_getTray(): undefined | Tray {
return this.tray;
return this.#tray;
}
}