From 18cda646ed279fa5584f94bffca4f94e886e4bce Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Wed, 23 Nov 2022 14:45:51 +0800 Subject: [PATCH] feat: inform wiki of titlebar change --- src/constants/channels.ts | 1 + src/constants/wiki.ts | 3 +++ src/preload/wikiOperation.ts | 10 +++++++- src/services/preferences/index.ts | 35 ++++++++++++++++++++++++---- src/services/wiki/wikiOperations.ts | 13 +++++++---- src/services/workspacesView/index.ts | 14 +++++++++++ 6 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 src/constants/wiki.ts diff --git a/src/constants/channels.ts b/src/constants/channels.ts index 55f41243..77aed173 100644 --- a/src/constants/channels.ts +++ b/src/constants/channels.ts @@ -57,6 +57,7 @@ export enum WikiChannel { setTiddlerTextDone = 'wiki-set-tiddler-text-done', /** show message inside tiddlywiki to show git sync progress */ syncProgress = 'wiki-sync-progress', + setState = "setState" } export enum WikiGitWorkspaceChannel { name = 'WikiGitWorkspaceChannel', diff --git a/src/constants/wiki.ts b/src/constants/wiki.ts new file mode 100644 index 00000000..4f9b7b30 --- /dev/null +++ b/src/constants/wiki.ts @@ -0,0 +1,3 @@ +export enum WikiStateKey { + titleBarOpened = 'titleBarOpened', +} diff --git a/src/preload/wikiOperation.ts b/src/preload/wikiOperation.ts index 7094510a..a255484c 100644 --- a/src/preload/wikiOperation.ts +++ b/src/preload/wikiOperation.ts @@ -3,10 +3,11 @@ /** * Call tiddlywiki api from electron * This file should be required by view.ts preload script to work + * + * You can use wrapped method in `services/wiki/index.ts` 's `wikiOperation()` instead. Available operations are registered in `src/services/wiki/wikiOperations.ts` */ import { ipcRenderer, webFrame } from 'electron'; import { WikiChannel } from '@/constants/channels'; -import { native } from './common/services'; /** * Execute statement with $tw when idle, so there won't be significant lagging. @@ -86,6 +87,13 @@ ipcRenderer.on(WikiChannel.syncProgress, async (event, message: string) => { { onlyWhenVisible: true }, ); }); +ipcRenderer.on(WikiChannel.setState, async (event, stateKey: string, content: string) => { + await executeTWJavaScriptWhenIdle( + ` + $tw.wiki.addTiddler({ title: '$:/state/${stateKey}', text: '${content}' }); + `, + ); +}); ipcRenderer.on(WikiChannel.generalNotification, async (event, message: string) => { await executeTWJavaScriptWhenIdle(` $tw.wiki.addTiddler({ title: '$:/state/notification/${WikiChannel.generalNotification}', text: '${message}' }); diff --git a/src/services/preferences/index.ts b/src/services/preferences/index.ts index 46b70dbb..d962b876 100755 --- a/src/services/preferences/index.ts +++ b/src/services/preferences/index.ts @@ -14,6 +14,10 @@ import { IPreferences, IPreferenceService } from './interface'; import { defaultPreferences } from './defaultPreferences'; import { lazyInject } from '@services/container'; import { requestChangeLanguage } from '@services/libs/i18n/requestChangeLanguage'; +import { IWikiService } from '@services/wiki/interface'; +import { IWorkspaceService } from '@services/workspaces/interface'; +import { WikiChannel } from '@/constants/channels'; +import { WikiStateKey } from '@/constants/wiki'; // eslint-disable-next-line @typescript-eslint/no-unsafe-argument const debouncedSetPreferenceFile = debounce(async (newPreferences: IPreferences) => await settings.set(`preferences`, { ...newPreferences } as any), 500); @@ -22,6 +26,8 @@ const debouncedSetPreferenceFile = debounce(async (newPreferences: IPreferences) export class Preference implements IPreferenceService { @lazyInject(serviceIdentifier.Window) private readonly windowService!: IWindowService; @lazyInject(serviceIdentifier.NotificationService) private readonly notificationService!: INotificationService; + @lazyInject(serviceIdentifier.Wiki) private readonly wikiService!: IWikiService; + @lazyInject(serviceIdentifier.Workspace) private readonly workspaceService!: IWorkspaceService; private cachedPreferences: IPreferences; public preference$: BehaviorSubject; @@ -96,11 +102,30 @@ export class Preference implements IPreferenceService { if (key.startsWith('pauseNotifications')) { await this.notificationService.updatePauseNotificationsInfo(); } - if (key === 'themeSource') { - nativeTheme.themeSource = value as IPreferences['themeSource']; - } - if (key === 'language') { - await requestChangeLanguage(value as string); + switch (key) { + case 'themeSource': { + nativeTheme.themeSource = value as IPreferences['themeSource']; + break; + } + case 'language': { + await requestChangeLanguage(value as string); + break; + } + case 'titleBar': { + /** + * Tell wiki titleBar is on/off, so opened-tiddlers-bar plugin can react to it. + * This also happened in reactWhenSetActiveWorkspaceView in src/services/workspacesView/index.ts + */ + const activeWorkspaceID = this.workspaceService.getActiveWorkspaceSync()?.id; + if (activeWorkspaceID !== undefined) { + this.wikiService.wikiOperation(WikiChannel.setState, [ + activeWorkspaceID, + WikiStateKey.titleBarOpened, + (value as IPreferences['titleBar']) ? 'yes' : 'no', + ]); + } + break; + } } } diff --git a/src/services/wiki/wikiOperations.ts b/src/services/wiki/wikiOperations.ts index a7af4215..8ca58f6d 100644 --- a/src/services/wiki/wikiOperations.ts +++ b/src/services/wiki/wikiOperations.ts @@ -1,14 +1,15 @@ import { WikiChannel } from '@/constants/channels'; +import { WikiStateKey } from '@/constants/wiki'; import { container } from '@services/container'; import serviceIdentifier from '@services/serviceIdentifier'; import { IViewService } from '@services/view/interface'; import { IWindowService } from '@services/windows/interface'; import { WindowNames } from '@services/windows/WindowProperties'; -function sendToMainWindow(type: WikiChannel, workspaceID: string, message: string): void { +function sendToMainWindow(type: WikiChannel, workspaceID: string, messages: string[]): void { const viewService = container.get(serviceIdentifier.View); const browserView = viewService.getView(workspaceID, WindowNames.main); - browserView?.webContents?.send?.(type, message); + browserView?.webContents?.send?.(type, ...messages); } /** * Handle sending message to trigger operations defined in `src/preload/wikiOperation.ts` @@ -19,9 +20,11 @@ export const wikiOperations = { const createWorkspaceWindow = windowService.get(WindowNames.addWorkspace); createWorkspaceWindow?.webContents?.send(WikiChannel.createProgress, message); }, - [WikiChannel.syncProgress]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.syncProgress, workspaceID, message), - [WikiChannel.generalNotification]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.generalNotification, workspaceID, message), - [WikiChannel.openTiddler]: (workspaceID: string, tiddlerName: string): void => sendToMainWindow(WikiChannel.openTiddler, workspaceID, tiddlerName), + [WikiChannel.syncProgress]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.syncProgress, workspaceID, [message]), + [WikiChannel.generalNotification]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.generalNotification, workspaceID, [message]), + [WikiChannel.openTiddler]: (workspaceID: string, tiddlerName: string): void => sendToMainWindow(WikiChannel.openTiddler, workspaceID, [tiddlerName]), + [WikiChannel.setState]: (workspaceID: string, stateKey: WikiStateKey, content: string): void => + sendToMainWindow(WikiChannel.setState, workspaceID, [stateKey, content]), // TODO: add more operations here from `src/preload/wikiOperation.ts` }; export type IWikiOperations = typeof wikiOperations; diff --git a/src/services/workspacesView/index.ts b/src/services/workspacesView/index.ts index 10802771..9d5b6f2a 100644 --- a/src/services/workspacesView/index.ts +++ b/src/services/workspacesView/index.ts @@ -23,6 +23,7 @@ import { SupportedStorageServices } from '@services/types'; import { WorkspaceFailedToLoadError } from './error'; import { WikiChannel } from '@/constants/channels'; import { tiddlywikiLanguagesMap } from '@/constants/languages'; +import { WikiStateKey } from '@/constants/wiki'; @injectable() export class WorkspaceView implements IWorkspaceViewService { @@ -323,6 +324,19 @@ export class WorkspaceView implements IWorkspaceViewService { if (oldActiveWorkspace !== undefined && oldActiveWorkspace.id !== nextWorkspaceID && oldActiveWorkspace.hibernateWhenUnused) { await this.hibernateWorkspaceView(oldActiveWorkspace.id); } + await this.reactWhenSetActiveWorkspaceView(newWorkspace); + } + + /** + * Apply things to active workspace's view when workspace become active + */ + private async reactWhenSetActiveWorkspaceView(newWorkspace: IWorkspace): Promise { + /** + * Tell wiki titleBar is on/off, so opened-tiddlers-bar plugin can react to it. + * This also happened in reactWhenPreferencesChanged in src/services/preferences/index.ts + */ + const titleBar = await this.preferenceService.get('titleBar'); + this.wikiService.wikiOperation(WikiChannel.setState, [newWorkspace.id, WikiStateKey.titleBarOpened, titleBar ? 'yes' : 'no']); } public async removeWorkspaceView(workspaceID: string): Promise {